OSThreads.h 2.31 KB
Newer Older
sof's avatar
sof committed
1
2
3
4
5
6
7
8
9
10
11
/* ---------------------------------------------------------------------------
 *
 * (c) The GHC Team, 2001
 *
 * Accessing OS threads functionality in a (mostly) OS-independent
 * manner. 
 *
 * 
 * --------------------------------------------------------------------------*/
#ifndef __OSTHREADS_H__
#define __OSTHREADS_H__
sof's avatar
sof committed
12
#if defined(RTS_SUPPORTS_THREADS) /* to the end */
sof's avatar
sof committed
13

sof's avatar
sof committed
14
15
16
17
# if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
#  include <pthread.h>
typedef pthread_cond_t  Condition;
typedef pthread_mutex_t Mutex;
sof's avatar
sof committed
18
19
typedef pthread_t       OSThreadId;

sof's avatar
sof committed
20
21
22
#define INIT_MUTEX_VAR      PTHREAD_MUTEX_INITIALIZER
#define INIT_COND_VAR       PTHREAD_COND_INITIALIZER

sof's avatar
sof committed
23
24
25
26
#ifdef LOCK_DEBUG
#define ACQUIRE_LOCK(mutex) fprintf(stderr, "ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); fflush(stderr); pthread_mutex_lock(mutex)
#define RELEASE_LOCK(mutex) fprintf(stderr, "RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); fflush(stderr); pthread_mutex_unlock(mutex)
#else
sof's avatar
sof committed
27
28
#define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
#define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
sof's avatar
sof committed
29
#endif
sof's avatar
sof committed
30
31

# elif defined(HAVE_WINDOWS_H)
sof's avatar
sof committed
32
#include <windows.h>
sof's avatar
sof committed
33

34
35
#include "RtsUtils.h"

sof's avatar
sof committed
36
37
typedef HANDLE Condition;
typedef HANDLE Mutex;
sof's avatar
sof committed
38
typedef DWORD OSThreadId;
sof's avatar
sof committed
39
40
41
42

#define INIT_MUTEX_VAR 0
#define INIT_COND_VAR  0

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
static inline void
ACQUIRE_LOCK(Mutex *mutex)
{
    if (WaitForSingleObject(*mutex,INFINITE) == WAIT_FAILED) {
	barf("WaitForSingleObject: %d", GetLastError());
    }
}

static inline void
RELEASE_LOCK(Mutex *mutex)
{
    if (ReleaseMutex(*mutex) == 0) {
	barf("ReleaseMutex: %d", GetLastError());
    }
}

sof's avatar
sof committed
59
60
61
# else
#  error "Threads not supported"
# endif
sof's avatar
sof committed
62

sof's avatar
sof committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
extern void initCondition         ( Condition* pCond );
extern void closeCondition        ( Condition* pCond );
extern rtsBool broadcastCondition ( Condition* pCond );
extern rtsBool signalCondition    ( Condition* pCond );
extern rtsBool waitCondition      ( Condition* pCond, 
				    Mutex* pMut );

extern void initMutex             ( Mutex* pMut );

extern OSThreadId osThreadId      ( void );
extern void shutdownThread        ( void );
extern void yieldThread           ( void );
extern int  createOSThread        ( OSThreadId* tid,
				    void (*startProc)(void) );
#else
sof's avatar
sof committed
78

sof's avatar
sof committed
79
80
#define ACQUIRE_LOCK(l)
#define RELEASE_LOCK(l)
sof's avatar
sof committed
81
82
83
84

#endif /* defined(RTS_SUPPORTS_THREADS) */

#endif /* __OSTHREADS_H__ */