OSDN Git Service

PR bootstrap/50888
[pf3gnuchains/gcc-fork.git] / libjava / include / win32-threads.h
1 // -*- c++ -*-
2 // win32-threads.h - Defines for using Win32 threads.
3
4 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2006 Free Software
5    Foundation
6
7    This file is part of libgcj.
8
9 This software is copyrighted work licensed under the terms of the
10 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
11 details.  */
12
13 #ifndef __JV_WIN32_THREADS__
14 #define __JV_WIN32_THREADS__
15
16 #define WIN32_LEAN_AND_MEAN
17 #include <windows.h>
18
19 //
20 // Typedefs.
21 //
22
23 typedef struct
24 {
25   // ev[0] (signal) is a Win32 auto-reset event for _Jv_CondNotify
26   // ev[1] (broadcast) is a Win32 manual-reset event for _Jv_CondNotifyAll
27   HANDLE ev[2];
28
29   // Number of threads waiting on this condition variable
30   int blocked_count;
31
32   // Protects access to the blocked_count variable
33   CRITICAL_SECTION count_mutex;
34
35 } _Jv_ConditionVariable_t;
36
37 typedef struct
38 {
39   // The thread-id of the owner thread if any, 0 otherwise
40   DWORD owner;
41
42   // Track nested mutex acquisitions by the same thread
43   int refcount;
44
45   // The actual Windows construct used to implement this mutex
46   CRITICAL_SECTION cs;
47
48 } _Jv_Mutex_t;
49
50 typedef struct _Jv_Thread_t
51 {
52   int flags;            // Flags are defined in implementation.
53   HANDLE handle;        // Actual handle to the thread
54
55   // Protects access to the thread's interrupt_flag and
56   // interrupt_event variables within this module.
57   CRITICAL_SECTION interrupt_mutex;
58   
59   // A Win32 auto-reset event for thread interruption
60   HANDLE interrupt_event;
61
62   java::lang::Thread *thread_obj;
63 } _Jv_Thread_t;
64
65 typedef DWORD _Jv_ThreadId_t;
66
67 inline _Jv_ThreadId_t
68 _Jv_ThreadSelf (void)
69 {
70   return GetCurrentThreadId();
71 }
72
73 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
74
75 // Type identifying a win32 thread.
76 typedef HANDLE _Jv_ThreadDesc_t;
77
78 inline _Jv_ThreadDesc_t
79 _Jv_GetPlatformThreadID(_Jv_Thread_t *t)
80 {
81   return t->handle;
82 }
83
84 //
85 // Condition variables.
86 //
87
88 #define _Jv_HaveCondDestroy
89 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos);
90 void _Jv_CondInit (_Jv_ConditionVariable_t *cv);
91 void _Jv_CondDestroy (_Jv_ConditionVariable_t *cv);
92 int _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
93 int _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
94
95 //
96 // Mutexes.
97 // We use CRITICAL_SECTIONs instead of CreateMutex() for better performance
98 //
99
100 // Returns 0 if the mutex lock is held by the current thread, and 1 otherwise.
101 inline int _Jv_MutexCheckMonitor (_Jv_Mutex_t *mu)
102 {
103   return (mu->owner != GetCurrentThreadId ( ));
104 }
105
106 inline void _Jv_MutexInit (_Jv_Mutex_t *mu)
107 {
108   mu->owner = 0UL;
109   mu->refcount = 0;
110   InitializeCriticalSection (&(mu->cs));
111 }
112
113 #define _Jv_HaveMutexDestroy
114 inline void _Jv_MutexDestroy (_Jv_Mutex_t *mu)
115 {
116   mu->owner = 0UL;
117   mu->refcount = 0;
118   DeleteCriticalSection (&(mu->cs));
119   mu = NULL;
120 }
121
122 inline int _Jv_MutexUnlock (_Jv_Mutex_t *mu)
123 {
124   if (mu->owner == GetCurrentThreadId ( ))
125     {
126       mu->refcount--;
127       if (mu->refcount == 0)
128         mu->owner = 0UL;
129       LeaveCriticalSection (&(mu->cs));
130       return 0;
131     }
132   else
133     return 1;
134 }
135
136 inline int _Jv_MutexLock (_Jv_Mutex_t *mu)
137 {
138   EnterCriticalSection (&(mu->cs));
139   mu->owner = GetCurrentThreadId ( );
140   mu->refcount++;
141   return 0;
142 }
143
144 //
145 // Thread creation and manipulation.
146 //
147
148 void _Jv_InitThreads (void);
149 _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
150 void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
151
152 inline java::lang::Thread* _Jv_ThreadCurrent (void)
153 {
154   extern DWORD _Jv_ThreadKey;
155   return (java::lang::Thread *) TlsGetValue(_Jv_ThreadKey);
156 }
157
158 inline _Jv_Thread_t *_Jv_ThreadCurrentData(void)
159 {
160   extern DWORD _Jv_ThreadDataKey;
161   return (_Jv_Thread_t *) TlsGetValue(_Jv_ThreadDataKey);
162 }
163
164 inline void _Jv_ThreadYield (void)
165 {
166   Sleep (0);
167 }
168
169 void _Jv_ThreadRegister (_Jv_Thread_t *data);
170 void _Jv_ThreadUnRegister ();
171
172 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
173 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
174                       _Jv_ThreadStartFunc *meth);
175 void _Jv_ThreadWait (void);
176 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
177
178 //
179 // Thread interruption support
180 //
181
182 // Gets the auto-reset event for the current thread which is
183 // signalled by _Jv_ThreadInterrupt. The caller can wait on this
184 // event in addition to other waitable objects.
185 //
186 // NOTE: After waiting on this event with WaitForMultipleObjects,
187 // you should ALWAYS use the return value of WaitForMultipleObjects
188 // to test whether this event was signalled and whether thread
189 // interruption has occurred. You should do this instead of checking
190 // the thread's interrupted_flag, because someone could have reset
191 // this flag in the interval of time between the return of
192 // WaitForMultipleObjects and the time you query interrupted_flag.
193 // See java/lang/natWin32Process.cc (waitFor) for an example.
194 HANDLE _Jv_Win32GetInterruptEvent (void);
195
196 // park() / unpark() support
197
198 struct ParkHelper
199 {
200   // We use LONG instead of obj_addr_t to avoid pulling in locks.h,
201   // which depends on size_t, ...
202   volatile LONG permit;
203
204   // The critical section is used for lazy initialization of our event
205   CRITICAL_SECTION cs;
206   HANDLE event;
207   
208   void init ();
209   void deactivate ();
210   void destroy ();
211   void park (jboolean isAbsolute, jlong time);
212   void unpark ();
213   
214 private:
215   void init_event();
216 };
217
218 // Remove defines from <windows.h> that conflict with various things in libgcj code
219
220 #undef TRUE
221 #undef FALSE
222 #undef MAX_PRIORITY
223 #undef MIN_PRIORITY
224 #undef min
225 #undef max
226 #undef interface
227 #undef STRICT
228 #undef VOID
229 #undef TEXT
230 #undef OUT
231
232 #endif /* __JV_WIN32_THREADS__ */