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");
84 // Reimplementation of the general algorithm described at
85 // http://www.cs.wustl.edu/~schmidt/win32-cv-1.html (isomorphic to
86 // 3.2, not a cut-and-paste).
89 _Jv_CondWait(_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
, jlong millis
, jint nanos
)
91 if (mu
->owner
!= GetCurrentThreadId ( ))
94 EnterCriticalSection (&cv
->count_mutex
);
95 ensure_condvar_initialized (cv
);
97 LeaveCriticalSection (&cv
->count_mutex
);
100 if ((millis
== 0) && (nanos
> 0)) time
= 1;
101 else if (millis
== 0) time
= INFINITE
;
104 _Jv_MutexUnlock (mu
);
106 DWORD rval
= WaitForMultipleObjects (2, &(cv
->ev
[0]), 0, time
);
108 EnterCriticalSection(&cv
->count_mutex
);
110 // If we were unblocked by the second event (the broadcast one)
111 // and nobody is left, then reset the event.
112 int last_waiter
= (rval
== (WAIT_OBJECT_0
+ 1)) && (cv
->blocked_count
== 0);
113 LeaveCriticalSection(&cv
->count_mutex
);
116 ResetEvent (cv
->ev
[1]);
124 _Jv_CondInit (_Jv_ConditionVariable_t
*cv
)
126 // we do lazy creation of Events since CreateEvent() is insanely expensive
128 InitializeCriticalSection (&cv
->count_mutex
);
129 cv
->blocked_count
= 0;
133 _Jv_CondDestroy (_Jv_ConditionVariable_t
*cv
)
137 CloseHandle (cv
->ev
[0]);
138 CloseHandle (cv
->ev
[1]);
143 DeleteCriticalSection (&cv
->count_mutex
);
147 _Jv_CondNotify (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
)
149 if (mu
->owner
!= GetCurrentThreadId ( ))
150 return _JV_NOT_OWNER
;
152 EnterCriticalSection (&cv
->count_mutex
);
153 ensure_condvar_initialized (cv
);
154 int somebody_is_blocked
= cv
->blocked_count
> 0;
155 LeaveCriticalSection (&cv
->count_mutex
);
157 if (somebody_is_blocked
)
158 SetEvent (cv
->ev
[0]);
164 _Jv_CondNotifyAll (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
)
166 if (mu
->owner
!= GetCurrentThreadId ( ))
167 return _JV_NOT_OWNER
;
169 EnterCriticalSection (&cv
->count_mutex
);
170 ensure_condvar_initialized (cv
);
171 int somebody_is_blocked
= cv
->blocked_count
> 0;
172 LeaveCriticalSection (&cv
->count_mutex
);
174 if (somebody_is_blocked
)
175 SetEvent (cv
->ev
[1]);
185 _Jv_InitThreads (void)
187 _Jv_ThreadKey
= TlsAlloc();
188 _Jv_ThreadDataKey
= TlsAlloc();
189 daemon_mutex
= CreateMutex (NULL
, 0, NULL
);
190 daemon_cond
= CreateEvent (NULL
, 1, 0, NULL
);
191 non_daemon_count
= 0;
195 _Jv_ThreadInitData (java::lang::Thread
* obj
)
197 _Jv_Thread_t
*data
= (_Jv_Thread_t
*)_Jv_Malloc(sizeof(_Jv_Thread_t
));
199 data
->thread_obj
= obj
;
205 _Jv_ThreadDestroyData (_Jv_Thread_t
*data
)
211 _Jv_ThreadSetPriority (_Jv_Thread_t
*data
, jint prio
)
213 int actual
= THREAD_PRIORITY_NORMAL
;
215 if (data
->flags
& FLAG_START
)
220 actual
= THREAD_PRIORITY_TIME_CRITICAL
;
223 actual
= THREAD_PRIORITY_HIGHEST
;
227 actual
= THREAD_PRIORITY_ABOVE_NORMAL
;
231 actual
= THREAD_PRIORITY_NORMAL
;
235 actual
= THREAD_PRIORITY_BELOW_NORMAL
;
238 actual
= THREAD_PRIORITY_LOWEST
;
241 actual
= THREAD_PRIORITY_IDLE
;
244 SetThreadPriority(data
->handle
, actual
);
249 _Jv_ThreadRegister (_Jv_Thread_t
*data
)
251 TlsSetValue (_Jv_ThreadKey
, data
->thread_obj
);
252 TlsSetValue (_Jv_ThreadDataKey
, data
);
256 _Jv_ThreadUnRegister ()
258 TlsSetValue (_Jv_ThreadKey
, NULL
);
259 TlsSetValue (_Jv_ThreadDataKey
, NULL
);
262 // This function is called when a thread is started. We don't arrange
263 // to call the `run' method directly, because this function must
266 really_start (void* x
)
268 struct starter
*info
= (struct starter
*) x
;
270 _Jv_ThreadRegister (info
->data
);
272 info
->method (info
->data
->thread_obj
);
274 if (! (info
->data
->flags
& FLAG_DAEMON
))
276 WaitForSingleObject (daemon_mutex
, INFINITE
);
278 if (! non_daemon_count
)
279 SetEvent (daemon_cond
);
280 ReleaseMutex (daemon_mutex
);
287 _Jv_ThreadStart (java::lang::Thread
*thread
, _Jv_Thread_t
*data
, _Jv_ThreadStartFunc
*meth
)
290 struct starter
*info
;
292 // Do nothing if thread has already started
293 if (data
->flags
& FLAG_START
)
295 data
->flags
|= FLAG_START
;
297 // FIXME: handle marking the info object for GC.
298 info
= (struct starter
*) _Jv_AllocBytes (sizeof (struct starter
));
302 if (! thread
->isDaemon ())
304 WaitForSingleObject (daemon_mutex
, INFINITE
);
306 ReleaseMutex (daemon_mutex
);
309 data
->flags
|= FLAG_DAEMON
;
311 HANDLE h
= GC_CreateThread(NULL
, 0, really_start
, info
, 0, &id
);
312 _Jv_ThreadSetPriority(data
, thread
->getPriority());
319 _Jv_ThreadWait (void)
321 WaitForSingleObject (daemon_mutex
, INFINITE
);
322 if (non_daemon_count
)
324 ReleaseMutex (daemon_mutex
);
325 WaitForSingleObject (daemon_cond
, INFINITE
);
330 _Jv_ThreadInterrupt (_Jv_Thread_t
*data
)
332 MessageBox(NULL
, "Unimplemented", "win32-threads.cc:_Jv_ThreadInterrupt", MB_OK
);