[project @ 2005-04-27 14:37:26 by simonmar]
[ghc-hetmet.git] / ghc / rts / RtsAPI.c
index 4f7e783..0a6b42e 100644 (file)
@@ -1,5 +1,4 @@
 /* ----------------------------------------------------------------------------
- * $Id: RtsAPI.c,v 1.44 2003/05/23 08:28:48 simonmar Exp $
  *
  * (c) The GHC Team, 1998-2001
  *
@@ -9,18 +8,20 @@
 
 #include "PosixSource.h"
 #include "Rts.h"
+#include "OSThreads.h"
 #include "Storage.h"
 #include "RtsAPI.h"
 #include "SchedAPI.h"
 #include "RtsFlags.h"
 #include "RtsUtils.h"
 #include "Prelude.h"
-#include "OSThreads.h"
 #include "Schedule.h"
 #include "Capability.h"
 
 #include <stdlib.h>
 
+static Capability *rtsApiCapability = NULL;
+
 /* ----------------------------------------------------------------------------
    Building Haskell objects from C datatypes.
    ------------------------------------------------------------------------- */
@@ -29,7 +30,7 @@ rts_mkChar (HsChar c)
 {
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
   SET_HDR(p, Czh_con_info, CCS_SYSTEM);
-  p->payload[0]  = (StgClosure *)(StgChar)c;
+  p->payload[0]  = (StgClosure *)(StgWord)(StgChar)c;
   return p;
 }
 
@@ -74,10 +75,10 @@ rts_mkInt32 (HsInt32 i)
 HaskellObj
 rts_mkInt64 (HsInt64 i)
 {
-  long long *tmp;
+  llong *tmp;
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,2));
   SET_HDR(p, I64zh_con_info, CCS_SYSTEM);
-  tmp  = (long long*)&(p->payload[0]);
+  tmp  = (llong*)&(p->payload[0]);
   *tmp = (StgInt64)i;
   return p;
 }
@@ -124,12 +125,12 @@ rts_mkWord32 (HsWord32 w)
 HaskellObj
 rts_mkWord64 (HsWord64 w)
 {
-  unsigned long long *tmp;
+  ullong *tmp;
 
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,2));
   /* see mk_Int8 comment */
   SET_HDR(p, W64zh_con_info, CCS_SYSTEM);
-  tmp  = (unsigned long long*)&(p->payload[0]);
+  tmp  = (ullong*)&(p->payload[0]);
   *tmp = (StgWord64)w;
   return p;
 }
@@ -198,9 +199,9 @@ rts_mkString (char *s)
 HaskellObj
 rts_apply (HaskellObj f, HaskellObj arg)
 {
-    StgClosure *ap;
+    StgThunk *ap;
 
-    ap = (StgClosure *)allocate(sizeofW(StgClosure) + 2);
+    ap = (StgThunk *)allocate(sizeofW(StgThunk) + 2);
     SET_HDR(ap, (StgInfoTable *)&stg_ap_2_upd_info, CCS_SYSTEM);
     ap->payload[0] = f;
     ap->payload[1] = arg;
@@ -258,7 +259,7 @@ rts_getInt32 (HaskellObj p)
     // See comment above:
     // ASSERT(p->header.info == I32zh_con_info ||
     //        p->header.info == I32zh_static_info);
-    return (HsInt32)(p->payload[0]);
+    return (HsInt32)(HsInt)(p->payload[0]);
 }
 
 HsInt64
@@ -304,7 +305,7 @@ rts_getWord32 (HaskellObj p)
     // See comment above:
     // ASSERT(p->header.info == W32zh_con_info ||
     //        p->header.info == W32zh_static_info);
-    return (HsWord32)(p->payload[0]);
+    return (HsWord32)(HsWord)(p->payload[0]);
 }
 
 
@@ -367,13 +368,14 @@ rts_getFunPtr (HaskellObj p)
 HsBool
 rts_getBool (HaskellObj p)
 {
-  if (p == True_closure) {
-    return 1;
-  } else if (p == False_closure) {
-    return 0;
-  } else {
-    barf("rts_getBool: not a Bool");
-  }
+    StgInfoTable *info;
+
+    info = get_itbl((StgClosure *)p);
+    if (info->srt_bitmap == 0) { // srt_bitmap is the constructor tag
+       return 0;
+    } else {
+       return 1;
+    }
 }
 
 /* ----------------------------------------------------------------------------
@@ -383,18 +385,22 @@ SchedulerStatus
 rts_eval (HaskellObj p, /*out*/HaskellObj *ret)
 {
     StgTSO *tso;
+    Capability *cap = rtsApiCapability;
+    rtsApiCapability = NULL;
 
     tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p);
-    return scheduleWaitThread(tso,ret);
+    return scheduleWaitThread(tso,ret,cap);
 }
 
 SchedulerStatus
 rts_eval_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
 {
     StgTSO *tso;
+    Capability *cap = rtsApiCapability;
+    rtsApiCapability = NULL;
     
     tso = createGenThread(stack_size, p);
-    return scheduleWaitThread(tso,ret);
+    return scheduleWaitThread(tso,ret,cap);
 }
 
 /*
@@ -405,24 +411,11 @@ SchedulerStatus
 rts_evalIO (HaskellObj p, /*out*/HaskellObj *ret)
 {
     StgTSO* tso; 
+    Capability *cap = rtsApiCapability;
+    rtsApiCapability = NULL;
     
     tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
-    return scheduleWaitThread(tso,ret);
-}
-
-/*
- * Identical to rts_evalLazyIO(), but won't create a new task/OS thread
- * to evaluate the Haskell thread. Used by main() only. Hack.
- */
-SchedulerStatus
-rts_mainLazyIO(HaskellObj p, /*out*/HaskellObj *ret)
-{
-    StgTSO* tso;
-    
-    tso = createIOThread(RtsFlags.GcFlags.initialStkSize, p);
-    scheduleThread(tso);
-    return waitThread(tso, ret);
+    return scheduleWaitThread(tso,ret,cap);
 }
 
 /*
@@ -437,12 +430,14 @@ rts_evalStableIO (HsStablePtr s, /*out*/HsStablePtr *ret)
     StgTSO* tso;
     StgClosure *p, *r;
     SchedulerStatus stat;
+    Capability *cap = rtsApiCapability;
+    rtsApiCapability = NULL;
     
     p = (StgClosure *)deRefStablePtr(s);
     tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
-    stat = scheduleWaitThread(tso,&r);
+    stat = scheduleWaitThread(tso,&r,cap);
 
-    if (stat == Success) {
+    if (stat == Success && ret != NULL) {
        ASSERT(r != NULL);
        *ret = getStablePtr((StgPtr)r);
     }
@@ -454,12 +449,25 @@ rts_evalStableIO (HsStablePtr s, /*out*/HsStablePtr *ret)
  * Like rts_evalIO(), but doesn't force the action's result.
  */
 SchedulerStatus
-rts_evalLazyIO (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
+rts_evalLazyIO (HaskellObj p, /*out*/HaskellObj *ret)
 {
     StgTSO *tso;
+    Capability *cap = rtsApiCapability;
+    rtsApiCapability = NULL;
+
+    tso = createIOThread(RtsFlags.GcFlags.initialStkSize, p);
+    return scheduleWaitThread(tso,ret,cap);
+}
+
+SchedulerStatus
+rts_evalLazyIO_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
+{
+    StgTSO *tso;
+    Capability *cap = rtsApiCapability;
+    rtsApiCapability = NULL;
 
     tso = createIOThread(stack_size, p);
-    return scheduleWaitThread(tso,ret);
+    return scheduleWaitThread(tso,ret,cap);
 }
 
 /* Convenience function for decoding the returned status. */
@@ -471,13 +479,13 @@ rts_checkSchedStatus ( char* site, SchedulerStatus rc )
     case Success:
        return;
     case Killed:
-       prog_belch("%s: uncaught exception",site);
+       errorBelch("%s: uncaught exception",site);
        stg_exit(EXIT_FAILURE);
     case Interrupted:
-       prog_belch("%s: interrupted", site);
+       errorBelch("%s: interrupted", site);
        stg_exit(EXIT_FAILURE);
     default:
-       prog_belch("%s: Return code (%d) not ok",(site),(rc));  
+       errorBelch("%s: Return code (%d) not ok",(site),(rc));  
        stg_exit(EXIT_FAILURE);
     }
 }
@@ -486,24 +494,15 @@ void
 rts_lock()
 {
 #ifdef RTS_SUPPORTS_THREADS
-       Capability *cap;
-       ACQUIRE_LOCK(&sched_mutex);
+    ACQUIRE_LOCK(&sched_mutex);
        
-               // we request to get the capability immediately, in order to
-               // a) stop other threads from using allocate()
-               // b) wake the current worker thread from awaitEvent()
-               //       (so that a thread started by rts_eval* will start immediately)
-       grabReturnCapability(&sched_mutex,&cap);
-       
-               // now that we have the capability, we don't need it anymore
-               // (other threads will continue to run as soon as we release the sched_mutex)
-       releaseCapability(cap);
-       
-               // In the RTS hasn't been entered yet,
-               // start a RTS task.
-               // If there is already a task available (waiting for the work capability),
-               // this will do nothing.
-       startSchedulerTask();
+    // we request to get the capability immediately, in order to
+    // a) stop other threads from using allocate()
+    // b) wake the current worker thread from awaitEvent()
+    //       (so that a thread started by rts_eval* will start immediately)
+    waitForReturnCapability(&sched_mutex,&rtsApiCapability);
+#else
+    grabCapability(&rtsApiCapability);
 #endif
 }
 
@@ -511,6 +510,10 @@ void
 rts_unlock()
 {
 #ifdef RTS_SUPPORTS_THREADS
-       RELEASE_LOCK(&sched_mutex);
+    if (rtsApiCapability) {
+       releaseCapability(rtsApiCapability);
+    }
+    rtsApiCapability = NULL;
+    RELEASE_LOCK(&sched_mutex);
 #endif
 }