RtsAPI.c 12.4 KB
Newer Older
1
/* ----------------------------------------------------------------------------
sof's avatar
sof committed
2
 * $Id: RtsAPI.c,v 1.33 2002/02/15 07:40:10 sof Exp $
3
 *
4
 * (c) The GHC Team, 1998-2001
5
6
7
8
9
 *
 * API for invoking Haskell functions via the RTS
 *
 * --------------------------------------------------------------------------*/

10
#include "PosixSource.h"
11
12
13
#include "Rts.h"
#include "Storage.h"
#include "RtsAPI.h"
sof's avatar
sof committed
14
#include "SchedAPI.h"
15
16
#include "RtsFlags.h"
#include "RtsUtils.h"
17
#include "Prelude.h"
sof's avatar
sof committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "OSThreads.h"
#include "Schedule.h"

#if defined(THREADED_RTS)
#define WAIT_MAIN_THREAD(tso,ret) waitThread_(tso,ret,rtsFalse)
#else
#define WAIT_MAIN_THREAD(tso,ret) waitThread(tso,ret)
#endif

#if defined(RTS_SUPPORTS_THREADS)
/* Cheesy locking scheme while waiting for the 
 * RTS API to change.
 */
static Mutex     alloc_mutex = INIT_MUTEX_VAR;
static Condition alloc_cond  = INIT_COND_VAR;
#define INVALID_THREAD_ID ((OSThreadId)(-1))

/* Thread currently owning the allocator */
static OSThreadId c_id = INVALID_THREAD_ID;

static StgPtr alloc(nat n)
{
  OSThreadId tid = osThreadId();
  ACQUIRE_LOCK(&alloc_mutex);
  if (tid == c_id) {
    /* I've got the lock, just allocate() */
    ;
  } else if (c_id == INVALID_THREAD_ID) {
    c_id = tid;
  } else {
    waitCondition(&alloc_cond, &alloc_mutex);
    c_id = tid;
  }
  RELEASE_LOCK(&alloc_mutex);
  return allocate(n);
}

static void releaseAllocLock(void)
{
  ACQUIRE_LOCK(&alloc_mutex);
  /* Reset the allocator owner */
  c_id = INVALID_THREAD_ID;
  RELEASE_LOCK(&alloc_mutex);

  /* Free up an OS thread waiting to get in */
  signalCondition(&alloc_cond);
}
#else
# define alloc(n) allocate(n)
# define releaseAllocLock() /* nothing */
#endif

70
71
72
73
74

/* ----------------------------------------------------------------------------
   Building Haskell objects from C datatypes.
   ------------------------------------------------------------------------- */
HaskellObj
75
rts_mkChar (HsChar c)
76
{
sof's avatar
sof committed
77
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
78
  SET_HDR(p, Czh_con_info, CCS_SYSTEM);
79
  p->payload[0]  = (StgClosure *)(StgChar)c;
80
81
82
83
  return p;
}

HaskellObj
84
rts_mkInt (HsInt i)
85
{
sof's avatar
sof committed
86
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
87
  SET_HDR(p, Izh_con_info, CCS_SYSTEM);
88
89
90
91
92
  p->payload[0]  = (StgClosure *)(StgInt)i;
  return p;
}

HaskellObj
93
rts_mkInt8 (HsInt8 i)
94
{
sof's avatar
sof committed
95
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
96
  SET_HDR(p, I8zh_con_info, CCS_SYSTEM);
97
98
99
100
101
102
  /* Make sure we mask out the bits above the lowest 8 */
  p->payload[0]  = (StgClosure *)(StgInt)((unsigned)i & 0xff);
  return p;
}

HaskellObj
103
rts_mkInt16 (HsInt16 i)
104
{
sof's avatar
sof committed
105
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
106
  SET_HDR(p, I16zh_con_info, CCS_SYSTEM);
107
108
109
110
111
112
  /* Make sure we mask out the relevant bits */
  p->payload[0]  = (StgClosure *)(StgInt)((unsigned)i & 0xffff);
  return p;
}

HaskellObj
113
rts_mkInt32 (HsInt32 i)
114
{
sof's avatar
sof committed
115
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
116
117
  SET_HDR(p, I32zh_con_info, CCS_SYSTEM);
  p->payload[0]  = (StgClosure *)(StgInt)((unsigned)i & 0xffffffff);
118
119
120
121
  return p;
}

HaskellObj
122
rts_mkInt64 (HsInt64 i)
123
124
{
  long long *tmp;
sof's avatar
sof committed
125
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,2));
126
  SET_HDR(p, I64zh_con_info, CCS_SYSTEM);
127
128
129
130
131
132
  tmp  = (long long*)&(p->payload[0]);
  *tmp = (StgInt64)i;
  return p;
}

HaskellObj
133
rts_mkWord (HsWord i)
134
{
sof's avatar
sof committed
135
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
136
  SET_HDR(p, Wzh_con_info, CCS_SYSTEM);
137
138
139
140
141
  p->payload[0]  = (StgClosure *)(StgWord)i;
  return p;
}

HaskellObj
142
rts_mkWord8 (HsWord8 w)
143
144
{
  /* see rts_mkInt* comments */
sof's avatar
sof committed
145
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
146
  SET_HDR(p, W8zh_con_info, CCS_SYSTEM);
147
148
149
150
151
  p->payload[0]  = (StgClosure *)(StgWord)(w & 0xff);
  return p;
}

HaskellObj
152
rts_mkWord16 (HsWord16 w)
153
154
{
  /* see rts_mkInt* comments */
sof's avatar
sof committed
155
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
156
  SET_HDR(p, W16zh_con_info, CCS_SYSTEM);
157
158
159
160
161
  p->payload[0]  = (StgClosure *)(StgWord)(w & 0xffff);
  return p;
}

HaskellObj
162
rts_mkWord32 (HsWord32 w)
163
164
{
  /* see rts_mkInt* comments */
sof's avatar
sof committed
165
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
166
167
  SET_HDR(p, W32zh_con_info, CCS_SYSTEM);
  p->payload[0]  = (StgClosure *)(StgWord)(w & 0xffffffff);
168
169
170
171
  return p;
}

HaskellObj
172
rts_mkWord64 (HsWord64 w)
173
174
175
{
  unsigned long long *tmp;

sof's avatar
sof committed
176
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,2));
177
  /* see mk_Int8 comment */
178
  SET_HDR(p, W64zh_con_info, CCS_SYSTEM);
179
  tmp  = (unsigned long long*)&(p->payload[0]);
sof's avatar
sof committed
180
  *tmp = (StgWord64)w;
181
182
183
184
  return p;
}

HaskellObj
185
rts_mkFloat (HsFloat f)
186
{
sof's avatar
sof committed
187
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,1));
188
  SET_HDR(p, Fzh_con_info, CCS_SYSTEM);
189
190
191
192
193
  ASSIGN_FLT((P_)p->payload, (StgFloat)f);
  return p;
}

HaskellObj
194
rts_mkDouble (HsDouble d)
195
{
sof's avatar
sof committed
196
  StgClosure *p = (StgClosure *)alloc(CONSTR_sizeW(0,sizeofW(StgDouble)));
197
  SET_HDR(p, Dzh_con_info, CCS_SYSTEM);
198
199
200
201
202
  ASSIGN_DBL((P_)p->payload, (StgDouble)d);
  return p;
}

HaskellObj
203
rts_mkStablePtr (HsStablePtr s)
204
{
sof's avatar
sof committed
205
  StgClosure *p = (StgClosure *)alloc(sizeofW(StgHeader)+1);
206
  SET_HDR(p, StablePtr_con_info, CCS_SYSTEM);
207
208
209
210
211
  p->payload[0]  = (StgClosure *)s;
  return p;
}

HaskellObj
212
rts_mkPtr (HsPtr a)
213
{
sof's avatar
sof committed
214
  StgClosure *p = (StgClosure *)alloc(sizeofW(StgHeader)+1);
215
  SET_HDR(p, Ptr_con_info, CCS_SYSTEM);
216
217
218
219
220
221
  p->payload[0]  = (StgClosure *)a;
  return p;
}

#ifdef COMPILER /* GHC has em, Hugs doesn't */
HaskellObj
222
rts_mkBool (HsBool b)
223
224
{
  if (b) {
225
    return (StgClosure *)True_closure;
226
  } else {
227
    return (StgClosure *)False_closure;
228
229
230
231
232
233
  }
}

HaskellObj
rts_mkString (char *s)
{
234
  return rts_apply((StgClosure *)unpackCString_closure, rts_mkPtr(s));
235
}
236
#endif /* COMPILER */
237
238
239
240

HaskellObj
rts_apply (HaskellObj f, HaskellObj arg)
{
sof's avatar
sof committed
241
  StgAP_UPD *ap = (StgAP_UPD *)alloc(AP_sizeW(1));
242
  SET_HDR(ap, &stg_AP_UPD_info, CCS_SYSTEM);
243
244
  ap->n_args = 1;
  ap->fun    = f;
245
  ap->payload[0] = arg;
246
247
248
249
250
251
252
  return (StgClosure *)ap;
}

/* ----------------------------------------------------------------------------
   Deconstructing Haskell objects
   ------------------------------------------------------------------------- */

253
HsChar
254
255
rts_getChar (HaskellObj p)
{
256
257
  if ( p->header.info == Czh_con_info || 
       p->header.info == Czh_static_info) {
258
    return (StgChar)(StgWord)(p->payload[0]);
259
  } else {
sof's avatar
sof committed
260
    barf("rts_getChar: not a Char");
261
262
263
  }
}

264
HsInt
265
266
rts_getInt (HaskellObj p)
{
sof's avatar
sof committed
267
  if ( 1 ||
268
269
       p->header.info == Izh_con_info || 
       p->header.info == Izh_static_info ) {
sof's avatar
sof committed
270
    return (HsInt)(p->payload[0]);
sof's avatar
sof committed
271
  } else {
sof's avatar
sof committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    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 ) {
    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 ) {
    return (HsInt16)(HsInt)(p->payload[0]);
  } else {
    barf("rts_getInt16: not an Int16");
sof's avatar
sof committed
297
298
299
  }
}

300
HsInt32
sof's avatar
sof committed
301
302
303
rts_getInt32 (HaskellObj p)
{
  if ( 1 ||
304
305
       p->header.info == I32zh_con_info || 
       p->header.info == I32zh_static_info ) {
sof's avatar
sof committed
306
    return (HsInt32)(p->payload[0]);
307
  } else {
sof's avatar
sof committed
308
    barf("rts_getInt32: not an Int32");
309
310
311
  }
}

sof's avatar
sof committed
312
313
314
315
316
317
318
319
320
321
322
323
324
HsInt64
rts_getInt64 (HaskellObj p)
{
  HsInt64* tmp;
  if ( 1 ||
       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");
  }
}
325
HsWord
326
327
rts_getWord (HaskellObj p)
{
sof's avatar
sof committed
328
  if ( 1 || /* see above comment */
329
330
       p->header.info == Wzh_con_info ||
       p->header.info == Wzh_static_info ) {
sof's avatar
sof committed
331
    return (HsWord)(p->payload[0]);
332
  } else {
sof's avatar
sof committed
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    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 ) {
    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 ) {
    return (HsWord16)(HsWord)(p->payload[0]);
  } else {
    barf("rts_getWord16: not a Word16");
358
359
360
  }
}

361
HsWord32
sof's avatar
sof committed
362
363
364
rts_getWord32 (HaskellObj p)
{
  if ( 1 || /* see above comment */
365
366
       p->header.info == W32zh_con_info ||
       p->header.info == W32zh_static_info ) {
sof's avatar
sof committed
367
368
    return (unsigned int)(p->payload[0]);
  } else {
sof's avatar
sof committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
    barf("rts_getWord: not a Word");
  }
}


HsWord64
rts_getWord64 (HaskellObj p)
{
  HsWord64* tmp;
  if ( 1 || /* see above comment */
       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");
sof's avatar
sof committed
385
386
387
  }
}

388
HsFloat
389
390
rts_getFloat (HaskellObj p)
{
391
392
  if ( p->header.info == Fzh_con_info || 
       p->header.info == Fzh_static_info ) {
393
394
    return (float)(PK_FLT((P_)p->payload));
  } else {
sof's avatar
sof committed
395
    barf("rts_getFloat: not a Float");
396
397
398
  }
}

399
HsDouble
400
401
rts_getDouble (HaskellObj p)
{
402
403
  if ( p->header.info == Dzh_con_info || 
       p->header.info == Dzh_static_info ) {
404
405
    return (double)(PK_DBL((P_)p->payload));
  } else {
sof's avatar
sof committed
406
    barf("rts_getDouble: not a Double");
407
408
409
  }
}

410
HsStablePtr
411
412
rts_getStablePtr (HaskellObj p)
{
413
414
  if ( p->header.info == StablePtr_con_info || 
       p->header.info == StablePtr_static_info ) {
415
416
    return (StgStablePtr)(p->payload[0]);
  } else {
sof's avatar
sof committed
417
    barf("rts_getStablePtr: not a StablePtr");
418
419
420
  }
}

421
422
HsPtr
rts_getPtr (HaskellObj p)
423
{
424
425
  if ( p->header.info == Ptr_con_info || 
       p->header.info == Ptr_static_info ) {
426
427
    return (void *)(p->payload[0]);
  } else {
sof's avatar
sof committed
428
    barf("rts_getPtr: not an Ptr");
429
430
431
432
  }
}

#ifdef COMPILER /* GHC has em, Hugs doesn't */
433
HsBool
434
435
rts_getBool (HaskellObj p)
{
436
  if (p == True_closure) {
437
    return 1;
438
  } else if (p == False_closure) {
439
    return 0;
440
  } else {
sof's avatar
sof committed
441
    barf("rts_getBool: not a Bool");
442
443
444
445
446
447
448
449
450
451
  }
}
#endif /* COMPILER */

/* ----------------------------------------------------------------------------
   Evaluating Haskell expressions
   ------------------------------------------------------------------------- */
SchedulerStatus
rts_eval (HaskellObj p, /*out*/HaskellObj *ret)
{
452
453
454
    StgTSO *tso;

    tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p);
sof's avatar
sof committed
455
    releaseAllocLock();
sof's avatar
sof committed
456
    scheduleExtThread(tso);
457
    return waitThread(tso, ret);
458
459
460
461
462
}

SchedulerStatus
rts_eval_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
{
463
464
465
    StgTSO *tso;
    
    tso = createGenThread(stack_size, p);
sof's avatar
sof committed
466
    releaseAllocLock();
sof's avatar
sof committed
467
    scheduleExtThread(tso);
468
    return waitThread(tso, ret);
469
470
}

sof's avatar
sof committed
471
472
473
474
/*
 * rts_evalIO() evaluates a value of the form (IO a), forcing the action's
 * result to WHNF before returning.
 */
475
476
477
SchedulerStatus
rts_evalIO (HaskellObj p, /*out*/HaskellObj *ret)
{
478
479
480
    StgTSO* tso; 
    
    tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
sof's avatar
sof committed
481
    releaseAllocLock();
sof's avatar
sof committed
482
    scheduleExtThread(tso);
483
484
485
    return waitThread(tso, ret);
}

sof's avatar
sof committed
486
487
488
489
490
491
492
493
494
495
496
/*
 * 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);
    releaseAllocLock();
sof's avatar
sof committed
497
    scheduleThread(tso);
sof's avatar
sof committed
498
499
500
    return WAIT_MAIN_THREAD(tso, ret);
}

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 * 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);
sof's avatar
sof committed
516
    releaseAllocLock();
sof's avatar
sof committed
517
    scheduleExtThread(tso);
518
519
520
521
522
523
524
525
    stat = waitThread(tso, &r);

    if (stat == Success) {
	ASSERT(r != NULL);
	*ret = getStablePtr((StgPtr)r);
    }

    return stat;
526
527
}

sof's avatar
sof committed
528
529
530
/*
 * Like rts_evalIO(), but doesn't force the action's result.
 */
531
SchedulerStatus
sof's avatar
sof committed
532
rts_evalLazyIO (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
533
{
534
535
536
    StgTSO *tso;

    tso = createIOThread(stack_size, p);
sof's avatar
sof committed
537
    releaseAllocLock();
sof's avatar
sof committed
538
    scheduleExtThread(tso);
539
    return waitThread(tso, ret);
540
541
542
543
}

/* Convenience function for decoding the returned status. */

544
545
546
547
548
549
550
551
552
553
554
555
556
void
rts_checkSchedStatus ( char* site, SchedulerStatus rc )
{
    switch (rc) {
    case Success:
	return;
    case Killed:
	barf("%s: uncaught exception",site);
    case Interrupted:
	barf("%s: interrupted", site);
    default:
	barf("%s: Return code (%d) not ok",(site),(rc));	
    }
557
}