OSDN Git Service

* win32-threads.cc: (ensure_interrupt_event_initialized) New
[pf3gnuchains/gcc-fork.git] / libjava / win32-threads.cc
1 // win32-threads.cc - interface between libjava and Win32 threads.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
4    Foundation, Inc.
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 #include <config.h>
13
14 // If we're using the Boehm GC, then we need to override some of the
15 // thread primitives.  This is fairly gross.
16 #ifdef HAVE_BOEHM_GC
17 extern "C"
18 {
19 #include <gc.h>
20 // <windows.h> #define's STRICT, which conflicts with Modifier.h
21 #undef STRICT
22 };
23 #endif /* HAVE_BOEHM_GC */
24
25 #include <gcj/cni.h>
26 #include <jvm.h>
27 #include <java/lang/Thread.h>
28 #include <java/lang/System.h>
29
30 #include <errno.h>
31
32 #ifndef ETIMEDOUT
33 #define ETIMEDOUT 116
34 #endif
35
36 // This is used to implement thread startup.
37 struct starter
38 {
39   _Jv_ThreadStartFunc *method;
40   _Jv_Thread_t *data;
41 };
42
43 // Controls access to the variable below
44 static HANDLE daemon_mutex;
45 static HANDLE daemon_cond;
46 // Number of non-daemon threads - _Jv_ThreadWait returns when this is 0
47 static int non_daemon_count;
48
49 // TLS key get Java object representing the thread
50 DWORD _Jv_ThreadKey;
51 // TLS key to get _Jv_Thread_t* representing the thread
52 DWORD _Jv_ThreadDataKey;
53
54 //
55 // These are the flags that can appear in _Jv_Thread_t.
56 //
57
58 // Thread started.
59 #define FLAG_START   0x01
60 // Thread is daemon.
61 #define FLAG_DAEMON  0x02
62
63 //
64 // Condition variables.
65 //
66
67 // we do lazy creation of Events since CreateEvent() is insanely
68 // expensive, and because the rest of libgcj will call _Jv_CondInit
69 // when only a mutex is needed.
70
71 inline void
72 ensure_condvar_initialized(_Jv_ConditionVariable_t *cv)
73 {
74   if (cv->ev[0] == 0)
75     {
76       cv->ev[0] = CreateEvent (NULL, 0, 0, NULL);
77       if (cv->ev[0] == 0) JvFail("CreateEvent() failed");
78
79       cv->ev[1] = CreateEvent (NULL, 1, 0, NULL);
80       if (cv->ev[1] == 0) JvFail("CreateEvent() failed");
81     }
82 }
83
84 inline void
85 ensure_interrupt_event_initialized(HANDLE& rhEvent)
86 {
87   if (!rhEvent)
88     {
89       rhEvent = CreateEvent (NULL, 0, 0, NULL);
90       if (!rhEvent) JvFail("CreateEvent() failed");
91     }
92 }
93
94 // Reimplementation of the general algorithm described at
95 // http://www.cs.wustl.edu/~schmidt/win32-cv-1.html (isomorphic to
96 // 3.2, not a cut-and-paste).
97
98 int
99 _Jv_CondWait(_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos)
100 {
101   if (mu->owner != GetCurrentThreadId ( ))
102     return _JV_NOT_OWNER;
103
104   _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
105   java::lang::Thread *current_obj = _Jv_ThreadCurrent ();
106
107   // Now that we hold the interrupt mutex, check if this thread has been 
108   // interrupted already.
109   EnterCriticalSection (&current->interrupt_mutex);
110   ensure_interrupt_event_initialized (current->interrupt_event);
111   jboolean interrupted = current_obj->interrupt_flag;
112   LeaveCriticalSection (&current->interrupt_mutex);
113
114   if (interrupted)
115     {
116       return _JV_INTERRUPTED;
117     }
118
119   EnterCriticalSection (&cv->count_mutex);
120   ensure_condvar_initialized (cv);
121   cv->blocked_count++;
122   LeaveCriticalSection (&cv->count_mutex);
123
124   DWORD time;
125   if ((millis == 0) && (nanos > 0)) time = 1;
126   else if (millis == 0) time = INFINITE;
127   else time = millis;
128
129   _Jv_MutexUnlock (mu);
130
131   // Set up our array of three events:
132   // - the auto-reset event (for notify())
133   // - the manual-reset event (for notifyAll())
134   // - the interrupt event (for interrupt())
135   // We wait for any one of these to be signaled.
136   HANDLE arh[3];
137   arh[0] = cv->ev[0];
138   arh[1] = cv->ev[1];
139   arh[2] = current->interrupt_event;
140   DWORD rval = WaitForMultipleObjects (3, arh, 0, time);
141
142   EnterCriticalSection (&current->interrupt_mutex);
143
144   // If we were unblocked by the third event (our thread's interrupt
145   // event), set the thread's interrupt flag. I think this sanity
146   // check guards against someone resetting our interrupt flag
147   // in the time between when interrupt_mutex is released in
148   // _Jv_ThreadInterrupt and the interval of time between the
149   // WaitForMultipleObjects call we just made and our acquisition
150   // of interrupt_mutex.
151   if (rval == (WAIT_OBJECT_0 + 2))
152     current_obj->interrupt_flag = true;
153     
154   interrupted = current_obj->interrupt_flag;
155   LeaveCriticalSection (&current->interrupt_mutex);
156
157   EnterCriticalSection(&cv->count_mutex);
158   cv->blocked_count--;
159   // If we were unblocked by the second event (the broadcast one)
160   // and nobody is left, then reset the event.
161   int last_waiter = (rval == (WAIT_OBJECT_0 + 1)) && (cv->blocked_count == 0);
162   LeaveCriticalSection(&cv->count_mutex);
163
164   if (last_waiter)
165     ResetEvent (cv->ev[1]);
166
167   _Jv_MutexLock (mu);
168   
169   return interrupted ? _JV_INTERRUPTED : 0;
170 }
171
172 void
173 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
174 {
175   // we do lazy creation of Events since CreateEvent() is insanely expensive
176   cv->ev[0] = 0;
177   InitializeCriticalSection (&cv->count_mutex);
178   cv->blocked_count = 0;
179 }
180
181 void
182 _Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
183 {
184   if (cv->ev[0] != 0)
185     {
186       CloseHandle (cv->ev[0]);
187       CloseHandle (cv->ev[1]);
188
189       cv->ev[0] = 0;
190     }
191
192   DeleteCriticalSection (&cv->count_mutex);
193 }
194
195 int
196 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
197 {
198   if (mu->owner != GetCurrentThreadId ( ))
199     return _JV_NOT_OWNER;
200
201   EnterCriticalSection (&cv->count_mutex);
202   ensure_condvar_initialized (cv);
203   int somebody_is_blocked = cv->blocked_count > 0;
204   LeaveCriticalSection (&cv->count_mutex);
205
206   if (somebody_is_blocked)
207     SetEvent (cv->ev[0]);
208
209   return 0;
210 }
211
212 int
213 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
214 {
215   if (mu->owner != GetCurrentThreadId ( ))
216     return _JV_NOT_OWNER;
217
218   EnterCriticalSection (&cv->count_mutex);
219   ensure_condvar_initialized (cv);
220   int somebody_is_blocked = cv->blocked_count > 0;
221   LeaveCriticalSection (&cv->count_mutex);
222
223   if (somebody_is_blocked)
224     SetEvent (cv->ev[1]);
225
226   return 0;
227 }
228
229 //
230 // Threads.
231 //
232
233 void
234 _Jv_InitThreads (void)
235 {
236   _Jv_ThreadKey = TlsAlloc();
237   _Jv_ThreadDataKey = TlsAlloc();
238   daemon_mutex = CreateMutex (NULL, 0, NULL);
239   daemon_cond = CreateEvent (NULL, 1, 0, NULL);
240   non_daemon_count = 0;
241 }
242
243 _Jv_Thread_t *
244 _Jv_ThreadInitData (java::lang::Thread* obj)
245 {
246   _Jv_Thread_t *data = (_Jv_Thread_t*)_Jv_Malloc(sizeof(_Jv_Thread_t));
247   data->flags = 0;
248   data->thread_obj = obj;
249   data->interrupt_event = 0;
250   InitializeCriticalSection (&data->interrupt_mutex);
251
252   return data;
253 }
254
255 void
256 _Jv_ThreadDestroyData (_Jv_Thread_t *data)
257 {
258   DeleteCriticalSection (&data->interrupt_mutex);
259   if (data->interrupt_event)
260     CloseHandle(data->interrupt_event);
261   _Jv_Free(data);
262 }
263
264 void
265 _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
266 {
267   int actual = THREAD_PRIORITY_NORMAL;
268
269   if (data->flags & FLAG_START)
270     {
271       switch (prio)
272         {
273           case 10:
274             actual = THREAD_PRIORITY_TIME_CRITICAL;
275             break;
276           case 9:
277             actual = THREAD_PRIORITY_HIGHEST;
278             break;
279           case 8:
280           case 7:
281             actual = THREAD_PRIORITY_ABOVE_NORMAL;
282             break;
283           case 6:
284           case 5:
285             actual = THREAD_PRIORITY_NORMAL;
286             break;
287           case 4:
288           case 3:
289             actual = THREAD_PRIORITY_BELOW_NORMAL;
290             break;
291           case 2:
292             actual = THREAD_PRIORITY_LOWEST;
293             break;
294           case 1:
295             actual = THREAD_PRIORITY_IDLE;
296             break;
297         }
298       SetThreadPriority(data->handle, actual);
299     }
300 }
301
302 void
303 _Jv_ThreadRegister (_Jv_Thread_t *data)
304 {
305   TlsSetValue (_Jv_ThreadKey, data->thread_obj);
306   TlsSetValue (_Jv_ThreadDataKey, data);
307 }
308
309 void
310 _Jv_ThreadUnRegister ()
311 {
312   TlsSetValue (_Jv_ThreadKey, NULL);
313   TlsSetValue (_Jv_ThreadDataKey, NULL);
314 }
315
316 // This function is called when a thread is started.  We don't arrange
317 // to call the `run' method directly, because this function must
318 // return a value.
319 static DWORD WINAPI
320 really_start (void* x)
321 {
322   struct starter *info = (struct starter *) x;
323
324   _Jv_ThreadRegister (info->data);
325
326   info->method (info->data->thread_obj);
327
328   if (! (info->data->flags & FLAG_DAEMON))
329     {
330       WaitForSingleObject (daemon_mutex, INFINITE);
331       non_daemon_count--;
332       if (! non_daemon_count)
333         SetEvent (daemon_cond);
334       ReleaseMutex (daemon_mutex);
335     }
336
337   return 0;
338 }
339
340 void
341 _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data, _Jv_ThreadStartFunc *meth)
342 {
343   DWORD id;
344   struct starter *info;
345
346   // Do nothing if thread has already started
347   if (data->flags & FLAG_START)
348     return;
349   data->flags |= FLAG_START;
350
351   // FIXME: handle marking the info object for GC.
352   info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
353   info->method = meth;
354   info->data = data;
355
356   if (! thread->isDaemon ())
357     {
358       WaitForSingleObject (daemon_mutex, INFINITE);
359       non_daemon_count++;
360       ReleaseMutex (daemon_mutex);
361     }
362   else
363     data->flags |= FLAG_DAEMON;
364
365   GC_CreateThread(NULL, 0, really_start, info, 0, &id);
366   _Jv_ThreadSetPriority(data, thread->getPriority());
367 }
368
369 void
370 _Jv_ThreadWait (void)
371 {
372   WaitForSingleObject (daemon_mutex, INFINITE);
373   if (non_daemon_count)
374     {
375       ReleaseMutex (daemon_mutex);
376       WaitForSingleObject (daemon_cond, INFINITE);
377     }
378 }
379
380 //
381 // Interrupt support
382 //
383
384 HANDLE
385 _Jv_Win32GetInterruptEvent (void)
386 {
387   _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
388   EnterCriticalSection (&current->interrupt_mutex);
389   ensure_interrupt_event_initialized (current->interrupt_event);
390   HANDLE hEvent = current->interrupt_event;
391   LeaveCriticalSection (&current->interrupt_mutex);
392   return hEvent;
393 }
394
395 void
396 _Jv_ThreadInterrupt (_Jv_Thread_t *data)
397 {
398   EnterCriticalSection (&data->interrupt_mutex);
399   ensure_interrupt_event_initialized (data->interrupt_event);
400   data->thread_obj->interrupt_flag = true;
401   SetEvent (data->interrupt_event);
402   LeaveCriticalSection (&data->interrupt_mutex);
403 }