1 // posix-threads.cc - interface between libjava and POSIX threads.
3 /* Copyright (C) 1998, 1999 Red Hat, Inc.
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 // * Document signal handling limitations
16 // If we're using the Boehm GC, then we need to override some of the
17 // thread primitives. This is fairly gross.
24 #endif /* HAVE_BOEHM_GC */
34 #include <java/lang/Thread.h>
35 #include <java/lang/System.h>
36 #include <java/lang/Long.h>
37 #include <java/lang/OutOfMemoryError.h>
39 // This is used to implement thread startup.
42 _Jv_ThreadStartFunc *method;
43 java::lang::Thread *object;
47 // This is the key used to map from the POSIX thread value back to the
48 // Java object representing the thread. The key is global to all
49 // threads, so it is ok to make it a global here.
50 pthread_key_t _Jv_ThreadKey;
52 // This is the key used to map from the POSIX thread value back to the
53 // _Jv_Thread_t* representing the thread.
54 pthread_key_t _Jv_ThreadDataKey;
56 // We keep a count of all non-daemon threads which are running. When
57 // this reaches zero, _Jv_ThreadWait returns.
58 static pthread_mutex_t daemon_mutex;
59 static pthread_cond_t daemon_cond;
60 static int non_daemon_count;
62 // The signal to use when interrupting a thread.
64 // LinuxThreads (prior to glibc 2.1) usurps both SIGUSR1 and SIGUSR2.
66 #else /* LINUX_THREADS */
68 #endif /* LINUX_THREADS */
71 // These are the flags that can appear in _Jv_Thread_t.
75 #define FLAG_START 0x01
77 #define FLAG_DAEMON 0x02
82 _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
83 jlong millis, jint nanos)
85 if (_Jv_PthreadCheckMonitor (mu))
89 pthread_mutex_t *pmu = _Jv_PthreadGetMutex (mu);
91 jlong m, m2, startTime;
92 bool done_sleeping = false;
94 if (millis == 0 && nanos == 0)
97 // pthread_cond_timedwait can be interrupted by a signal on linux, while
98 // pthread_cond_wait can not. So pthread_cond_timedwait() forever.
99 m = java::lang::Long::MAX_VALUE;
100 ts.tv_sec = LONG_MAX;
106 startTime = java::lang::System::currentTimeMillis();
107 m = millis + startTime;
108 ts.tv_sec = m / 1000;
109 ts.tv_nsec = ((m % 1000) * 1000000) + nanos;
112 java::lang::Thread *current = _Jv_ThreadCurrent();
117 // Check to ensure the thread hasn't already been interrupted.
118 if (!(current->isInterrupted ()))
121 // FIXME: in theory, interrupt() could be called on this thread
122 // between the test above and the wait below, resulting in the
123 // interupt() call failing. I don't see a way to fix this
124 // without significant changes to the implementation.
125 r = pthread_cond_timedwait (cv, pmu, &ts);
127 if (millis == 0 && nanos == 0)
128 r = pthread_cond_wait (cv, pmu);
130 r = pthread_cond_timedwait (cv, pmu, &ts);
136 /* We were interrupted by a signal. Either this is
137 because we were interrupted intentionally (i.e. by
138 Thread.interrupt()) or by the GC if it is
140 if (current->isInterrupted ())
143 done_sleeping = true;
147 /* We were woken up by the GC or another signal. */
148 m2 = java::lang::System::currentTimeMillis ();
152 done_sleeping = true;
156 else if (r == ETIMEDOUT)
158 /* A timeout is a normal result. */
160 done_sleeping = true;
163 done_sleeping = true;
165 while (! done_sleeping);
170 #ifndef RECURSIVE_MUTEX_IS_DEFAULT
173 _Jv_MutexInit (_Jv_Mutex_t *mu)
175 #ifdef HAVE_RECURSIVE_MUTEX
176 pthread_mutexattr_t *val = NULL;
178 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE)
179 pthread_mutexattr_t attr;
181 // If this is slow, then allocate it statically and only initialize
183 pthread_mutexattr_init (&attr);
184 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
186 #elif defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
187 pthread_mutexattr_t attr;
188 pthread_mutexattr_init (&attr);
189 pthread_mutexattr_setkind_np (&attr, PTHREAD_MUTEX_RECURSIVE_NP);
193 pthread_mutex_init (_Jv_PthreadGetMutex (mu), val);
194 #ifdef PTHREAD_MUTEX_IS_STRUCT
198 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE) || defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
199 pthread_mutexattr_destroy (&attr);
202 #else /* HAVE_RECURSIVE_MUTEX */
204 // No recursive mutex, so simulate one.
205 pthread_mutex_init (&mu->mutex, NULL);
206 pthread_mutex_init (&mu->mutex2, NULL);
207 pthread_cond_init (&mu->cond, 0);
210 #endif /* HAVE_RECURSIVE_MUTEX */
213 #endif /* not RECURSIVE_MUTEX_IS_DEFAULT */
215 #if ! defined (LINUX_THREADS) && ! defined (HAVE_RECURSIVE_MUTEX)
218 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
220 pthread_mutex_destroy (&mu->mutex);
221 pthread_mutex_destroy (&mu->mutex2);
222 pthread_cond_destroy (&mu->cond);
226 _Jv_MutexLock (_Jv_Mutex_t *mu)
228 if (pthread_mutex_lock (&mu->mutex))
235 mu->thread = pthread_self ();
237 pthread_mutex_lock (&mu->mutex2);
240 else if (pthread_self () == mu->thread)
242 // Already have the lock.
248 // Try to acquire the lock.
249 pthread_cond_wait (&mu->cond, &mu->mutex);
252 pthread_mutex_unlock (&mu->mutex);
257 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
259 if (pthread_mutex_lock (&mu->mutex))
262 if (mu->count == 0 || pthread_self () != mu->thread)
269 pthread_mutex_unlock (&mu->mutex2);
270 pthread_cond_signal (&mu->cond);
273 pthread_mutex_unlock (&mu->mutex);
277 #endif /* not LINUX_THREADS and not HAVE_RECURSIVE_MUTEX */
286 _Jv_InitThreads (void)
288 pthread_key_create (&_Jv_ThreadKey, NULL);
289 pthread_key_create (&_Jv_ThreadDataKey, NULL);
290 pthread_mutex_init (&daemon_mutex, NULL);
291 pthread_cond_init (&daemon_cond, 0);
292 non_daemon_count = 0;
294 // Arrange for the interrupt signal to interrupt system calls.
295 struct sigaction act;
296 act.sa_handler = handle_intr;
297 sigemptyset (&act.sa_mask);
299 sigaction (INTR, &act, NULL);
303 _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *)
305 _Jv_Thread_t *info = new _Jv_Thread_t;
308 // FIXME register a finalizer for INFO here.
309 // FIXME also must mark INFO somehow.
315 _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
317 if (data->flags & FLAG_START)
319 struct sched_param param;
321 param.sched_priority = prio;
322 pthread_setschedparam (data->thread, SCHED_RR, ¶m);
326 // This function is called when a thread is started. We don't arrange
327 // to call the `run' method directly, because this function must
330 really_start (void *x)
332 struct starter *info = (struct starter *) x;
334 pthread_setspecific (_Jv_ThreadKey, info->object);
335 pthread_setspecific (_Jv_ThreadDataKey, info->data);
336 info->method (info->object);
338 if (! (info->data->flags & FLAG_DAEMON))
340 pthread_mutex_lock (&daemon_mutex);
342 if (! non_daemon_count)
343 pthread_cond_signal (&daemon_cond);
344 pthread_mutex_unlock (&daemon_mutex);
351 _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
352 _Jv_ThreadStartFunc *meth)
354 struct sched_param param;
356 struct starter *info;
358 if (data->flags & FLAG_START)
360 data->flags |= FLAG_START;
362 param.sched_priority = thread->getPriority();
364 pthread_attr_init (&attr);
365 pthread_attr_setschedparam (&attr, ¶m);
367 // FIXME: handle marking the info object for GC.
368 info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
370 info->object = thread;
373 if (! thread->isDaemon())
375 pthread_mutex_lock (&daemon_mutex);
377 pthread_mutex_unlock (&daemon_mutex);
380 data->flags |= FLAG_DAEMON;
381 int r = pthread_create (&data->thread, &attr, really_start, (void *) info);
383 pthread_attr_destroy (&attr);
387 const char* msg = "Cannot create additional threads";
388 JvThrow (new java::lang::OutOfMemoryError (JvNewStringUTF (msg)));
393 _Jv_ThreadWait (void)
395 pthread_mutex_lock (&daemon_mutex);
396 if (non_daemon_count)
397 pthread_cond_wait (&daemon_cond, &daemon_mutex);
398 pthread_mutex_unlock (&daemon_mutex);
402 _Jv_ThreadInterrupt (_Jv_Thread_t *data)
404 pthread_kill (data->thread, INTR);