[project @ 2003-10-21 11:51:15 by stolz]
[ghc-hetmet.git] / ghc / rts / OSThreads.c
index a0d3ca8..5d3c8c9 100644 (file)
@@ -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 <process.h>
 
 /* Win32 threads and synchronisation objects */
@@ -112,8 +139,8 @@ 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 ) {
@@ -157,6 +184,13 @@ waitCondition ( Condition* pCond, Mutex* pMut )
 }
 
 void
+yieldThread()
+{
+  Sleep(0);
+  return;
+}
+
+void
 shutdownThread()
 {
   _endthreadex(0);
@@ -174,12 +208,12 @@ 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
@@ -199,6 +233,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) */
+