[project @ 2002-02-14 07:40:17 by sof]
[ghc-hetmet.git] / ghc / rts / OSThreads.h
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2001
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 #if defined(RTS_SUPPORTS_THREADS) /* to the end */
13
14 # if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
15 #  include <pthread.h>
16 typedef pthread_cond_t  Condition;
17 typedef pthread_mutex_t Mutex;
18 typedef pthread_t       OSThreadId;
19
20 #define INIT_MUTEX_VAR      PTHREAD_MUTEX_INITIALIZER
21 #define INIT_COND_VAR       PTHREAD_COND_INITIALIZER
22
23 #define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
24 #define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
25
26 # elif defined(HAVE_WINDOWS_H)
27 #include <windows.h>
28
29 typedef HANDLE Condition;
30 typedef HANDLE Mutex;
31 typedef DWORD OSThreadId;
32
33 #define INIT_MUTEX_VAR 0
34 #define INIT_COND_VAR  0
35
36 #define ACQUIRE_LOCK(mutex)   WaitForSingleObject(mutex,INFINITE)
37 #define RELEASE_LOCK(mutex)   ReleaseMutex(mutex)
38 # else
39 #  error "Threads not supported"
40 # endif
41
42 extern void initCondition         ( Condition* pCond );
43 extern void closeCondition        ( Condition* pCond );
44 extern rtsBool broadcastCondition ( Condition* pCond );
45 extern rtsBool signalCondition    ( Condition* pCond );
46 extern rtsBool waitCondition      ( Condition* pCond, 
47                                     Mutex* pMut );
48
49 extern void initMutex             ( Mutex* pMut );
50
51 extern OSThreadId osThreadId      ( void );
52 extern void shutdownThread        ( void );
53 extern void yieldThread           ( void );
54 extern int  createOSThread        ( OSThreadId* tid,
55                                     void (*startProc)(void) );
56 #else
57
58 #define ACQUIRE_LOCK(l)
59 #define RELEASE_LOCK(l)
60
61 #endif /* defined(RTS_SUPPORTS_THREADS) */
62
63 #endif /* __OSTHREADS_H__ */