X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fincludes%2FOSThreads.h;h=90431445b7befaf93a4d200a2405a5a54d1937fa;hb=3d9ae0ef3505f158b8ed1e2c8d655a581074d3a2;hp=df65dcd1d8be5540b2927ec48ad8ecfb8774d091;hpb=3fe03f82f9bb1a17bb46925c8804a26e016d3a51;p=ghc-hetmet.git diff --git a/ghc/includes/OSThreads.h b/ghc/includes/OSThreads.h index df65dcd..9043144 100644 --- a/ghc/includes/OSThreads.h +++ b/ghc/includes/OSThreads.h @@ -1,6 +1,6 @@ /* --------------------------------------------------------------------------- * - * (c) The GHC Team, 2001 + * (c) The GHC Team, 2001-2005 * * Accessing OS threads functionality in a (mostly) OS-independent * manner. @@ -23,7 +23,6 @@ typedef pthread_key_t ThreadLocalKey; #define OSThreadProcAttr /* nothing */ -#define INIT_MUTEX_VAR PTHREAD_MUTEX_INITIALIZER #define INIT_COND_VAR PTHREAD_COND_INITIALIZER #ifdef LOCK_DEBUG @@ -73,26 +72,64 @@ typedef pthread_key_t ThreadLocalKey; #include typedef HANDLE Condition; -typedef HANDLE Mutex; typedef DWORD OSThreadId; typedef DWORD ThreadLocalKey; #define OSThreadProcAttr __stdcall -#define INIT_MUTEX_VAR 0 #define INIT_COND_VAR 0 +// We have a choice for implementing Mutexes on Windows. Standard +// Mutexes are kernel objects that require kernel calls to +// acquire/release, whereas CriticalSections are spin-locks that block +// in the kernel after spinning for a configurable number of times. +// CriticalSections are *much* faster, so we use those. The Mutex +// implementation is left here for posterity. +#define USE_CRITICAL_SECTIONS 1 + +#if USE_CRITICAL_SECTIONS + +typedef CRITICAL_SECTION Mutex; + +#ifdef LOCK_DEBUG + +#define ACQUIRE_LOCK(mutex) \ + debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \ + EnterCriticalSection(mutex) +#define RELEASE_LOCK(mutex) \ + debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \ + LeaveCriticalSection(mutex) +#define ASSERT_LOCK_HELD(mutex) /* nothing */ + +#else + +#define ACQUIRE_LOCK(mutex) EnterCriticalSection(mutex) +#define RELEASE_LOCK(mutex) LeaveCriticalSection(mutex) + +// I don't know how to do this. TryEnterCriticalSection() doesn't do +// the right thing. +#define ASSERT_LOCK_HELD(mutex) /* nothing */ + +#endif + +#else + +typedef HANDLE Mutex; + +// casting to (Mutex *) here required due to use in .cmm files where +// the argument has (void *) type. #define ACQUIRE_LOCK(mutex) \ - if (WaitForSingleObject(*mutex,INFINITE) == WAIT_FAILED) { \ + if (WaitForSingleObject(*((Mutex *)mutex),INFINITE) == WAIT_FAILED) { \ barf("WaitForSingleObject: %d", GetLastError()); \ } #define RELEASE_LOCK(mutex) \ - if (ReleaseMutex(*mutex) == 0) { \ + if (ReleaseMutex(*((Mutex *)mutex)) == 0) { \ barf("ReleaseMutex: %d", GetLastError()); \ } #define ASSERT_LOCK_HELD(mutex) /* nothing */ +#endif # else # error "Threads not supported" @@ -138,6 +175,6 @@ void setThreadLocalVar (ThreadLocalKey *key, void *value); #define RELEASE_LOCK(l) #define ASSERT_LOCK_HELD(l) -#endif /* defined(RTS_SUPPORTS_THREADS) */ +#endif /* defined(THREADED_RTS) */ #endif /* __OSTHREADS_H__ */