[project @ 2002-12-02 14:33:10 by simonmar]
[ghc-hetmet.git] / ghc / rts / RtsAPI.c
index cd0ef4c..7bfb9d8 100644 (file)
@@ -1,5 +1,5 @@
 /* ----------------------------------------------------------------------------
- * $Id: RtsAPI.c,v 1.34 2002/04/13 05:28:04 sof Exp $
+ * $Id: RtsAPI.c,v 1.37 2002/12/02 14:33:10 simonmar Exp $
  *
  * (c) The GHC Team, 1998-2001
  *
 #include "OSThreads.h"
 #include "Schedule.h"
 
-#if defined(THREADED_RTS)
-#define WAIT_MAIN_THREAD(tso,ret) waitThread_(tso,ret,rtsFalse)
-#define WAIT_EXT_THREAD(tso,ret) waitThread_(tso,ret,rtsTrue)
-#else
-#define WAIT_MAIN_THREAD(tso,ret) waitThread(tso,ret)
-#define WAIT_EXT_THREAD(tso,ret) waitThread(tso,ret)
-#endif
-
 #if defined(RTS_SUPPORTS_THREADS)
 /* Cheesy locking scheme while waiting for the 
  * RTS API to change.
@@ -240,195 +232,161 @@ rts_mkString (char *s)
 HaskellObj
 rts_apply (HaskellObj f, HaskellObj arg)
 {
-  StgAP_UPD *ap = (StgAP_UPD *)alloc(AP_sizeW(1));
-  SET_HDR(ap, &stg_AP_UPD_info, CCS_SYSTEM);
-  ap->n_args = 1;
-  ap->fun    = f;
-  ap->payload[0] = arg;
-  return (StgClosure *)ap;
+    StgClosure *ap;
+
+    ap = (StgClosure *)alloc(sizeofW(StgClosure) + 2);
+    SET_HDR(ap, (StgInfoTable *)&stg_ap_2_upd_info, CCS_SYSTEM);
+    ap->payload[0] = f;
+    ap->payload[1] = arg;
+    return (StgClosure *)ap;
 }
 
 /* ----------------------------------------------------------------------------
    Deconstructing Haskell objects
+
+   We would like to assert that we have the right kind of object in
+   each case, but this is problematic because in GHCi the info table
+   for the D# constructor (say) might be dynamically loaded.  Hence we
+   omit these assertions for now.
    ------------------------------------------------------------------------- */
 
 HsChar
 rts_getChar (HaskellObj p)
 {
-  if ( p->header.info == Czh_con_info || 
-       p->header.info == Czh_static_info) {
+    // See comment above:
+    // ASSERT(p->header.info == Czh_con_info ||
+    //        p->header.info == Czh_static_info);
     return (StgChar)(StgWord)(p->payload[0]);
-  } else {
-    barf("rts_getChar: not a Char");
-  }
 }
 
 HsInt
 rts_getInt (HaskellObj p)
 {
-  if ( 1 ||
-       p->header.info == Izh_con_info || 
-       p->header.info == Izh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == Izh_con_info ||
+    //        p->header.info == Izh_static_info);
     return (HsInt)(p->payload[0]);
-  } else {
-    barf("rts_getInt: not an Int");
-  }
 }
 
 HsInt8
 rts_getInt8 (HaskellObj p)
 {
-  if ( 1 ||
-       p->header.info == I8zh_con_info || 
-       p->header.info == I8zh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == I8zh_con_info ||
+    //        p->header.info == I8zh_static_info);
     return (HsInt8)(HsInt)(p->payload[0]);
-  } else {
-    barf("rts_getInt8: not an Int8");
-  }
 }
 
 HsInt16
 rts_getInt16 (HaskellObj p)
 {
-  if ( 1 ||
-       p->header.info == I16zh_con_info || 
-       p->header.info == I16zh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == I16zh_con_info ||
+    //        p->header.info == I16zh_static_info);
     return (HsInt16)(HsInt)(p->payload[0]);
-  } else {
-    barf("rts_getInt16: not an Int16");
-  }
 }
 
 HsInt32
 rts_getInt32 (HaskellObj p)
 {
-  if ( 1 ||
-       p->header.info == I32zh_con_info || 
-       p->header.info == I32zh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == I32zh_con_info ||
+    //        p->header.info == I32zh_static_info);
     return (HsInt32)(p->payload[0]);
-  } else {
-    barf("rts_getInt32: not an Int32");
-  }
 }
 
 HsInt64
 rts_getInt64 (HaskellObj p)
 {
-  HsInt64* tmp;
-  if ( 1 ||
-       p->header.info == I64zh_con_info || 
-       p->header.info == I64zh_static_info ) {
+    HsInt64* tmp;
+    // See comment above:
+    // ASSERT(p->header.info == I64zh_con_info ||
+    //        p->header.info == I64zh_static_info);
     tmp = (HsInt64*)&(p->payload[0]);
     return *tmp;
-  } else {
-    barf("rts_getInt64: not an Int64");
-  }
 }
 HsWord
 rts_getWord (HaskellObj p)
 {
-  if ( 1 || /* see above comment */
-       p->header.info == Wzh_con_info ||
-       p->header.info == Wzh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == Wzh_con_info ||
+    //        p->header.info == Wzh_static_info);
     return (HsWord)(p->payload[0]);
-  } else {
-    barf("rts_getWord: not a Word");
-  }
 }
 
 HsWord8
 rts_getWord8 (HaskellObj p)
 {
-  if ( 1 || /* see above comment */
-       p->header.info == W8zh_con_info ||
-       p->header.info == W8zh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == W8zh_con_info ||
+    //        p->header.info == W8zh_static_info);
     return (HsWord8)(HsWord)(p->payload[0]);
-  } else {
-    barf("rts_getWord8: not a Word8");
-  }
 }
 
 HsWord16
 rts_getWord16 (HaskellObj p)
 {
-  if ( 1 || /* see above comment */
-       p->header.info == W16zh_con_info ||
-       p->header.info == W16zh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == W16zh_con_info ||
+    //        p->header.info == W16zh_static_info);
     return (HsWord16)(HsWord)(p->payload[0]);
-  } else {
-    barf("rts_getWord16: not a Word16");
-  }
 }
 
 HsWord32
 rts_getWord32 (HaskellObj p)
 {
-  if ( 1 || /* see above comment */
-       p->header.info == W32zh_con_info ||
-       p->header.info == W32zh_static_info ) {
-    return (unsigned int)(p->payload[0]);
-  } else {
-    barf("rts_getWord: not a Word");
-  }
+    // See comment above:
+    // ASSERT(p->header.info == W32zh_con_info ||
+    //        p->header.info == W32zh_static_info);
+    return (HsWord32)(p->payload[0]);
 }
 
 
 HsWord64
 rts_getWord64 (HaskellObj p)
 {
-  HsWord64* tmp;
-  if ( 1 || /* see above comment */
-       p->header.info == W64zh_con_info ||
-       p->header.info == W64zh_static_info ) {
+    HsWord64* tmp;
+    // See comment above:
+    // ASSERT(p->header.info == W64zh_con_info ||
+    //        p->header.info == W64zh_static_info);
     tmp = (HsWord64*)&(p->payload[0]);
     return *tmp;
-  } else {
-    barf("rts_getWord64: not a Word64");
-  }
 }
 
 HsFloat
 rts_getFloat (HaskellObj p)
 {
-  if ( p->header.info == Fzh_con_info || 
-       p->header.info == Fzh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == Fzh_con_info ||
+    //        p->header.info == Fzh_static_info);
     return (float)(PK_FLT((P_)p->payload));
-  } else {
-    barf("rts_getFloat: not a Float");
-  }
 }
 
 HsDouble
 rts_getDouble (HaskellObj p)
 {
-  if ( p->header.info == Dzh_con_info || 
-       p->header.info == Dzh_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == Dzh_con_info ||
+    //        p->header.info == Dzh_static_info);
     return (double)(PK_DBL((P_)p->payload));
-  } else {
-    barf("rts_getDouble: not a Double");
-  }
 }
 
 HsStablePtr
 rts_getStablePtr (HaskellObj p)
 {
-  if ( p->header.info == StablePtr_con_info || 
-       p->header.info == StablePtr_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == StablePtr_con_info ||
+    //        p->header.info == StablePtr_static_info);
     return (StgStablePtr)(p->payload[0]);
-  } else {
-    barf("rts_getStablePtr: not a StablePtr");
-  }
 }
 
 HsPtr
 rts_getPtr (HaskellObj p)
 {
-  if ( p->header.info == Ptr_con_info || 
-       p->header.info == Ptr_static_info ) {
+    // See comment above:
+    // ASSERT(p->header.info == Ptr_con_info ||
+    //        p->header.info == Ptr_static_info);
     return (void *)(p->payload[0]);
-  } else {
-    barf("rts_getPtr: not an Ptr");
-  }
 }
 
 #ifdef COMPILER /* GHC has em, Hugs doesn't */
@@ -455,8 +413,7 @@ rts_eval (HaskellObj p, /*out*/HaskellObj *ret)
 
     tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p);
     releaseAllocLock();
-    scheduleExtThread(tso);
-    return WAIT_EXT_THREAD(tso, ret);
+    return scheduleWaitThread(tso,ret);
 }
 
 SchedulerStatus
@@ -466,8 +423,7 @@ rts_eval_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
     
     tso = createGenThread(stack_size, p);
     releaseAllocLock();
-    scheduleExtThread(tso);
-    return WAIT_EXT_THREAD(tso, ret);
+    return scheduleWaitThread(tso,ret);
 }
 
 /*
@@ -481,8 +437,7 @@ rts_evalIO (HaskellObj p, /*out*/HaskellObj *ret)
     
     tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
     releaseAllocLock();
-    scheduleExtThread(tso);
-    return WAIT_EXT_THREAD(tso, ret);
+    return scheduleWaitThread(tso,ret);
 }
 
 /*
@@ -497,7 +452,7 @@ rts_mainEvalIO(HaskellObj p, /*out*/HaskellObj *ret)
     tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
     releaseAllocLock();
     scheduleThread(tso);
-    return WAIT_MAIN_THREAD(tso, ret);
+    return waitThread(tso, ret);
 }
 
 /*
@@ -516,8 +471,7 @@ rts_evalStableIO (HsStablePtr s, /*out*/HsStablePtr *ret)
     p = (StgClosure *)deRefStablePtr(s);
     tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
     releaseAllocLock();
-    scheduleExtThread(tso);
-    stat = WAIT_EXT_THREAD(tso, &r);
+    stat = scheduleWaitThread(tso,&r);
 
     if (stat == Success) {
        ASSERT(r != NULL);
@@ -537,8 +491,7 @@ rts_evalLazyIO (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
 
     tso = createIOThread(stack_size, p);
     releaseAllocLock();
-    scheduleExtThread(tso);
-    return WAIT_EXT_THREAD(tso, ret);
+    return scheduleWaitThread(tso,ret);
 }
 
 /* Convenience function for decoding the returned status. */