* Makefile.tpl: Reindent.
[official-gcc.git] / libjava / win32-threads.cc
blob3a3999a8f2d394811e35186b10237ca9484c7237
1 // win32-threads.cc - interface between libjava and Win32 threads.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
4 Foundation, Inc.
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
10 details. */
12 #include <config.h>
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"
19 #include <gc.h>
20 // <windows.h> #define's STRICT, which conflicts with Modifier.h
21 #undef STRICT
23 #endif /* HAVE_BOEHM_GC */
25 #include <gcj/cni.h>
26 #include <jvm.h>
27 #include <java/lang/Thread.h>
28 #include <java/lang/System.h>
30 #include <errno.h>
32 #ifndef ETIMEDOUT
33 #define ETIMEDOUT 116
34 #endif
36 // This is used to implement thread startup.
37 struct starter
39 _Jv_ThreadStartFunc *method;
40 _Jv_Thread_t *data;
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
50 DWORD _Jv_ThreadKey;
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.
58 // Thread started.
59 #define FLAG_START 0x01
60 // Thread is daemon.
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.
71 inline void
72 ensure_condvar_initialized(_Jv_ConditionVariable_t *cv)
74 if (cv->ev[0] == 0)
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).
88 int
89 _Jv_CondWait(_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos)
91 if (mu->owner != GetCurrentThreadId ( ))
92 return _JV_NOT_OWNER;
94 EnterCriticalSection (&cv->count_mutex);
95 ensure_condvar_initialized (cv);
96 cv->blocked_count++;
97 LeaveCriticalSection (&cv->count_mutex);
99 DWORD time;
100 if ((millis == 0) && (nanos > 0)) time = 1;
101 else if (millis == 0) time = INFINITE;
102 else time = millis;
104 _Jv_MutexUnlock (mu);
106 DWORD rval = WaitForMultipleObjects (2, &(cv->ev[0]), 0, time);
108 EnterCriticalSection(&cv->count_mutex);
109 cv->blocked_count--;
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);
115 if (last_waiter)
116 ResetEvent (cv->ev[1]);
118 _Jv_MutexLock (mu);
120 return 0;
123 void
124 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
126 // we do lazy creation of Events since CreateEvent() is insanely expensive
127 cv->ev[0] = 0;
128 InitializeCriticalSection (&cv->count_mutex);
129 cv->blocked_count = 0;
132 void
133 _Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
135 if (cv->ev[0] != 0)
137 CloseHandle (cv->ev[0]);
138 CloseHandle (cv->ev[1]);
140 cv->ev[0] = 0;
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]);
160 return 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]);
177 return 0;
181 // Threads.
184 void
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;
194 _Jv_Thread_t *
195 _Jv_ThreadInitData (java::lang::Thread* obj)
197 _Jv_Thread_t *data = (_Jv_Thread_t*)_Jv_Malloc(sizeof(_Jv_Thread_t));
198 data->flags = 0;
199 data->thread_obj = obj;
201 return data;
204 void
205 _Jv_ThreadDestroyData (_Jv_Thread_t *data)
207 _Jv_Free(data);
210 void
211 _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
213 int actual = THREAD_PRIORITY_NORMAL;
215 if (data->flags & FLAG_START)
217 switch (prio)
219 case 10:
220 actual = THREAD_PRIORITY_TIME_CRITICAL;
221 break;
222 case 9:
223 actual = THREAD_PRIORITY_HIGHEST;
224 break;
225 case 8:
226 case 7:
227 actual = THREAD_PRIORITY_ABOVE_NORMAL;
228 break;
229 case 6:
230 case 5:
231 actual = THREAD_PRIORITY_NORMAL;
232 break;
233 case 4:
234 case 3:
235 actual = THREAD_PRIORITY_BELOW_NORMAL;
236 break;
237 case 2:
238 actual = THREAD_PRIORITY_LOWEST;
239 break;
240 case 1:
241 actual = THREAD_PRIORITY_IDLE;
242 break;
244 SetThreadPriority(data->handle, actual);
248 void
249 _Jv_ThreadRegister (_Jv_Thread_t *data)
251 TlsSetValue (_Jv_ThreadKey, data->thread_obj);
252 TlsSetValue (_Jv_ThreadDataKey, data);
255 void
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
264 // return a value.
265 static DWORD WINAPI
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);
277 non_daemon_count--;
278 if (! non_daemon_count)
279 SetEvent (daemon_cond);
280 ReleaseMutex (daemon_mutex);
283 return 0;
286 void
287 _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data, _Jv_ThreadStartFunc *meth)
289 DWORD id;
290 struct starter *info;
292 // Do nothing if thread has already started
293 if (data->flags & FLAG_START)
294 return;
295 data->flags |= FLAG_START;
297 // FIXME: handle marking the info object for GC.
298 info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
299 info->method = meth;
300 info->data = data;
302 if (! thread->isDaemon ())
304 WaitForSingleObject (daemon_mutex, INFINITE);
305 non_daemon_count++;
306 ReleaseMutex (daemon_mutex);
308 else
309 data->flags |= FLAG_DAEMON;
311 HANDLE h = GC_CreateThread(NULL, 0, really_start, info, 0, &id);
312 _Jv_ThreadSetPriority(data, thread->getPriority());
314 //if (!h)
315 //JvThrow ();
318 void
319 _Jv_ThreadWait (void)
321 WaitForSingleObject (daemon_mutex, INFINITE);
322 if (non_daemon_count)
324 ReleaseMutex (daemon_mutex);
325 WaitForSingleObject (daemon_cond, INFINITE);
329 void
330 _Jv_ThreadInterrupt (_Jv_Thread_t *data)
332 MessageBox(NULL, "Unimplemented", "win32-threads.cc:_Jv_ThreadInterrupt", MB_OK);
333 // FIXME: