|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. |
| 3 | + * |
| 4 | + * This software is available to you under a choice of one of two |
| 5 | + * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | + * General Public License (GPL) Version 2, available from the file |
| 7 | + * COPYING in the main directory of this source tree, or the |
| 8 | + * OpenIB.org BSD license below: |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or |
| 11 | + * without modification, are permitted provided that the following |
| 12 | + * conditions are met: |
| 13 | + * |
| 14 | + * - Redistributions of source code must retain the above |
| 15 | + * copyright notice, this list of conditions and the following |
| 16 | + * disclaimer. |
| 17 | + * |
| 18 | + * - Redistributions in binary form must reproduce the above |
| 19 | + * copyright notice, this list of conditions and the following |
| 20 | + * disclaimer in the documentation and/or other materials |
| 21 | + * provided with the distribution. |
| 22 | + * |
| 23 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | + * SOFTWARE. |
| 31 | + */ |
| 32 | + |
| 33 | +#pragma once |
| 34 | + |
| 35 | +#if __cplusplus |
| 36 | +#include <chrono> |
| 37 | +#include <thread> |
| 38 | +#else |
| 39 | +#include <time.h> |
| 40 | +#include <stdint.h> |
| 41 | +#endif |
| 42 | + |
| 43 | +#if __cplusplus |
| 44 | +namespace nbu |
| 45 | +{ |
| 46 | +namespace mft |
| 47 | +{ |
| 48 | +namespace common |
| 49 | +{ |
| 50 | +/** |
| 51 | + * @brief Pauses the execution of the current thread for at least the specified number of milliseconds. |
| 52 | + * @param msecs The number of milliseconds to sleep. |
| 53 | + * |
| 54 | + * The execution of the current thread is stopped until at least msecs milliseconds has |
| 55 | + # passed from now. Other threads continue their execution. |
| 56 | + */ |
| 57 | +inline void mft_msleep(uint32_t msecs) |
| 58 | +{ |
| 59 | + ::std::this_thread::sleep_for(std::chrono::milliseconds(msecs)); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief Pauses the execution of the current thread for at least the specified number of microseconds. |
| 64 | + * @param msecs The number of microseconds to sleep. |
| 65 | + * |
| 66 | + * The execution of the current thread is stopped until at least msecs microseconds has |
| 67 | + # passed from now. Other threads continue their execution. |
| 68 | + */ |
| 69 | +inline void mft_usleep(uint64_t usecs) |
| 70 | +{ |
| 71 | + ::std::this_thread::sleep_for(std::chrono::microseconds(usecs)); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace common |
| 75 | +} // namespace mft |
| 76 | +} // namespace nbu |
| 77 | + |
| 78 | +#define msleep(x) nbu::mft::common::mft_msleep(x) |
| 79 | + |
| 80 | +#else // C { |
| 81 | + |
| 82 | +/** |
| 83 | + * @brief Pauses the execution of the current thread for at least the specified number of milliseconds. |
| 84 | + * @param msecs The number of milliseconds for which to pause the execution. |
| 85 | + * @return 0 on success, -1 otherwise. If interrupted by a signal, `errno` is set to `EINTR`. |
| 86 | + * |
| 87 | + * The execution of the current thread is stopped until at least msecs milliseconds has passed from now. |
| 88 | + * Other threads continue their execution. There no support for Windows platforms. |
| 89 | + */ |
| 90 | +static inline int mft_msleep(uint32_t msecs) |
| 91 | +{ |
| 92 | + struct timespec req; |
| 93 | + req.tv_sec = msecs / 1000; |
| 94 | + req.tv_nsec = ((long int)msecs % 1000) * 1000000; |
| 95 | + return nanosleep(&req, NULL); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @brief Pauses the execution of the current thread for at least the specified number of microseconds. |
| 100 | + * @param msecs The number of microseconds for which to pause the execution. |
| 101 | + * @return 0 on success, -1 otherwise. If interrupted by a signal, `errno` is set to `EINTR`. |
| 102 | + * |
| 103 | + * The execution of the current thread is stopped until at least msecs microseconds has passed from now. |
| 104 | + * Other threads continue their execution. There no support for Windows platforms. |
| 105 | + */ |
| 106 | +static inline int mft_usleep(uint64_t usecs) |
| 107 | +{ |
| 108 | + struct timespec req; |
| 109 | + req.tv_sec = usecs / 1000000; |
| 110 | + req.tv_nsec = ((long int)usecs % 1000000) * 1000; |
| 111 | + return nanosleep(&req, NULL); |
| 112 | +} |
| 113 | + |
| 114 | +#define msleep(x) mft_msleep(x) |
| 115 | + |
| 116 | +#endif // } C |
0 commit comments