1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2005
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * --------------------------------------------------------------------------*/
10 #ifndef __OSTHREADS_H__
11 #define __OSTHREADS_H__
13 #if defined(THREADED_RTS) /* to the end */
15 # if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
19 #define ACQUIRE_LOCK(mutex) foreign "C" pthread_mutex_lock(mutex)
20 #define RELEASE_LOCK(mutex) foreign "C" pthread_mutex_unlock(mutex)
21 #define ASSERT_LOCK_HELD(mutex) /* nothing */
28 typedef pthread_cond_t Condition;
29 typedef pthread_mutex_t Mutex;
30 typedef pthread_t OSThreadId;
31 typedef pthread_key_t ThreadLocalKey;
33 #define OSThreadProcAttr /* nothing */
35 #define INIT_COND_VAR PTHREAD_COND_INITIALIZER
38 #define LOCK_DEBUG_BELCH(what, mutex) \
39 debugBelch("%s(0x%p) %s %d\n", what, mutex, __FILE__, __LINE__)
41 #define LOCK_DEBUG_BELCH(what, mutex) /* nothing */
44 /* Always check the result of lock and unlock. */
45 #define ACQUIRE_LOCK(mutex) \
46 LOCK_DEBUG_BELCH("ACQUIRE_LOCK", mutex); \
47 if (pthread_mutex_lock(mutex) == EDEADLK) { \
48 barf("multiple ACQUIRE_LOCK: %s %d", __FILE__,__LINE__); \
51 #define RELEASE_LOCK(mutex) \
52 LOCK_DEBUG_BELCH("RELEASE_LOCK", mutex); \
53 if (pthread_mutex_unlock(mutex) != 0) { \
54 barf("RELEASE_LOCK: I do not own this lock: %s %d", __FILE__,__LINE__); \
57 #define ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
61 # elif defined(HAVE_WINDOWS_H)
65 /* We jump through a hoop here to get a CCall EnterCriticalSection
66 and LeaveCriticalSection, as that's what C-- wants. */
68 #define ACQUIRE_LOCK(mutex) foreign "stdcall" EnterCriticalSection(mutex)
69 #define RELEASE_LOCK(mutex) foreign "stdcall" LeaveCriticalSection(mutex)
70 #define ASSERT_LOCK_HELD(mutex) /* nothing */
76 typedef HANDLE Condition;
77 typedef DWORD OSThreadId;
78 // don't be tempted to use HANDLE as the OSThreadId: there can be
79 // many HANDLES to a given thread, so comparison would not work.
80 typedef DWORD ThreadLocalKey;
82 #define OSThreadProcAttr __stdcall
84 #define INIT_COND_VAR 0
86 // We have a choice for implementing Mutexes on Windows. Standard
87 // Mutexes are kernel objects that require kernel calls to
88 // acquire/release, whereas CriticalSections are spin-locks that block
89 // in the kernel after spinning for a configurable number of times.
90 // CriticalSections are *much* faster, so we use those. The Mutex
91 // implementation is left here for posterity.
92 #define USE_CRITICAL_SECTIONS 1
94 #if USE_CRITICAL_SECTIONS
96 typedef CRITICAL_SECTION Mutex;
100 #define ACQUIRE_LOCK(mutex) \
101 debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
102 EnterCriticalSection(mutex)
103 #define RELEASE_LOCK(mutex) \
104 debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
105 LeaveCriticalSection(mutex)
106 #define ASSERT_LOCK_HELD(mutex) /* nothing */
110 #define ACQUIRE_LOCK(mutex) EnterCriticalSection(mutex)
111 #define RELEASE_LOCK(mutex) LeaveCriticalSection(mutex)
113 // I don't know how to do this. TryEnterCriticalSection() doesn't do
115 #define ASSERT_LOCK_HELD(mutex) /* nothing */
121 typedef HANDLE Mutex;
123 // casting to (Mutex *) here required due to use in .cmm files where
124 // the argument has (void *) type.
125 #define ACQUIRE_LOCK(mutex) \
126 if (WaitForSingleObject(*((Mutex *)mutex),INFINITE) == WAIT_FAILED) { \
127 barf("WaitForSingleObject: %d", GetLastError()); \
130 #define RELEASE_LOCK(mutex) \
131 if (ReleaseMutex(*((Mutex *)mutex)) == 0) { \
132 barf("ReleaseMutex: %d", GetLastError()); \
135 #define ASSERT_LOCK_HELD(mutex) /* nothing */
138 #endif // CMINUSMINUS
141 # error "Threads not supported"
147 // General thread operations
149 extern OSThreadId osThreadId ( void );
150 extern void shutdownThread ( void );
151 extern void yieldThread ( void );
153 typedef void OSThreadProcAttr OSThreadProc(void *);
155 extern int createOSThread ( OSThreadId* tid,
156 OSThreadProc *startProc, void *param);
157 extern rtsBool osThreadIsAlive ( OSThreadId id );
160 // Condition Variables
162 extern void initCondition ( Condition* pCond );
163 extern void closeCondition ( Condition* pCond );
164 extern rtsBool broadcastCondition ( Condition* pCond );
165 extern rtsBool signalCondition ( Condition* pCond );
166 extern rtsBool waitCondition ( Condition* pCond,
172 extern void initMutex ( Mutex* pMut );
173 extern void closeMutex ( Mutex* pMut );
176 // Thread-local storage
178 void newThreadLocalKey (ThreadLocalKey *key);
179 void *getThreadLocalVar (ThreadLocalKey *key);
180 void setThreadLocalVar (ThreadLocalKey *key, void *value);
181 void freeThreadLocalKey (ThreadLocalKey *key);
183 #endif // !CMINUSMINUS
187 #define ACQUIRE_LOCK(l)
188 #define RELEASE_LOCK(l)
189 #define ASSERT_LOCK_HELD(l)
191 #endif /* defined(THREADED_RTS) */
193 #endif /* __OSTHREADS_H__ */