Skip to content

Commit

Permalink
win32: use native threading APIs instead of pthreads
Browse files Browse the repository at this point in the history
* use fiber local storage if NO_PTHREADS is defined
* use critical sections instead of mutexes

Contributing-author: Ben Viglietta <benvi@microsoft.com>
  • Loading branch information
DHowett committed Jan 22, 2018
1 parent 2ea1111 commit 7a226d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
43 changes: 32 additions & 11 deletions arc.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,36 @@

#ifndef NO_PTHREADS
#include <pthread.h>
pthread_key_t ARCThreadKey;
typedef pthread_key_t arc_tls_key_t;
#define arc_tls_store pthread_setspecific
#define arc_tls_load pthread_getspecific
#define TLS_CALLBACK(name) void name

static inline pthread_key_t arc_tls_key_create(void(*cleanupFunction)(void*))
{
pthread_key_t key;
pthread_key_create(&key, cleanupFunction);
return key;
}
#else // if defined(NO_PTHREADS)
// We're using the Fiber-Local Storage APIs on Windows
// because the TLS APIs won't pass app certification.
// Additionally, the FLS API surface is 1:1 mapped to
// the TLS API surface when fibers are not in use.
#include <Windows.h>
typedef DWORD arc_tls_key_t;
#define arc_tls_store FlsSetValue
#define arc_tls_load FlsGetValue
#define TLS_CALLBACK(name) void WINAPI name

static inline DWORD arc_tls_key_create(void WINAPI(*cleanupFunction)(void*))
{
return FlsAlloc(cleanupFunction);
}
#endif

arc_tls_key_t ARCThreadKey;

extern void _NSConcreteMallocBlock;
extern void _NSConcreteStackBlock;
extern void _NSConcreteGlobalBlock;
Expand Down Expand Up @@ -59,17 +86,13 @@ - (void)release;

static inline struct arc_tls* getARCThreadData(void)
{
#ifdef NO_PTHREADS
return NULL;
#else
struct arc_tls *tls = pthread_getspecific(ARCThreadKey);
struct arc_tls *tls = arc_tls_load(ARCThreadKey);
if (NULL == tls)
{
tls = calloc(sizeof(struct arc_tls), 1);
pthread_setspecific(ARCThreadKey, tls);
arc_tls_store(ARCThreadKey, tls);
}
return tls;
#endif
}
int count = 0;
int poolCount = 0;
Expand Down Expand Up @@ -132,7 +155,7 @@ static void emptyPool(struct arc_tls *tls, id *stop)
//fprintf(stderr, "New insert: %p. Stop: %p\n", tls->pool->insert, stop);
}

static void cleanupPools(struct arc_tls* tls)
static TLS_CALLBACK(cleanupPools)(struct arc_tls* tls)
{
if (tls->returnRetained)
{
Expand Down Expand Up @@ -515,9 +538,7 @@ PRIVATE void init_arc(void)
{
weak_ref_initialize(&weakRefs, 128);
INIT_LOCK(weakRefLock);
#ifndef NO_PTHREADS
pthread_key_create(&ARCThreadKey, (void(*)(void*))cleanupPools);
#endif
ARCThreadKey = arc_tls_key_create((void(*)(void*))cleanupPools);
}

void* block_load_weak(void *block);
Expand Down
10 changes: 5 additions & 5 deletions lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#define BOOL _WINBOOL
# include <windows.h>
#undef BOOL
typedef HANDLE mutex_t;
# define INIT_LOCK(x) x = CreateMutex(NULL, FALSE, NULL)
# define LOCK(x) WaitForSingleObject(*x, INFINITE)
# define UNLOCK(x) ReleaseMutex(*x)
# define DESTROY_LOCK(x) CloseHandle(*x)
typedef CRITICAL_SECTION mutex_t;
# define INIT_LOCK(x) InitializeCriticalSection(&(x))
# define LOCK(x) EnterCriticalSection(x)
# define UNLOCK(x) LeaveCriticalSection(x)
# define DESTROY_LOCK(x) DeleteCriticalSection(x)
#else

# include <pthread.h>
Expand Down

0 comments on commit 7a226d8

Please sign in to comment.