aboutsummaryrefslogtreecommitdiff
path: root/libjava/posix-threads.cc
blob: 825b0206c1906f6ca51364a146c833965cf0c015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// posix-threads.cc - interface between libjava and POSIX threads.

/* Copyright (C) 1998, 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

// TO DO:
// * Document signal handling limitations

#include <config.h>

// If we're using the Boehm GC, then we need to override some of the
// thread primitives.  This is fairly gross.
#ifdef HAVE_BOEHM_GC
extern "C"
{
#include <boehm-config.h>
#include <gc.h>
};
#endif /* HAVE_BOEHM_GC */

#include <stdlib.h>
#include <time.h>
#include <signal.h>

#include <cni.h>
#include <jvm.h>
#include <java/lang/Thread.h>
#include <java/lang/System.h>

// This is used to implement thread startup.
struct starter
{
  _Jv_ThreadStartFunc *method;
  java::lang::Thread *object;
  _Jv_Thread_t *data;
};

// This is the key used to map from the POSIX thread value back to the
// Java object representing the thread.  The key is global to all
// threads, so it is ok to make it a global here.
pthread_key_t _Jv_ThreadKey;

// We keep a count of all non-daemon threads which are running.  When
// this reaches zero, _Jv_ThreadWait returns.
static pthread_mutex_t daemon_mutex;
static pthread_cond_t daemon_cond;
static int non_daemon_count;

// The signal to use when interrupting a thread.
#ifdef LINUX_THREADS
  // LinuxThreads usurps both SIGUSR1 and SIGUSR2.
#  define INTR SIGHUP
#else /* LINUX_THREADS */
#  define INTR SIGUSR2
#endif /* LINUX_THREADS */

//
// These are the flags that can appear in _Jv_Thread_t.
//

// Thread started.
#define FLAG_START   0x01
// Thread is daemon.
#define FLAG_DAEMON  0x02



int
_Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
	      jlong millis, jint nanos)
{
  int r;
  pthread_mutex_t *pmu;
#ifdef HAVE_RECURSIVE_MUTEX
  pmu = mu;
#else
  pmu = &mu->mutex2;
#endif
  if (millis == 0 && nanos == 0)
    r = pthread_cond_wait (cv, pmu);
  else
    {
      struct timespec ts; 
      jlong m = millis + java::lang::System::currentTimeMillis (); 
      ts.tv_sec = m / 1000; 
      ts.tv_nsec = ((m % 1000) * 1000000) + nanos; 
             
      r = pthread_cond_timedwait (cv, pmu, &ts);
    }
  return r;
}

#ifndef RECURSIVE_MUTEX_IS_DEFAULT

void
_Jv_MutexInit (_Jv_Mutex_t *mu)
{
#ifdef HAVE_RECURSIVE_MUTEX
  pthread_mutexattr_t *val = NULL;

#if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE)
  pthread_mutexattr_t attr;

  // If this is slow, then allocate it statically and only initialize
  // it once.
  pthread_mutexattr_init (&attr);
  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
  val = &attr;
#elif defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
  pthread_mutexattr_t attr;
  pthread_mutexattr_init (&attr);
  pthread_mutexattr_setkind_np (&attr, PTHREAD_MUTEX_RECURSIVE_NP);
  val = &attr;
#endif

  pthread_mutex_init (mu, val);

#if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE) || defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
  pthread_mutexattr_destroy (&attr);
#endif

#else /* HAVE_RECURSIVE_MUTEX */

  // No recursive mutex, so simulate one.
  pthread_mutex_init (&mu->mutex, NULL);
  pthread_mutex_init (&mu->mutex2, NULL);
  pthread_cond_init (&mu->cond, 0);
  mu->count = 0;

#endif /* HAVE_RECURSIVE_MUTEX */
}

#endif /* not RECURSIVE_MUTEX_IS_DEFAULT */

#if ! defined (LINUX_THREADS) && ! defined (HAVE_RECURSIVE_MUTEX)

void
_Jv_MutexDestroy (_Jv_Mutex_t *mu)
{
  pthread_mutex_destroy (&mu->mutex);
  pthread_mutex_destroy (&mu->mutex2);
  pthread_cond_destroy (&mu->cond);
}

int
_Jv_MutexLock (_Jv_Mutex_t *mu)
{
  if (pthread_mutex_lock (&mu->mutex))
    return -1;
  while (1)
    {
      if (mu->count == 0)
	{
	  // Grab the lock.
	  mu->thread = pthread_self ();
	  mu->count = 1;
	  pthread_mutex_lock (&mu->mutex2);
	  break;
	}
      else if (pthread_self () == mu->thread)
	{
	  // Already have the lock.
	  mu->count += 1;
	  break;
	}
      else
	{
	  // Try to acquire the lock.
	  pthread_cond_wait (&mu->cond, &mu->mutex);
	}
    }
  pthread_mutex_unlock (&mu->mutex);
  return 0;
}

int
_Jv_MutexUnlock (_Jv_Mutex_t *mu)
{
  if (pthread_mutex_lock (&mu->mutex))
    return -1;
  int r = 0;
  if (mu->count == 0 || pthread_self () != mu->thread)
    r = -1;
  else
    {
      mu->count -= 1;
      if (! mu->count)
	{
	  pthread_mutex_unlock (&mu->mutex2);
	  pthread_cond_signal (&mu->cond);
	}
    }
  pthread_mutex_unlock (&mu->mutex);
  return r;
}

#endif /* not LINUX_THREADS and not HAVE_RECURSIVE_MUTEX */

static void
handle_intr (int)
{
  // Do nothing.
}

void
_Jv_InitThreads (void)
{
  pthread_key_create (&_Jv_ThreadKey, NULL);
  pthread_mutex_init (&daemon_mutex, NULL);
  pthread_cond_init (&daemon_cond, 0);
  non_daemon_count = 0;

  // Arrange for the interrupt signal to interrupt system calls.
  struct sigaction act;
  act.sa_handler = handle_intr;
  sigemptyset (&act.sa_mask);
  act.sa_flags = 0;
  sigaction (INTR, &act, NULL);

  // Arrange for SIGINT to be blocked to all threads.  It is only
  // deliverable to the master thread.
  sigset_t mask;
  sigemptyset (&mask);
  sigaddset (&mask, SIGINT);
  pthread_sigmask (SIG_BLOCK, &mask, NULL);
}

void
_Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *)
{
  _Jv_Thread_t *info = new _Jv_Thread_t;

  info->flags = 0;
  info->exception = NULL;

  // FIXME register a finalizer for INFO here.
  // FIXME also must mark INFO somehow.

  *data = info;
}

void
_Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
{
  if (data->flags & FLAG_START)
    {
      struct sched_param param;

      param.sched_priority = prio;
      pthread_setschedparam (data->thread, SCHED_RR, &param);
    }
}


// This is called as a cleanup handler when a thread is exiting.  We
// use it to throw the requested exception.  It's entirely possible
// that this approach is doomed to failure, in which case we'll need
// to adopt some alternate.  For instance, use a signal to implement
// _Jv_ThreadCancel.
static void
throw_cleanup (void *data)
{
  _Jv_Thread_t *td = (_Jv_Thread_t *) data;
  _Jv_Throw ((java::lang::Throwable *) td->exception);
}

void
_Jv_ThreadCancel (_Jv_Thread_t *data, void *error)
{
  data->exception = error;
  pthread_cancel (data->thread);
}

// This function is called when a thread is started.  We don't arrange
// to call the `run' method directly, because this function must
// return a value.
static void *
really_start (void *x)
{
  struct starter *info = (struct starter *) x;

  pthread_cleanup_push (throw_cleanup, info->data);
  pthread_setspecific (_Jv_ThreadKey, info->object);
  info->method (info->object);
  pthread_cleanup_pop (0);

  if (! (info->data->flags & FLAG_DAEMON))
    {
      pthread_mutex_lock (&daemon_mutex);
      --non_daemon_count;
      if (! non_daemon_count)
	pthread_cond_signal (&daemon_cond);
      pthread_mutex_unlock (&daemon_mutex);
    }

  return NULL;
}

void
_Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
		 _Jv_ThreadStartFunc *meth)
{
  struct sched_param param;
  pthread_attr_t attr;
  struct starter *info;

  if (data->flags & FLAG_START)
    return;
  data->flags |= FLAG_START;

  param.sched_priority = thread->getPriority();

  pthread_attr_init (&attr);
  pthread_attr_setschedparam (&attr, &param);

  // FIXME: handle marking the info object for GC.
  info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
  info->method = meth;
  info->object = thread;
  info->data = data;

  if (! thread->isDaemon())
    {
      pthread_mutex_lock (&daemon_mutex);
      ++non_daemon_count;
      pthread_mutex_unlock (&daemon_mutex);
    }
  else
    data->flags |= FLAG_DAEMON;
  pthread_create (&data->thread, &attr, really_start, (void *) info);

  pthread_attr_destroy (&attr);
}

void
_Jv_ThreadWait (void)
{
  // Arrange for SIGINT to be delivered to the master thread.
  sigset_t mask;
  sigemptyset (&mask);
  sigaddset (&mask, SIGINT);
  pthread_sigmask (SIG_UNBLOCK, &mask, NULL);

  pthread_mutex_lock (&daemon_mutex);
  if (non_daemon_count)
    pthread_cond_wait (&daemon_cond, &daemon_mutex);
  pthread_mutex_unlock (&daemon_mutex);
}

void
_Jv_ThreadInterrupt (_Jv_Thread_t *data)
{
  pthread_kill (data->thread, INTR);
}