OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [win32-threads.cc] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
// win32-threads.cc - interface between libjava and Win32 threads.
2
 
3
/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
4
   Foundation, Inc.
5
 
6
   This file is part of libgcj.
7
 
8
This software is copyrighted work licensed under the terms of the
9
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
10
details.  */
11
 
12
#include <config.h>
13
 
14
// If we're using the Boehm GC, then we need to override some of the
15
// thread primitives.  This is fairly gross.
16
#ifdef HAVE_BOEHM_GC
17
extern "C"
18
{
19
#include <gc.h>
20
// <windows.h> #define's STRICT, which conflicts with Modifier.h
21
#undef STRICT
22
};
23
#endif /* HAVE_BOEHM_GC */
24
 
25
#include <gcj/cni.h>
26
#include <jvm.h>
27
#include <java/lang/Thread.h>
28
#include <java/lang/System.h>
29
 
30
#include <errno.h>
31
 
32
#ifndef ETIMEDOUT
33
#define ETIMEDOUT 116
34
#endif
35
 
36
// This is used to implement thread startup.
37
struct starter
38
{
39
  _Jv_ThreadStartFunc *method;
40
  _Jv_Thread_t *data;
41
};
42
 
43
// Controls access to the variable below
44
static HANDLE daemon_mutex;
45
static HANDLE daemon_cond;
46
// Number of non-daemon threads - _Jv_ThreadWait returns when this is 0
47
static int non_daemon_count;
48
 
49
// TLS key get Java object representing the thread
50
DWORD _Jv_ThreadKey;
51
// TLS key to get _Jv_Thread_t* representing the thread
52
DWORD _Jv_ThreadDataKey;
53
 
54
//
55
// These are the flags that can appear in _Jv_Thread_t.
56
//
57
 
58
// Thread started.
59
#define FLAG_START   0x01
60
// Thread is daemon.
61
#define FLAG_DAEMON  0x02
62
 
63
//
64
// Condition variables.
65
//
66
 
67
// we do lazy creation of Events since CreateEvent() is insanely
68
// expensive, and because the rest of libgcj will call _Jv_CondInit
69
// when only a mutex is needed.
70
 
71
inline void
72
ensure_condvar_initialized(_Jv_ConditionVariable_t *cv)
73
{
74
  if (cv->ev[0] == 0)
75
    {
76
      cv->ev[0] = CreateEvent (NULL, 0, 0, NULL);
77
      if (cv->ev[0] == 0) JvFail("CreateEvent() failed");
78
 
79
      cv->ev[1] = CreateEvent (NULL, 1, 0, NULL);
80
      if (cv->ev[1] == 0) JvFail("CreateEvent() failed");
81
    }
82
}
83
 
84
inline void
85
ensure_interrupt_event_initialized(HANDLE& rhEvent)
86
{
87
  if (!rhEvent)
88
    {
89
      rhEvent = CreateEvent (NULL, 0, 0, NULL);
90
      if (!rhEvent) JvFail("CreateEvent() failed");
91
    }
92
}
93
 
94
// Reimplementation of the general algorithm described at
95
// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html (isomorphic to
96
// 3.2, not a cut-and-paste).
97
 
98
int
99
_Jv_CondWait(_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos)
100
{
101
  if (mu->owner != GetCurrentThreadId ( ))
102
    return _JV_NOT_OWNER;
103
 
104
  _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
105
  java::lang::Thread *current_obj = _Jv_ThreadCurrent ();
106
 
107
  // Now that we hold the interrupt mutex, check if this thread has been 
108
  // interrupted already.
109
  EnterCriticalSection (&current->interrupt_mutex);
110
  ensure_interrupt_event_initialized (current->interrupt_event);
111
  jboolean interrupted = current_obj->interrupt_flag;
112
  LeaveCriticalSection (&current->interrupt_mutex);
113
 
114
  if (interrupted)
115
    {
116
      return _JV_INTERRUPTED;
117
    }
118
 
119
  EnterCriticalSection (&cv->count_mutex);
120
  ensure_condvar_initialized (cv);
121
  cv->blocked_count++;
122
  LeaveCriticalSection (&cv->count_mutex);
123
 
124
  DWORD time;
125
  if ((millis == 0) && (nanos > 0)) time = 1;
126
  else if (millis == 0) time = INFINITE;
127
  else time = millis;
128
 
129
  // Record the current lock depth, so it can be restored
130
  // when we reacquire it.
131
  int count = mu->refcount;
132
  int curcount = count;
133
 
134
  // Call _Jv_MutexUnlock repeatedly until this thread
135
  // has completely released the monitor.
136
  while (curcount > 0)
137
    {
138
      _Jv_MutexUnlock (mu);
139
      --curcount;
140
    }
141
 
142
  // Set up our array of three events:
143
  // - the auto-reset event (for notify())
144
  // - the manual-reset event (for notifyAll())
145
  // - the interrupt event (for interrupt())
146
  // We wait for any one of these to be signaled.
147
  HANDLE arh[3];
148
  arh[0] = cv->ev[0];
149
  arh[1] = cv->ev[1];
150
  arh[2] = current->interrupt_event;
151
  DWORD rval = WaitForMultipleObjects (3, arh, 0, time);
152
 
153
  EnterCriticalSection (&current->interrupt_mutex);
154
 
155
  // If we were unblocked by the third event (our thread's interrupt
156
  // event), set the thread's interrupt flag. I think this sanity
157
  // check guards against someone resetting our interrupt flag
158
  // in the time between when interrupt_mutex is released in
159
  // _Jv_ThreadInterrupt and the interval of time between the
160
  // WaitForMultipleObjects call we just made and our acquisition
161
  // of interrupt_mutex.
162
  if (rval == (WAIT_OBJECT_0 + 2))
163
    current_obj->interrupt_flag = true;
164
 
165
  interrupted = current_obj->interrupt_flag;
166
  LeaveCriticalSection (&current->interrupt_mutex);
167
 
168
  EnterCriticalSection(&cv->count_mutex);
169
  cv->blocked_count--;
170
  // If we were unblocked by the second event (the broadcast one)
171
  // and nobody is left, then reset the event.
172
  int last_waiter = (rval == (WAIT_OBJECT_0 + 1)) && (cv->blocked_count == 0);
173
  LeaveCriticalSection(&cv->count_mutex);
174
 
175
  if (last_waiter)
176
    ResetEvent (cv->ev[1]);
177
 
178
  // Call _Jv_MutexLock repeatedly until the mutex's refcount is the
179
  // same as before we originally released it.
180
  while (curcount < count)
181
    {
182
      _Jv_MutexLock (mu);
183
      ++curcount;
184
    }
185
 
186
  return interrupted ? _JV_INTERRUPTED : 0;
187
}
188
 
189
void
190
_Jv_CondInit (_Jv_ConditionVariable_t *cv)
191
{
192
  // we do lazy creation of Events since CreateEvent() is insanely expensive
193
  cv->ev[0] = 0;
194
  InitializeCriticalSection (&cv->count_mutex);
195
  cv->blocked_count = 0;
196
}
197
 
198
void
199
_Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
200
{
201
  if (cv->ev[0] != 0)
202
    {
203
      CloseHandle (cv->ev[0]);
204
      CloseHandle (cv->ev[1]);
205
 
206
      cv->ev[0] = 0;
207
    }
208
 
209
  DeleteCriticalSection (&cv->count_mutex);
210
}
211
 
212
int
213
_Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
214
{
215
  if (mu->owner != GetCurrentThreadId ( ))
216
    return _JV_NOT_OWNER;
217
 
218
  EnterCriticalSection (&cv->count_mutex);
219
  ensure_condvar_initialized (cv);
220
  int somebody_is_blocked = cv->blocked_count > 0;
221
  LeaveCriticalSection (&cv->count_mutex);
222
 
223
  if (somebody_is_blocked)
224
    SetEvent (cv->ev[0]);
225
 
226
  return 0;
227
}
228
 
229
int
230
_Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
231
{
232
  if (mu->owner != GetCurrentThreadId ( ))
233
    return _JV_NOT_OWNER;
234
 
235
  EnterCriticalSection (&cv->count_mutex);
236
  ensure_condvar_initialized (cv);
237
  int somebody_is_blocked = cv->blocked_count > 0;
238
  LeaveCriticalSection (&cv->count_mutex);
239
 
240
  if (somebody_is_blocked)
241
    SetEvent (cv->ev[1]);
242
 
243
  return 0;
244
}
245
 
246
//
247
// Threads.
248
//
249
 
250
void
251
_Jv_InitThreads (void)
252
{
253
  _Jv_ThreadKey = TlsAlloc();
254
  _Jv_ThreadDataKey = TlsAlloc();
255
  daemon_mutex = CreateMutex (NULL, 0, NULL);
256
  daemon_cond = CreateEvent (NULL, 1, 0, NULL);
257
  non_daemon_count = 0;
258
}
259
 
260
_Jv_Thread_t *
261
_Jv_ThreadInitData (java::lang::Thread* obj)
262
{
263
  _Jv_Thread_t *data = (_Jv_Thread_t*)_Jv_Malloc(sizeof(_Jv_Thread_t));
264
  data->flags = 0;
265
  data->handle = 0;
266
  data->thread_obj = obj;
267
  data->interrupt_event = 0;
268
  InitializeCriticalSection (&data->interrupt_mutex);
269
 
270
  return data;
271
}
272
 
273
void
274
_Jv_ThreadDestroyData (_Jv_Thread_t *data)
275
{
276
  DeleteCriticalSection (&data->interrupt_mutex);
277
  if (data->interrupt_event)
278
    CloseHandle(data->interrupt_event);
279
  CloseHandle(data->handle);
280
  _Jv_Free(data);
281
}
282
 
283
void
284
_Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
285
{
286
  int actual = THREAD_PRIORITY_NORMAL;
287
 
288
  if (data->flags & FLAG_START)
289
    {
290
      switch (prio)
291
        {
292
          case 10:
293
            actual = THREAD_PRIORITY_TIME_CRITICAL;
294
            break;
295
          case 9:
296
            actual = THREAD_PRIORITY_HIGHEST;
297
            break;
298
          case 8:
299
          case 7:
300
            actual = THREAD_PRIORITY_ABOVE_NORMAL;
301
            break;
302
          case 6:
303
          case 5:
304
            actual = THREAD_PRIORITY_NORMAL;
305
            break;
306
          case 4:
307
          case 3:
308
            actual = THREAD_PRIORITY_BELOW_NORMAL;
309
            break;
310
          case 2:
311
            actual = THREAD_PRIORITY_LOWEST;
312
            break;
313
          case 1:
314
            actual = THREAD_PRIORITY_IDLE;
315
            break;
316
        }
317
      SetThreadPriority(data->handle, actual);
318
    }
319
}
320
 
321
void
322
_Jv_ThreadRegister (_Jv_Thread_t *data)
323
{
324
  TlsSetValue (_Jv_ThreadKey, data->thread_obj);
325
  TlsSetValue (_Jv_ThreadDataKey, data);
326
}
327
 
328
void
329
_Jv_ThreadUnRegister ()
330
{
331
  TlsSetValue (_Jv_ThreadKey, NULL);
332
  TlsSetValue (_Jv_ThreadDataKey, NULL);
333
}
334
 
335
// This function is called when a thread is started.  We don't arrange
336
// to call the `run' method directly, because this function must
337
// return a value.
338
static DWORD WINAPI
339
really_start (void* x)
340
{
341
  struct starter *info = (struct starter *) x;
342
 
343
  _Jv_ThreadRegister (info->data);
344
 
345
  info->method (info->data->thread_obj);
346
 
347
  if (! (info->data->flags & FLAG_DAEMON))
348
    {
349
      WaitForSingleObject (daemon_mutex, INFINITE);
350
      non_daemon_count--;
351
      if (! non_daemon_count)
352
        SetEvent (daemon_cond);
353
      ReleaseMutex (daemon_mutex);
354
    }
355
 
356
  return 0;
357
}
358
 
359
void
360
_Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data, _Jv_ThreadStartFunc *meth)
361
{
362
  DWORD id;
363
  struct starter *info;
364
 
365
  // Do nothing if thread has already started
366
  if (data->flags & FLAG_START)
367
    return;
368
  data->flags |= FLAG_START;
369
 
370
  info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
371
  info->method = meth;
372
  info->data = data;
373
 
374
  if (! thread->isDaemon ())
375
    {
376
      WaitForSingleObject (daemon_mutex, INFINITE);
377
      non_daemon_count++;
378
      ReleaseMutex (daemon_mutex);
379
    }
380
  else
381
    data->flags |= FLAG_DAEMON;
382
 
383
  data->handle = GC_CreateThread(NULL, 0, really_start, info, 0, &id);
384
  _Jv_ThreadSetPriority(data, thread->getPriority());
385
}
386
 
387
void
388
_Jv_ThreadWait (void)
389
{
390
  WaitForSingleObject (daemon_mutex, INFINITE);
391
  if (non_daemon_count)
392
    {
393
      ReleaseMutex (daemon_mutex);
394
      WaitForSingleObject (daemon_cond, INFINITE);
395
    }
396
}
397
 
398
//
399
// Interrupt support
400
//
401
 
402
HANDLE
403
_Jv_Win32GetInterruptEvent (void)
404
{
405
  _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
406
  EnterCriticalSection (&current->interrupt_mutex);
407
  ensure_interrupt_event_initialized (current->interrupt_event);
408
  HANDLE hEvent = current->interrupt_event;
409
  LeaveCriticalSection (&current->interrupt_mutex);
410
  return hEvent;
411
}
412
 
413
void
414
_Jv_ThreadInterrupt (_Jv_Thread_t *data)
415
{
416
  EnterCriticalSection (&data->interrupt_mutex);
417
  ensure_interrupt_event_initialized (data->interrupt_event);
418
  data->thread_obj->interrupt_flag = true;
419
  SetEvent (data->interrupt_event);
420
  LeaveCriticalSection (&data->interrupt_mutex);
421
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.