summaryrefslogtreecommitdiff
path: root/libjava/jni/gtk-peer/gthread-jni.c
blob: a762b4e56edf87018b910ecc0bc73a1f1948de6f (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* gthread-jni.c -- JNI threading routines for GLIB
   Copyright (C) 1998 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

/************************************************************************/
/* Header				     				*/
/************************************************************************/

/*
 * Julian Dolby (dolby@us.ibm.com)
 * February 7, 2003
 *
 *  This code implements the GThreadFunctions interface for GLIB using 
 * Java threading primitives.  All of the locking and conditional variable
 * functionality required by GThreadFunctions is implemented using the
 * monitor and wait/notify functionality of Java objects.  The thread-
 * local fucntionality uses the java.lang.ThreadLocal class. 
 *
 *  This code is designed to be portable in that it makes no assumptions
 * about the underlying VM beyond that it implements the JNI functionality
 * that this code uses.
 *
 *  The one piece that does not really work is trylock for mutexes.  The
 * Java locking model does not include such functionality, and I do not
 * see how to implement it without knowing something about how the VM
 * implements locking.  
 *
 * NOTES:
 *
 *  I have tested it only on JikesRVM---the CVS head as of early February
 * 2003.
 *
 *  Currently, use of this code is governed by the configuration option
 * --enable-portable-native-sync
 *
 */


/************************************************************************/
/* Global data				     				*/
/************************************************************************/

#include "gthread-jni.h"

/*  The VM handle.  This is set in GtkToolkitMain.gtkInit */
JavaVM *gdk_vm;


/************************************************************************/
/* Utilities to reflect exceptions back to the VM			*/
/************************************************************************/

/*  This function checks for a pending exception, and rethrows it with
 * a wrapper RuntimeException to deal with possible type problems (in
 * case some calling piece of code does not expect the exception being
 * thrown) and to include the given extra message.
 */
static void maybe_rethrow(JNIEnv *gdk_env, char *message, char *file, int line) {
  jthrowable cause;

  /* rethrow if an exception happened */
  if ((cause = (*gdk_env)->ExceptionOccurred(gdk_env)) != NULL) {
    jstring jmessage;
  jclass obj_class;
    jobject obj;
    jmethodID ctor;

    /* allocate local message in Java */
    int len = strlen(message) + strlen(file) + 25;
    char buf[ len ];
    bzero(buf, len);
    sprintf(buf, "%s (at %s:%d)", message, file, line);
    jmessage = (*gdk_env)->NewStringUTF(gdk_env, buf);
    
    /* create RuntimeException wrapper object */
    obj_class = (*gdk_env)->FindClass (gdk_env, "java/lang/RuntimeException");
    ctor = (*gdk_env)->GetMethodID(gdk_env, obj_class, "<init>", "(Ljava/langString;Ljava/lang/Throwable)V");
    obj = (*gdk_env)->NewObject (gdk_env, obj_class, ctor, jmessage, cause);

    /* throw it */
    (*gdk_env)->Throw(gdk_env, (jthrowable)obj);
    }
}

/* This macro is used to include a source location in the exception message */
#define MAYBE_RETHROW(_class, _message) \
maybe_rethrow(_class, _message, __FILE__, __LINE__)


/************************************************************************/
/* Utilities to allocate and free java.lang.Objects			*/
/************************************************************************/

/*  Both the mutexes and the condition variables are java.lang.Object objects,
 * which this method allocates and returns a global ref.  Note that global
 * refs must be explicitly freed (isn't C fun?).
 */
static jobject *allocatePlainObject() {
  jclass obj_class;
  jobject *obj;
  JNIEnv *gdk_env;
  jmethodID ctor;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  obj_class = (*gdk_env)->FindClass (gdk_env, "java/lang/Object");
  MAYBE_RETHROW(gdk_env, "cannot find Object");

  ctor = (*gdk_env)->GetMethodID(gdk_env, obj_class, "<init>", "()V");
  MAYBE_RETHROW(gdk_env, "cannot find constructor");

  obj = (jobject *) g_malloc (sizeof (jobject));
  *obj = (*gdk_env)->NewObject (gdk_env, obj_class, ctor);
  MAYBE_RETHROW(gdk_env, "cannot allocate object");
  
  *obj = (*gdk_env)->NewGlobalRef (gdk_env, *obj);
  MAYBE_RETHROW(gdk_env, "cannot make global ref");

  return obj;
}

/*  Frees a Java object given a global ref (isn't C fun?) */
static void freePlainObject(jobject *obj) {
  JNIEnv *gdk_env;

  if (obj) {
    (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

    (*gdk_env)->DeleteGlobalRef (gdk_env, *obj);
    MAYBE_RETHROW(gdk_env, "cannot delete global ref");
  
    g_free (obj);
  }
}


/************************************************************************/
/* Locking code				     				*/
/************************************************************************/

/* Lock a Java object */
static void takeLock(JNIEnv *gdk_env, void *mutex) {
  (*gdk_env)->MonitorEnter (gdk_env, *((jobject *)mutex));
  MAYBE_RETHROW(gdk_env, "cannot get lock");
}

/* Unlock a Java object */
static void releaseLock(JNIEnv *gdk_env, void *mutex) {
    (*gdk_env)->MonitorExit (gdk_env, *((jobject *)mutex));
  MAYBE_RETHROW(gdk_env, "cannot release lock");
}

/* Create a mutex, which is a java.lang.Object for us */
static GMutex *g_mutex_new_jni_impl (void) {
  return (GMutex*) allocatePlainObject();
}

/* Lock a mutex. */
static void g_mutex_lock_jni_impl (GMutex *mutex __attribute__((unused))) {
  JNIEnv *gdk_env;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  takeLock(gdk_env, mutex);
}

/*  Try to lock a mutex.  Actually, do not try because Java objects
 * do not provide such an interface.  To be at least minimally correct,
 * pretend we tried and failed.
 */
static gboolean g_mutex_trylock_jni_impl
  (GMutex *mutex __attribute__((unused)))
{
  // Shall we implement this in a JikesRVM-specific way under a flag?
  return FALSE;
}

/* Unlock a mutex. */
static void g_mutex_unlock_jni_impl (GMutex *mutex) {
  JNIEnv *gdk_env;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  releaseLock(gdk_env, mutex);
}

/* Free a mutex (isn't C fun?) */
static void g_mutex_free_jni_impl (GMutex *mutex)
{
  freePlainObject( (jobject*)mutex );
}


/************************************************************************/
/* Condition variable code		     				*/
/************************************************************************/

/* Create a new condition variable.  This is a java.lang.Object for us. */
static GCond *g_cond_new_jni_impl () {
  return (GCond*)allocatePlainObject();
}

/*  Signal on a condition variable.  This is simply calling Object.notify
 * for us.
 */
static void g_cond_signal_jni_impl (GCond *cond) {
  jclass lcl_class;
  jmethodID signal_mth;
  JNIEnv *gdk_env;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  lcl_class = (*gdk_env)->FindClass (gdk_env, "java.lang.Object");
  MAYBE_RETHROW(gdk_env, "cannot find Object");
  
  signal_mth = (*gdk_env)->GetMethodID(gdk_env, lcl_class, "notify", "()V");
  MAYBE_RETHROW(gdk_env, "cannot find Object.<notify>");

  /* Must have locked an object to call notify */
  takeLock(gdk_env, cond);

  (*gdk_env)->CallVoidMethod(gdk_env, *(jobject*)cond, signal_mth);
  MAYBE_RETHROW(gdk_env, "cannot signal mutex");

  releaseLock(gdk_env, cond);
}

/*  Broadcast to all waiting on a condition variable.  This is simply 
 * calling Object.notifyAll for us.
 */
static void g_cond_broadcast_jni_impl (GCond *cond) {
  jclass lcl_class;
  jmethodID bcast_mth;
  JNIEnv *gdk_env;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  lcl_class = (*gdk_env)->FindClass (gdk_env, "java.lang.Object");
  MAYBE_RETHROW(gdk_env, "cannot find Object");
  
  bcast_mth = (*gdk_env)->GetMethodID(gdk_env, lcl_class, "notifyAll", "()V");
  MAYBE_RETHROW(gdk_env, "cannot find Object.<notifyAll>");

  /* Must have locked an object to call notifyAll */
  takeLock(gdk_env, cond);

  (*gdk_env)->CallVoidMethod(gdk_env, *(jobject*)cond, bcast_mth);
  MAYBE_RETHROW(gdk_env, "cannot broadcast to mutex");

  releaseLock(gdk_env, cond);
}


/*  Wait on a condition variable.  For us, this simply means call
 * Object.wait.
 */
static void g_cond_wait_jni_impl
  (GCond *cond, GMutex *mutex __attribute__((unused)))
{
  jclass lcl_class;
  jmethodID wait_mth;
  JNIEnv *gdk_env;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  lcl_class = (*gdk_env)->FindClass (gdk_env, "java.lang.Object");
  MAYBE_RETHROW(gdk_env, "cannot find Object");
  
  wait_mth = (*gdk_env)->GetMethodID(gdk_env, lcl_class, "wait", "()V");
  MAYBE_RETHROW(gdk_env, "cannot find Object.<wait>");

  /* Must have locked an object to call wait */
  takeLock(gdk_env, cond);

  (*gdk_env)->CallVoidMethod(gdk_env, *(jobject*)cond, wait_mth);
  MAYBE_RETHROW(gdk_env, "cannot wait on mutex");

  releaseLock(gdk_env, cond);
}

/*  Wait on a condition vairable until a timeout.  This is a little tricky
 * for us.  We first call Object.wait(J) giving it the appropriate timeout
 * value.  On return, we check whether an InterruptedException happened.  If
 * so, that is Java-speak for wait timing out.
 */
static gboolean
g_cond_timed_wait_jni_impl
  (GCond *cond, GMutex *mutex __attribute__((unused)),
   GTimeVal *end_time)
{
  jclass lcl_class;
  jmethodID wait_mth;
  JNIEnv *gdk_env;
  jlong time;
  jthrowable cause;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  lcl_class = (*gdk_env)->FindClass (gdk_env, "java.lang.Object");
  MAYBE_RETHROW(gdk_env, "cannot find Object");
  
  wait_mth = (*gdk_env)->GetMethodID(gdk_env, lcl_class, "wait", "(J)V");
  MAYBE_RETHROW(gdk_env, "cannot find Object.<wait(J)>");
  
  time = end_time->tv_sec*1000;
  time += end_time->tv_usec/1000;

  /* Must have locked an object to call wait */
  takeLock(gdk_env, cond);

  (*gdk_env)->CallVoidMethod(gdk_env, *(jobject*)cond, wait_mth, time);

  if ((cause = (*gdk_env)->ExceptionOccurred(gdk_env)) != NULL) {
    jclass intr = (*gdk_env)->FindClass (gdk_env, "java.lang.InterruptedException");
    if ( (*gdk_env)->IsInstanceOf(gdk_env, cause, intr) ) {
      releaseLock(gdk_env, cond);
  return FALSE;
    } else {
      MAYBE_RETHROW(gdk_env, "error in timed wait");
    }
  }

  releaseLock(gdk_env, cond);

  return TRUE;
}

/* Free a condition variable.  (isn't C fun?) */
static void g_cond_free_jni_impl (GCond *cond) {
  freePlainObject( (jobject*)cond );
}


/************************************************************************/
/* Thread-local data code		     				*/
/************************************************************************/

/*  Create a new thread-local key.  We use java.lang.ThreadLocal objects
 * for this.
 */
static GPrivate *g_private_new_jni_impl
  (GDestroyNotify notify __attribute__((unused)))
{
  jclass lcl_class;
  jobject *local;
  JNIEnv *gdk_env;
  jmethodID ctor;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  lcl_class = (*gdk_env)->FindClass (gdk_env, "java.lang.ThreadLocal");
  MAYBE_RETHROW(gdk_env, "cannot find ThreadLocal");

  ctor = (*gdk_env)->GetMethodID(gdk_env, lcl_class, "<init>", "()V");
  MAYBE_RETHROW(gdk_env, "cannot find ThreadLocal.<init>");

  local = (jobject *) g_malloc (sizeof (jobject));
  *local = (*gdk_env)->NewObject(gdk_env, lcl_class, ctor);
  MAYBE_RETHROW(gdk_env, "cannot allocate a ThreadLocal");
  
  *local = ((*gdk_env)->NewGlobalRef (gdk_env, *local));
  MAYBE_RETHROW(gdk_env, "cannot create a GlobalRef");

  return (GPrivate*) local;
}

/*  Get this thread's value for a thread-local key.  This is simply
 * ThreadLocal.get for us.
 */
static gpointer g_private_get_jni_impl (GPrivate *private) {
  jclass lcl_class;
  jobject lcl_obj;
  JNIEnv *gdk_env;
  jmethodID get_mth;
  jclass int_class;
  jmethodID val_mth;
  jint int_val;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  lcl_class = (*gdk_env)->FindClass (gdk_env, "java.lang.ThreadLocal");
  MAYBE_RETHROW(gdk_env, "cannot find ThreadLocal");

  get_mth = (*gdk_env)->GetMethodID(gdk_env, lcl_class, "get", "()Ljava/lang/Object;");
  MAYBE_RETHROW(gdk_env, "cannot find ThreadLocal.<get>");

  lcl_obj = (*gdk_env)->CallObjectMethod(gdk_env, *(jobject*)private, get_mth);
  MAYBE_RETHROW(gdk_env, "cannot find thread-local object");

  int_class = (*gdk_env)->FindClass (gdk_env, "java.lang.Integer");
  MAYBE_RETHROW(gdk_env, "cannot find Integer");

  val_mth = (*gdk_env)->GetMethodID(gdk_env, int_class, "intValue", "()I");
  MAYBE_RETHROW(gdk_env, "cannot find Integer.<intValue>");

  int_val = (*gdk_env)->CallIntMethod(gdk_env, lcl_obj, val_mth);
  MAYBE_RETHROW(gdk_env, "cannot get thread local value");

  return (gpointer) int_val;
}

/*  Set this thread's value for a thread-local key.  This is simply
 * ThreadLocal.set for us.
 */
static void g_private_set_jni_impl (GPrivate *private, gpointer data) {
  jclass lcl_class, int_class;
  jobject lcl_obj;
  JNIEnv *gdk_env;
  jmethodID new_int, set_mth;

  (*gdk_vm)->GetEnv(gdk_vm, (void **)&gdk_env, JNI_VERSION_1_1);

  int_class = (*gdk_env)->FindClass (gdk_env, "java.lang.Integer");
  MAYBE_RETHROW(gdk_env, "cannot find Integer");

  new_int = (*gdk_env)->GetMethodID(gdk_env, int_class, "<init>", "(I)V");
  MAYBE_RETHROW(gdk_env, "cannot find Integer.<init>");

  lcl_obj = (*gdk_env)->NewObject(gdk_env, int_class, new_int, (jint)data);
  MAYBE_RETHROW(gdk_env, "cannot create an Integer");

  lcl_class = (*gdk_env)->FindClass (gdk_env, "java.lang.ThreadLocal");
  MAYBE_RETHROW(gdk_env, "cannot find ThreadLocal");

  set_mth = (*gdk_env)->GetMethodID(gdk_env, lcl_class, "set", "(Ljava/lang/Object;)V");
  MAYBE_RETHROW(gdk_env, "cannot find ThreadLocal.<set>");

  (*gdk_env)->CallVoidMethod(gdk_env, *(jobject*)private, set_mth, lcl_obj);
  MAYBE_RETHROW(gdk_env, "cannot set thread local value");
}


/************************************************************************/
/* GLIB interface			     				*/
/************************************************************************/

/* set of function pointers to give to glib. */
GThreadFunctions g_thread_jni_functions =
{
  g_mutex_new_jni_impl,	      /* mutex_new */
  g_mutex_lock_jni_impl,      /* mutex_lock */
  g_mutex_trylock_jni_impl,   /* mutex_try_lock */
  g_mutex_unlock_jni_impl,    /* mutex_unlock */
  g_mutex_free_jni_impl,      /* mutex_free */
  g_cond_new_jni_impl,        /* cond_new */
  g_cond_signal_jni_impl,     /* cond_signal */
  g_cond_broadcast_jni_impl,  /* cond_broadcast */
  g_cond_wait_jni_impl,       /* cond_wait */
  g_cond_timed_wait_jni_impl, /* cond_timed_wait */
  g_cond_free_jni_impl,       /* cond_free */
  g_private_new_jni_impl,     /* private_new */
  g_private_get_jni_impl,     /* private_get */
  g_private_set_jni_impl,     /* private_set */
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL
};

/* ??? */
void gdk_threads_wake () {

}