posix-threads.cc (_Jv_MutexLock): Moved back to posix-threads.h.
[gcc.git] / libjava / posix-threads.cc
1 // posix-threads.cc - interface between libjava and POSIX threads.
2
3 /* Copyright (C) 1998, 1999 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 // TO DO:
12 // * Document signal handling limitations
13
14 #include <config.h>
15
16 // If we're using the Boehm GC, then we need to override some of the
17 // thread primitives. This is fairly gross.
18 #ifdef HAVE_BOEHM_GC
19 extern "C"
20 {
21 #include <gcconfig.h>
22 #include <gc.h>
23 };
24 #endif /* HAVE_BOEHM_GC */
25
26 #include <stdlib.h>
27 #include <time.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <limits.h>
31
32 #include <gcj/cni.h>
33 #include <jvm.h>
34 #include <java/lang/Thread.h>
35 #include <java/lang/System.h>
36 #include <java/lang/Long.h>
37 #include <java/lang/OutOfMemoryError.h>
38
39 // This is used to implement thread startup.
40 struct starter
41 {
42 _Jv_ThreadStartFunc *method;
43 _Jv_Thread_t *data;
44 };
45
46 // This is the key used to map from the POSIX thread value back to the
47 // Java object representing the thread. The key is global to all
48 // threads, so it is ok to make it a global here.
49 pthread_key_t _Jv_ThreadKey;
50
51 // This is the key used to map from the POSIX thread value back to the
52 // _Jv_Thread_t* representing the thread.
53 pthread_key_t _Jv_ThreadDataKey;
54
55 // We keep a count of all non-daemon threads which are running. When
56 // this reaches zero, _Jv_ThreadWait returns.
57 static pthread_mutex_t daemon_mutex;
58 static pthread_cond_t daemon_cond;
59 static int non_daemon_count;
60
61 // The signal to use when interrupting a thread.
62 #ifdef LINUX_THREADS
63 // LinuxThreads (prior to glibc 2.1) usurps both SIGUSR1 and SIGUSR2.
64 # define INTR SIGHUP
65 #else /* LINUX_THREADS */
66 # define INTR SIGUSR2
67 #endif /* LINUX_THREADS */
68
69 //
70 // These are the flags that can appear in _Jv_Thread_t.
71 //
72
73 // Thread started.
74 #define FLAG_START 0x01
75 // Thread is daemon.
76 #define FLAG_DAEMON 0x02
77
78 \f
79
80 // Wait for the condition variable "CV" to be notified.
81 // Return values:
82 // 0: the condition was notified, or the timeout expired.
83 // _JV_NOT_OWNER: the thread does not own the mutex "MU".
84 // _JV_INTERRUPTED: the thread was interrupted. Its interrupted flag is set.
85 int
86 _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
87 jlong millis, jint nanos)
88 {
89 pthread_t self = pthread_self();
90 if (mu->owner != self)
91 return _JV_NOT_OWNER;
92
93 struct timespec ts;
94 jlong m, startTime;
95
96 if (millis > 0 || nanos > 0)
97 {
98 startTime = java::lang::System::currentTimeMillis();
99 m = millis + startTime;
100 ts.tv_sec = m / 1000;
101 ts.tv_nsec = ((m % 1000) * 1000000) + nanos;
102 }
103
104 _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
105 java::lang::Thread *current_obj = _Jv_ThreadCurrent ();
106
107 // Add this thread to the cv's wait set.
108 current->next = NULL;
109
110 if (cv->first == NULL)
111 cv->first = current;
112 else
113 for (_Jv_Thread_t *t = cv->first;; t = t->next)
114 {
115 if (t->next == NULL)
116 {
117 t->next = current;
118 break;
119 }
120 }
121
122 pthread_mutex_lock (&current->wait_mutex);
123
124 // Now that we hold the wait mutex, check if this thread has been
125 // interrupted already.
126 if (current_obj->interrupt_flag)
127 {
128 pthread_mutex_unlock (&current->wait_mutex);
129 return _JV_INTERRUPTED;
130 }
131
132 // Record the current lock depth, so it can be restored when we re-aquire it.
133 int count = mu->count;
134
135 // Release the monitor mutex.
136 mu->count = 0;
137 mu->owner = 0;
138 pthread_mutex_unlock (&mu->mutex);
139
140 int r = 0;
141 bool done_sleeping = false;
142
143 while (! done_sleeping)
144 {
145 if (millis == 0 && nanos == 0)
146 r = pthread_cond_wait (&current->wait_cond, &current->wait_mutex);
147 else
148 r = pthread_cond_timedwait (&current->wait_cond, &current->wait_mutex,
149 &ts);
150
151 // In older glibc's (prior to 2.1.3), the cond_wait functions may
152 // spuriously wake up on a signal. Catch that here.
153 if (r != EINTR)
154 done_sleeping = true;
155 }
156
157 // Check for an interrupt *before* unlocking the wait mutex.
158 jboolean interrupted = current_obj->interrupt_flag;
159
160 pthread_mutex_unlock (&current->wait_mutex);
161
162 // Reaquire the monitor mutex, and restore the lock count.
163 pthread_mutex_lock (&mu->mutex);
164 mu->owner = self;
165 mu->count = count;
166
167 // If we were interrupted, or if a timeout occured, remove ourself from
168 // the cv wait list now. (If we were notified normally, notify() will have
169 // already taken care of this)
170 if (r == ETIMEDOUT || interrupted)
171 {
172 _Jv_Thread_t *prev = NULL;
173 for (_Jv_Thread_t *t = cv->first; t != NULL; t = t->next)
174 {
175 if (t == current)
176 {
177 if (prev != NULL)
178 prev->next = t->next;
179 else
180 cv->first = t->next;
181 t->next = NULL;
182 break;
183 }
184 prev = t;
185 }
186 if (interrupted)
187 return _JV_INTERRUPTED;
188 }
189
190 return 0;
191 }
192
193 int
194 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
195 {
196 if (_Jv_PthreadCheckMonitor (mu))
197 return _JV_NOT_OWNER;
198
199 _Jv_Thread_t *target;
200 _Jv_Thread_t *prev = NULL;
201
202 for (target = cv->first; target != NULL; target = target->next)
203 {
204 pthread_mutex_lock (&target->wait_mutex);
205
206 if (target->thread_obj->interrupt_flag)
207 {
208 // Don't notify a thread that has already been interrupted.
209 pthread_mutex_unlock (&target->wait_mutex);
210 prev = target;
211 continue;
212 }
213
214 pthread_cond_signal (&target->wait_cond);
215 pthread_mutex_unlock (&target->wait_mutex);
216
217 // Two successive notify() calls should not be delivered to the same
218 // thread, so we remove the target thread from the cv wait list now.
219 if (prev == NULL)
220 cv->first = target->next;
221 else
222 prev->next = target->next;
223
224 target->next = NULL;
225
226 break;
227 }
228
229 return 0;
230 }
231
232 int
233 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
234 {
235 if (_Jv_PthreadCheckMonitor (mu))
236 return _JV_NOT_OWNER;
237
238 _Jv_Thread_t *target;
239 _Jv_Thread_t *prev = NULL;
240
241 for (target = cv->first; target != NULL; target = target->next)
242 {
243 pthread_mutex_lock (&target->wait_mutex);
244 pthread_cond_signal (&target->wait_cond);
245 pthread_mutex_unlock (&target->wait_mutex);
246
247 if (prev != NULL)
248 prev->next = NULL;
249 prev = target;
250 }
251 if (prev != NULL)
252 prev->next = NULL;
253
254 cv->first = NULL;
255
256 return 0;
257 }
258
259 void
260 _Jv_ThreadInterrupt (_Jv_Thread_t *data)
261 {
262 pthread_mutex_lock (&data->wait_mutex);
263
264 // Set the thread's interrupted flag *after* aquiring its wait_mutex. This
265 // ensures that there are no races with the interrupt flag being set after
266 // the waiting thread checks it and before pthread_cond_wait is entered.
267 data->thread_obj->interrupt_flag = true;
268
269 // Interrupt blocking system calls using a signal.
270 // pthread_kill (data->thread, INTR);
271
272 pthread_cond_signal (&data->wait_cond);
273
274 pthread_mutex_unlock (&data->wait_mutex);
275 }
276
277 static void
278 handle_intr (int)
279 {
280 // Do nothing.
281 }
282
283 void
284 _Jv_InitThreads (void)
285 {
286 pthread_key_create (&_Jv_ThreadKey, NULL);
287 pthread_key_create (&_Jv_ThreadDataKey, NULL);
288 pthread_mutex_init (&daemon_mutex, NULL);
289 pthread_cond_init (&daemon_cond, 0);
290 non_daemon_count = 0;
291
292 // Arrange for the interrupt signal to interrupt system calls.
293 struct sigaction act;
294 act.sa_handler = handle_intr;
295 sigemptyset (&act.sa_mask);
296 act.sa_flags = 0;
297 sigaction (INTR, &act, NULL);
298 }
299
300 void
301 _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *obj)
302 {
303 _Jv_Thread_t *info = new _Jv_Thread_t;
304 info->flags = 0;
305 info->thread_obj = obj;
306
307 pthread_mutex_init (&info->wait_mutex, NULL);
308 pthread_cond_init (&info->wait_cond, NULL);
309
310 // FIXME register a finalizer for INFO here.
311 // FIXME also must mark INFO somehow.
312
313 *data = info;
314 }
315
316 void
317 _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
318 {
319 if (data->flags & FLAG_START)
320 {
321 struct sched_param param;
322
323 param.sched_priority = prio;
324 pthread_setschedparam (data->thread, SCHED_RR, &param);
325 }
326 }
327
328 // This function is called when a thread is started. We don't arrange
329 // to call the `run' method directly, because this function must
330 // return a value.
331 static void *
332 really_start (void *x)
333 {
334 struct starter *info = (struct starter *) x;
335
336 pthread_setspecific (_Jv_ThreadKey, info->data->thread_obj);
337 pthread_setspecific (_Jv_ThreadDataKey, info->data);
338
339 // glibc 2.1.3 doesn't set the value of `thread' until after start_routine
340 // is called. Since it may need to be accessed from the new thread, work
341 // around the potential race here by explicitly setting it again.
342 info->data->thread = pthread_self ();
343
344 info->method (info->data->thread_obj);
345
346 if (! (info->data->flags & FLAG_DAEMON))
347 {
348 pthread_mutex_lock (&daemon_mutex);
349 --non_daemon_count;
350 if (! non_daemon_count)
351 pthread_cond_signal (&daemon_cond);
352 pthread_mutex_unlock (&daemon_mutex);
353 }
354
355 #ifndef LINUX_THREADS
356 // Clean up. These calls do nothing on Linux.
357 pthread_mutex_destroy (&info->data->wait_mutex);
358 pthread_cond_destroy (&info->data->wait_cond);
359 #endif /* ! LINUX_THREADS */
360
361 return NULL;
362 }
363
364 void
365 _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
366 _Jv_ThreadStartFunc *meth)
367 {
368 struct sched_param param;
369 pthread_attr_t attr;
370 struct starter *info;
371
372 if (data->flags & FLAG_START)
373 return;
374 data->flags |= FLAG_START;
375
376 param.sched_priority = thread->getPriority();
377
378 pthread_attr_init (&attr);
379 pthread_attr_setschedparam (&attr, &param);
380
381 // FIXME: handle marking the info object for GC.
382 info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
383 info->method = meth;
384 info->data = data;
385
386 if (! thread->isDaemon())
387 {
388 pthread_mutex_lock (&daemon_mutex);
389 ++non_daemon_count;
390 pthread_mutex_unlock (&daemon_mutex);
391 }
392 else
393 data->flags |= FLAG_DAEMON;
394 int r = pthread_create (&data->thread, &attr, really_start, (void *) info);
395
396 pthread_attr_destroy (&attr);
397
398 if (r)
399 {
400 const char* msg = "Cannot create additional threads";
401 JvThrow (new java::lang::OutOfMemoryError (JvNewStringUTF (msg)));
402 }
403 }
404
405 void
406 _Jv_ThreadWait (void)
407 {
408 pthread_mutex_lock (&daemon_mutex);
409 if (non_daemon_count)
410 pthread_cond_wait (&daemon_cond, &daemon_mutex);
411 pthread_mutex_unlock (&daemon_mutex);
412 }