X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Frts%2FRtsAPI.c;h=4f7e783595700daf2c87408f6f3ea260c68935eb;hb=40df40956025fdb4d7d81e89f6c27755a9df8f46;hp=76bddc9495f66a9a2a6ac4f086895c5fafb21ccd;hpb=13350796d17620070d7cacce688072877aca6af4;p=ghc-hetmet.git diff --git a/ghc/rts/RtsAPI.c b/ghc/rts/RtsAPI.c index 76bddc9..4f7e783 100644 --- a/ghc/rts/RtsAPI.c +++ b/ghc/rts/RtsAPI.c @@ -1,5 +1,5 @@ /* ---------------------------------------------------------------------------- - * $Id: RtsAPI.c,v 1.29 2001/08/29 11:20:40 simonmar Exp $ + * $Id: RtsAPI.c,v 1.44 2003/05/23 08:28:48 simonmar Exp $ * * (c) The GHC Team, 1998-2001 * @@ -15,6 +15,11 @@ #include "RtsFlags.h" #include "RtsUtils.h" #include "Prelude.h" +#include "OSThreads.h" +#include "Schedule.h" +#include "Capability.h" + +#include /* ---------------------------------------------------------------------------- Building Haskell objects from C datatypes. @@ -165,7 +170,15 @@ rts_mkPtr (HsPtr a) return p; } -#ifdef COMPILER /* GHC has em, Hugs doesn't */ +HaskellObj +rts_mkFunPtr (HsFunPtr a) +{ + StgClosure *p = (StgClosure *)allocate(sizeofW(StgHeader)+1); + SET_HDR(p, FunPtr_con_info, CCS_SYSTEM); + p->payload[0] = (StgClosure *)a; + return p; +} + HaskellObj rts_mkBool (HsBool b) { @@ -181,203 +194,176 @@ rts_mkString (char *s) { return rts_apply((StgClosure *)unpackCString_closure, rts_mkPtr(s)); } -#endif /* COMPILER */ HaskellObj rts_apply (HaskellObj f, HaskellObj arg) { - StgAP_UPD *ap = (StgAP_UPD *)allocate(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 *)allocate(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]); +} + +HsFunPtr +rts_getFunPtr (HaskellObj p) +{ + // See comment above: + // ASSERT(p->header.info == FunPtr_con_info || + // p->header.info == FunPtr_static_info); return (void *)(p->payload[0]); - } else { - barf("rts_getPtr: not an Ptr"); - } } -#ifdef COMPILER /* GHC has em, Hugs doesn't */ HsBool rts_getBool (HaskellObj p) { @@ -389,7 +375,6 @@ rts_getBool (HaskellObj p) barf("rts_getBool: not a Bool"); } } -#endif /* COMPILER */ /* ---------------------------------------------------------------------------- Evaluating Haskell expressions @@ -397,17 +382,19 @@ rts_getBool (HaskellObj p) SchedulerStatus rts_eval (HaskellObj p, /*out*/HaskellObj *ret) { - StgTSO *tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p); - scheduleThread(tso); - return waitThread(tso, ret); + StgTSO *tso; + + tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p); + return scheduleWaitThread(tso,ret); } SchedulerStatus rts_eval_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret) { - StgTSO *tso = createGenThread(stack_size, p); - scheduleThread(tso); - return waitThread(tso, ret); + StgTSO *tso; + + tso = createGenThread(stack_size, p); + return scheduleWaitThread(tso,ret); } /* @@ -417,9 +404,50 @@ rts_eval_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret) SchedulerStatus rts_evalIO (HaskellObj p, /*out*/HaskellObj *ret) { - StgTSO* tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p); - scheduleThread(tso); - return waitThread(tso, ret); + StgTSO* tso; + + 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); +} + +/* + * rts_evalStableIO() is suitable for calling from Haskell. It + * evaluates a value of the form (StablePtr (IO a)), forcing the + * action's result to WHNF before returning. The result is returned + * in a StablePtr. + */ +SchedulerStatus +rts_evalStableIO (HsStablePtr s, /*out*/HsStablePtr *ret) +{ + StgTSO* tso; + StgClosure *p, *r; + SchedulerStatus stat; + + p = (StgClosure *)deRefStablePtr(s); + tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p); + stat = scheduleWaitThread(tso,&r); + + if (stat == Success) { + ASSERT(r != NULL); + *ret = getStablePtr((StgPtr)r); + } + + return stat; } /* @@ -428,9 +456,10 @@ rts_evalIO (HaskellObj p, /*out*/HaskellObj *ret) SchedulerStatus rts_evalLazyIO (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret) { - StgTSO *tso = createIOThread(stack_size, p); - scheduleThread(tso); - return waitThread(tso, ret); + StgTSO *tso; + + tso = createIOThread(stack_size, p); + return scheduleWaitThread(tso,ret); } /* Convenience function for decoding the returned status. */ @@ -442,12 +471,46 @@ rts_checkSchedStatus ( char* site, SchedulerStatus rc ) case Success: return; case Killed: - barf("%s: uncaught exception",site); + prog_belch("%s: uncaught exception",site); + stg_exit(EXIT_FAILURE); case Interrupted: - barf("%s: interrupted", site); - case Deadlock: - barf("%s: no threads to run: infinite loop or deadlock?", site); + prog_belch("%s: interrupted", site); + stg_exit(EXIT_FAILURE); default: - barf("%s: Return code (%d) not ok",(site),(rc)); + prog_belch("%s: Return code (%d) not ok",(site),(rc)); + stg_exit(EXIT_FAILURE); } } + +void +rts_lock() +{ +#ifdef RTS_SUPPORTS_THREADS + Capability *cap; + 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(); +#endif +} + +void +rts_unlock() +{ +#ifdef RTS_SUPPORTS_THREADS + RELEASE_LOCK(&sched_mutex); +#endif +}