allow all arm targets to use -mstructure-size-boundary=XX
[official-gcc.git] / libjava / posix-threads.cc
blob2ddc9bff67237b83bc35ad43c98dde4385c1133c
1 // posix-threads.cc - interface between libjava and POSIX threads.
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
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
9 details. */
11 // TO DO:
12 // * Document signal handling limitations
14 #include <config.h>
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"
21 #include <boehm-config.h>
22 #include <gc.h>
24 #endif /* HAVE_BOEHM_GC */
26 #include <stdlib.h>
27 #include <time.h>
28 #include <signal.h>
29 #include <errno.h>
31 #include <gcj/cni.h>
32 #include <jvm.h>
33 #include <java/lang/Thread.h>
34 #include <java/lang/System.h>
36 // This is used to implement thread startup.
37 struct starter
39 _Jv_ThreadStartFunc *method;
40 java::lang::Thread *object;
41 _Jv_Thread_t *data;
44 // This is the key used to map from the POSIX thread value back to the
45 // Java object representing the thread. The key is global to all
46 // threads, so it is ok to make it a global here.
47 pthread_key_t _Jv_ThreadKey;
49 // This is the key used to map from the POSIX thread value back to the
50 // _Jv_Thread_t* representing the thread.
51 pthread_key_t _Jv_ThreadDataKey;
53 // We keep a count of all non-daemon threads which are running. When
54 // this reaches zero, _Jv_ThreadWait returns.
55 static pthread_mutex_t daemon_mutex;
56 static pthread_cond_t daemon_cond;
57 static int non_daemon_count;
59 // The signal to use when interrupting a thread.
60 #ifdef LINUX_THREADS
61 // LinuxThreads usurps both SIGUSR1 and SIGUSR2.
62 # define INTR SIGHUP
63 #else /* LINUX_THREADS */
64 # define INTR SIGUSR2
65 #endif /* LINUX_THREADS */
68 // These are the flags that can appear in _Jv_Thread_t.
71 // Thread started.
72 #define FLAG_START 0x01
73 // Thread is daemon.
74 #define FLAG_DAEMON 0x02
75 // Thread was interrupted by _Jv_ThreadInterrupt.
76 #define FLAG_INTERRUPTED 0x04
80 int
81 _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
82 jlong millis, jint nanos)
84 if (_Jv_PthreadCheckMonitor (mu))
85 return 1;
87 int r;
88 pthread_mutex_t *pmu = _Jv_PthreadGetMutex (mu);
89 struct timespec ts;
90 jlong m, m2, startTime;
91 bool done_sleeping = false;
93 if (millis == 0 && nanos == 0)
94 r = pthread_cond_wait (cv, pmu);
95 else
97 startTime = java::lang::System::currentTimeMillis();
98 m = millis + startTime;
102 ts.tv_sec = m / 1000;
103 ts.tv_nsec = ((m % 1000) * 1000000) + nanos;
105 r = pthread_cond_timedwait (cv, pmu, &ts);
107 if (r == EINTR)
109 /* We were interrupted by a signal. Either this is
110 because we were interrupted intentionally (i.e. by
111 Thread.interrupt()) or by the GC if it is
112 signal-based. */
113 _Jv_Thread_t *current = _Jv_ThreadCurrentData();
114 if (current->flags & FLAG_INTERRUPTED)
116 current->flags &= ~(FLAG_INTERRUPTED);
117 done_sleeping = true;
119 else
121 /* We were woken up by the GC or another signal. */
122 m2 = java::lang::System::currentTimeMillis ();
123 if (m2 >= m)
125 r = 0;
126 done_sleeping = true;
130 else if (r == ETIMEDOUT)
132 /* A timeout is a normal result. */
133 r = 0;
134 done_sleeping = true;
136 else
137 done_sleeping = true;
139 while (! done_sleeping);
142 return r != 0;
145 #ifndef RECURSIVE_MUTEX_IS_DEFAULT
147 void
148 _Jv_MutexInit (_Jv_Mutex_t *mu)
150 #ifdef HAVE_RECURSIVE_MUTEX
151 pthread_mutexattr_t *val = NULL;
153 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE)
154 pthread_mutexattr_t attr;
156 // If this is slow, then allocate it statically and only initialize
157 // it once.
158 pthread_mutexattr_init (&attr);
159 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
160 val = &attr;
161 #elif defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
162 pthread_mutexattr_t attr;
163 pthread_mutexattr_init (&attr);
164 pthread_mutexattr_setkind_np (&attr, PTHREAD_MUTEX_RECURSIVE_NP);
165 val = &attr;
166 #endif
168 pthread_mutex_init (mu, val);
170 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE) || defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
171 pthread_mutexattr_destroy (&attr);
172 #endif
174 #else /* HAVE_RECURSIVE_MUTEX */
176 // No recursive mutex, so simulate one.
177 pthread_mutex_init (&mu->mutex, NULL);
178 pthread_mutex_init (&mu->mutex2, NULL);
179 pthread_cond_init (&mu->cond, 0);
180 mu->count = 0;
182 #endif /* HAVE_RECURSIVE_MUTEX */
185 #endif /* not RECURSIVE_MUTEX_IS_DEFAULT */
187 #if ! defined (LINUX_THREADS) && ! defined (HAVE_RECURSIVE_MUTEX)
189 void
190 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
192 pthread_mutex_destroy (&mu->mutex);
193 pthread_mutex_destroy (&mu->mutex2);
194 pthread_cond_destroy (&mu->cond);
198 _Jv_MutexLock (_Jv_Mutex_t *mu)
200 if (pthread_mutex_lock (&mu->mutex))
201 return -1;
202 while (1)
204 if (mu->count == 0)
206 // Grab the lock.
207 mu->thread = pthread_self ();
208 mu->count = 1;
209 pthread_mutex_lock (&mu->mutex2);
210 break;
212 else if (pthread_self () == mu->thread)
214 // Already have the lock.
215 mu->count += 1;
216 break;
218 else
220 // Try to acquire the lock.
221 pthread_cond_wait (&mu->cond, &mu->mutex);
224 pthread_mutex_unlock (&mu->mutex);
225 return 0;
229 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
231 if (pthread_mutex_lock (&mu->mutex))
232 return -1;
233 int r = 0;
234 if (mu->count == 0 || pthread_self () != mu->thread)
235 r = -1;
236 else
238 mu->count -= 1;
239 if (! mu->count)
241 pthread_mutex_unlock (&mu->mutex2);
242 pthread_cond_signal (&mu->cond);
245 pthread_mutex_unlock (&mu->mutex);
246 return r;
249 #endif /* not LINUX_THREADS and not HAVE_RECURSIVE_MUTEX */
251 static void
252 handle_intr (int)
254 // Do nothing.
257 void
258 _Jv_InitThreads (void)
260 pthread_key_create (&_Jv_ThreadKey, NULL);
261 pthread_key_create (&_Jv_ThreadDataKey, NULL);
262 pthread_mutex_init (&daemon_mutex, NULL);
263 pthread_cond_init (&daemon_cond, 0);
264 non_daemon_count = 0;
266 // Arrange for the interrupt signal to interrupt system calls.
267 struct sigaction act;
268 act.sa_handler = handle_intr;
269 sigemptyset (&act.sa_mask);
270 act.sa_flags = 0;
271 sigaction (INTR, &act, NULL);
273 // Arrange for SIGINT to be blocked to all threads. It is only
274 // deliverable to the master thread.
275 sigset_t mask;
276 sigemptyset (&mask);
277 sigaddset (&mask, SIGINT);
278 pthread_sigmask (SIG_BLOCK, &mask, NULL);
281 void
282 _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *)
284 _Jv_Thread_t *info = new _Jv_Thread_t;
286 info->flags = 0;
287 info->exception = NULL;
289 // FIXME register a finalizer for INFO here.
290 // FIXME also must mark INFO somehow.
292 *data = info;
295 void
296 _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
298 if (data->flags & FLAG_START)
300 struct sched_param param;
302 param.sched_priority = prio;
303 pthread_setschedparam (data->thread, SCHED_RR, &param);
308 // This is called as a cleanup handler when a thread is exiting. We
309 // use it to throw the requested exception. It's entirely possible
310 // that this approach is doomed to failure, in which case we'll need
311 // to adopt some alternate. For instance, use a signal to implement
312 // _Jv_ThreadCancel.
313 static void
314 throw_cleanup (void *data)
316 _Jv_Thread_t *td = (_Jv_Thread_t *) data;
317 _Jv_Throw ((java::lang::Throwable *) td->exception);
320 void
321 _Jv_ThreadCancel (_Jv_Thread_t *data, void *error)
323 data->exception = error;
324 pthread_cancel (data->thread);
327 // This function is called when a thread is started. We don't arrange
328 // to call the `run' method directly, because this function must
329 // return a value.
330 static void *
331 really_start (void *x)
333 struct starter *info = (struct starter *) x;
335 pthread_cleanup_push (throw_cleanup, info->data);
336 pthread_setspecific (_Jv_ThreadKey, info->object);
337 pthread_setspecific (_Jv_ThreadDataKey, info->data);
338 info->method (info->object);
339 pthread_cleanup_pop (0);
341 if (! (info->data->flags & FLAG_DAEMON))
343 pthread_mutex_lock (&daemon_mutex);
344 --non_daemon_count;
345 if (! non_daemon_count)
346 pthread_cond_signal (&daemon_cond);
347 pthread_mutex_unlock (&daemon_mutex);
350 return NULL;
353 void
354 _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
355 _Jv_ThreadStartFunc *meth)
357 struct sched_param param;
358 pthread_attr_t attr;
359 struct starter *info;
361 if (data->flags & FLAG_START)
362 return;
363 data->flags |= FLAG_START;
365 param.sched_priority = thread->getPriority();
367 pthread_attr_init (&attr);
368 pthread_attr_setschedparam (&attr, &param);
370 // FIXME: handle marking the info object for GC.
371 info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
372 info->method = meth;
373 info->object = thread;
374 info->data = data;
376 if (! thread->isDaemon())
378 pthread_mutex_lock (&daemon_mutex);
379 ++non_daemon_count;
380 pthread_mutex_unlock (&daemon_mutex);
382 else
383 data->flags |= FLAG_DAEMON;
384 pthread_create (&data->thread, &attr, really_start, (void *) info);
386 pthread_attr_destroy (&attr);
389 void
390 _Jv_ThreadWait (void)
392 // Arrange for SIGINT to be delivered to the master thread.
393 sigset_t mask;
394 sigemptyset (&mask);
395 sigaddset (&mask, SIGINT);
396 pthread_sigmask (SIG_UNBLOCK, &mask, NULL);
398 pthread_mutex_lock (&daemon_mutex);
399 if (non_daemon_count)
400 pthread_cond_wait (&daemon_cond, &daemon_mutex);
401 pthread_mutex_unlock (&daemon_mutex);
404 void
405 _Jv_ThreadInterrupt (_Jv_Thread_t *data)
407 data->flags |= FLAG_INTERRUPTED;
408 pthread_kill (data->thread, INTR);