1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2009
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * Do not #include this file directly: #include "Rts.h" instead.
10 * To understand the structure of the RTS headers, see the wiki:
11 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
13 * --------------------------------------------------------------------------*/
15 #ifndef RTS_OSTHREADS_H
16 #define RTS_OSTHREADS_H
18 #if defined(THREADED_RTS) /* to the end */
20 #if defined(HAVE_PTHREAD_H) && !defined(mingw32_HOST_OS)
24 #define ACQUIRE_LOCK(mutex) foreign "C" pthread_mutex_lock(mutex)
25 #define RELEASE_LOCK(mutex) foreign "C" pthread_mutex_unlock(mutex)
26 #define ASSERT_LOCK_HELD(mutex) /* nothing */
33 typedef pthread_cond_t Condition;
34 typedef pthread_mutex_t Mutex;
35 typedef pthread_t OSThreadId;
36 typedef pthread_key_t ThreadLocalKey;
38 #define OSThreadProcAttr /* nothing */
40 #define INIT_COND_VAR PTHREAD_COND_INITIALIZER
43 #define LOCK_DEBUG_BELCH(what, mutex) \
44 debugBelch("%s(0x%p) %s %d\n", what, mutex, __FILE__, __LINE__)
46 #define LOCK_DEBUG_BELCH(what, mutex) /* nothing */
49 /* Always check the result of lock and unlock. */
50 #define ACQUIRE_LOCK(mutex) \
51 LOCK_DEBUG_BELCH("ACQUIRE_LOCK", mutex); \
52 if (pthread_mutex_lock(mutex) == EDEADLK) { \
53 barf("multiple ACQUIRE_LOCK: %s %d", __FILE__,__LINE__); \
56 // Returns zero if the lock was acquired.
57 EXTERN_INLINE int TRY_ACQUIRE_LOCK(pthread_mutex_t *mutex);
58 EXTERN_INLINE int TRY_ACQUIRE_LOCK(pthread_mutex_t *mutex)
60 LOCK_DEBUG_BELCH("TRY_ACQUIRE_LOCK", mutex);
61 return pthread_mutex_trylock(mutex);
64 #define RELEASE_LOCK(mutex) \
65 LOCK_DEBUG_BELCH("RELEASE_LOCK", mutex); \
66 if (pthread_mutex_unlock(mutex) != 0) { \
67 barf("RELEASE_LOCK: I do not own this lock: %s %d", __FILE__,__LINE__); \
70 // Note: this assertion calls pthread_mutex_lock() on a mutex that
71 // is already held by the calling thread. The mutex should therefore
72 // have been created with PTHREAD_MUTEX_ERRORCHECK, otherwise this
73 // assertion will hang. We always initialise mutexes with
74 // PTHREAD_MUTEX_ERRORCHECK when DEBUG is on (see rts/posix/OSThreads.h).
75 #define ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
79 # elif defined(HAVE_WINDOWS_H)
83 /* We jump through a hoop here to get a CCall EnterCriticalSection
84 and LeaveCriticalSection, as that's what C-- wants. */
86 #define ACQUIRE_LOCK(mutex) foreign "stdcall" EnterCriticalSection(mutex)
87 #define RELEASE_LOCK(mutex) foreign "stdcall" LeaveCriticalSection(mutex)
88 #define ASSERT_LOCK_HELD(mutex) /* nothing */
94 typedef HANDLE Condition;
95 typedef DWORD OSThreadId;
96 // don't be tempted to use HANDLE as the OSThreadId: there can be
97 // many HANDLES to a given thread, so comparison would not work.
98 typedef DWORD ThreadLocalKey;
100 #define OSThreadProcAttr __stdcall
102 #define INIT_COND_VAR 0
104 // We have a choice for implementing Mutexes on Windows. Standard
105 // Mutexes are kernel objects that require kernel calls to
106 // acquire/release, whereas CriticalSections are spin-locks that block
107 // in the kernel after spinning for a configurable number of times.
108 // CriticalSections are *much* faster, so we use those. The Mutex
109 // implementation is left here for posterity.
110 #define USE_CRITICAL_SECTIONS 1
112 #if USE_CRITICAL_SECTIONS
114 typedef CRITICAL_SECTION Mutex;
118 #define ACQUIRE_LOCK(mutex) \
119 debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
120 EnterCriticalSection(mutex)
121 #define RELEASE_LOCK(mutex) \
122 debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
123 LeaveCriticalSection(mutex)
124 #define ASSERT_LOCK_HELD(mutex) /* nothing */
128 #define ACQUIRE_LOCK(mutex) EnterCriticalSection(mutex)
129 #define TRY_ACQUIRE_LOCK(mutex) (TryEnterCriticalSection(mutex) == 0)
130 #define RELEASE_LOCK(mutex) LeaveCriticalSection(mutex)
132 // I don't know how to do this. TryEnterCriticalSection() doesn't do
134 #define ASSERT_LOCK_HELD(mutex) /* nothing */
140 typedef HANDLE Mutex;
142 // casting to (Mutex *) here required due to use in .cmm files where
143 // the argument has (void *) type.
144 #define ACQUIRE_LOCK(mutex) \
145 if (WaitForSingleObject(*((Mutex *)mutex),INFINITE) == WAIT_FAILED) { \
146 barf("WaitForSingleObject: %d", GetLastError()); \
149 #define RELEASE_LOCK(mutex) \
150 if (ReleaseMutex(*((Mutex *)mutex)) == 0) { \
151 barf("ReleaseMutex: %d", GetLastError()); \
154 #define ASSERT_LOCK_HELD(mutex) /* nothing */
157 #endif // CMINUSMINUS
160 # error "Threads not supported"
166 // General thread operations
168 extern OSThreadId osThreadId ( void );
169 extern void shutdownThread ( void ) GNUC3_ATTRIBUTE(__noreturn__);
170 extern void yieldThread ( void );
172 typedef void OSThreadProcAttr OSThreadProc(void *);
174 extern int createOSThread ( OSThreadId* tid,
175 OSThreadProc *startProc, void *param);
176 extern rtsBool osThreadIsAlive ( OSThreadId id );
177 extern void interruptOSThread (OSThreadId id);
180 // Condition Variables
182 extern void initCondition ( Condition* pCond );
183 extern void closeCondition ( Condition* pCond );
184 extern rtsBool broadcastCondition ( Condition* pCond );
185 extern rtsBool signalCondition ( Condition* pCond );
186 extern rtsBool waitCondition ( Condition* pCond,
192 extern void initMutex ( Mutex* pMut );
193 extern void closeMutex ( Mutex* pMut );
196 // Thread-local storage
198 void newThreadLocalKey (ThreadLocalKey *key);
199 void *getThreadLocalVar (ThreadLocalKey *key);
200 void setThreadLocalVar (ThreadLocalKey *key, void *value);
201 void freeThreadLocalKey (ThreadLocalKey *key);
203 // Processors and affinity
204 nat getNumberOfProcessors (void);
205 void setThreadAffinity (nat n, nat m);
206 #endif // !CMINUSMINUS
210 #define ACQUIRE_LOCK(l)
211 #define RELEASE_LOCK(l)
212 #define ASSERT_LOCK_HELD(l)
214 #endif /* defined(THREADED_RTS) */
217 // Support for forkOS (defined regardless of THREADED_RTS, but does
218 // nothing when !THREADED_RTS).
221 int forkOS_createThread ( HsStablePtr entry );
224 #endif /* RTS_OSTHREADS_H */