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 <[email protected]>
  • Loading branch information
DHowett committed Mar 1, 2018
1 parent 14889c5 commit 430ab75
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
57 changes: 47 additions & 10 deletions arc.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,44 @@
#import "objc/objc-arc.h"
#import "objc/blocks_runtime.h"

#ifndef NO_PTHREADS
#include <pthread.h>
pthread_key_t ARCThreadKey;
#if defined(_WIN32)
// 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 "safewindows.h"
# define arc_tls_store FlsSetValue
# define arc_tls_load FlsGetValue
# define TLS_CALLBACK(name) void WINAPI name

typedef DWORD arc_tls_key_t;
typedef void WINAPI(*arc_cleanup_function_t)(void*);
static inline arc_tls_key_t arc_tls_key_create(arc_cleanup_function_t cleanupFunction)
{
return FlsAlloc(cleanupFunction);
}

#else // if defined(_WIN32)

# ifndef NO_PTHREADS
# include <pthread.h>
# define arc_tls_store pthread_setspecific
# define arc_tls_load pthread_getspecific
# define TLS_CALLBACK(name) void name

typedef pthread_key_t arc_tls_key_t;
typedef void (*arc_cleanup_function_t)(void*);
static inline arc_tls_key_t arc_tls_key_create(arc_cleanup_function_t cleanupFunction)
{
pthread_key_t key;
pthread_key_create(&key, cleanupFunction);
return key;
}
# endif
#endif

#ifdef arc_tls_store
arc_tls_key_t ARCThreadKey;
#endif

extern void _NSConcreteMallocBlock;
Expand Down Expand Up @@ -60,14 +95,14 @@ - (void)release;

static inline struct arc_tls* getARCThreadData(void)
{
#ifdef NO_PTHREADS
#ifndef arc_tls_store
return NULL;
#else
struct arc_tls *tls = pthread_getspecific(ARCThreadKey);
#else // !defined arc_tls_store
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
Expand Down Expand Up @@ -133,7 +168,8 @@ 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)
#ifdef arc_tls_store
static TLS_CALLBACK(cleanupPools)(struct arc_tls* tls)
{
if (tls->returnRetained)
{
Expand All @@ -151,6 +187,7 @@ static void cleanupPools(struct arc_tls* tls)
}
free(tls);
}
#endif


static Class AutoreleasePool;
Expand Down Expand Up @@ -596,8 +633,8 @@ PRIVATE void init_arc(void)
{
weak_ref_initialize(&weakRefs, 128);
INIT_LOCK(weakRefLock);
#ifndef NO_PTHREADS
pthread_key_create(&ARCThreadKey, (void(*)(void*))cleanupPools);
#ifdef arc_tls_store
ARCThreadKey = arc_tls_key_create((arc_cleanup_function_t)cleanupPools);
#endif
}

Expand Down
16 changes: 7 additions & 9 deletions lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

#ifndef __LIBOBJC_LOCK_H_INCLUDED__
#define __LIBOBJC_LOCK_H_INCLUDED__
#ifdef WIN32
#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)
#ifdef _WIN32
# include "safewindows.h"
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
20 changes: 20 additions & 0 deletions safewindows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __LIBOBJC_SAFEWINDOWS_H_INCLUDED__
#define __LIBOBJC_SAFEWINDOWS_H_INCLUDED__

#pragma push_macro("BOOL")

#ifdef BOOL
#undef BOOL
#endif
#define BOOL _WINBOOL

#include <Windows.h>

// Windows.h defines interface -> struct
#ifdef interface
#undef interface
#endif

#pragma pop_macro("BOOL")

#endif // __LIBOBJC_SAFEWINDOWS_H_INCLUDED__
4 changes: 2 additions & 2 deletions spinlock.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifdef __MINGW32__
#include <windows.h>
#ifdef _WIN32
#include "safewindows.h"
static unsigned sleep(unsigned seconds)
{
Sleep(seconds*1000);
Expand Down

0 comments on commit 430ab75

Please sign in to comment.