X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Frts%2FOSThreads.c;h=7ed6fd8b8e732ee4e579182ea2d02ed0e8db6d1b;hb=c064c9e3c3dec9e4f4f7f409711264b1b4893465;hp=14dc78eee5d3d19d57bdf4879d2c1602336f2243;hpb=48ccd03ca6db00ad6a12ca173826e2bccf3734f7;p=ghc-hetmet.git diff --git a/ghc/rts/OSThreads.c b/ghc/rts/OSThreads.c index 14dc78e..7ed6fd8 100644 --- a/ghc/rts/OSThreads.c +++ b/ghc/rts/OSThreads.c @@ -20,64 +20,114 @@ * */ -void initCondVar( CondVar* pCond ) +void +initCondition( Condition* pCond ) { pthread_cond_init(pCond, NULL); return; } -void closeCondVar( CondVar* pCond ) +void +closeCondition( Condition* pCond ) { pthread_cond_destroy(pCond); return; } rtsBool -broadcastCondVar ( CondVar* pCond ) +broadcastCondition ( Condition* pCond ) { return (pthread_cond_broadcast(pCond) == 0); } rtsBool -signalCondVar ( CondVar* pCond ) +signalCondition ( Condition* pCond ) { return (pthread_cond_signal(pCond) == 0); } rtsBool -waitCondVar ( CondVar* pCond, MutexVar* pMut ) +waitCondition ( Condition* pCond, Mutex* pMut ) { return (pthread_cond_wait(pCond,pMut) == 0); } -void shutdownThread() +void +yieldThread() +{ + sched_yield(); + return; +} + +void +shutdownThread() { pthread_exit(NULL); } -int createOSThread ( OSThreadId* pId, void *(*startProc)(void*)) +/* Don't need the argument nor the result, at least not yet. */ +static void* startProcWrapper(void* pProc); +static void* +startProcWrapper(void* pProc) +{ + ((void (*)(void))pProc)(); + return NULL; +} + + +int +createOSThread ( OSThreadId* pId, void (*startProc)(void)) { - return pthread_create(pId, NULL, startProc, NULL); + int result = pthread_create(pId, NULL, startProcWrapper, (void*)startProc); + if(!result) + pthread_detach(*pId); + return result; } -OSThreadId osThreadId() +OSThreadId +osThreadId() { return pthread_self(); } -void initMutexVar (MutexVar* pMut) +void +initMutex(Mutex* pMut) { pthread_mutex_init(pMut,NULL); return; } +static void * +forkOS_createThreadWrapper ( void * entry ) +{ + rts_lock(); + rts_evalStableIO((HsStablePtr) entry, NULL); + rts_unlock(); + return NULL; +} + +int +forkOS_createThread ( HsStablePtr entry ) +{ + pthread_t tid; + int result = pthread_create(&tid, NULL, + forkOS_createThreadWrapper, (void*)entry); + if(!result) + pthread_detach(tid); + return result; +} + #elif defined(HAVE_WINDOWS_H) +/* For reasons not yet clear, the entire contents of process.h is protected + * by __STRICT_ANSI__ not being defined. + */ +#undef __STRICT_ANSI__ #include /* Win32 threads and synchronisation objects */ -/* A CondVar is represented by a Win32 Event object, - * a MutexVar by a Mutex kernel object. +/* A Condition is represented by a Win32 Event object; + * a Mutex by a Mutex kernel object. * * ToDo: go through the defn and usage of these to * make sure the semantics match up with that of @@ -85,81 +135,97 @@ void initMutexVar (MutexVar* pMut) * just a first pass at getting something compilable. */ -void initCondVar( CondVar* pCond ) +void +initCondition( Condition* pCond ) { HANDLE h = CreateEvent(NULL, - TRUE, /* manual reset */ - TRUE, /* initially signalled */ + FALSE, /* auto reset */ + FALSE, /* initially not signalled */ NULL); /* unnamed => process-local. */ if ( h == NULL ) { - belch("initCondVar: unable to create"); + errorBelch("initCondition: unable to create"); } *pCond = h; return; } -void closeCondVar( CondVar* pCond ) +void +closeCondition( Condition* pCond ) { if ( CloseHandle(*pCond) == 0 ) { - belch("closeCondVar: failed to close"); + errorBelch("closeCondition: failed to close"); } return; } rtsBool -broadcastCondVar ( CondVar* pCond ) +broadcastCondition ( Condition* pCond ) { PulseEvent(*pCond); return rtsTrue; } rtsBool -signalCondVar ( CondVar* pCond ) +signalCondition ( Condition* pCond ) { - SetEvent(*pCond); - return rtsTrue; + if (SetEvent(*pCond) == 0) { + barf("SetEvent: %d", GetLastError()); + } + return rtsTrue; } rtsBool -waitCondVar ( CondVar* pCond, MutexVar* pMut ) +waitCondition ( Condition* pCond, Mutex* pMut ) { - ReleaseMutex(*pMut); + RELEASE_LOCK(pMut); WaitForSingleObject(*pCond, INFINITE); /* Hmm..use WaitForMultipleObjects() ? */ - WaitForSingleObject(*pMut, INFINITE); + ACQUIRE_LOCK(pMut); return rtsTrue; } -void shutdownThread() +void +yieldThread() +{ + Sleep(0); + return; +} + +void +shutdownThread() { _endthreadex(0); } static unsigned __stdcall startProcWrapper(void* pReal); -static unsigned __stdcall startProcWrapper(void* pReal) +static unsigned __stdcall +startProcWrapper(void* pReal) { - ((void (*)(void*))pReal)(NULL); + ((void (*)(void))pReal)(); return 0; } -int createOSThread ( OSThreadId* pId, void *(*startProc)(void*)) +int +createOSThread ( OSThreadId* pId, void (*startProc)(void)) { - return _beginthreadex ( NULL, /* default security attributes */ - 0, - startProcWrapper, - (void*)startProc, - 0, - (unsigned*)pId); + return (_beginthreadex ( NULL, /* default security attributes */ + 0, + startProcWrapper, + (void*)startProc, + 0, + (unsigned*)pId) == 0); } -OSThreadId osThreadId() +OSThreadId +osThreadId() { return GetCurrentThreadId(); } -void initMutexVar (MutexVar* pMut) +void +initMutex (Mutex* pMut) { HANDLE h = CreateMutex ( NULL, /* default sec. attributes */ FALSE, /* not owned => initially signalled */ @@ -169,6 +235,36 @@ void initMutexVar (MutexVar* pMut) return; } +static unsigned __stdcall +forkOS_createThreadWrapper ( void * entry ) +{ + rts_lock(); + rts_evalStableIO((HsStablePtr) entry, NULL); + rts_unlock(); + return 0; +} + +int +forkOS_createThread ( HsStablePtr entry ) +{ + unsigned long pId; + return (_beginthreadex ( NULL, /* default security attributes */ + 0, + forkOS_createThreadWrapper, + (void*)entry, + 0, + (unsigned*)&pId) == 0); +} + #endif /* defined(HAVE_PTHREAD_H) */ -#endif /* defined(RTS_SUPPORTS_THREADS) */ +#else /* !defined(RTS_SUPPORTS_THREADS) */ + +int +forkOS_createThread ( HsStablePtr entry STG_UNUSED ) +{ + return -1; +} + +#endif /* !defined(RTS_SUPPORTS_THREADS) */ +