Merge pull request #59 from Microsoft/cherry-pick-7a226d8

win32: use native threading APIs instead of pthreads
main
davidchisnall 8 years ago committed by GitHub
commit b1494c8823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

57
arc.m

@ -12,9 +12,44 @@
#import "objc/objc-arc.h" #import "objc/objc-arc.h"
#import "objc/blocks_runtime.h" #import "objc/blocks_runtime.h"
#ifndef NO_PTHREADS #if defined(_WIN32)
#include <pthread.h> // We're using the Fiber-Local Storage APIs on Windows
pthread_key_t ARCThreadKey; // 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 #endif
extern void _NSConcreteMallocBlock; extern void _NSConcreteMallocBlock;
@ -60,14 +95,14 @@ struct arc_tls
static inline struct arc_tls* getARCThreadData(void) static inline struct arc_tls* getARCThreadData(void)
{ {
#ifdef NO_PTHREADS #ifndef arc_tls_store
return NULL; return NULL;
#else #else // !defined arc_tls_store
struct arc_tls *tls = pthread_getspecific(ARCThreadKey); struct arc_tls *tls = arc_tls_load(ARCThreadKey);
if (NULL == tls) if (NULL == tls)
{ {
tls = calloc(sizeof(struct arc_tls), 1); tls = calloc(sizeof(struct arc_tls), 1);
pthread_setspecific(ARCThreadKey, tls); arc_tls_store(ARCThreadKey, tls);
} }
return tls; return tls;
#endif #endif
@ -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); //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) if (tls->returnRetained)
{ {
@ -151,6 +187,7 @@ static void cleanupPools(struct arc_tls* tls)
} }
free(tls); free(tls);
} }
#endif
static Class AutoreleasePool; static Class AutoreleasePool;
@ -596,8 +633,8 @@ PRIVATE void init_arc(void)
{ {
weak_ref_initialize(&weakRefs, 128); weak_ref_initialize(&weakRefs, 128);
INIT_LOCK(weakRefLock); INIT_LOCK(weakRefLock);
#ifndef NO_PTHREADS #ifdef arc_tls_store
pthread_key_create(&ARCThreadKey, (void(*)(void*))cleanupPools); ARCThreadKey = arc_tls_key_create((arc_cleanup_function_t)cleanupPools);
#endif #endif
} }

@ -562,7 +562,7 @@ struct thread_data *get_thread_data(void)
} }
return td; return td;
#else #else
return &td; return &thread_data;
#endif #endif
} }
@ -572,7 +572,7 @@ struct thread_data *get_thread_data_fast(void)
struct thread_data *td = pthread_getspecific(key); struct thread_data *td = pthread_getspecific(key);
return td; return td;
#else #else
return &td; return &thread_data;
#endif #endif
} }

@ -6,15 +6,13 @@
#ifndef __LIBOBJC_LOCK_H_INCLUDED__ #ifndef __LIBOBJC_LOCK_H_INCLUDED__
#define __LIBOBJC_LOCK_H_INCLUDED__ #define __LIBOBJC_LOCK_H_INCLUDED__
#ifdef WIN32 #ifdef _WIN32
#define BOOL _WINBOOL # include "safewindows.h"
# include <windows.h> typedef CRITICAL_SECTION mutex_t;
#undef BOOL # define INIT_LOCK(x) InitializeCriticalSection(&(x))
typedef HANDLE mutex_t; # define LOCK(x) EnterCriticalSection(x)
# define INIT_LOCK(x) x = CreateMutex(NULL, FALSE, NULL) # define UNLOCK(x) LeaveCriticalSection(x)
# define LOCK(x) WaitForSingleObject(*x, INFINITE) # define DESTROY_LOCK(x) DeleteCriticalSection(x)
# define UNLOCK(x) ReleaseMutex(*x)
# define DESTROY_LOCK(x) CloseHandle(*x)
#else #else
# include <pthread.h> # include <pthread.h>

@ -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__

@ -1,5 +1,5 @@
#ifdef __MINGW32__ #ifdef _WIN32
#include <windows.h> #include "safewindows.h"
static unsigned sleep(unsigned seconds) static unsigned sleep(unsigned seconds)
{ {
Sleep(seconds*1000); Sleep(seconds*1000);

Loading…
Cancel
Save