/* ----------------------------------------------------------------------------
- * $Id: RtsAPI.c,v 1.31 2002/01/22 13:54:22 simonmar Exp $
+ * $Id: RtsAPI.c,v 1.44 2003/05/23 08:28:48 simonmar Exp $
*
* (c) The GHC Team, 1998-2001
*
#include "RtsFlags.h"
#include "RtsUtils.h"
#include "Prelude.h"
+#include "OSThreads.h"
+#include "Schedule.h"
+#include "Capability.h"
+
+#include <stdlib.h>
/* ----------------------------------------------------------------------------
Building Haskell objects from C datatypes.
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)
{
{
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)
{
barf("rts_getBool: not a Bool");
}
}
-#endif /* COMPILER */
/* ----------------------------------------------------------------------------
Evaluating Haskell expressions
StgTSO *tso;
tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p);
- scheduleThread(tso);
- return waitThread(tso, ret);
+ return scheduleWaitThread(tso,ret);
}
SchedulerStatus
StgTSO *tso;
tso = createGenThread(stack_size, p);
- scheduleThread(tso);
- return waitThread(tso, ret);
+ return scheduleWaitThread(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);
}
p = (StgClosure *)deRefStablePtr(s);
tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
- scheduleThread(tso);
- stat = waitThread(tso, &r);
+ stat = scheduleWaitThread(tso,&r);
if (stat == Success) {
ASSERT(r != NULL);
StgTSO *tso;
tso = createIOThread(stack_size, p);
- scheduleThread(tso);
- return waitThread(tso, ret);
+ return scheduleWaitThread(tso,ret);
}
/* Convenience function for decoding the returned status. */
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);
+ 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
+}