[project @ 2000-09-11 15:02:51 by rrt]
[ghc-hetmet.git] / ghc / rts / PrimOps.hc
index 8a2db25..74575d7 100644 (file)
@@ -1,7 +1,7 @@
 /* -----------------------------------------------------------------------------
- * $Id: PrimOps.hc,v 1.39 2000/01/13 14:34:03 hwloidl Exp $
+ * $Id: PrimOps.hc,v 1.54 2000/08/25 13:12:07 simonmar Exp $
  *
- * (c) The GHC Team, 1998-1999
+ * (c) The GHC Team, 1998-2000
  *
  * Primitive functions / data
  *
@@ -19,6 +19,8 @@
 #include "StablePriv.h"
 #include "HeapStackCheck.h"
 #include "StgRun.h"
+#include "Itimer.h"
+#include "Prelude.h"
 
 /* ** temporary **
 
@@ -183,7 +185,13 @@ W_ GHC_ZCCReturnable_static_info[0];
 #else /* 0 Regs available */
 
 #define PUSH_P(o,x) Sp[-o] = (W_)(x)
-#define PUSH_N(o,x) Sp[1-o] = (W_)(x); /* Sp[-o] = ARGTAG(1) */
+
+#ifdef DEBUG
+#define PUSH_N(o,x) Sp[1-o] = (W_)(x);  Sp[-o] = ARG_TAG(1);
+#else
+#define PUSH_N(o,x) Sp[1-o] = (W_)(x);
+#endif
+
 #define PUSHED(m)   Sp -= (m); JMP_(ENTRY_CODE(Sp[m]));
 
 /* Here's how to construct these macros:
@@ -255,13 +263,14 @@ W_ GHC_ZCCReturnable_static_info[0];
    FE_                                                 \
  }
 
-newByteArray(Char,   sizeof(C_))
-newByteArray(Int,    sizeof(I_));
-newByteArray(Word,   sizeof(W_));
-newByteArray(Addr,   sizeof(P_));
-newByteArray(Float,  sizeof(StgFloat));
-newByteArray(Double, sizeof(StgDouble));
-newByteArray(StablePtr, sizeof(StgStablePtr));
+newByteArray(Char,   1)
+/* Char arrays really contain only 8-bit bytes for compatibility. */
+newByteArray(Int,    sizeof(I_))
+newByteArray(Word,   sizeof(W_))
+newByteArray(Addr,   sizeof(P_))
+newByteArray(Float,  sizeof(StgFloat))
+newByteArray(Double, sizeof(StgDouble))
+newByteArray(StablePtr, sizeof(StgStablePtr))
 
 FN_(newArrayzh_fast)
 {
@@ -316,14 +325,14 @@ FN_(newMutVarzh_fast)
    -------------------------------------------------------------------------- */
 
 #ifndef PAR
-FN_(makeForeignObjzh_fast)
+FN_(mkForeignObjzh_fast)
 {
   /* R1.p = ptr to foreign object,
   */
   StgForeignObj *result;
   FB_
 
-  HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, makeForeignObjzh_fast,);
+  HP_CHK_GEN_TICKY(sizeofW(StgForeignObj), NO_PTRS, mkForeignObjzh_fast,);
   TICK_ALLOC_PRIM(sizeofW(StgHeader),
                  sizeofW(StgForeignObj)-sizeofW(StgHeader), 0);
   CCS_ALLOC(CCCS,sizeofW(StgForeignObj)); /* ccs prof */
@@ -361,12 +370,16 @@ FN_(mkWeakzh_fast)
 {
   /* R1.p = key
      R2.p = value
-     R3.p = finalizer
+     R3.p = finalizer (or NULL)
   */
   StgWeak *w;
   FB_
 
-  HP_CHK_GEN_TICKY(sizeofW(StgWeak), R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
+  if (R3.cl == NULL) {
+    R3.cl = &NO_FINALIZER_closure;
+  }
+
+  HP_CHK_GEN_TICKY(sizeofW(StgWeak),R1_PTR|R2_PTR|R3_PTR, mkWeakzh_fast,);
   TICK_ALLOC_PRIM(sizeofW(StgHeader)+1,  // +1 is for the link field
                  sizeofW(StgWeak)-sizeofW(StgHeader)-1, 0);
   CCS_ALLOC(CCCS,sizeofW(StgWeak)); /* ccs prof */
@@ -376,11 +389,7 @@ FN_(mkWeakzh_fast)
 
   w->key        = R1.cl;
   w->value      = R2.cl;
-  if (R3.cl) {
-     w->finalizer  = R3.cl;
-  } else {
-     w->finalizer  = &NO_FINALIZER_closure;
-  }
+  w->finalizer  = R3.cl;
 
   w->link       = weak_ptr_list;
   weak_ptr_list = w;
@@ -633,7 +642,7 @@ FN_(word64ToIntegerzh_fast)
 FN_(name)                                                              \
 {                                                                      \
   MP_INT arg1, arg2, result;                                           \
-  I_ s1, s2;                                                   \
+  I_ s1, s2;                                                           \
   StgArrWords* d1;                                                     \
   StgArrWords* d2;                                                     \
   FB_                                                                  \
@@ -668,7 +677,7 @@ FN_(name)                                                           \
 FN_(name)                                                              \
 {                                                                      \
   MP_INT arg1, arg2, result1, result2;                                 \
-  I_ s1, s2;                                                   \
+  I_ s1, s2;                                                           \
   StgArrWords* d1;                                                     \
   StgArrWords* d2;                                                     \
   FB_                                                                  \
@@ -880,6 +889,47 @@ FN_(takeMVarzh_fast)
   FE_
 }
 
+FN_(tryTakeMVarzh_fast)
+{
+  StgMVar *mvar;
+  StgClosure *val;
+  const StgInfoTable *info;
+
+  FB_
+  /* args: R1 = MVar closure */
+
+  mvar = (StgMVar *)R1.p;
+
+#ifdef SMP
+  info = LOCK_CLOSURE(mvar);
+#else
+  info = GET_INFO(mvar);
+#endif
+
+  if (info == &EMPTY_MVAR_info) {
+
+#ifdef SMP
+    /* unlock the MVar */
+    mvar->header.info = &EMPTY_MVAR_info;
+#endif
+
+    /* HACK: we need a pointer to pass back, so we abuse NO_FINALIZER_closure */
+    RET_NP(0, &NO_FINALIZER_closure);
+  }
+
+  val = mvar->value;
+  mvar->value = (StgClosure *)&END_TSO_QUEUE_closure;
+
+  /* do this last... we might have locked the MVar in the SMP case,
+   * and writing the info pointer will unlock it.
+   */
+  SET_INFO(mvar,&EMPTY_MVAR_info);
+
+  TICK_RET_UNBOXED_TUP(1);
+  RET_NP(1,val);
+  FE_
+}
+
 FN_(putMVarzh_fast)
 {
   StgMVar *mvar;
@@ -897,7 +947,13 @@ FN_(putMVarzh_fast)
 #endif
 
   if (info == &FULL_MVAR_info) {
-    barf("putMVar#: MVar already full");
+#ifdef INTERPRETER
+    fprintf(stderr, "fatal: put on a full MVar in Hugs; aborting\n" );
+    exit(1);
+#else
+    R1.cl = (StgClosure *)PutFullMVar_closure;
+    JMP_(raisezh_fast);
+#endif
   }
   
   mvar->value = R2.cl;
@@ -908,7 +964,7 @@ FN_(putMVarzh_fast)
   if (mvar->head != (StgTSO *)&END_TSO_QUEUE_closure) {
     ASSERT(mvar->head->why_blocked == BlockedOnMVar);
 #if defined(GRAN)
-# error FixME
+    mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
 #elif defined(PAR)
     // ToDo: check 2nd arg (mvar) is right
     mvar->head = RET_STGCALL2(StgTSO *,unblockOne,mvar->head,mvar);
@@ -993,6 +1049,8 @@ FN_(waitWritezh_fast)
 
 FN_(delayzh_fast)
 {
+  StgTSO *t, *prev;
+  nat target;
   FB_
     /* args: R1.i */
     ASSERT(CurrentTSO->why_blocked == NotBlocked);
@@ -1000,16 +1058,26 @@ FN_(delayzh_fast)
 
     ACQUIRE_LOCK(&sched_mutex);
 
-    /* Add on ticks_since_select, since these will be subtracted at
-     * the next awaitEvent call.
-     */
-    CurrentTSO->block_info.delay = R1.i + ticks_since_select;
+    target = (R1.i / (TICK_MILLISECS*1000)) + timestamp + ticks_since_timestamp;
+    CurrentTSO->block_info.target = target;
 
-    APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
+    /* Insert the new thread in the sleeping queue. */
+    prev = NULL;
+    t = sleeping_queue;
+    while (t != END_TSO_QUEUE && t->block_info.target < target) {
+       prev = t;
+       t = t->link;
+    }
+
+    CurrentTSO->link = t;
+    if (prev == NULL) {
+       sleeping_queue = CurrentTSO;
+    } else {
+       prev->link = CurrentTSO;
+    }
 
     RELEASE_LOCK(&sched_mutex);
     JMP_(stg_block_noregs);
   FE_
 }
 
-