Skip to content

Commit

Permalink
fix: Fix the issue _Thread_local isn't supported by MSVC (Windows)
Browse files Browse the repository at this point in the history
  • Loading branch information
vain0x committed Apr 11, 2022
1 parent 907f053 commit dd13303
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/MiloneShared/UtilSymbol.milone
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ __nativeDecl
#include <hashmap.h>
_Thread_local bool t_initialized;
_Thread_local struct hashmap_s t_hashmap;
_Thread_local struct String t_entries[4000];
_Thread_local uint32_t t_entries_len;
_Thread_local uint32_t t_rank;
#if defined(_MSC_VER) // On Windows MSVC
#define THREAD_LOCAL __declspec(thread)
#else
#define THREAD_LOCAL _Thread_local
#endif
THREAD_LOCAL bool t_initialized;
THREAD_LOCAL struct hashmap_s t_hashmap;
THREAD_LOCAL struct String t_entries[4000];
THREAD_LOCAL uint32_t t_entries_len;
THREAD_LOCAL uint32_t t_rank;
static void _initialize(void) {
if (!t_initialized) {
Expand Down
16 changes: 11 additions & 5 deletions src/libmilonert/milone.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

#include <milone.h>

#if defined(_MSC_VER) // On Windows MSVC
#define THREAD_LOCAL __declspec(thread)
#else
#define THREAD_LOCAL _Thread_local
#endif

// -----------------------------------------------
// memory management (memory pool)
// -----------------------------------------------
Expand Down Expand Up @@ -52,11 +58,11 @@ struct MemoryChunk {
struct MemoryChunk *parent;
};

_Thread_local struct MemoryChunk s_heap;
_Thread_local size_t s_heap_level; // depth of current region
_Thread_local size_t s_heap_size; // consumed size in all regions
_Thread_local size_t s_heap_alloc; // allocated size in all regions
_Thread_local size_t s_alloc_cost; // allocation count
THREAD_LOCAL struct MemoryChunk s_heap;
THREAD_LOCAL size_t s_heap_level; // depth of current region
THREAD_LOCAL size_t s_heap_size; // consumed size in all regions
THREAD_LOCAL size_t s_heap_alloc; // allocated size in all regions
THREAD_LOCAL size_t s_alloc_cost; // allocation count

_Noreturn static void oom(void) {
fprintf(stderr, "Out of memory.\n");
Expand Down

0 comments on commit dd13303

Please sign in to comment.