From 48ccd03ca6db00ad6a12ca173826e2bccf3734f7 Mon Sep 17 00:00:00 2001 From: sof Date: Thu, 31 Jan 2002 23:04:30 +0000 Subject: [PATCH] [project @ 2002-01-31 23:04:15 by sof] Win32 implementation, first pass. --- ghc/rts/OSThreads.c | 57 ++++++++++++++++++++++++++++++++++++++++----------- ghc/rts/OSThreads.h | 2 +- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/ghc/rts/OSThreads.c b/ghc/rts/OSThreads.c index b7fcb49..14dc78e 100644 --- a/ghc/rts/OSThreads.c +++ b/ghc/rts/OSThreads.c @@ -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 + /* 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; } diff --git a/ghc/rts/OSThreads.h b/ghc/rts/OSThreads.h index 8cc06c2..feca45f 100644 --- a/ghc/rts/OSThreads.h +++ b/ghc/rts/OSThreads.h @@ -23,7 +23,7 @@ typedef pthread_t OSThreadId; #include typedef HANDLE CondVar; typedef HANDLE MutexVar; -typedef HANDLE OSThreadId; +typedef DWORD OSThreadId; #define INIT_MUTEX_VAR 0 #define INIT_COND_VAR 0 -- 1.7.10.4