OSDN Git Service

* builtins.c (cos_builtin, sin_builtin, sqrt_builtin): Delete.
[pf3gnuchains/gcc-fork.git] / libjava / win32-threads.cc
index 8da274f..3a3999a 100644 (file)
@@ -1,6 +1,7 @@
 // win32-threads.cc - interface between libjava and Win32 threads.
 
-/* Copyright (C) 1998, 1999  Red Hat, Inc.
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
+   Foundation, Inc.
 
    This file is part of libgcj.
 
@@ -15,8 +16,9 @@ details.  */
 #ifdef HAVE_BOEHM_GC
 extern "C"
 {
-#include <boehm-config.h>
 #include <gc.h>
+// <windows.h> #define's STRICT, which conflicts with Modifier.h
+#undef STRICT
 };
 #endif /* HAVE_BOEHM_GC */
 
@@ -35,7 +37,6 @@ extern "C"
 struct starter
 {
   _Jv_ThreadStartFunc *method;
-  java::lang::Thread *object;
   _Jv_Thread_t *data;
 };
 
@@ -63,50 +64,117 @@ DWORD _Jv_ThreadDataKey;
 // Condition variables.
 //
 
+// we do lazy creation of Events since CreateEvent() is insanely
+// expensive, and because the rest of libgcj will call _Jv_CondInit
+// when only a mutex is needed.
+
+inline void
+ensure_condvar_initialized(_Jv_ConditionVariable_t *cv)
+{
+  if (cv->ev[0] == 0)
+    {
+      cv->ev[0] = CreateEvent (NULL, 0, 0, NULL);
+      if (cv->ev[0] == 0) JvFail("CreateEvent() failed");
+
+      cv->ev[1] = CreateEvent (NULL, 1, 0, NULL);
+      if (cv->ev[1] == 0) JvFail("CreateEvent() failed");
+    }
+}
+
+// Reimplementation of the general algorithm described at
+// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html (isomorphic to
+// 3.2, not a cut-and-paste).
+
 int
-_Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos)
+_Jv_CondWait(_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos)
 {
-  DWORD time;
-  DWORD rval;
+  if (mu->owner != GetCurrentThreadId ( ))
+    return _JV_NOT_OWNER;
 
-  // FIXME: check for mutex ownership?
+  EnterCriticalSection (&cv->count_mutex);
+  ensure_condvar_initialized (cv);
+  cv->blocked_count++;
+  LeaveCriticalSection (&cv->count_mutex);
+
+  DWORD time;
+  if ((millis == 0) && (nanos > 0)) time = 1;
+  else if (millis == 0) time = INFINITE;
+  else time = millis;
 
   _Jv_MutexUnlock (mu);
 
-  if((millis == 0) && (nanos > 0))
-    time = 1;
-  else if(millis == 0)
-    time = INFINITE;
-  else
-    time = millis;
+  DWORD rval = WaitForMultipleObjects (2, &(cv->ev[0]), 0, time);
+
+  EnterCriticalSection(&cv->count_mutex);
+  cv->blocked_count--;
+  // If we were unblocked by the second event (the broadcast one)
+  // and nobody is left, then reset the event.
+  int last_waiter = (rval == (WAIT_OBJECT_0 + 1)) && (cv->blocked_count == 0);
+  LeaveCriticalSection(&cv->count_mutex);
+
+  if (last_waiter)
+    ResetEvent (cv->ev[1]);
 
-  rval = WaitForSingleObject (*cv, time);
   _Jv_MutexLock (mu);
 
-  if (rval == WAIT_FAILED)
-    return _JV_NOT_OWNER;       // FIXME?
-  else
-    return 0;
+  return 0;
 }
 
-//
-// Mutexes.
-//
+void
+_Jv_CondInit (_Jv_ConditionVariable_t *cv)
+{
+  // we do lazy creation of Events since CreateEvent() is insanely expensive
+  cv->ev[0] = 0;
+  InitializeCriticalSection (&cv->count_mutex);
+  cv->blocked_count = 0;
+}
+
+void
+_Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
+{
+  if (cv->ev[0] != 0)
+    {
+      CloseHandle (cv->ev[0]);
+      CloseHandle (cv->ev[1]);
+
+      cv->ev[0] = 0;
+    }
+
+  DeleteCriticalSection (&cv->count_mutex);
+}
 
 int
-_Jv_MutexLock (_Jv_Mutex_t *mu)
+_Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
 {
-  DWORD rval;
+  if (mu->owner != GetCurrentThreadId ( ))
+    return _JV_NOT_OWNER;
 
-  // FIXME: Are Win32 mutexs recursive? Should we use critical section objects
-  rval = WaitForSingleObject (*mu, INFINITE);
+  EnterCriticalSection (&cv->count_mutex);
+  ensure_condvar_initialized (cv);
+  int somebody_is_blocked = cv->blocked_count > 0;
+  LeaveCriticalSection (&cv->count_mutex);
 
-  if (rval == WAIT_FAILED)
-    return GetLastError ();       // FIXME: Map to errno?
-  else if (rval == WAIT_TIMEOUT)
-    return ETIMEDOUT;
-  else
-    return 0;
+  if (somebody_is_blocked)
+    SetEvent (cv->ev[0]);
+
+  return 0;
+}
+
+int
+_Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
+{
+  if (mu->owner != GetCurrentThreadId ( ))
+    return _JV_NOT_OWNER;
+
+  EnterCriticalSection (&cv->count_mutex);
+  ensure_condvar_initialized (cv);
+  int somebody_is_blocked = cv->blocked_count > 0;
+  LeaveCriticalSection (&cv->count_mutex);
+
+  if (somebody_is_blocked)
+    SetEvent (cv->ev[1]);
+
+  return 0;
 }
 
 //
@@ -118,21 +186,25 @@ _Jv_InitThreads (void)
 {
   _Jv_ThreadKey = TlsAlloc();
   _Jv_ThreadDataKey = TlsAlloc();
-  daemon_mutex = CreateMutex(NULL, 0, NULL);
-  daemon_cond = CreateEvent(NULL, 0, 0, NULL);
+  daemon_mutex = CreateMutex (NULL, 0, NULL);
+  daemon_cond = CreateEvent (NULL, 1, 0, NULL);
   non_daemon_count = 0;
 }
 
-void
-_Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *)
+_Jv_Thread_t *
+_Jv_ThreadInitData (java::lang::Thread* obj)
 {
-  _Jv_Thread_t *info = new _Jv_Thread_t;
-  info->flags = 0;
+  _Jv_Thread_t *data = (_Jv_Thread_t*)_Jv_Malloc(sizeof(_Jv_Thread_t));
+  data->flags = 0;
+  data->thread_obj = obj;
 
-  // FIXME register a finalizer for INFO here.
-  // FIXME also must mark INFO somehow.
+  return data;
+}
 
-  *data = info;
+void
+_Jv_ThreadDestroyData (_Jv_Thread_t *data)
+{
+  _Jv_Free(data);
 }
 
 void
@@ -173,24 +245,38 @@ _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
     }
 }
 
+void
+_Jv_ThreadRegister (_Jv_Thread_t *data)
+{
+  TlsSetValue (_Jv_ThreadKey, data->thread_obj);
+  TlsSetValue (_Jv_ThreadDataKey, data);
+}
+
+void
+_Jv_ThreadUnRegister ()
+{
+  TlsSetValue (_Jv_ThreadKey, NULL);
+  TlsSetValue (_Jv_ThreadDataKey, NULL);
+}
+
 // 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 DWORD __stdcall
+static DWORD WINAPI
 really_start (void* x)
 {
   struct starter *info = (struct starter *) x;
 
-  TlsSetValue (_Jv_ThreadKey, info->object);
-  TlsSetValue (_Jv_ThreadDataKey, info->data);
-  info->method (info->object);
+  _Jv_ThreadRegister (info->data);
+
+  info->method (info->data->thread_obj);
 
   if (! (info->data->flags & FLAG_DAEMON))
     {
       WaitForSingleObject (daemon_mutex, INFINITE);
       non_daemon_count--;
       if (! non_daemon_count)
-          PulseEvent (daemon_cond);
+        SetEvent (daemon_cond);
       ReleaseMutex (daemon_mutex);
     }
 
@@ -211,7 +297,6 @@ _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data, _Jv_ThreadStart
   // 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 ())
@@ -223,7 +308,7 @@ _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data, _Jv_ThreadStart
   else
     data->flags |= FLAG_DAEMON;
 
-  HANDLE h = CreateThread(NULL, 0, really_start, info, 0, &id);
+  HANDLE h = GC_CreateThread(NULL, 0, really_start, info, 0, &id);
   _Jv_ThreadSetPriority(data, thread->getPriority());
 
   //if (!h)
@@ -233,10 +318,12 @@ _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data, _Jv_ThreadStart
 void
 _Jv_ThreadWait (void)
 {
-  WaitForSingleObject(daemon_mutex, INFINITE);
-  if(non_daemon_count)
-      SignalObjectAndWait(daemon_mutex, daemon_cond, INFINITE, 0);
-  ReleaseMutex(daemon_mutex);
+  WaitForSingleObject (daemon_mutex, INFINITE);
+  if (non_daemon_count)
+    {
+      ReleaseMutex (daemon_mutex);
+      WaitForSingleObject (daemon_cond, INFINITE);
+    }
 }
 
 void