[multiple changes]
[gcc.git] / libjava / include / win32-threads.h
1 // -*- c++ -*-
2 // win32-threads.h - Defines for using Win32 threads.
3
4 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
5
6 This file is part of libgcj.
7
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 details. */
11
12 #ifndef __JV_WIN32_THREADS__
13 #define __JV_WIN32_THREADS__
14
15 #include <windows.h>
16
17 //
18 // Typedefs.
19 //
20
21 typedef HANDLE _Jv_ConditionVariable_t;
22 typedef HANDLE _Jv_Mutex_t;
23
24 typedef struct
25 {
26 int flags; // Flags are defined in implementation.
27 HANDLE handle; // Actual handle to the thread
28 } _Jv_Thread_t;
29
30 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
31
32 //
33 // Condition variables.
34 //
35
36 inline void
37 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
38 {
39 *cv = CreateEvent (NULL, 0, 0, NULL);
40 }
41
42 #define _Jv_HaveCondDestroy
43
44 inline void
45 _Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
46 {
47 CloseHandle (*cv);
48 cv = NULL;
49 }
50
51 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
52 jlong millis, jint nanos);
53
54 inline int
55 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
56 {
57 return PulseEvent (*cv) ? 0 : GetLastError (); // FIXME: Map error code?
58 }
59
60 inline int
61 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
62 {
63 return PulseEvent (*cv) ? 0 : GetLastError (); // FIXME: Map error code?
64 }
65
66 //
67 // Mutexes.
68 //
69
70 inline void
71 _Jv_MutexInit (_Jv_Mutex_t *mu)
72 {
73 *mu = CreateMutex (NULL, 0, NULL);
74 }
75
76 #define _Jv_HaveMutexDestroy
77
78 inline void
79 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
80 {
81 CloseHandle (*mu);
82 mu = NULL;
83 }
84
85 int _Jv_MutexLock (_Jv_Mutex_t *mu);
86
87 inline int
88 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
89 {
90 return ReleaseMutex(*mu) ? 0 : GetLastError(); // FIXME: Map error code?
91 }
92
93 //
94 // Thread creation and manipulation.
95 //
96
97 void _Jv_InitThreads (void);
98 void _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *thread);
99
100 inline java::lang::Thread *
101 _Jv_ThreadCurrent (void)
102 {
103 extern DWORD _Jv_ThreadKey;
104 return (java::lang::Thread *) TlsGetValue(_Jv_ThreadKey);
105 }
106
107 inline _Jv_Thread_t *
108 _Jv_ThreadCurrentData (void)
109 {
110 extern DWORD _Jv_ThreadDataKey;
111 return (_Jv_Thread_t *) TlsGetValue(_Jv_ThreadDataKey);
112 }
113
114 inline void
115 _Jv_ThreadYield (void)
116 {
117 Sleep (0);
118 }
119
120 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
121 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
122 _Jv_ThreadStartFunc *meth);
123 void _Jv_ThreadWait (void);
124 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
125
126 // Remove defines from <windows.h> that conflict with various things in libgcj code
127
128 #undef TRUE
129 #undef FALSE
130 #undef MAX_PRIORITY
131 #undef MIN_PRIORITY
132 #undef min
133 #undef max
134 #undef interface
135 #undef STRICT
136 #undef VOID
137
138 #endif /* __JV_WIN32_THREADS__ */