/* ----------------------------------------------------------------------------
- * $Id: RtsAPI.c,v 1.40 2003/01/27 11:08:16 wolfgang Exp $
+ * $Id: RtsAPI.c,v 1.46 2003/09/21 22:20:56 wolfgang Exp $
*
* (c) The GHC Team, 1998-2001
*
#include <stdlib.h>
+static Capability *rtsApiCapability = NULL;
+
/* ----------------------------------------------------------------------------
Building Haskell objects from C datatypes.
------------------------------------------------------------------------- */
{
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;
}
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)
// 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
// 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]);
}
return (void *)(p->payload[0]);
}
-#ifdef COMPILER /* GHC has em, Hugs doesn't */
+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]);
+}
+
HsBool
rts_getBool (HaskellObj p)
{
barf("rts_getBool: not a Bool");
}
}
-#endif /* COMPILER */
/* ----------------------------------------------------------------------------
Evaluating Haskell expressions
StgTSO *tso;
tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p);
- return scheduleWaitThread(tso,ret);
+ return scheduleWaitThread(tso,ret,rtsApiCapability);
}
SchedulerStatus
StgTSO *tso;
tso = createGenThread(stack_size, p);
- return scheduleWaitThread(tso,ret);
+ return scheduleWaitThread(tso,ret,rtsApiCapability);
}
/*
StgTSO* tso;
tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
- return scheduleWaitThread(tso,ret);
-}
-
-/*
- * Identical to rts_evalIO(), but won't create a new task/OS thread
- * to evaluate the Haskell thread. Used by main() only. Hack.
- */
-
-SchedulerStatus
-rts_mainEvalIO(HaskellObj p, /*out*/HaskellObj *ret)
-{
- StgTSO* tso;
-
- tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
- scheduleThread(tso);
- return waitThread(tso, ret);
+ return scheduleWaitThread(tso,ret,rtsApiCapability);
}
/*
p = (StgClosure *)deRefStablePtr(s);
tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
- stat = scheduleWaitThread(tso,&r);
+ stat = scheduleWaitThread(tso,&r,rtsApiCapability);
- if (stat == Success) {
+ if (stat == Success && ret != NULL) {
ASSERT(r != NULL);
*ret = getStablePtr((StgPtr)r);
}
* 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;
+
+ tso = createIOThread(RtsFlags.GcFlags.initialStkSize, p);
+ return scheduleWaitThread(tso,ret,rtsApiCapability);
+}
+
+SchedulerStatus
+rts_evalLazyIO_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
{
StgTSO *tso;
tso = createIOThread(stack_size, p);
- return scheduleWaitThread(tso,ret);
+ return scheduleWaitThread(tso,ret,rtsApiCapability);
}
/* Convenience function for decoding the returned status. */
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);
+ grabReturnCapability(&sched_mutex,&rtsApiCapability);
// In the RTS hasn't been entered yet,
// start a RTS task.
rts_unlock()
{
#ifdef RTS_SUPPORTS_THREADS
+ rtsApiCapability = NULL;
RELEASE_LOCK(&sched_mutex);
#endif
}