[project @ 2002-01-31 23:04:15 by sof]
authorsof <unknown>
Thu, 31 Jan 2002 23:04:30 +0000 (23:04 +0000)
committersof <unknown>
Thu, 31 Jan 2002 23:04:30 +0000 (23:04 +0000)
Win32 implementation, first pass.

ghc/rts/OSThreads.c
ghc/rts/OSThreads.h

index b7fcb49..14dc78e 100644 (file)
@@ -10,7 +10,7 @@
 #include "Rts.h"
 #if defined(RTS_SUPPORTS_THREADS)
 #include "OSThreads.h"
-
+#include "RtsUtils.h"
 
 #if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
 /*
@@ -72,10 +72,17 @@ void initMutexVar (MutexVar* pMut)
 }
 
 #elif defined(HAVE_WINDOWS_H)
+#include <process.h>
+
 /* Win32 threads and synchronisation objects */
 
 /* A CondVar is represented by a Win32 Event object,
  * a MutexVar by a Mutex kernel object.
+ *
+ * ToDo: go through the defn and usage of these to
+ * make sure the semantics match up with that of 
+ * the (assumed) pthreads behaviour. This is really
+ * just a first pass at getting something compilable.
  */
 
 void initCondVar( CondVar* pCond )
@@ -85,54 +92,80 @@ void initCondVar( CondVar* pCond )
                          TRUE,  /* initially signalled */
                          NULL); /* unnamed => process-local. */
   
-
-
-  pthread_cond_init(pCond, NULL);
+  if ( h == NULL ) {
+    belch("initCondVar: unable to create");
+  }
+  *pCond = h;
   return;
 }
 
 void closeCondVar( CondVar* pCond )
 {
-  pthread_cond_destroy(pCond);
+  if ( CloseHandle(*pCond) == 0 ) {
+    belch("closeCondVar: failed to close");
+  }
   return;
 }
 
 rtsBool
 broadcastCondVar ( CondVar* pCond )
 {
-  return (pthread_cond_broadcast(pCond) == 0);
+  PulseEvent(*pCond);
+  return rtsTrue;
 }
 
 rtsBool
 signalCondVar ( CondVar* pCond )
 {
-  return (pthread_cond_signal(pCond) == 0);
+  SetEvent(*pCond);
+  return rtsTrue;
 }
 
 rtsBool
 waitCondVar ( CondVar* pCond, MutexVar* pMut )
 {
-  return (pthread_cond_wait(pCond,pMut) == 0);
+  ReleaseMutex(*pMut);
+  WaitForSingleObject(*pCond, INFINITE);
+  /* Hmm..use WaitForMultipleObjects() ? */
+  WaitForSingleObject(*pMut, INFINITE);
+  return rtsTrue;
 }
 
 void shutdownThread()
 {
-  pthread_exit(NULL);
+  _endthreadex(0);
+}
+
+static unsigned __stdcall startProcWrapper(void* pReal);
+static unsigned __stdcall startProcWrapper(void* pReal)
+{
+  ((void (*)(void*))pReal)(NULL);
+  return 0;
 }
 
 int createOSThread ( OSThreadId* pId, void *(*startProc)(void*))
 {
-  return pthread_create(pId, NULL, startProc, NULL);
+  
+  return _beginthreadex ( NULL,  /* default security attributes */
+                         0,
+                         startProcWrapper,
+                         (void*)startProc,
+                         0,
+                         (unsigned*)pId);
 }
 
 OSThreadId osThreadId()
 {
-  return pthread_self();
+  return GetCurrentThreadId();
 }
 
 void initMutexVar (MutexVar* pMut)
 {
-  pthread_mutex_init(pMut);
+  HANDLE h = CreateMutex ( NULL,  /* default sec. attributes */
+                          FALSE, /* not owned => initially signalled */
+                          NULL
+                          );
+  *pMut = h;
   return;
 }
 
index 8cc06c2..feca45f 100644 (file)
@@ -23,7 +23,7 @@ typedef pthread_t       OSThreadId;
 #include <windows.h>
 typedef HANDLE CondVar;
 typedef HANDLE MutexVar;
-typedef HANDLE OSThreadId;
+typedef DWORD OSThreadId;
 
 #define INIT_MUTEX_VAR 0
 #define INIT_COND_VAR  0