X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Frts%2FOSThreads.c;h=7ed6fd8b8e732ee4e579182ea2d02ed0e8db6d1b;hb=0cc009952787f2112365100a04dc73aa3d0fca67;hp=16a4ca19888e7d139221dd6fa1d60f940a177cdc;hpb=bf96cc34ae37e24412f4a110666aca90b8b8161e;p=ghc-hetmet.git diff --git a/ghc/rts/OSThreads.c b/ghc/rts/OSThreads.c index 16a4ca1..7ed6fd8 100644 --- a/ghc/rts/OSThreads.c +++ b/ghc/rts/OSThreads.c @@ -78,7 +78,10 @@ startProcWrapper(void* pProc) int createOSThread ( OSThreadId* pId, void (*startProc)(void)) { - return pthread_create(pId, NULL, startProcWrapper, (void*)startProc); + int result = pthread_create(pId, NULL, startProcWrapper, (void*)startProc); + if(!result) + pthread_detach(*pId); + return result; } OSThreadId @@ -94,7 +97,31 @@ initMutex(Mutex* pMut) 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 */ @@ -112,12 +139,12 @@ 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("initCondition: unable to create"); + errorBelch("initCondition: unable to create"); } *pCond = h; return; @@ -127,7 +154,7 @@ void closeCondition( Condition* pCond ) { if ( CloseHandle(*pCond) == 0 ) { - belch("closeCondition: failed to close"); + errorBelch("closeCondition: failed to close"); } return; } @@ -142,21 +169,30 @@ broadcastCondition ( Condition* pCond ) rtsBool signalCondition ( Condition* pCond ) { - SetEvent(*pCond); - return rtsTrue; + if (SetEvent(*pCond) == 0) { + barf("SetEvent: %d", GetLastError()); + } + return rtsTrue; } rtsBool 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 +yieldThread() +{ + Sleep(0); + return; +} + +void shutdownThread() { _endthreadex(0); @@ -171,15 +207,15 @@ startProcWrapper(void* pReal) } int -createOSThread ( OSThreadId* pId, void (*startProc)(void*)) +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 @@ -199,6 +235,36 @@ initMutex (Mutex* 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) */ +