Skip to content

Commit 274c8d5

Browse files
dsciebushefty
authored andcommitted
core/logging: Fix data race on log_prefix
In ofi_get_core_info, which is supposed to be thread safe ("Multiple threads may call fi_getinfo simultaneously, without any requirement for serialization."), a global variable 'log_prefix' is modified, which may lead to a data race. Changing the variable to a thread local one, fixes that problem. Signed-off-by: Dariusz Sciebura <[email protected]>
1 parent d12675b commit 274c8d5

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

include/ofi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ struct ofi_filter {
320320

321321
extern struct ofi_filter prov_log_filter;
322322
extern struct fi_provider core_prov;
323-
extern const char *log_prefix;
323+
extern OFI_THREAD_LOCAL const char *log_prefix;
324324

325325
void ofi_create_filter(struct ofi_filter *filter, const char *env_name);
326326
void ofi_free_filter(struct ofi_filter *filter);

include/unix/osd.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@
8383

8484
#define OFI_MAX_SOCKET_BUF_SIZE SIZE_MAX
8585

86+
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
87+
// C23 and above: use thread_local directly
88+
#define OFI_THREAD_LOCAL thread_local
89+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L) && defined(_Thread_local)
90+
// C11: use _Thread_local
91+
#define OFI_THREAD_LOCAL _Thread_local
92+
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__) || defined(__clang__)
93+
// GCC/Clang/Intel/SunPro/IBM compilers
94+
#define OFI_THREAD_LOCAL __thread
95+
#else
96+
// Unsupported compiler
97+
#warning "Thread-local storage is not supported on this platform"
98+
#define OFI_THREAD_LOCAL
99+
#endif
100+
101+
86102
struct util_shm
87103
{
88104
int shared_fd;

include/windows/osd.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,23 @@ ofi_cpuid(unsigned func, unsigned subfunc, unsigned cpuinfo[4])
11411141

11421142
#endif /* defined(_M_X64) || defined(_M_AMD64) */
11431143

1144+
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
1145+
// C23 and above: use thread_local directly
1146+
#define OFI_THREAD_LOCAL thread_local
1147+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L) && defined(_Thread_local)
1148+
// C11: use _Thread_local
1149+
#define OFI_THREAD_LOCAL _Thread_local
1150+
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__) || defined(__clang__)
1151+
// GCC/Clang/Intel/SunPro/IBM compilers
1152+
#define OFI_THREAD_LOCAL __thread
1153+
#elif defined(_MSC_VER)
1154+
// Microsoft Visual C++ compiler
1155+
#define OFI_THREAD_LOCAL __declspec(thread)
1156+
#else
1157+
// Unsupported compiler
1158+
#warning "Thread-local storage is not supported on this platform"
1159+
#define OFI_THREAD_LOCAL
1160+
#endif
11441161

11451162
#ifdef __cplusplus
11461163
}

src/common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2429,4 +2429,4 @@ size_t ofi_vrb_speed(uint8_t speed, uint8_t width)
24292429
}
24302430

24312431
/* log_prefix is used by fi_log and by prov/util */
2432-
const char *log_prefix = "";
2432+
OFI_THREAD_LOCAL const char *log_prefix = "";

0 commit comments

Comments
 (0)