Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/native/minipal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ if(NOT WIN32 AND NOT HOST_WASM AND NOT (CLR_CMAKE_TARGET_ARCH_WASM AND NOT CLR_C
endif()

if(CLR_CMAKE_HOST_UNIX)
list(APPEND SOURCES cpucount.c)
list(APPEND SOURCES
cpucount.c
thread.c
)
endif()
Comment thread
lateralusX marked this conversation as resolved.

# Provide an object library for scenarios where we ship static libraries
Expand Down
9 changes: 9 additions & 0 deletions src/native/minipal/thread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <stddef.h>
#include <minipal/utils.h>

#if !defined(__wasm) || defined(_REENTRANT)
PLATFORM_THREAD_LOCAL size_t minipal_cached_thread_id;
#endif
19 changes: 8 additions & 11 deletions src/native/minipal/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <minipal/utils.h>

#if defined(__linux__)
#include <unistd.h>
Expand Down Expand Up @@ -79,6 +80,10 @@ static inline size_t minipal_get_current_thread_id_no_cache(void)
return tid;
}

#if !defined(__wasm) || defined(_REENTRANT)
extern PLATFORM_THREAD_LOCAL size_t minipal_cached_thread_id;
#endif

/**
* Get the current thread ID.
*
Expand All @@ -90,20 +95,12 @@ static inline size_t minipal_get_current_thread_id(void)
return minipal_get_current_thread_id_no_cache();

#else // !__wasm || _REENTRANT
#if defined(__GNUC__) && !defined(__clang__) && defined(__cplusplus)
// gcc doesn't like _Thread_local when __cplusplus is defined.
// although thread_local is C2x, which other compilers don't allow with C11.
static thread_local size_t tid = 0;
#else
static _Thread_local size_t tid = 0;
#endif

if (!tid)
if (!minipal_cached_thread_id)
{
tid = minipal_get_current_thread_id_no_cache();
minipal_cached_thread_id = minipal_get_current_thread_id_no_cache();
}

return tid;
return minipal_cached_thread_id;
#endif // __wasm && !_REENTRANT
}

Expand Down
Loading