1 // win32-threads.cc - interface between libjava and Win32 threads.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 // If we're using the Boehm GC, then we need to override some of the
15 // thread primitives. This is fairly gross.
20 // <windows.h> #define's STRICT, which conflicts with Modifier.h
23 #endif /* HAVE_BOEHM_GC */
27 #include <java/lang/Thread.h>
28 #include <java/lang/System.h>
36 // This is used to implement thread startup.
39 _Jv_ThreadStartFunc
*method
;
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
;
49 // TLS key get Java object representing the thread
51 // TLS key to get _Jv_Thread_t* representing the thread
52 DWORD _Jv_ThreadDataKey
;
55 // These are the flags that can appear in _Jv_Thread_t.
59 #define FLAG_START 0x01
61 #define FLAG_DAEMON 0x02
64 // Condition variables.
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.
72 ensure_condvar_initialized(_Jv_ConditionVariable_t
*cv
)
76 cv
->ev
[0] = CreateEvent (NULL
, 0, 0, NULL
);
77 if (cv
->ev
[0] == 0) JvFail("CreateEvent() failed");
79 cv
->ev
[1] = CreateEvent (NULL
, 1, 0, NULL
);
80 if (cv
->ev
[1] == 0) JvFail("CreateEvent() failed");
85 ensure_interrupt_event_initialized(HANDLE
& rhEvent
)
89 rhEvent
= CreateEvent (NULL
, 0, 0, NULL
);
90 if (!rhEvent
) JvFail("CreateEvent() failed");
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).
99 _Jv_CondWait(_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
, jlong millis
, jint nanos
)
101 if (mu
->owner
!= GetCurrentThreadId ( ))
102 return _JV_NOT_OWNER
;
104 _Jv_Thread_t
*current
= _Jv_ThreadCurrentData ();
105 java::lang::Thread
*current_obj
= _Jv_ThreadCurrent ();
107 // Now that we hold the interrupt mutex, check if this thread has been
108 // interrupted already.
109 EnterCriticalSection (¤t
->interrupt_mutex
);
110 ensure_interrupt_event_initialized (current
->interrupt_event
);
111 jboolean interrupted
= current_obj
->interrupt_flag
;
112 LeaveCriticalSection (¤t
->interrupt_mutex
);
116 return _JV_INTERRUPTED
;
119 EnterCriticalSection (&cv
->count_mutex
);
120 ensure_condvar_initialized (cv
);
122 LeaveCriticalSection (&cv
->count_mutex
);
125 if ((millis
== 0) && (nanos
> 0)) time
= 1;
126 else if (millis
== 0) time
= INFINITE
;
129 // Record the current lock depth, so it can be restored
130 // when we reacquire it.
131 int count
= mu
->refcount
;
132 int curcount
= count
;
134 // Call _Jv_MutexUnlock repeatedly until this thread
135 // has completely released the monitor.
138 _Jv_MutexUnlock (mu
);
142 // Set up our array of three events:
143 // - the auto-reset event (for notify())
144 // - the manual-reset event (for notifyAll())
145 // - the interrupt event (for interrupt())
146 // We wait for any one of these to be signaled.
150 arh
[2] = current
->interrupt_event
;
151 DWORD rval
= WaitForMultipleObjects (3, arh
, 0, time
);
153 EnterCriticalSection (¤t
->interrupt_mutex
);
155 // If we were unblocked by the third event (our thread's interrupt
156 // event), set the thread's interrupt flag. I think this sanity
157 // check guards against someone resetting our interrupt flag
158 // in the time between when interrupt_mutex is released in
159 // _Jv_ThreadInterrupt and the interval of time between the
160 // WaitForMultipleObjects call we just made and our acquisition
161 // of interrupt_mutex.
162 if (rval
== (WAIT_OBJECT_0
+ 2))
163 current_obj
->interrupt_flag
= true;
165 interrupted
= current_obj
->interrupt_flag
;
166 LeaveCriticalSection (¤t
->interrupt_mutex
);
168 EnterCriticalSection(&cv
->count_mutex
);
170 // If we were unblocked by the second event (the broadcast one)
171 // and nobody is left, then reset the event.
172 int last_waiter
= (rval
== (WAIT_OBJECT_0
+ 1)) && (cv
->blocked_count
== 0);
173 LeaveCriticalSection(&cv
->count_mutex
);
176 ResetEvent (cv
->ev
[1]);
178 // Call _Jv_MutexLock repeatedly until the mutex's refcount is the
179 // same as before we originally released it.
180 while (curcount
< count
)
186 return interrupted
? _JV_INTERRUPTED
: 0;
190 _Jv_CondInit (_Jv_ConditionVariable_t
*cv
)
192 // we do lazy creation of Events since CreateEvent() is insanely expensive
194 InitializeCriticalSection (&cv
->count_mutex
);
195 cv
->blocked_count
= 0;
199 _Jv_CondDestroy (_Jv_ConditionVariable_t
*cv
)
203 CloseHandle (cv
->ev
[0]);
204 CloseHandle (cv
->ev
[1]);
209 DeleteCriticalSection (&cv
->count_mutex
);
213 _Jv_CondNotify (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
)
215 if (mu
->owner
!= GetCurrentThreadId ( ))
216 return _JV_NOT_OWNER
;
218 EnterCriticalSection (&cv
->count_mutex
);
219 ensure_condvar_initialized (cv
);
220 int somebody_is_blocked
= cv
->blocked_count
> 0;
221 LeaveCriticalSection (&cv
->count_mutex
);
223 if (somebody_is_blocked
)
224 SetEvent (cv
->ev
[0]);
230 _Jv_CondNotifyAll (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
)
232 if (mu
->owner
!= GetCurrentThreadId ( ))
233 return _JV_NOT_OWNER
;
235 EnterCriticalSection (&cv
->count_mutex
);
236 ensure_condvar_initialized (cv
);
237 int somebody_is_blocked
= cv
->blocked_count
> 0;
238 LeaveCriticalSection (&cv
->count_mutex
);
240 if (somebody_is_blocked
)
241 SetEvent (cv
->ev
[1]);
251 _Jv_InitThreads (void)
253 _Jv_ThreadKey
= TlsAlloc();
254 _Jv_ThreadDataKey
= TlsAlloc();
255 daemon_mutex
= CreateMutex (NULL
, 0, NULL
);
256 daemon_cond
= CreateEvent (NULL
, 1, 0, NULL
);
257 non_daemon_count
= 0;
261 _Jv_ThreadInitData (java::lang::Thread
* obj
)
263 _Jv_Thread_t
*data
= (_Jv_Thread_t
*)_Jv_Malloc(sizeof(_Jv_Thread_t
));
265 data
->thread_obj
= obj
;
266 data
->interrupt_event
= 0;
267 InitializeCriticalSection (&data
->interrupt_mutex
);
273 _Jv_ThreadDestroyData (_Jv_Thread_t
*data
)
275 DeleteCriticalSection (&data
->interrupt_mutex
);
276 if (data
->interrupt_event
)
277 CloseHandle(data
->interrupt_event
);
282 _Jv_ThreadSetPriority (_Jv_Thread_t
*data
, jint prio
)
284 int actual
= THREAD_PRIORITY_NORMAL
;
286 if (data
->flags
& FLAG_START
)
291 actual
= THREAD_PRIORITY_TIME_CRITICAL
;
294 actual
= THREAD_PRIORITY_HIGHEST
;
298 actual
= THREAD_PRIORITY_ABOVE_NORMAL
;
302 actual
= THREAD_PRIORITY_NORMAL
;
306 actual
= THREAD_PRIORITY_BELOW_NORMAL
;
309 actual
= THREAD_PRIORITY_LOWEST
;
312 actual
= THREAD_PRIORITY_IDLE
;
315 SetThreadPriority(data
->handle
, actual
);
320 _Jv_ThreadRegister (_Jv_Thread_t
*data
)
322 TlsSetValue (_Jv_ThreadKey
, data
->thread_obj
);
323 TlsSetValue (_Jv_ThreadDataKey
, data
);
327 _Jv_ThreadUnRegister ()
329 TlsSetValue (_Jv_ThreadKey
, NULL
);
330 TlsSetValue (_Jv_ThreadDataKey
, NULL
);
333 // This function is called when a thread is started. We don't arrange
334 // to call the `run' method directly, because this function must
337 really_start (void* x
)
339 struct starter
*info
= (struct starter
*) x
;
341 _Jv_ThreadRegister (info
->data
);
343 info
->method (info
->data
->thread_obj
);
345 if (! (info
->data
->flags
& FLAG_DAEMON
))
347 WaitForSingleObject (daemon_mutex
, INFINITE
);
349 if (! non_daemon_count
)
350 SetEvent (daemon_cond
);
351 ReleaseMutex (daemon_mutex
);
358 _Jv_ThreadStart (java::lang::Thread
*thread
, _Jv_Thread_t
*data
, _Jv_ThreadStartFunc
*meth
)
361 struct starter
*info
;
363 // Do nothing if thread has already started
364 if (data
->flags
& FLAG_START
)
366 data
->flags
|= FLAG_START
;
368 // FIXME: handle marking the info object for GC.
369 info
= (struct starter
*) _Jv_AllocBytes (sizeof (struct starter
));
373 if (! thread
->isDaemon ())
375 WaitForSingleObject (daemon_mutex
, INFINITE
);
377 ReleaseMutex (daemon_mutex
);
380 data
->flags
|= FLAG_DAEMON
;
382 GC_CreateThread(NULL
, 0, really_start
, info
, 0, &id
);
383 _Jv_ThreadSetPriority(data
, thread
->getPriority());
387 _Jv_ThreadWait (void)
389 WaitForSingleObject (daemon_mutex
, INFINITE
);
390 if (non_daemon_count
)
392 ReleaseMutex (daemon_mutex
);
393 WaitForSingleObject (daemon_cond
, INFINITE
);
402 _Jv_Win32GetInterruptEvent (void)
404 _Jv_Thread_t
*current
= _Jv_ThreadCurrentData ();
405 EnterCriticalSection (¤t
->interrupt_mutex
);
406 ensure_interrupt_event_initialized (current
->interrupt_event
);
407 HANDLE hEvent
= current
->interrupt_event
;
408 LeaveCriticalSection (¤t
->interrupt_mutex
);
413 _Jv_ThreadInterrupt (_Jv_Thread_t
*data
)
415 EnterCriticalSection (&data
->interrupt_mutex
);
416 ensure_interrupt_event_initialized (data
->interrupt_event
);
417 data
->thread_obj
->interrupt_flag
= true;
418 SetEvent (data
->interrupt_event
);
419 LeaveCriticalSection (&data
->interrupt_mutex
);