1 /* ----------------------------------------------------------------------------
2 * $Id: RtsAPI.c,v 1.46 2003/09/21 22:20:56 wolfgang Exp $
4 * (c) The GHC Team, 1998-2001
6 * API for invoking Haskell functions via the RTS
8 * --------------------------------------------------------------------------*/
10 #include "PosixSource.h"
18 #include "OSThreads.h"
20 #include "Capability.h"
24 static Capability *rtsApiCapability = NULL;
26 /* ----------------------------------------------------------------------------
27 Building Haskell objects from C datatypes.
28 ------------------------------------------------------------------------- */
32 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
33 SET_HDR(p, Czh_con_info, CCS_SYSTEM);
34 p->payload[0] = (StgClosure *)(StgWord)(StgChar)c;
41 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
42 SET_HDR(p, Izh_con_info, CCS_SYSTEM);
43 p->payload[0] = (StgClosure *)(StgInt)i;
50 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
51 SET_HDR(p, I8zh_con_info, CCS_SYSTEM);
52 /* Make sure we mask out the bits above the lowest 8 */
53 p->payload[0] = (StgClosure *)(StgInt)((unsigned)i & 0xff);
58 rts_mkInt16 (HsInt16 i)
60 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
61 SET_HDR(p, I16zh_con_info, CCS_SYSTEM);
62 /* Make sure we mask out the relevant bits */
63 p->payload[0] = (StgClosure *)(StgInt)((unsigned)i & 0xffff);
68 rts_mkInt32 (HsInt32 i)
70 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
71 SET_HDR(p, I32zh_con_info, CCS_SYSTEM);
72 p->payload[0] = (StgClosure *)(StgInt)((unsigned)i & 0xffffffff);
77 rts_mkInt64 (HsInt64 i)
80 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,2));
81 SET_HDR(p, I64zh_con_info, CCS_SYSTEM);
82 tmp = (long long*)&(p->payload[0]);
90 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
91 SET_HDR(p, Wzh_con_info, CCS_SYSTEM);
92 p->payload[0] = (StgClosure *)(StgWord)i;
97 rts_mkWord8 (HsWord8 w)
99 /* see rts_mkInt* comments */
100 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
101 SET_HDR(p, W8zh_con_info, CCS_SYSTEM);
102 p->payload[0] = (StgClosure *)(StgWord)(w & 0xff);
107 rts_mkWord16 (HsWord16 w)
109 /* see rts_mkInt* comments */
110 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
111 SET_HDR(p, W16zh_con_info, CCS_SYSTEM);
112 p->payload[0] = (StgClosure *)(StgWord)(w & 0xffff);
117 rts_mkWord32 (HsWord32 w)
119 /* see rts_mkInt* comments */
120 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
121 SET_HDR(p, W32zh_con_info, CCS_SYSTEM);
122 p->payload[0] = (StgClosure *)(StgWord)(w & 0xffffffff);
127 rts_mkWord64 (HsWord64 w)
129 unsigned long long *tmp;
131 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,2));
132 /* see mk_Int8 comment */
133 SET_HDR(p, W64zh_con_info, CCS_SYSTEM);
134 tmp = (unsigned long long*)&(p->payload[0]);
140 rts_mkFloat (HsFloat f)
142 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
143 SET_HDR(p, Fzh_con_info, CCS_SYSTEM);
144 ASSIGN_FLT((P_)p->payload, (StgFloat)f);
149 rts_mkDouble (HsDouble d)
151 StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,sizeofW(StgDouble)));
152 SET_HDR(p, Dzh_con_info, CCS_SYSTEM);
153 ASSIGN_DBL((P_)p->payload, (StgDouble)d);
158 rts_mkStablePtr (HsStablePtr s)
160 StgClosure *p = (StgClosure *)allocate(sizeofW(StgHeader)+1);
161 SET_HDR(p, StablePtr_con_info, CCS_SYSTEM);
162 p->payload[0] = (StgClosure *)s;
169 StgClosure *p = (StgClosure *)allocate(sizeofW(StgHeader)+1);
170 SET_HDR(p, Ptr_con_info, CCS_SYSTEM);
171 p->payload[0] = (StgClosure *)a;
176 rts_mkFunPtr (HsFunPtr a)
178 StgClosure *p = (StgClosure *)allocate(sizeofW(StgHeader)+1);
179 SET_HDR(p, FunPtr_con_info, CCS_SYSTEM);
180 p->payload[0] = (StgClosure *)a;
185 rts_mkBool (HsBool b)
188 return (StgClosure *)True_closure;
190 return (StgClosure *)False_closure;
195 rts_mkString (char *s)
197 return rts_apply((StgClosure *)unpackCString_closure, rts_mkPtr(s));
201 rts_apply (HaskellObj f, HaskellObj arg)
205 ap = (StgClosure *)allocate(sizeofW(StgClosure) + 2);
206 SET_HDR(ap, (StgInfoTable *)&stg_ap_2_upd_info, CCS_SYSTEM);
208 ap->payload[1] = arg;
209 return (StgClosure *)ap;
212 /* ----------------------------------------------------------------------------
213 Deconstructing Haskell objects
215 We would like to assert that we have the right kind of object in
216 each case, but this is problematic because in GHCi the info table
217 for the D# constructor (say) might be dynamically loaded. Hence we
218 omit these assertions for now.
219 ------------------------------------------------------------------------- */
222 rts_getChar (HaskellObj p)
224 // See comment above:
225 // ASSERT(p->header.info == Czh_con_info ||
226 // p->header.info == Czh_static_info);
227 return (StgChar)(StgWord)(p->payload[0]);
231 rts_getInt (HaskellObj p)
233 // See comment above:
234 // ASSERT(p->header.info == Izh_con_info ||
235 // p->header.info == Izh_static_info);
236 return (HsInt)(p->payload[0]);
240 rts_getInt8 (HaskellObj p)
242 // See comment above:
243 // ASSERT(p->header.info == I8zh_con_info ||
244 // p->header.info == I8zh_static_info);
245 return (HsInt8)(HsInt)(p->payload[0]);
249 rts_getInt16 (HaskellObj p)
251 // See comment above:
252 // ASSERT(p->header.info == I16zh_con_info ||
253 // p->header.info == I16zh_static_info);
254 return (HsInt16)(HsInt)(p->payload[0]);
258 rts_getInt32 (HaskellObj p)
260 // See comment above:
261 // ASSERT(p->header.info == I32zh_con_info ||
262 // p->header.info == I32zh_static_info);
263 return (HsInt32)(HsInt)(p->payload[0]);
267 rts_getInt64 (HaskellObj p)
270 // See comment above:
271 // ASSERT(p->header.info == I64zh_con_info ||
272 // p->header.info == I64zh_static_info);
273 tmp = (HsInt64*)&(p->payload[0]);
277 rts_getWord (HaskellObj p)
279 // See comment above:
280 // ASSERT(p->header.info == Wzh_con_info ||
281 // p->header.info == Wzh_static_info);
282 return (HsWord)(p->payload[0]);
286 rts_getWord8 (HaskellObj p)
288 // See comment above:
289 // ASSERT(p->header.info == W8zh_con_info ||
290 // p->header.info == W8zh_static_info);
291 return (HsWord8)(HsWord)(p->payload[0]);
295 rts_getWord16 (HaskellObj p)
297 // See comment above:
298 // ASSERT(p->header.info == W16zh_con_info ||
299 // p->header.info == W16zh_static_info);
300 return (HsWord16)(HsWord)(p->payload[0]);
304 rts_getWord32 (HaskellObj p)
306 // See comment above:
307 // ASSERT(p->header.info == W32zh_con_info ||
308 // p->header.info == W32zh_static_info);
309 return (HsWord32)(HsWord)(p->payload[0]);
314 rts_getWord64 (HaskellObj p)
317 // See comment above:
318 // ASSERT(p->header.info == W64zh_con_info ||
319 // p->header.info == W64zh_static_info);
320 tmp = (HsWord64*)&(p->payload[0]);
325 rts_getFloat (HaskellObj p)
327 // See comment above:
328 // ASSERT(p->header.info == Fzh_con_info ||
329 // p->header.info == Fzh_static_info);
330 return (float)(PK_FLT((P_)p->payload));
334 rts_getDouble (HaskellObj p)
336 // See comment above:
337 // ASSERT(p->header.info == Dzh_con_info ||
338 // p->header.info == Dzh_static_info);
339 return (double)(PK_DBL((P_)p->payload));
343 rts_getStablePtr (HaskellObj p)
345 // See comment above:
346 // ASSERT(p->header.info == StablePtr_con_info ||
347 // p->header.info == StablePtr_static_info);
348 return (StgStablePtr)(p->payload[0]);
352 rts_getPtr (HaskellObj p)
354 // See comment above:
355 // ASSERT(p->header.info == Ptr_con_info ||
356 // p->header.info == Ptr_static_info);
357 return (void *)(p->payload[0]);
361 rts_getFunPtr (HaskellObj p)
363 // See comment above:
364 // ASSERT(p->header.info == FunPtr_con_info ||
365 // p->header.info == FunPtr_static_info);
366 return (void *)(p->payload[0]);
370 rts_getBool (HaskellObj p)
372 if (p == True_closure) {
374 } else if (p == False_closure) {
377 barf("rts_getBool: not a Bool");
381 /* ----------------------------------------------------------------------------
382 Evaluating Haskell expressions
383 ------------------------------------------------------------------------- */
385 rts_eval (HaskellObj p, /*out*/HaskellObj *ret)
389 tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p);
390 return scheduleWaitThread(tso,ret,rtsApiCapability);
394 rts_eval_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
398 tso = createGenThread(stack_size, p);
399 return scheduleWaitThread(tso,ret,rtsApiCapability);
403 * rts_evalIO() evaluates a value of the form (IO a), forcing the action's
404 * result to WHNF before returning.
407 rts_evalIO (HaskellObj p, /*out*/HaskellObj *ret)
411 tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
412 return scheduleWaitThread(tso,ret,rtsApiCapability);
416 * rts_evalStableIO() is suitable for calling from Haskell. It
417 * evaluates a value of the form (StablePtr (IO a)), forcing the
418 * action's result to WHNF before returning. The result is returned
422 rts_evalStableIO (HsStablePtr s, /*out*/HsStablePtr *ret)
426 SchedulerStatus stat;
428 p = (StgClosure *)deRefStablePtr(s);
429 tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
430 stat = scheduleWaitThread(tso,&r,rtsApiCapability);
432 if (stat == Success && ret != NULL) {
434 *ret = getStablePtr((StgPtr)r);
441 * Like rts_evalIO(), but doesn't force the action's result.
444 rts_evalLazyIO (HaskellObj p, /*out*/HaskellObj *ret)
448 tso = createIOThread(RtsFlags.GcFlags.initialStkSize, p);
449 return scheduleWaitThread(tso,ret,rtsApiCapability);
453 rts_evalLazyIO_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
457 tso = createIOThread(stack_size, p);
458 return scheduleWaitThread(tso,ret,rtsApiCapability);
461 /* Convenience function for decoding the returned status. */
464 rts_checkSchedStatus ( char* site, SchedulerStatus rc )
470 prog_belch("%s: uncaught exception",site);
471 stg_exit(EXIT_FAILURE);
473 prog_belch("%s: interrupted", site);
474 stg_exit(EXIT_FAILURE);
476 prog_belch("%s: Return code (%d) not ok",(site),(rc));
477 stg_exit(EXIT_FAILURE);
484 #ifdef RTS_SUPPORTS_THREADS
485 ACQUIRE_LOCK(&sched_mutex);
487 // we request to get the capability immediately, in order to
488 // a) stop other threads from using allocate()
489 // b) wake the current worker thread from awaitEvent()
490 // (so that a thread started by rts_eval* will start immediately)
491 grabReturnCapability(&sched_mutex,&rtsApiCapability);
493 // In the RTS hasn't been entered yet,
495 // If there is already a task available (waiting for the work capability),
496 // this will do nothing.
497 startSchedulerTask();
504 #ifdef RTS_SUPPORTS_THREADS
505 rtsApiCapability = NULL;
506 RELEASE_LOCK(&sched_mutex);