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) pthread_mutex_lock(mutex)
20 #define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
21 #define ASSERT_LOCK_HELD(mutex) /* nothing */
27 typedef pthread_cond_t Condition;
28 typedef pthread_mutex_t Mutex;
29 typedef pthread_t OSThreadId;
30 typedef pthread_key_t ThreadLocalKey;
32 #define OSThreadProcAttr /* nothing */
34 #define INIT_COND_VAR PTHREAD_COND_INITIALIZER
38 #define ACQUIRE_LOCK(mutex) \
39 debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
40 pthread_mutex_lock(mutex)
41 #define RELEASE_LOCK(mutex) \
42 debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
43 pthread_mutex_unlock(mutex)
44 #define ASSERT_LOCK_HELD(mutex) /* nothing */
46 #elif defined(DEBUG) && defined(linux_HOST_OS)
49 * On Linux, we can use extensions to determine whether we already
50 * hold a lock or not, which is useful for debugging.
52 #define ACQUIRE_LOCK(mutex) \
53 if (pthread_mutex_lock(mutex) == EDEADLK) { \
54 barf("multiple ACQUIRE_LOCK: %s %d", __FILE__,__LINE__); \
56 #define RELEASE_LOCK(mutex) \
57 if (pthread_mutex_unlock(mutex) != 0) { \
58 barf("RELEASE_LOCK: I do not own this lock: %s %d", __FILE__,__LINE__); \
61 #define ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
63 #define ASSERT_LOCK_NOTHELD(mutex) \
64 if (pthread_mutex_lock(mutex) != EDEADLK) { \
65 pthread_mutex_unlock(mutex); \
73 #define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
74 #define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
75 #define ASSERT_LOCK_HELD(mutex) /* nothing */
81 # elif defined(HAVE_WINDOWS_H)
84 typedef HANDLE Condition;
85 typedef DWORD OSThreadId;
86 // don't be tempted to use HANDLE as the OSThreadId: there can be
87 // many HANDLES to a given thread, so comparison would not work.
88 typedef DWORD ThreadLocalKey;
90 #define OSThreadProcAttr __stdcall
92 #define INIT_COND_VAR 0
94 // We have a choice for implementing Mutexes on Windows. Standard
95 // Mutexes are kernel objects that require kernel calls to
96 // acquire/release, whereas CriticalSections are spin-locks that block
97 // in the kernel after spinning for a configurable number of times.
98 // CriticalSections are *much* faster, so we use those. The Mutex
99 // implementation is left here for posterity.
100 #define USE_CRITICAL_SECTIONS 1
102 #if USE_CRITICAL_SECTIONS
104 typedef CRITICAL_SECTION Mutex;
108 #define ACQUIRE_LOCK(mutex) \
109 debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
110 EnterCriticalSection(mutex)
111 #define RELEASE_LOCK(mutex) \
112 debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
113 LeaveCriticalSection(mutex)
114 #define ASSERT_LOCK_HELD(mutex) /* nothing */
118 #define ACQUIRE_LOCK(mutex) EnterCriticalSection(mutex)
119 #define RELEASE_LOCK(mutex) LeaveCriticalSection(mutex)
121 // I don't know how to do this. TryEnterCriticalSection() doesn't do
123 #define ASSERT_LOCK_HELD(mutex) /* nothing */
129 typedef HANDLE Mutex;
131 // casting to (Mutex *) here required due to use in .cmm files where
132 // the argument has (void *) type.
133 #define ACQUIRE_LOCK(mutex) \
134 if (WaitForSingleObject(*((Mutex *)mutex),INFINITE) == WAIT_FAILED) { \
135 barf("WaitForSingleObject: %d", GetLastError()); \
138 #define RELEASE_LOCK(mutex) \
139 if (ReleaseMutex(*((Mutex *)mutex)) == 0) { \
140 barf("ReleaseMutex: %d", GetLastError()); \
143 #define ASSERT_LOCK_HELD(mutex) /* nothing */
147 # error "Threads not supported"
153 // General thread operations
155 extern OSThreadId osThreadId ( void );
156 extern void shutdownThread ( void );
157 extern void yieldThread ( void );
159 typedef void OSThreadProcAttr OSThreadProc(void *);
161 extern int createOSThread ( OSThreadId* tid,
162 OSThreadProc *startProc, void *param);
163 extern rtsBool osThreadIsAlive ( OSThreadId id );
166 // Condition Variables
168 extern void initCondition ( Condition* pCond );
169 extern void closeCondition ( Condition* pCond );
170 extern rtsBool broadcastCondition ( Condition* pCond );
171 extern rtsBool signalCondition ( Condition* pCond );
172 extern rtsBool waitCondition ( Condition* pCond,
178 extern void initMutex ( Mutex* pMut );
179 extern void closeMutex ( Mutex* pMut );
182 // Thread-local storage
184 void newThreadLocalKey (ThreadLocalKey *key);
185 void *getThreadLocalVar (ThreadLocalKey *key);
186 void setThreadLocalVar (ThreadLocalKey *key, void *value);
187 void freeThreadLocalKey (ThreadLocalKey *key);
189 #endif // !CMINUSMINUS
193 #define ACQUIRE_LOCK(l)
194 #define RELEASE_LOCK(l)
195 #define ASSERT_LOCK_HELD(l)
197 #endif /* defined(THREADED_RTS) */
199 #endif /* __OSTHREADS_H__ */