40c260bc0feddd397c9dd443d3f5331f8f309487
[ghc-hetmet.git] / ghc / includes / OSThreads.h
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2001-2005
4  *
5  * Accessing OS threads functionality in a (mostly) OS-independent
6  * manner. 
7  * 
8  * --------------------------------------------------------------------------*/
9
10 #ifndef __OSTHREADS_H__
11 #define __OSTHREADS_H__
12
13 #if defined(THREADED_RTS) /* to the end */
14
15 # if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
16
17 #include <pthread.h>
18
19 typedef pthread_cond_t  Condition;
20 typedef pthread_mutex_t Mutex;
21 typedef pthread_t       OSThreadId;
22 typedef pthread_key_t   ThreadLocalKey;
23
24 #define OSThreadProcAttr /* nothing */
25
26 #define INIT_COND_VAR       PTHREAD_COND_INITIALIZER
27
28 #ifdef LOCK_DEBUG
29
30 #define ACQUIRE_LOCK(mutex) \
31   debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
32   pthread_mutex_lock(mutex)
33 #define RELEASE_LOCK(mutex) \
34   debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
35   pthread_mutex_unlock(mutex)
36 #define ASSERT_LOCK_HELD(mutex) /* nothing */
37
38 #elif defined(DEBUG) && defined(linux_HOST_OS)
39 #include <errno.h>
40 /* 
41  * On Linux, we can use extensions to determine whether we already
42  * hold a lock or not, which is useful for debugging.
43  */
44 #define ACQUIRE_LOCK(mutex) \
45   if (pthread_mutex_lock(mutex) == EDEADLK) { \
46     barf("multiple ACQUIRE_LOCK: %s %d", __FILE__,__LINE__); \
47   }
48 #define RELEASE_LOCK(mutex) \
49   if (pthread_mutex_unlock(mutex) != 0) { \
50     barf("RELEASE_LOCK: I do not own this lock: %s %d", __FILE__,__LINE__); \
51   }
52
53 #define ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
54
55 #define ASSERT_LOCK_NOTHELD(mutex)              \
56   if (pthread_mutex_lock(mutex) != EDEADLK) {   \
57      pthread_mutex_unlock(mutex);               \
58   } else {                                      \
59     ASSERT(0);                                  \
60   }
61
62
63 #else
64
65 #define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
66 #define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
67 #define ASSERT_LOCK_HELD(mutex) /* nothing */
68
69 #endif
70
71 # elif defined(HAVE_WINDOWS_H)
72 #include <windows.h>
73
74 typedef HANDLE Condition;
75 typedef DWORD OSThreadId;
76 typedef DWORD ThreadLocalKey;
77
78 #define OSThreadProcAttr __stdcall
79
80 #define INIT_COND_VAR  0
81
82 // We have a choice for implementing Mutexes on Windows.  Standard
83 // Mutexes are kernel objects that require kernel calls to
84 // acquire/release, whereas CriticalSections are spin-locks that block
85 // in the kernel after spinning for a configurable number of times.
86 // CriticalSections are *much* faster, so we use those.  The Mutex
87 // implementation is left here for posterity.
88 #define USE_CRITICAL_SECTIONS 1
89
90 #if USE_CRITICAL_SECTIONS
91
92 typedef CRITICAL_SECTION Mutex;
93 #define ACQUIRE_LOCK(mutex)  EnterCriticalSection(mutex)
94 #define RELEASE_LOCK(mutex)  LeaveCriticalSection(mutex)
95
96 // I don't know how to do this.  TryEnterCriticalSection() doesn't do
97 // the right thing.
98 #define ASSERT_LOCK_HELD(mutex) /* nothing */
99
100 #else
101
102 typedef HANDLE Mutex;
103
104 // casting to (Mutex *) here required due to use in .cmm files where
105 // the argument has (void *) type.
106 #define ACQUIRE_LOCK(mutex)                                     \
107     if (WaitForSingleObject(*((Mutex *)mutex),INFINITE) == WAIT_FAILED) { \
108         barf("WaitForSingleObject: %d", GetLastError());        \
109     }
110
111 #define RELEASE_LOCK(mutex)                             \
112     if (ReleaseMutex(*((Mutex *)mutex)) == 0) {         \
113         barf("ReleaseMutex: %d", GetLastError());       \
114     }
115
116 #define ASSERT_LOCK_HELD(mutex) /* nothing */
117 #endif
118
119 # else
120 #  error "Threads not supported"
121 # endif
122
123 //
124 // General thread operations
125 //
126 extern OSThreadId osThreadId      ( void );
127 extern void shutdownThread        ( void );
128 extern void yieldThread           ( void );
129
130 typedef void OSThreadProcAttr OSThreadProc(void *);
131
132 extern int  createOSThread        ( OSThreadId* tid, 
133                                     OSThreadProc *startProc, void *param);
134
135 //
136 // Condition Variables
137 //
138 extern void initCondition         ( Condition* pCond );
139 extern void closeCondition        ( Condition* pCond );
140 extern rtsBool broadcastCondition ( Condition* pCond );
141 extern rtsBool signalCondition    ( Condition* pCond );
142 extern rtsBool waitCondition      ( Condition* pCond, 
143                                     Mutex* pMut );
144
145 //
146 // Mutexes
147 //
148 extern void initMutex             ( Mutex* pMut );
149
150 //
151 // Thread-local storage
152 //
153 void  newThreadLocalKey (ThreadLocalKey *key);
154 void *getThreadLocalVar (ThreadLocalKey *key);
155 void  setThreadLocalVar (ThreadLocalKey *key, void *value);
156
157 #else
158
159 #define ACQUIRE_LOCK(l)
160 #define RELEASE_LOCK(l)
161 #define ASSERT_LOCK_HELD(l)
162
163 #endif /* defined(THREADED_RTS) */
164
165 #endif /* __OSTHREADS_H__ */