Make the threaded RTS compilable using -fasm
[ghc-hetmet.git] / 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 #if CMINUSMINUS
18
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 */
22
23 #else
24
25 #include <pthread.h>
26
27 typedef pthread_cond_t  Condition;
28 typedef pthread_mutex_t Mutex;
29 typedef pthread_t       OSThreadId;
30 typedef pthread_key_t   ThreadLocalKey;
31
32 #define OSThreadProcAttr /* nothing */
33
34 #define INIT_COND_VAR       PTHREAD_COND_INITIALIZER
35
36 #ifdef LOCK_DEBUG
37
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 */
45
46 #elif defined(DEBUG) && defined(linux_HOST_OS)
47 #include <errno.h>
48 /* 
49  * On Linux, we can use extensions to determine whether we already
50  * hold a lock or not, which is useful for debugging.
51  */
52 #define ACQUIRE_LOCK(mutex) \
53   if (pthread_mutex_lock(mutex) == EDEADLK) { \
54     barf("multiple ACQUIRE_LOCK: %s %d", __FILE__,__LINE__); \
55   }
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__); \
59   }
60
61 #define ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
62
63 #define ASSERT_LOCK_NOTHELD(mutex)              \
64   if (pthread_mutex_lock(mutex) != EDEADLK) {   \
65      pthread_mutex_unlock(mutex);               \
66   } else {                                      \
67     ASSERT(0);                                  \
68   }
69
70
71 #else
72
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 */
76
77 #endif
78
79 #endif // CMINUSMINUS
80
81 # elif defined(HAVE_WINDOWS_H)
82 #include <windows.h>
83
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;
89
90 #define OSThreadProcAttr __stdcall
91
92 #define INIT_COND_VAR  0
93
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
101
102 #if USE_CRITICAL_SECTIONS
103
104 typedef CRITICAL_SECTION Mutex;
105
106 #ifdef LOCK_DEBUG
107
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 */
115
116 #else
117
118 #define ACQUIRE_LOCK(mutex)  EnterCriticalSection(mutex)
119 #define RELEASE_LOCK(mutex)  LeaveCriticalSection(mutex)
120
121 // I don't know how to do this.  TryEnterCriticalSection() doesn't do
122 // the right thing.
123 #define ASSERT_LOCK_HELD(mutex) /* nothing */
124
125 #endif
126
127 #else
128
129 typedef HANDLE Mutex;
130
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());        \
136     }
137
138 #define RELEASE_LOCK(mutex)                             \
139     if (ReleaseMutex(*((Mutex *)mutex)) == 0) {         \
140         barf("ReleaseMutex: %d", GetLastError());       \
141     }
142
143 #define ASSERT_LOCK_HELD(mutex) /* nothing */
144 #endif
145
146 # else
147 #  error "Threads not supported"
148 # endif
149
150
151 #ifndef CMINUSMINUS
152 //
153 // General thread operations
154 //
155 extern OSThreadId osThreadId      ( void );
156 extern void shutdownThread        ( void );
157 extern void yieldThread           ( void );
158
159 typedef void OSThreadProcAttr OSThreadProc(void *);
160
161 extern int  createOSThread        ( OSThreadId* tid, 
162                                     OSThreadProc *startProc, void *param);
163 extern rtsBool osThreadIsAlive    ( OSThreadId id );
164
165 //
166 // Condition Variables
167 //
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, 
173                                     Mutex* pMut );
174
175 //
176 // Mutexes
177 //
178 extern void initMutex             ( Mutex* pMut );
179 extern void closeMutex            ( Mutex* pMut );
180
181 //
182 // Thread-local storage
183 //
184 void  newThreadLocalKey (ThreadLocalKey *key);
185 void *getThreadLocalVar (ThreadLocalKey *key);
186 void  setThreadLocalVar (ThreadLocalKey *key, void *value);
187 void  freeThreadLocalKey (ThreadLocalKey *key);
188
189 #endif // !CMINUSMINUS
190
191 #else
192
193 #define ACQUIRE_LOCK(l)
194 #define RELEASE_LOCK(l)
195 #define ASSERT_LOCK_HELD(l)
196
197 #endif /* defined(THREADED_RTS) */
198
199 #endif /* __OSTHREADS_H__ */