Skip to content

Commit 6f27a2b

Browse files
AlexBlagoogalbxela
authored andcommitted
user: common: Rework msleep routine
This introduces a "mft_msleep/mft_usleep"-api intended to suspend execution of the current thread for specified time. Rationale for the change: Legacy code relies heavily on "msleep"-macro which is implemented as a wrapper around "usleep"-api. Apart from that, there are a number of places across the codebase where "usleep" is used directly. However "usleep" has been declared as obsolete since POSIX.1-2001 suggesting "nanosleep"-api instead. In POSIX.1-2008 it is removed. Newly added implementation separates C and C++ cases. For C++ it utilizes "std::this_thread::sleep_for"-api which is part of the standard starting C++11. For C implementation is based on "nanosleep"-api (https://man7.org/linux/man-pages/man2/nanosleep.2.html) Separation is done in order to keep implementation in the header relying on compiler ability to inline it where it is possible (api is time sensitive so that extra function calls better to be avoided).
1 parent 71cf028 commit 6f27a2b

27 files changed

+562
-68
lines changed

common/BUILD

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
# Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
2+
#
3+
# This software is available to you under a choice of one of two
4+
# licenses. You may choose to be licensed under the terms of the GNU
5+
# General Public License (GPL) Version 2, available from the file
6+
# COPYING in the main directory of this source tree, or the
7+
# OpenIB.org BSD license below:
8+
#
9+
# Redistribution and use in source and binary forms, with or
10+
# without modification, are permitted provided that the following
11+
# conditions are met:
12+
#
13+
# - Redistributions of source code must retain the above
14+
# copyright notice, this list of conditions and the following
15+
# disclaimer.
16+
#
17+
# - Redistributions in binary form must reproduce the above
18+
# copyright notice, this list of conditions and the following
19+
# disclaimer in the documentation and/or other materials
20+
# provided with the distribution.
21+
#
22+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
# SOFTWARE.
30+
131
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
232

333
package(
@@ -280,3 +310,55 @@ cc_test(
280310
"@com_google_googletest//:gtest_main",
281311
],
282312
)
313+
314+
cc_library(
315+
name = "tools-time",
316+
srcs = [
317+
"tools_time.c",
318+
"tools_time.cpp",
319+
],
320+
hdrs = [
321+
"tools_time.h",
322+
],
323+
copts = [
324+
"-Wall",
325+
"-Wextra",
326+
"-Wpedantic",
327+
],
328+
)
329+
330+
cc_test(
331+
name = "tools-time-test",
332+
size = "small",
333+
srcs = [
334+
"tools_time_test.cpp",
335+
],
336+
copts = [
337+
"-Wall",
338+
"-Wextra",
339+
"-Wpedantic",
340+
],
341+
deps = [
342+
":tools-time",
343+
"@com_google_googletest//:gtest_main",
344+
],
345+
)
346+
347+
cc_test(
348+
name = "tools-time-c-test",
349+
size = "small",
350+
srcs = [
351+
"tools_time_c_test.c",
352+
"tools_time_c_test.cpp",
353+
"tools_time_c_test.h",
354+
],
355+
copts = [
356+
"-Wall",
357+
"-Wextra",
358+
"-Wpedantic",
359+
],
360+
deps = [
361+
":tools-time",
362+
"@com_google_googletest//:gtest_main",
363+
],
364+
)

common/Makefile.am

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
1+
# Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
22
#
33
# This software is available to you under a choice of one of two
44
# licenses. You may choose to be licensed under the terms of the GNU
@@ -30,12 +30,20 @@
3030

3131
# Makefile.am -- Process this file with automake to produce Makefile.in
3232

33-
noinst_HEADERS = compatibility.h bit_slice.h tools_utils.h tools_utils.h tools_version.h tools_filesystem.h tools_regex.h tools_algorithm.h
34-
3533
noinst_LTLIBRARIES = libcommon.la
34+
3635
libcommon_la_SOURCES = \
36+
bit_slice.h \
37+
compatibility.h \
38+
tools_algorithm.h \
3739
tools_filesystem.cpp \
38-
tools_regex.cpp
40+
tools_filesystem.h \
41+
tools_regex.cpp \
42+
tools_regex.h \
43+
tools_time.h \
44+
tools_utils.h \
45+
tools_utils.h \
46+
tools_version.h
3947

4048
commonincludedir = $(includedir)/mstflint/common/
4149
commoninclude_HEADERS = compatibility.h

common/compatibility.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) Jan 2006 Mellanox Technologies Ltd. All rights reserved.
3-
* Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
44
*
55
* This software is available to you under a choice of one of two
66
* licenses. You may choose to be licensed under the terms of the GNU
@@ -340,7 +340,6 @@ inline
340340

341341
typedef struct stat Stat;
342342

343-
#include <sys/time.h>
344343
#include <strings.h>
345344

346345
#endif
@@ -402,13 +401,6 @@ typedef uint8_t u_int8_t;
402401

403402
#endif
404403

405-
/* define msleep(x) - sleeps for x milliseconds */
406-
#if defined(_WIN32) || defined(_WIN64) || defined(__MINGW32__) || defined(__MINGW64__)
407-
#define msleep(x) Sleep(x)
408-
#else
409-
#define msleep(x) usleep(((unsigned long)x) * 1000)
410-
#endif
411-
412404
// Convert BYTES - DWORDS with MEMCPY BE
413405
#define BYTES_TO_DWORD_BE(dw_dest, byte_src) \
414406
do \

common/tools_time.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
// purpose of this file is to verify compilation of tools time library against C code
34+
#include "tools_time.h"

common/tools_time.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
// purpose of this file is to verify compilation of tools time library against C++ code
34+
#include "tools_time.h"

common/tools_time.h

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

common/tools_time_c_test.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
/**
34+
* @file tools_time_c_test.c
35+
* @brief Provides integration between the googleest framework and the C implementation of the tools time library.
36+
*
37+
* This source file contains helper functions that bridge calls from C++ unit tests
38+
* to the C implementation of the tools time library.
39+
*/
40+
41+
#include "tools_time_c_test.h"
42+
43+
#include "tools_time.h"
44+
45+
int mft_msleep_cwrapper(uint32_t msecs)
46+
{
47+
return mft_msleep(msecs);
48+
}
49+
50+
int mft_usleep_cwrapper(uint64_t usecs)
51+
{
52+
return mft_usleep(usecs);
53+
}

0 commit comments

Comments
 (0)