1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * Out-of-line primitive operations
7 * This file contains the implementations of all the primitive
8 * operations ("primops") which are not expanded inline. See
9 * ghc/compiler/prelude/primops.txt.pp for a list of all the primops;
10 * this file contains code for most of those with the attribute
13 * Entry convention: the entry convention for a primop is that all the
14 * args are in Stg registers (R1, R2, etc.). This is to make writing
15 * the primops easier. (see compiler/codeGen/CgCallConv.hs).
17 * Return convention: results from a primop are generally returned
18 * using the ordinary unboxed tuple return convention. The C-- parser
19 * implements the RET_xxxx() macros to perform unboxed-tuple returns
20 * based on the prevailing return convention.
22 * This file is written in a subset of C--, extended with various
23 * features specific to GHC. It is compiled by GHC directly. For the
24 * syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
26 * ---------------------------------------------------------------------------*/
31 import pthread_mutex_lock;
32 import pthread_mutex_unlock;
34 import base_ControlziExceptionziBase_nestedAtomically_closure;
35 import EnterCriticalSection;
36 import LeaveCriticalSection;
37 import ghczmprim_GHCziBool_False_closure;
39 /*-----------------------------------------------------------------------------
42 Basically just new*Array - the others are all inline macros.
44 The size arg is always passed in R1, and the result returned in R1.
46 The slow entry point is for returning from a heap check, the saved
47 size argument must be re-loaded from the stack.
48 -------------------------------------------------------------------------- */
50 /* for objects that are *less* than the size of a word, make sure we
51 * round up to the nearest word for the size of the array.
56 W_ words, payload_words, n, p;
57 MAYBE_GC(NO_PTRS,stg_newByteArrayzh);
59 payload_words = ROUNDUP_BYTES_TO_WDS(n);
60 words = BYTES_TO_WDS(SIZEOF_StgArrWords) + payload_words;
61 ("ptr" p) = foreign "C" allocate(MyCapability() "ptr",words) [];
62 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
63 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
64 StgArrWords_words(p) = payload_words;
69 #define BA_MASK (BA_ALIGN-1)
71 stg_newPinnedByteArrayzh
73 W_ words, bytes, payload_words, p;
75 MAYBE_GC(NO_PTRS,stg_newPinnedByteArrayzh);
77 /* payload_words is what we will tell the profiler we had to allocate */
78 payload_words = ROUNDUP_BYTES_TO_WDS(bytes);
79 /* When we actually allocate memory, we need to allow space for the
81 bytes = bytes + SIZEOF_StgArrWords;
82 /* And we want to align to BA_ALIGN bytes, so we need to allow space
83 to shift up to BA_ALIGN - 1 bytes: */
84 bytes = bytes + BA_ALIGN - 1;
85 /* Now we convert to a number of words: */
86 words = ROUNDUP_BYTES_TO_WDS(bytes);
88 ("ptr" p) = foreign "C" allocatePinned(MyCapability() "ptr", words) [];
89 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
91 /* Now we need to move p forward so that the payload is aligned
93 p = p + ((-p - SIZEOF_StgArrWords) & BA_MASK);
95 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
96 StgArrWords_words(p) = payload_words;
100 stg_newAlignedPinnedByteArrayzh
102 W_ words, bytes, payload_words, p, alignment;
104 MAYBE_GC(NO_PTRS,stg_newAlignedPinnedByteArrayzh);
108 /* payload_words is what we will tell the profiler we had to allocate */
109 payload_words = ROUNDUP_BYTES_TO_WDS(bytes);
111 /* When we actually allocate memory, we need to allow space for the
113 bytes = bytes + SIZEOF_StgArrWords;
114 /* And we want to align to <alignment> bytes, so we need to allow space
115 to shift up to <alignment - 1> bytes: */
116 bytes = bytes + alignment - 1;
117 /* Now we convert to a number of words: */
118 words = ROUNDUP_BYTES_TO_WDS(bytes);
120 ("ptr" p) = foreign "C" allocatePinned(MyCapability() "ptr", words) [];
121 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
123 /* Now we need to move p forward so that the payload is aligned
124 to <alignment> bytes. Note that we are assuming that
125 <alignment> is a power of 2, which is technically not guaranteed */
126 p = p + ((-p - SIZEOF_StgArrWords) & (alignment - 1));
128 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
129 StgArrWords_words(p) = payload_words;
135 W_ words, n, init, arr, p, size;
136 /* Args: R1 = words, R2 = initialisation value */
139 MAYBE_GC(R2_PTR,stg_newArrayzh);
141 // the mark area contains one byte for each 2^MUT_ARR_PTRS_CARD_BITS words
142 // in the array, making sure we round up, and then rounding up to a whole
144 size = n + mutArrPtrsCardWords(n);
145 words = BYTES_TO_WDS(SIZEOF_StgMutArrPtrs) + size;
146 ("ptr" arr) = foreign "C" allocate(MyCapability() "ptr",words) [R2];
147 TICK_ALLOC_PRIM(SIZEOF_StgMutArrPtrs, WDS(n), 0);
149 SET_HDR(arr, stg_MUT_ARR_PTRS_DIRTY_info, W_[CCCS]);
150 StgMutArrPtrs_ptrs(arr) = n;
151 StgMutArrPtrs_size(arr) = size;
153 // Initialise all elements of the the array with the value in R2
155 p = arr + SIZEOF_StgMutArrPtrs;
157 if (p < arr + WDS(words)) {
162 // Initialise the mark bits with 0
164 if (p < arr + WDS(size)) {
173 stg_unsafeThawArrayzh
175 // SUBTLETY TO DO WITH THE OLD GEN MUTABLE LIST
177 // A MUT_ARR_PTRS lives on the mutable list, but a MUT_ARR_PTRS_FROZEN
178 // normally doesn't. However, when we freeze a MUT_ARR_PTRS, we leave
179 // it on the mutable list for the GC to remove (removing something from
180 // the mutable list is not easy).
182 // So that we can tell whether a MUT_ARR_PTRS_FROZEN is on the mutable list,
183 // when we freeze it we set the info ptr to be MUT_ARR_PTRS_FROZEN0
184 // to indicate that it is still on the mutable list.
186 // So, when we thaw a MUT_ARR_PTRS_FROZEN, we must cope with two cases:
187 // either it is on a mut_list, or it isn't. We adopt the convention that
188 // the closure type is MUT_ARR_PTRS_FROZEN0 if it is on the mutable list,
189 // and MUT_ARR_PTRS_FROZEN otherwise. In fact it wouldn't matter if
190 // we put it on the mutable list more than once, but it would get scavenged
191 // multiple times during GC, which would be unnecessarily slow.
193 if (StgHeader_info(R1) != stg_MUT_ARR_PTRS_FROZEN0_info) {
194 SET_INFO(R1,stg_MUT_ARR_PTRS_DIRTY_info);
195 recordMutable(R1, R1);
196 // must be done after SET_INFO, because it ASSERTs closure_MUTABLE()
199 SET_INFO(R1,stg_MUT_ARR_PTRS_DIRTY_info);
204 /* -----------------------------------------------------------------------------
206 -------------------------------------------------------------------------- */
211 /* Args: R1 = initialisation value */
213 ALLOC_PRIM( SIZEOF_StgMutVar, R1_PTR, stg_newMutVarzh);
215 mv = Hp - SIZEOF_StgMutVar + WDS(1);
216 SET_HDR(mv,stg_MUT_VAR_DIRTY_info,W_[CCCS]);
217 StgMutVar_var(mv) = R1;
222 stg_atomicModifyMutVarzh
224 W_ mv, f, z, x, y, r, h;
225 /* Args: R1 :: MutVar#, R2 :: a -> (a,b) */
227 /* If x is the current contents of the MutVar#, then
228 We want to make the new contents point to
232 and the return value is
236 obviously we can share (f x).
238 z = [stg_ap_2 f x] (max (HS + 2) MIN_UPD_SIZE)
239 y = [stg_sel_0 z] (max (HS + 1) MIN_UPD_SIZE)
240 r = [stg_sel_1 z] (max (HS + 1) MIN_UPD_SIZE)
244 #define THUNK_1_SIZE (SIZEOF_StgThunkHeader + WDS(MIN_UPD_SIZE))
245 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),WDS(MIN_UPD_SIZE-1))
247 #define THUNK_1_SIZE (SIZEOF_StgThunkHeader + WDS(1))
248 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),0)
252 #define THUNK_2_SIZE (SIZEOF_StgThunkHeader + WDS(MIN_UPD_SIZE))
253 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),WDS(MIN_UPD_SIZE-2))
255 #define THUNK_2_SIZE (SIZEOF_StgThunkHeader + WDS(2))
256 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),0)
259 #define SIZE (THUNK_2_SIZE + THUNK_1_SIZE + THUNK_1_SIZE)
261 HP_CHK_GEN_TICKY(SIZE, R1_PTR & R2_PTR, stg_atomicModifyMutVarzh);
266 TICK_ALLOC_THUNK_2();
267 CCCS_ALLOC(THUNK_2_SIZE);
268 z = Hp - THUNK_2_SIZE + WDS(1);
269 SET_HDR(z, stg_ap_2_upd_info, W_[CCCS]);
270 LDV_RECORD_CREATE(z);
271 StgThunk_payload(z,0) = f;
273 TICK_ALLOC_THUNK_1();
274 CCCS_ALLOC(THUNK_1_SIZE);
275 y = z - THUNK_1_SIZE;
276 SET_HDR(y, stg_sel_0_upd_info, W_[CCCS]);
277 LDV_RECORD_CREATE(y);
278 StgThunk_payload(y,0) = z;
280 TICK_ALLOC_THUNK_1();
281 CCCS_ALLOC(THUNK_1_SIZE);
282 r = y - THUNK_1_SIZE;
283 SET_HDR(r, stg_sel_1_upd_info, W_[CCCS]);
284 LDV_RECORD_CREATE(r);
285 StgThunk_payload(r,0) = z;
288 x = StgMutVar_var(mv);
289 StgThunk_payload(z,1) = x;
291 (h) = foreign "C" cas(mv + SIZEOF_StgHeader + OFFSET_StgMutVar_var, x, y) [];
292 if (h != x) { goto retry; }
294 StgMutVar_var(mv) = y;
297 if (GET_INFO(mv) == stg_MUT_VAR_CLEAN_info) {
298 foreign "C" dirty_MUT_VAR(BaseReg "ptr", mv "ptr") [];
304 /* -----------------------------------------------------------------------------
305 Weak Pointer Primitives
306 -------------------------------------------------------------------------- */
308 STRING(stg_weak_msg,"New weak pointer at %p\n")
314 R3 = finalizer (or NULL)
319 R3 = stg_NO_FINALIZER_closure;
322 ALLOC_PRIM( SIZEOF_StgWeak, R1_PTR & R2_PTR & R3_PTR, stg_mkWeakzh );
324 w = Hp - SIZEOF_StgWeak + WDS(1);
325 SET_HDR(w, stg_WEAK_info, W_[CCCS]);
327 // We don't care about cfinalizer here.
328 // Should StgWeak_cfinalizer(w) be stg_NO_FINALIZER_closure or
332 StgWeak_value(w) = R2;
333 StgWeak_finalizer(w) = R3;
334 StgWeak_cfinalizer(w) = stg_NO_FINALIZER_closure;
336 ACQUIRE_LOCK(sm_mutex);
337 StgWeak_link(w) = W_[weak_ptr_list];
338 W_[weak_ptr_list] = w;
339 RELEASE_LOCK(sm_mutex);
341 IF_DEBUG(weak, foreign "C" debugBelch(stg_weak_msg,w) []);
346 stg_mkWeakForeignEnvzh
352 R5 = has environment (0 or 1)
355 W_ w, payload_words, words, p;
357 W_ key, val, fptr, ptr, flag, eptr;
366 ALLOC_PRIM( SIZEOF_StgWeak, R1_PTR & R2_PTR, stg_mkWeakForeignEnvzh );
368 w = Hp - SIZEOF_StgWeak + WDS(1);
369 SET_HDR(w, stg_WEAK_info, W_[CCCS]);
372 words = BYTES_TO_WDS(SIZEOF_StgArrWords) + payload_words;
373 ("ptr" p) = foreign "C" allocate(MyCapability() "ptr", words) [];
375 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
376 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
378 StgArrWords_words(p) = payload_words;
379 StgArrWords_payload(p,0) = fptr;
380 StgArrWords_payload(p,1) = ptr;
381 StgArrWords_payload(p,2) = eptr;
382 StgArrWords_payload(p,3) = flag;
384 // We don't care about the value here.
385 // Should StgWeak_value(w) be stg_NO_FINALIZER_closure or something else?
387 StgWeak_key(w) = key;
388 StgWeak_value(w) = val;
389 StgWeak_finalizer(w) = stg_NO_FINALIZER_closure;
390 StgWeak_cfinalizer(w) = p;
392 ACQUIRE_LOCK(sm_mutex);
393 StgWeak_link(w) = W_[weak_ptr_list];
394 W_[weak_ptr_list] = w;
395 RELEASE_LOCK(sm_mutex);
397 IF_DEBUG(weak, foreign "C" debugBelch(stg_weak_msg,w) []);
411 if (GET_INFO(w) == stg_DEAD_WEAK_info) {
412 RET_NP(0,stg_NO_FINALIZER_closure);
418 // A weak pointer is inherently used, so we do not need to call
419 // LDV_recordDead_FILL_SLOP_DYNAMIC():
420 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
421 // or, LDV_recordDead():
422 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
423 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
424 // large as weak pointers, so there is no need to fill the slop, either.
425 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
429 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
431 SET_INFO(w,stg_DEAD_WEAK_info);
432 LDV_RECORD_CREATE(w);
434 f = StgWeak_finalizer(w);
435 arr = StgWeak_cfinalizer(w);
437 StgDeadWeak_link(w) = StgWeak_link(w);
439 if (arr != stg_NO_FINALIZER_closure) {
440 foreign "C" runCFinalizer(StgArrWords_payload(arr,0),
441 StgArrWords_payload(arr,1),
442 StgArrWords_payload(arr,2),
443 StgArrWords_payload(arr,3)) [];
446 /* return the finalizer */
447 if (f == stg_NO_FINALIZER_closure) {
448 RET_NP(0,stg_NO_FINALIZER_closure);
460 if (GET_INFO(w) == stg_WEAK_info) {
462 val = StgWeak_value(w);
470 /* -----------------------------------------------------------------------------
471 Floating point operations.
472 -------------------------------------------------------------------------- */
474 stg_decodeFloatzuIntzh
481 STK_CHK_GEN( WDS(2), NO_PTRS, stg_decodeFloatzuIntzh );
483 mp_tmp1 = Sp - WDS(1);
484 mp_tmp_w = Sp - WDS(2);
486 /* arguments: F1 = Float# */
489 /* Perform the operation */
490 foreign "C" __decodeFloat_Int(mp_tmp1 "ptr", mp_tmp_w "ptr", arg) [];
492 /* returns: (Int# (mantissa), Int# (exponent)) */
493 RET_NN(W_[mp_tmp1], W_[mp_tmp_w]);
496 stg_decodeDoublezu2Intzh
505 STK_CHK_GEN( WDS(4), NO_PTRS, stg_decodeDoublezu2Intzh );
507 mp_tmp1 = Sp - WDS(1);
508 mp_tmp2 = Sp - WDS(2);
509 mp_result1 = Sp - WDS(3);
510 mp_result2 = Sp - WDS(4);
512 /* arguments: D1 = Double# */
515 /* Perform the operation */
516 foreign "C" __decodeDouble_2Int(mp_tmp1 "ptr", mp_tmp2 "ptr",
517 mp_result1 "ptr", mp_result2 "ptr",
521 (Int# (mant sign), Word# (mant high), Word# (mant low), Int# (expn)) */
522 RET_NNNN(W_[mp_tmp1], W_[mp_tmp2], W_[mp_result1], W_[mp_result2]);
525 /* -----------------------------------------------------------------------------
526 * Concurrency primitives
527 * -------------------------------------------------------------------------- */
531 /* args: R1 = closure to spark */
533 MAYBE_GC(R1_PTR, stg_forkzh);
539 ("ptr" threadid) = foreign "C" createIOThread( MyCapability() "ptr",
540 RtsFlags_GcFlags_initialStkSize(RtsFlags),
543 /* start blocked if the current thread is blocked */
544 StgTSO_flags(threadid) =
545 StgTSO_flags(threadid) | (StgTSO_flags(CurrentTSO) &
546 (TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32));
548 foreign "C" scheduleThread(MyCapability() "ptr", threadid "ptr") [];
550 // context switch soon, but not immediately: we don't want every
551 // forkIO to force a context-switch.
552 Capability_context_switch(MyCapability()) = 1 :: CInt;
559 /* args: R1 = cpu, R2 = closure to spark */
561 MAYBE_GC(R2_PTR, stg_forkOnzh);
569 ("ptr" threadid) = foreign "C" createIOThread( MyCapability() "ptr",
570 RtsFlags_GcFlags_initialStkSize(RtsFlags),
573 /* start blocked if the current thread is blocked */
574 StgTSO_flags(threadid) =
575 StgTSO_flags(threadid) | (StgTSO_flags(CurrentTSO) &
576 (TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32));
578 foreign "C" scheduleThreadOn(MyCapability() "ptr", cpu, threadid "ptr") [];
580 // context switch soon, but not immediately: we don't want every
581 // forkIO to force a context-switch.
582 Capability_context_switch(MyCapability()) = 1 :: CInt;
589 jump stg_yield_noregs;
604 foreign "C" labelThread(R1 "ptr", R2 "ptr") [];
606 jump %ENTRY_CODE(Sp(0));
609 stg_isCurrentThreadBoundzh
613 (r) = foreign "C" isThreadBound(CurrentTSO) [];
619 /* args: R1 :: ThreadId# */
627 if (TO_W_(StgTSO_what_next(tso)) == ThreadRelocated) {
628 tso = StgTSO__link(tso);
632 what_next = TO_W_(StgTSO_what_next(tso));
633 why_blocked = TO_W_(StgTSO_why_blocked(tso));
634 // Note: these two reads are not atomic, so they might end up
635 // being inconsistent. It doesn't matter, since we
636 // only return one or the other. If we wanted to return the
637 // contents of block_info too, then we'd have to do some synchronisation.
639 if (what_next == ThreadComplete) {
640 ret = 16; // NB. magic, matches up with GHC.Conc.threadStatus
642 if (what_next == ThreadKilled) {
651 /* -----------------------------------------------------------------------------
653 * -------------------------------------------------------------------------- */
657 // Catch retry frame ------------------------------------------------------------
659 INFO_TABLE_RET(stg_catch_retry_frame, CATCH_RETRY_FRAME,
660 #if defined(PROFILING)
661 W_ unused1, W_ unused2,
663 W_ unused3, P_ unused4, P_ unused5)
665 W_ r, frame, trec, outer;
668 trec = StgTSO_trec(CurrentTSO);
669 outer = StgTRecHeader_enclosing_trec(trec);
670 (r) = foreign "C" stmCommitNestedTransaction(MyCapability() "ptr", trec "ptr") [];
672 /* Succeeded (either first branch or second branch) */
673 StgTSO_trec(CurrentTSO) = outer;
674 Sp = Sp + SIZEOF_StgCatchRetryFrame;
675 jump %ENTRY_CODE(Sp(SP_OFF));
677 /* Did not commit: re-execute */
679 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
680 StgTSO_trec(CurrentTSO) = new_trec;
681 if (StgCatchRetryFrame_running_alt_code(frame) != 0::I32) {
682 R1 = StgCatchRetryFrame_alt_code(frame);
684 R1 = StgCatchRetryFrame_first_code(frame);
691 // Atomically frame ------------------------------------------------------------
693 INFO_TABLE_RET(stg_atomically_frame, ATOMICALLY_FRAME,
694 #if defined(PROFILING)
695 W_ unused1, W_ unused2,
697 P_ code, P_ next_invariant_to_check, P_ result)
699 W_ frame, trec, valid, next_invariant, q, outer;
702 trec = StgTSO_trec(CurrentTSO);
704 outer = StgTRecHeader_enclosing_trec(trec);
706 if (outer == NO_TREC) {
707 /* First time back at the atomically frame -- pick up invariants */
708 ("ptr" q) = foreign "C" stmGetInvariantsToCheck(MyCapability() "ptr", trec "ptr") [];
709 StgAtomicallyFrame_next_invariant_to_check(frame) = q;
710 StgAtomicallyFrame_result(frame) = result;
713 /* Second/subsequent time back at the atomically frame -- abort the
714 * tx that's checking the invariant and move on to the next one */
715 StgTSO_trec(CurrentTSO) = outer;
716 q = StgAtomicallyFrame_next_invariant_to_check(frame);
717 StgInvariantCheckQueue_my_execution(q) = trec;
718 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
719 /* Don't free trec -- it's linked from q and will be stashed in the
720 * invariant if we eventually commit. */
721 q = StgInvariantCheckQueue_next_queue_entry(q);
722 StgAtomicallyFrame_next_invariant_to_check(frame) = q;
726 q = StgAtomicallyFrame_next_invariant_to_check(frame);
728 if (q != END_INVARIANT_CHECK_QUEUE) {
729 /* We can't commit yet: another invariant to check */
730 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", trec "ptr") [];
731 StgTSO_trec(CurrentTSO) = trec;
733 next_invariant = StgInvariantCheckQueue_invariant(q);
734 R1 = StgAtomicInvariant_code(next_invariant);
739 /* We've got no more invariants to check, try to commit */
740 (valid) = foreign "C" stmCommitTransaction(MyCapability() "ptr", trec "ptr") [];
742 /* Transaction was valid: commit succeeded */
743 StgTSO_trec(CurrentTSO) = NO_TREC;
744 R1 = StgAtomicallyFrame_result(frame);
745 Sp = Sp + SIZEOF_StgAtomicallyFrame;
746 jump %ENTRY_CODE(Sp(SP_OFF));
748 /* Transaction was not valid: try again */
749 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", NO_TREC "ptr") [];
750 StgTSO_trec(CurrentTSO) = trec;
751 StgAtomicallyFrame_next_invariant_to_check(frame) = END_INVARIANT_CHECK_QUEUE;
752 R1 = StgAtomicallyFrame_code(frame);
758 INFO_TABLE_RET(stg_atomically_waiting_frame, ATOMICALLY_FRAME,
759 #if defined(PROFILING)
760 W_ unused1, W_ unused2,
762 P_ code, P_ next_invariant_to_check, P_ result)
764 W_ frame, trec, valid;
768 /* The TSO is currently waiting: should we stop waiting? */
769 (valid) = foreign "C" stmReWait(MyCapability() "ptr", CurrentTSO "ptr") [];
771 /* Previous attempt is still valid: no point trying again yet */
772 jump stg_block_noregs;
774 /* Previous attempt is no longer valid: try again */
775 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", NO_TREC "ptr") [];
776 StgTSO_trec(CurrentTSO) = trec;
777 StgHeader_info(frame) = stg_atomically_frame_info;
778 R1 = StgAtomicallyFrame_code(frame);
783 // STM catch frame --------------------------------------------------------------
787 /* Catch frames are very similar to update frames, but when entering
788 * one we just pop the frame off the stack and perform the correct
789 * kind of return to the activation record underneath us on the stack.
792 INFO_TABLE_RET(stg_catch_stm_frame, CATCH_STM_FRAME,
793 #if defined(PROFILING)
794 W_ unused1, W_ unused2,
796 P_ unused3, P_ unused4)
798 W_ r, frame, trec, outer;
800 trec = StgTSO_trec(CurrentTSO);
801 outer = StgTRecHeader_enclosing_trec(trec);
802 (r) = foreign "C" stmCommitNestedTransaction(MyCapability() "ptr", trec "ptr") [];
804 /* Commit succeeded */
805 StgTSO_trec(CurrentTSO) = outer;
806 Sp = Sp + SIZEOF_StgCatchSTMFrame;
811 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
812 StgTSO_trec(CurrentTSO) = new_trec;
813 R1 = StgCatchSTMFrame_code(frame);
819 // Primop definition ------------------------------------------------------------
827 // stmStartTransaction may allocate
828 MAYBE_GC (R1_PTR, stg_atomicallyzh);
830 /* Args: R1 = m :: STM a */
831 STK_CHK_GEN(SIZEOF_StgAtomicallyFrame + WDS(1), R1_PTR, stg_atomicallyzh);
833 old_trec = StgTSO_trec(CurrentTSO);
835 /* Nested transactions are not allowed; raise an exception */
836 if (old_trec != NO_TREC) {
837 R1 = base_ControlziExceptionziBase_nestedAtomically_closure;
841 /* Set up the atomically frame */
842 Sp = Sp - SIZEOF_StgAtomicallyFrame;
845 SET_HDR(frame,stg_atomically_frame_info, W_[CCCS]);
846 StgAtomicallyFrame_code(frame) = R1;
847 StgAtomicallyFrame_result(frame) = NO_TREC;
848 StgAtomicallyFrame_next_invariant_to_check(frame) = END_INVARIANT_CHECK_QUEUE;
850 /* Start the memory transcation */
851 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", old_trec "ptr") [R1];
852 StgTSO_trec(CurrentTSO) = new_trec;
854 /* Apply R1 to the realworld token */
863 /* Args: R1 :: STM a */
864 /* Args: R2 :: Exception -> STM a */
865 STK_CHK_GEN(SIZEOF_StgCatchSTMFrame + WDS(1), R1_PTR & R2_PTR, stg_catchSTMzh);
867 /* Set up the catch frame */
868 Sp = Sp - SIZEOF_StgCatchSTMFrame;
871 SET_HDR(frame, stg_catch_stm_frame_info, W_[CCCS]);
872 StgCatchSTMFrame_handler(frame) = R2;
873 StgCatchSTMFrame_code(frame) = R1;
875 /* Start a nested transaction to run the body of the try block in */
878 cur_trec = StgTSO_trec(CurrentTSO);
879 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", cur_trec "ptr");
880 StgTSO_trec(CurrentTSO) = new_trec;
882 /* Apply R1 to the realworld token */
893 // stmStartTransaction may allocate
894 MAYBE_GC (R1_PTR & R2_PTR, stg_catchRetryzh);
896 /* Args: R1 :: STM a */
897 /* Args: R2 :: STM a */
898 STK_CHK_GEN(SIZEOF_StgCatchRetryFrame + WDS(1), R1_PTR & R2_PTR, stg_catchRetryzh);
900 /* Start a nested transaction within which to run the first code */
901 trec = StgTSO_trec(CurrentTSO);
902 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", trec "ptr") [R1,R2];
903 StgTSO_trec(CurrentTSO) = new_trec;
905 /* Set up the catch-retry frame */
906 Sp = Sp - SIZEOF_StgCatchRetryFrame;
909 SET_HDR(frame, stg_catch_retry_frame_info, W_[CCCS]);
910 StgCatchRetryFrame_running_alt_code(frame) = 0 :: CInt; // false;
911 StgCatchRetryFrame_first_code(frame) = R1;
912 StgCatchRetryFrame_alt_code(frame) = R2;
914 /* Apply R1 to the realworld token */
927 MAYBE_GC (NO_PTRS, stg_retryzh); // STM operations may allocate
929 // Find the enclosing ATOMICALLY_FRAME or CATCH_RETRY_FRAME
931 StgTSO_sp(CurrentTSO) = Sp;
932 (frame_type) = foreign "C" findRetryFrameHelper(CurrentTSO "ptr") [];
933 Sp = StgTSO_sp(CurrentTSO);
935 trec = StgTSO_trec(CurrentTSO);
936 outer = StgTRecHeader_enclosing_trec(trec);
938 if (frame_type == CATCH_RETRY_FRAME) {
939 // The retry reaches a CATCH_RETRY_FRAME before the atomic frame
940 ASSERT(outer != NO_TREC);
941 // Abort the transaction attempting the current branch
942 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
943 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
944 if (!StgCatchRetryFrame_running_alt_code(frame) != 0::I32) {
945 // Retry in the first branch: try the alternative
946 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
947 StgTSO_trec(CurrentTSO) = trec;
948 StgCatchRetryFrame_running_alt_code(frame) = 1 :: CInt; // true;
949 R1 = StgCatchRetryFrame_alt_code(frame);
952 // Retry in the alternative code: propagate the retry
953 StgTSO_trec(CurrentTSO) = outer;
954 Sp = Sp + SIZEOF_StgCatchRetryFrame;
955 goto retry_pop_stack;
959 // We've reached the ATOMICALLY_FRAME: attempt to wait
960 ASSERT(frame_type == ATOMICALLY_FRAME);
961 if (outer != NO_TREC) {
962 // We called retry while checking invariants, so abort the current
963 // invariant check (merging its TVar accesses into the parents read
964 // set so we'll wait on them)
965 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
966 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
968 StgTSO_trec(CurrentTSO) = trec;
969 outer = StgTRecHeader_enclosing_trec(trec);
971 ASSERT(outer == NO_TREC);
973 (r) = foreign "C" stmWait(MyCapability() "ptr", CurrentTSO "ptr", trec "ptr") [];
975 // Transaction was valid: stmWait put us on the TVars' queues, we now block
976 StgHeader_info(frame) = stg_atomically_waiting_frame_info;
978 // Fix up the stack in the unregisterised case: the return convention is different.
979 R3 = trec; // passing to stmWaitUnblock()
980 jump stg_block_stmwait;
982 // Transaction was not valid: retry immediately
983 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
984 StgTSO_trec(CurrentTSO) = trec;
985 R1 = StgAtomicallyFrame_code(frame);
996 /* Args: R1 = invariant closure */
997 MAYBE_GC (R1_PTR, stg_checkzh);
999 trec = StgTSO_trec(CurrentTSO);
1001 foreign "C" stmAddInvariantToCheck(MyCapability() "ptr",
1005 jump %ENTRY_CODE(Sp(0));
1014 /* Args: R1 = initialisation value */
1016 MAYBE_GC (R1_PTR, stg_newTVarzh);
1018 ("ptr" tv) = foreign "C" stmNewTVar(MyCapability() "ptr", new_value "ptr") [];
1029 /* Args: R1 = TVar closure */
1031 MAYBE_GC (R1_PTR, stg_readTVarzh); // Call to stmReadTVar may allocate
1032 trec = StgTSO_trec(CurrentTSO);
1034 ("ptr" result) = foreign "C" stmReadTVar(MyCapability() "ptr", trec "ptr", tvar "ptr") [];
1044 result = StgTVar_current_value(R1);
1045 if (%INFO_PTR(result) == stg_TREC_HEADER_info) {
1057 /* Args: R1 = TVar closure */
1058 /* R2 = New value */
1060 MAYBE_GC (R1_PTR & R2_PTR, stg_writeTVarzh); // Call to stmWriteTVar may allocate
1061 trec = StgTSO_trec(CurrentTSO);
1064 foreign "C" stmWriteTVar(MyCapability() "ptr", trec "ptr", tvar "ptr", new_value "ptr") [];
1066 jump %ENTRY_CODE(Sp(0));
1070 /* -----------------------------------------------------------------------------
1073 * take & putMVar work as follows. Firstly, an important invariant:
1075 * If the MVar is full, then the blocking queue contains only
1076 * threads blocked on putMVar, and if the MVar is empty then the
1077 * blocking queue contains only threads blocked on takeMVar.
1080 * MVar empty : then add ourselves to the blocking queue
1081 * MVar full : remove the value from the MVar, and
1082 * blocking queue empty : return
1083 * blocking queue non-empty : perform the first blocked putMVar
1084 * from the queue, and wake up the
1085 * thread (MVar is now full again)
1087 * putMVar is just the dual of the above algorithm.
1089 * How do we "perform a putMVar"? Well, we have to fiddle around with
1090 * the stack of the thread waiting to do the putMVar. See
1091 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
1092 * the stack layout, and the PerformPut and PerformTake macros below.
1094 * It is important that a blocked take or put is woken up with the
1095 * take/put already performed, because otherwise there would be a
1096 * small window of vulnerability where the thread could receive an
1097 * exception and never perform its take or put, and we'd end up with a
1100 * -------------------------------------------------------------------------- */
1104 /* args: R1 = MVar closure */
1106 if (StgMVar_value(R1) == stg_END_TSO_QUEUE_closure) {
1118 ALLOC_PRIM ( SIZEOF_StgMVar, NO_PTRS, stg_newMVarzh );
1120 mvar = Hp - SIZEOF_StgMVar + WDS(1);
1121 SET_HDR(mvar,stg_MVAR_DIRTY_info,W_[CCCS]);
1122 // MVARs start dirty: generation 0 has no mutable list
1123 StgMVar_head(mvar) = stg_END_TSO_QUEUE_closure;
1124 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1125 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1130 #define PerformTake(tso, value) \
1131 W_[StgTSO_sp(tso) + WDS(1)] = value; \
1132 W_[StgTSO_sp(tso) + WDS(0)] = stg_gc_unpt_r1_info;
1134 #define PerformPut(tso,lval) \
1135 StgTSO_sp(tso) = StgTSO_sp(tso) + WDS(3); \
1136 lval = W_[StgTSO_sp(tso) - WDS(1)];
1140 W_ mvar, val, info, tso;
1142 /* args: R1 = MVar closure */
1145 #if defined(THREADED_RTS)
1146 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [];
1148 info = GET_INFO(mvar);
1151 if (info == stg_MVAR_CLEAN_info) {
1152 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr") [];
1155 /* If the MVar is empty, put ourselves on its blocking queue,
1156 * and wait until we're woken up.
1158 if (StgMVar_value(mvar) == stg_END_TSO_QUEUE_closure) {
1159 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1160 StgMVar_head(mvar) = CurrentTSO;
1162 foreign "C" setTSOLink(MyCapability() "ptr",
1163 StgMVar_tail(mvar) "ptr",
1166 StgTSO__link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1167 StgTSO_block_info(CurrentTSO) = mvar;
1168 // write barrier for throwTo(), which looks at block_info
1169 // if why_blocked==BlockedOnMVar.
1170 prim %write_barrier() [];
1171 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1172 StgMVar_tail(mvar) = CurrentTSO;
1175 jump stg_block_takemvar;
1178 /* we got the value... */
1179 val = StgMVar_value(mvar);
1181 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure)
1183 /* There are putMVar(s) waiting...
1184 * wake up the first thread on the queue
1186 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1188 /* actually perform the putMVar for the thread that we just woke up */
1189 tso = StgMVar_head(mvar);
1190 PerformPut(tso,StgMVar_value(mvar));
1192 if (TO_W_(StgTSO_dirty(tso)) == 0) {
1193 foreign "C" dirty_TSO(MyCapability() "ptr", tso "ptr") [];
1196 ("ptr" tso) = foreign "C" unblockOne_(MyCapability() "ptr",
1197 StgMVar_head(mvar) "ptr", 1) [];
1198 StgMVar_head(mvar) = tso;
1200 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1201 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1204 #if defined(THREADED_RTS)
1205 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1207 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1213 /* No further putMVars, MVar is now empty */
1214 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1216 #if defined(THREADED_RTS)
1217 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1219 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1229 W_ mvar, val, info, tso;
1231 /* args: R1 = MVar closure */
1235 #if defined(THREADED_RTS)
1236 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [];
1238 info = GET_INFO(mvar);
1241 if (StgMVar_value(mvar) == stg_END_TSO_QUEUE_closure) {
1242 #if defined(THREADED_RTS)
1243 unlockClosure(mvar, info);
1245 /* HACK: we need a pointer to pass back,
1246 * so we abuse NO_FINALIZER_closure
1248 RET_NP(0, stg_NO_FINALIZER_closure);
1251 if (info == stg_MVAR_CLEAN_info) {
1252 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1255 /* we got the value... */
1256 val = StgMVar_value(mvar);
1258 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1260 /* There are putMVar(s) waiting...
1261 * wake up the first thread on the queue
1263 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1265 /* actually perform the putMVar for the thread that we just woke up */
1266 tso = StgMVar_head(mvar);
1267 PerformPut(tso,StgMVar_value(mvar));
1268 if (TO_W_(StgTSO_dirty(tso)) == 0) {
1269 foreign "C" dirty_TSO(MyCapability() "ptr", tso "ptr") [];
1272 ("ptr" tso) = foreign "C" unblockOne_(MyCapability() "ptr",
1273 StgMVar_head(mvar) "ptr", 1) [];
1274 StgMVar_head(mvar) = tso;
1276 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1277 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1279 #if defined(THREADED_RTS)
1280 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1282 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1287 /* No further putMVars, MVar is now empty */
1288 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1289 #if defined(THREADED_RTS)
1290 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1292 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1302 W_ mvar, val, info, tso;
1304 /* args: R1 = MVar, R2 = value */
1308 #if defined(THREADED_RTS)
1309 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [];
1311 info = GET_INFO(mvar);
1314 if (info == stg_MVAR_CLEAN_info) {
1315 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1318 if (StgMVar_value(mvar) != stg_END_TSO_QUEUE_closure) {
1319 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1320 StgMVar_head(mvar) = CurrentTSO;
1322 foreign "C" setTSOLink(MyCapability() "ptr",
1323 StgMVar_tail(mvar) "ptr",
1326 StgTSO__link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1327 StgTSO_block_info(CurrentTSO) = mvar;
1328 // write barrier for throwTo(), which looks at block_info
1329 // if why_blocked==BlockedOnMVar.
1330 prim %write_barrier() [];
1331 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1332 StgMVar_tail(mvar) = CurrentTSO;
1336 jump stg_block_putmvar;
1339 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1341 /* There are takeMVar(s) waiting: wake up the first one
1343 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1345 /* actually perform the takeMVar */
1346 tso = StgMVar_head(mvar);
1347 PerformTake(tso, val);
1348 if (TO_W_(StgTSO_dirty(tso)) == 0) {
1349 foreign "C" dirty_TSO(MyCapability() "ptr", tso "ptr") [];
1352 ("ptr" tso) = foreign "C" unblockOne_(MyCapability() "ptr",
1353 StgMVar_head(mvar) "ptr", 1) [];
1354 StgMVar_head(mvar) = tso;
1356 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1357 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1360 #if defined(THREADED_RTS)
1361 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1363 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1365 jump %ENTRY_CODE(Sp(0));
1369 /* No further takes, the MVar is now full. */
1370 StgMVar_value(mvar) = val;
1372 #if defined(THREADED_RTS)
1373 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1375 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1377 jump %ENTRY_CODE(Sp(0));
1380 /* ToDo: yield afterward for better communication performance? */
1388 /* args: R1 = MVar, R2 = value */
1391 #if defined(THREADED_RTS)
1392 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [R2];
1394 info = GET_INFO(mvar);
1397 if (StgMVar_value(mvar) != stg_END_TSO_QUEUE_closure) {
1398 #if defined(THREADED_RTS)
1399 unlockClosure(mvar, info);
1404 if (info == stg_MVAR_CLEAN_info) {
1405 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1408 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1410 /* There are takeMVar(s) waiting: wake up the first one
1412 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1414 /* actually perform the takeMVar */
1415 tso = StgMVar_head(mvar);
1416 PerformTake(tso, R2);
1417 if (TO_W_(StgTSO_dirty(tso)) == 0) {
1418 foreign "C" dirty_TSO(MyCapability() "ptr", tso "ptr") [];
1421 ("ptr" tso) = foreign "C" unblockOne_(MyCapability() "ptr",
1422 StgMVar_head(mvar) "ptr", 1) [];
1423 StgMVar_head(mvar) = tso;
1425 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1426 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1429 #if defined(THREADED_RTS)
1430 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1432 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1437 /* No further takes, the MVar is now full. */
1438 StgMVar_value(mvar) = R2;
1440 #if defined(THREADED_RTS)
1441 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1443 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1448 /* ToDo: yield afterward for better communication performance? */
1452 /* -----------------------------------------------------------------------------
1453 Stable pointer primitives
1454 ------------------------------------------------------------------------- */
1456 stg_makeStableNamezh
1460 ALLOC_PRIM( SIZEOF_StgStableName, R1_PTR, stg_makeStableNamezh );
1462 (index) = foreign "C" lookupStableName(R1 "ptr") [];
1464 /* Is there already a StableName for this heap object?
1465 * stable_ptr_table is a pointer to an array of snEntry structs.
1467 if ( snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry) == NULL ) {
1468 sn_obj = Hp - SIZEOF_StgStableName + WDS(1);
1469 SET_HDR(sn_obj, stg_STABLE_NAME_info, W_[CCCS]);
1470 StgStableName_sn(sn_obj) = index;
1471 snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry) = sn_obj;
1473 sn_obj = snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry);
1484 MAYBE_GC(R1_PTR, stg_makeStablePtrzh);
1485 ("ptr" sp) = foreign "C" getStablePtr(R1 "ptr") [];
1489 stg_deRefStablePtrzh
1491 /* Args: R1 = the stable ptr */
1494 r = snEntry_addr(W_[stable_ptr_table] + sp*SIZEOF_snEntry);
1498 /* -----------------------------------------------------------------------------
1499 Bytecode object primitives
1500 ------------------------------------------------------------------------- */
1510 W_ bco, bitmap_arr, bytes, words;
1514 words = BYTES_TO_WDS(SIZEOF_StgBCO) + StgArrWords_words(bitmap_arr);
1517 ALLOC_PRIM( bytes, R1_PTR&R2_PTR&R3_PTR&R5_PTR, stg_newBCOzh );
1519 bco = Hp - bytes + WDS(1);
1520 SET_HDR(bco, stg_BCO_info, W_[CCCS]);
1522 StgBCO_instrs(bco) = R1;
1523 StgBCO_literals(bco) = R2;
1524 StgBCO_ptrs(bco) = R3;
1525 StgBCO_arity(bco) = HALF_W_(R4);
1526 StgBCO_size(bco) = HALF_W_(words);
1528 // Copy the arity/bitmap info into the BCO
1532 if (i < StgArrWords_words(bitmap_arr)) {
1533 StgBCO_bitmap(bco,i) = StgArrWords_payload(bitmap_arr,i);
1544 // R1 = the BCO# for the AP
1548 // This function is *only* used to wrap zero-arity BCOs in an
1549 // updatable wrapper (see ByteCodeLink.lhs). An AP thunk is always
1550 // saturated and always points directly to a FUN or BCO.
1551 ASSERT(%INFO_TYPE(%GET_STD_INFO(R1)) == HALF_W_(BCO) &&
1552 StgBCO_arity(R1) == HALF_W_(0));
1554 HP_CHK_GEN_TICKY(SIZEOF_StgAP, R1_PTR, stg_mkApUpd0zh);
1555 TICK_ALLOC_UP_THK(0, 0);
1556 CCCS_ALLOC(SIZEOF_StgAP);
1558 ap = Hp - SIZEOF_StgAP + WDS(1);
1559 SET_HDR(ap, stg_AP_info, W_[CCCS]);
1561 StgAP_n_args(ap) = HALF_W_(0);
1569 /* args: R1 = closure to analyze */
1570 // TODO: Consider the absence of ptrs or nonptrs as a special case ?
1572 W_ info, ptrs, nptrs, p, ptrs_arr, nptrs_arr;
1573 info = %GET_STD_INFO(UNTAG(R1));
1575 // Some closures have non-standard layout, so we omit those here.
1577 type = TO_W_(%INFO_TYPE(info));
1578 switch [0 .. N_CLOSURE_TYPES] type {
1579 case THUNK_SELECTOR : {
1584 case THUNK, THUNK_1_0, THUNK_0_1, THUNK_2_0, THUNK_1_1,
1585 THUNK_0_2, THUNK_STATIC, AP, PAP, AP_STACK, BCO : {
1591 ptrs = TO_W_(%INFO_PTRS(info));
1592 nptrs = TO_W_(%INFO_NPTRS(info));
1597 W_ ptrs_arr_sz, ptrs_arr_cards, nptrs_arr_sz;
1598 nptrs_arr_sz = SIZEOF_StgArrWords + WDS(nptrs);
1599 ptrs_arr_cards = mutArrPtrsCardWords(ptrs);
1600 ptrs_arr_sz = SIZEOF_StgMutArrPtrs + WDS(ptrs) + WDS(ptrs_arr_cards);
1602 ALLOC_PRIM (ptrs_arr_sz + nptrs_arr_sz, R1_PTR, stg_unpackClosurezh);
1607 ptrs_arr = Hp - nptrs_arr_sz - ptrs_arr_sz + WDS(1);
1608 nptrs_arr = Hp - nptrs_arr_sz + WDS(1);
1610 SET_HDR(ptrs_arr, stg_MUT_ARR_PTRS_FROZEN_info, W_[CCCS]);
1611 StgMutArrPtrs_ptrs(ptrs_arr) = ptrs;
1612 StgMutArrPtrs_size(ptrs_arr) = ptrs + ptrs_arr_cards;
1617 W_[ptrs_arr + SIZEOF_StgMutArrPtrs + WDS(p)] = StgClosure_payload(clos,p);
1621 /* We can leave the card table uninitialised, since the array is
1622 allocated in the nursery. The GC will fill it in if/when the array
1625 SET_HDR(nptrs_arr, stg_ARR_WORDS_info, W_[CCCS]);
1626 StgArrWords_words(nptrs_arr) = nptrs;
1630 W_[BYTE_ARR_CTS(nptrs_arr) + WDS(p)] = StgClosure_payload(clos, p+ptrs);
1634 RET_NPP(info, ptrs_arr, nptrs_arr);
1637 /* -----------------------------------------------------------------------------
1638 Thread I/O blocking primitives
1639 -------------------------------------------------------------------------- */
1641 /* Add a thread to the end of the blocked queue. (C-- version of the C
1642 * macro in Schedule.h).
1644 #define APPEND_TO_BLOCKED_QUEUE(tso) \
1645 ASSERT(StgTSO__link(tso) == END_TSO_QUEUE); \
1646 if (W_[blocked_queue_hd] == END_TSO_QUEUE) { \
1647 W_[blocked_queue_hd] = tso; \
1649 foreign "C" setTSOLink(MyCapability() "ptr", W_[blocked_queue_tl] "ptr", tso) []; \
1651 W_[blocked_queue_tl] = tso;
1657 foreign "C" barf("waitRead# on threaded RTS") never returns;
1660 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1661 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
1662 StgTSO_block_info(CurrentTSO) = R1;
1663 // No locking - we're not going to use this interface in the
1664 // threaded RTS anyway.
1665 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1666 jump stg_block_noregs;
1674 foreign "C" barf("waitWrite# on threaded RTS") never returns;
1677 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1678 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
1679 StgTSO_block_info(CurrentTSO) = R1;
1680 // No locking - we're not going to use this interface in the
1681 // threaded RTS anyway.
1682 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1683 jump stg_block_noregs;
1688 STRING(stg_delayzh_malloc_str, "stg_delayzh")
1691 #ifdef mingw32_HOST_OS
1699 foreign "C" barf("delay# on threaded RTS") never returns;
1702 /* args: R1 (microsecond delay amount) */
1703 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1704 StgTSO_why_blocked(CurrentTSO) = BlockedOnDelay::I16;
1706 #ifdef mingw32_HOST_OS
1708 /* could probably allocate this on the heap instead */
1709 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1710 stg_delayzh_malloc_str);
1711 (reqID) = foreign "C" addDelayRequest(R1);
1712 StgAsyncIOResult_reqID(ares) = reqID;
1713 StgAsyncIOResult_len(ares) = 0;
1714 StgAsyncIOResult_errCode(ares) = 0;
1715 StgTSO_block_info(CurrentTSO) = ares;
1717 /* Having all async-blocked threads reside on the blocked_queue
1718 * simplifies matters, so change the status to OnDoProc put the
1719 * delayed thread on the blocked_queue.
1721 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
1722 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1723 jump stg_block_async_void;
1729 (time) = foreign "C" getourtimeofday() [R1];
1730 divisor = TO_W_(RtsFlags_MiscFlags_tickInterval(RtsFlags));
1734 divisor = divisor * 1000;
1735 target = ((R1 + divisor - 1) / divisor) /* divide rounding up */
1736 + time + 1; /* Add 1 as getourtimeofday rounds down */
1737 StgTSO_block_info(CurrentTSO) = target;
1739 /* Insert the new thread in the sleeping queue. */
1741 t = W_[sleeping_queue];
1743 if (t != END_TSO_QUEUE && StgTSO_block_info(t) < target) {
1745 t = StgTSO__link(t);
1749 StgTSO__link(CurrentTSO) = t;
1751 W_[sleeping_queue] = CurrentTSO;
1753 foreign "C" setTSOLink(MyCapability() "ptr", prev "ptr", CurrentTSO) [];
1755 jump stg_block_noregs;
1757 #endif /* !THREADED_RTS */
1761 #ifdef mingw32_HOST_OS
1762 STRING(stg_asyncReadzh_malloc_str, "stg_asyncReadzh")
1769 foreign "C" barf("asyncRead# on threaded RTS") never returns;
1772 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
1773 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1774 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
1776 /* could probably allocate this on the heap instead */
1777 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1778 stg_asyncReadzh_malloc_str)
1780 (reqID) = foreign "C" addIORequest(R1, 0/*FALSE*/,R2,R3,R4 "ptr") [];
1781 StgAsyncIOResult_reqID(ares) = reqID;
1782 StgAsyncIOResult_len(ares) = 0;
1783 StgAsyncIOResult_errCode(ares) = 0;
1784 StgTSO_block_info(CurrentTSO) = ares;
1785 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1786 jump stg_block_async;
1790 STRING(stg_asyncWritezh_malloc_str, "stg_asyncWritezh")
1797 foreign "C" barf("asyncWrite# on threaded RTS") never returns;
1800 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
1801 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1802 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
1804 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1805 stg_asyncWritezh_malloc_str)
1807 (reqID) = foreign "C" addIORequest(R1, 1/*TRUE*/,R2,R3,R4 "ptr") [];
1809 StgAsyncIOResult_reqID(ares) = reqID;
1810 StgAsyncIOResult_len(ares) = 0;
1811 StgAsyncIOResult_errCode(ares) = 0;
1812 StgTSO_block_info(CurrentTSO) = ares;
1813 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1814 jump stg_block_async;
1818 STRING(stg_asyncDoProczh_malloc_str, "stg_asyncDoProczh")
1825 foreign "C" barf("asyncDoProc# on threaded RTS") never returns;
1828 /* args: R1 = proc, R2 = param */
1829 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1830 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
1832 /* could probably allocate this on the heap instead */
1833 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1834 stg_asyncDoProczh_malloc_str)
1836 (reqID) = foreign "C" addDoProcRequest(R1 "ptr",R2 "ptr") [];
1837 StgAsyncIOResult_reqID(ares) = reqID;
1838 StgAsyncIOResult_len(ares) = 0;
1839 StgAsyncIOResult_errCode(ares) = 0;
1840 StgTSO_block_info(CurrentTSO) = ares;
1841 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1842 jump stg_block_async;
1847 // noDuplicate# tries to ensure that none of the thunks under
1848 // evaluation by the current thread are also under evaluation by
1849 // another thread. It relies on *both* threads doing noDuplicate#;
1850 // the second one will get blocked if they are duplicating some work.
1853 SAVE_THREAD_STATE();
1854 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
1855 foreign "C" threadPaused (MyCapability() "ptr", CurrentTSO "ptr") [];
1857 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
1858 jump stg_threadFinished;
1860 LOAD_THREAD_STATE();
1861 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
1862 jump %ENTRY_CODE(Sp(0));
1868 W_ ap_stack, offset, val, ok;
1870 /* args: R1 = AP_STACK, R2 = offset */
1874 if (%INFO_PTR(ap_stack) == stg_AP_STACK_info) {
1876 val = StgAP_STACK_payload(ap_stack,offset);
1884 /* -----------------------------------------------------------------------------
1886 -------------------------------------------------------------------------- */
1888 // Write the cost center stack of the first argument on stderr; return
1889 // the second. Possibly only makes sense for already evaluated
1896 ccs = StgHeader_ccs(UNTAG(R1));
1897 foreign "C" fprintCCS_stderr(ccs "ptr") [R2];
1908 #ifndef THREADED_RTS
1909 RET_NP(0,ghczmprim_GHCziBool_False_closure);
1911 (spark) = foreign "C" findSpark(MyCapability());
1915 RET_NP(0,ghczmprim_GHCziBool_False_closure);
1925 #if defined(TRACING) || defined(DEBUG)
1927 foreign "C" traceUserMsg(MyCapability() "ptr", msg "ptr") [];
1929 #elif defined(DTRACE)
1933 // We should go through the macro HASKELLEVENT_USER_MSG_ENABLED from
1934 // RtsProbes.h, but that header file includes unistd.h, which doesn't
1936 (enabled) = foreign "C" __dtrace_isenabled$HaskellEvent$user__msg$v1() [];
1938 foreign "C" dtraceUserMsgWrapper(MyCapability() "ptr", msg "ptr") [];
1942 jump %ENTRY_CODE(Sp(0));