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" allocateLocal(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(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(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;
136 /* Args: R1 = words, R2 = initialisation value */
139 MAYBE_GC(R2_PTR,stg_newArrayzh);
141 words = BYTES_TO_WDS(SIZEOF_StgMutArrPtrs) + n;
142 ("ptr" arr) = foreign "C" allocateLocal(MyCapability() "ptr",words) [R2];
143 TICK_ALLOC_PRIM(SIZEOF_StgMutArrPtrs, WDS(n), 0);
145 SET_HDR(arr, stg_MUT_ARR_PTRS_DIRTY_info, W_[CCCS]);
146 StgMutArrPtrs_ptrs(arr) = n;
148 // Initialise all elements of the the array with the value in R2
150 p = arr + SIZEOF_StgMutArrPtrs;
152 if (p < arr + WDS(words)) {
161 stg_unsafeThawArrayzh
163 // SUBTLETY TO DO WITH THE OLD GEN MUTABLE LIST
165 // A MUT_ARR_PTRS lives on the mutable list, but a MUT_ARR_PTRS_FROZEN
166 // normally doesn't. However, when we freeze a MUT_ARR_PTRS, we leave
167 // it on the mutable list for the GC to remove (removing something from
168 // the mutable list is not easy, because the mut_list is only singly-linked).
170 // So that we can tell whether a MUT_ARR_PTRS_FROZEN is on the mutable list,
171 // when we freeze it we set the info ptr to be MUT_ARR_PTRS_FROZEN0
172 // to indicate that it is still on the mutable list.
174 // So, when we thaw a MUT_ARR_PTRS_FROZEN, we must cope with two cases:
175 // either it is on a mut_list, or it isn't. We adopt the convention that
176 // the closure type is MUT_ARR_PTRS_FROZEN0 if it is on the mutable list,
177 // and MUT_ARR_PTRS_FROZEN otherwise. In fact it wouldn't matter if
178 // we put it on the mutable list more than once, but it would get scavenged
179 // multiple times during GC, which would be unnecessarily slow.
181 if (StgHeader_info(R1) != stg_MUT_ARR_PTRS_FROZEN0_info) {
182 SET_INFO(R1,stg_MUT_ARR_PTRS_DIRTY_info);
183 recordMutable(R1, R1);
184 // must be done after SET_INFO, because it ASSERTs closure_MUTABLE()
187 SET_INFO(R1,stg_MUT_ARR_PTRS_DIRTY_info);
192 /* -----------------------------------------------------------------------------
194 -------------------------------------------------------------------------- */
199 /* Args: R1 = initialisation value */
201 ALLOC_PRIM( SIZEOF_StgMutVar, R1_PTR, stg_newMutVarzh);
203 mv = Hp - SIZEOF_StgMutVar + WDS(1);
204 SET_HDR(mv,stg_MUT_VAR_DIRTY_info,W_[CCCS]);
205 StgMutVar_var(mv) = R1;
210 stg_atomicModifyMutVarzh
212 W_ mv, f, z, x, y, r, h;
213 /* Args: R1 :: MutVar#, R2 :: a -> (a,b) */
215 /* If x is the current contents of the MutVar#, then
216 We want to make the new contents point to
220 and the return value is
224 obviously we can share (f x).
226 z = [stg_ap_2 f x] (max (HS + 2) MIN_UPD_SIZE)
227 y = [stg_sel_0 z] (max (HS + 1) MIN_UPD_SIZE)
228 r = [stg_sel_1 z] (max (HS + 1) MIN_UPD_SIZE)
232 #define THUNK_1_SIZE (SIZEOF_StgThunkHeader + WDS(MIN_UPD_SIZE))
233 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),WDS(MIN_UPD_SIZE-1))
235 #define THUNK_1_SIZE (SIZEOF_StgThunkHeader + WDS(1))
236 #define TICK_ALLOC_THUNK_1() TICK_ALLOC_UP_THK(WDS(1),0)
240 #define THUNK_2_SIZE (SIZEOF_StgThunkHeader + WDS(MIN_UPD_SIZE))
241 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),WDS(MIN_UPD_SIZE-2))
243 #define THUNK_2_SIZE (SIZEOF_StgThunkHeader + WDS(2))
244 #define TICK_ALLOC_THUNK_2() TICK_ALLOC_UP_THK(WDS(2),0)
247 #define SIZE (THUNK_2_SIZE + THUNK_1_SIZE + THUNK_1_SIZE)
249 HP_CHK_GEN_TICKY(SIZE, R1_PTR & R2_PTR, stg_atomicModifyMutVarzh);
254 TICK_ALLOC_THUNK_2();
255 CCCS_ALLOC(THUNK_2_SIZE);
256 z = Hp - THUNK_2_SIZE + WDS(1);
257 SET_HDR(z, stg_ap_2_upd_info, W_[CCCS]);
258 LDV_RECORD_CREATE(z);
259 StgThunk_payload(z,0) = f;
261 TICK_ALLOC_THUNK_1();
262 CCCS_ALLOC(THUNK_1_SIZE);
263 y = z - THUNK_1_SIZE;
264 SET_HDR(y, stg_sel_0_upd_info, W_[CCCS]);
265 LDV_RECORD_CREATE(y);
266 StgThunk_payload(y,0) = z;
268 TICK_ALLOC_THUNK_1();
269 CCCS_ALLOC(THUNK_1_SIZE);
270 r = y - THUNK_1_SIZE;
271 SET_HDR(r, stg_sel_1_upd_info, W_[CCCS]);
272 LDV_RECORD_CREATE(r);
273 StgThunk_payload(r,0) = z;
276 x = StgMutVar_var(mv);
277 StgThunk_payload(z,1) = x;
279 (h) = foreign "C" cas(mv + SIZEOF_StgHeader + OFFSET_StgMutVar_var, x, y) [];
280 if (h != x) { goto retry; }
282 StgMutVar_var(mv) = y;
285 if (GET_INFO(mv) == stg_MUT_VAR_CLEAN_info) {
286 foreign "C" dirty_MUT_VAR(BaseReg "ptr", mv "ptr") [];
292 /* -----------------------------------------------------------------------------
293 Weak Pointer Primitives
294 -------------------------------------------------------------------------- */
296 STRING(stg_weak_msg,"New weak pointer at %p\n")
302 R3 = finalizer (or NULL)
307 R3 = stg_NO_FINALIZER_closure;
310 ALLOC_PRIM( SIZEOF_StgWeak, R1_PTR & R2_PTR & R3_PTR, stg_mkWeakzh );
312 w = Hp - SIZEOF_StgWeak + WDS(1);
313 SET_HDR(w, stg_WEAK_info, W_[CCCS]);
315 // We don't care about cfinalizer here.
316 // Should StgWeak_cfinalizer(w) be stg_NO_FINALIZER_closure or
320 StgWeak_value(w) = R2;
321 StgWeak_finalizer(w) = R3;
322 StgWeak_cfinalizer(w) = stg_NO_FINALIZER_closure;
324 StgWeak_link(w) = W_[weak_ptr_list];
325 W_[weak_ptr_list] = w;
327 IF_DEBUG(weak, foreign "C" debugBelch(stg_weak_msg,w) []);
332 stg_mkWeakForeignEnvzh
338 R5 = has environment (0 or 1)
341 W_ w, payload_words, words, p;
343 W_ key, val, fptr, ptr, flag, eptr;
352 ALLOC_PRIM( SIZEOF_StgWeak, R1_PTR & R2_PTR, stg_mkWeakForeignEnvzh );
354 w = Hp - SIZEOF_StgWeak + WDS(1);
355 SET_HDR(w, stg_WEAK_info, W_[CCCS]);
358 words = BYTES_TO_WDS(SIZEOF_StgArrWords) + payload_words;
359 ("ptr" p) = foreign "C" allocateLocal(MyCapability() "ptr", words) [];
361 TICK_ALLOC_PRIM(SIZEOF_StgArrWords,WDS(payload_words),0);
362 SET_HDR(p, stg_ARR_WORDS_info, W_[CCCS]);
364 StgArrWords_words(p) = payload_words;
365 StgArrWords_payload(p,0) = fptr;
366 StgArrWords_payload(p,1) = ptr;
367 StgArrWords_payload(p,2) = eptr;
368 StgArrWords_payload(p,3) = flag;
370 // We don't care about the value here.
371 // Should StgWeak_value(w) be stg_NO_FINALIZER_closure or something else?
373 StgWeak_key(w) = key;
374 StgWeak_value(w) = val;
375 StgWeak_finalizer(w) = stg_NO_FINALIZER_closure;
376 StgWeak_cfinalizer(w) = p;
378 StgWeak_link(w) = W_[weak_ptr_list];
379 W_[weak_ptr_list] = w;
381 IF_DEBUG(weak, foreign "C" debugBelch(stg_weak_msg,w) []);
395 if (GET_INFO(w) == stg_DEAD_WEAK_info) {
396 RET_NP(0,stg_NO_FINALIZER_closure);
402 // A weak pointer is inherently used, so we do not need to call
403 // LDV_recordDead_FILL_SLOP_DYNAMIC():
404 // LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)w);
405 // or, LDV_recordDead():
406 // LDV_recordDead((StgClosure *)w, sizeofW(StgWeak) - sizeofW(StgProfHeader));
407 // Furthermore, when PROFILING is turned on, dead weak pointers are exactly as
408 // large as weak pointers, so there is no need to fill the slop, either.
409 // See stg_DEAD_WEAK_info in StgMiscClosures.hc.
413 // Todo: maybe use SET_HDR() and remove LDV_recordCreate()?
415 SET_INFO(w,stg_DEAD_WEAK_info);
416 LDV_RECORD_CREATE(w);
418 f = StgWeak_finalizer(w);
419 arr = StgWeak_cfinalizer(w);
421 StgDeadWeak_link(w) = StgWeak_link(w);
423 if (arr != stg_NO_FINALIZER_closure) {
424 foreign "C" runCFinalizer(StgArrWords_payload(arr,0),
425 StgArrWords_payload(arr,1),
426 StgArrWords_payload(arr,2),
427 StgArrWords_payload(arr,3)) [];
430 /* return the finalizer */
431 if (f == stg_NO_FINALIZER_closure) {
432 RET_NP(0,stg_NO_FINALIZER_closure);
444 if (GET_INFO(w) == stg_WEAK_info) {
446 val = StgWeak_value(w);
454 /* -----------------------------------------------------------------------------
455 Floating point operations.
456 -------------------------------------------------------------------------- */
458 stg_decodeFloatzuIntzh
465 STK_CHK_GEN( WDS(2), NO_PTRS, stg_decodeFloatzuIntzh );
467 mp_tmp1 = Sp - WDS(1);
468 mp_tmp_w = Sp - WDS(2);
470 /* arguments: F1 = Float# */
473 /* Perform the operation */
474 foreign "C" __decodeFloat_Int(mp_tmp1 "ptr", mp_tmp_w "ptr", arg) [];
476 /* returns: (Int# (mantissa), Int# (exponent)) */
477 RET_NN(W_[mp_tmp1], W_[mp_tmp_w]);
480 stg_decodeDoublezu2Intzh
489 STK_CHK_GEN( WDS(4), NO_PTRS, stg_decodeDoublezu2Intzh );
491 mp_tmp1 = Sp - WDS(1);
492 mp_tmp2 = Sp - WDS(2);
493 mp_result1 = Sp - WDS(3);
494 mp_result2 = Sp - WDS(4);
496 /* arguments: D1 = Double# */
499 /* Perform the operation */
500 foreign "C" __decodeDouble_2Int(mp_tmp1 "ptr", mp_tmp2 "ptr",
501 mp_result1 "ptr", mp_result2 "ptr",
505 (Int# (mant sign), Word# (mant high), Word# (mant low), Int# (expn)) */
506 RET_NNNN(W_[mp_tmp1], W_[mp_tmp2], W_[mp_result1], W_[mp_result2]);
509 /* -----------------------------------------------------------------------------
510 * Concurrency primitives
511 * -------------------------------------------------------------------------- */
515 /* args: R1 = closure to spark */
517 MAYBE_GC(R1_PTR, stg_forkzh);
523 ("ptr" threadid) = foreign "C" createIOThread( MyCapability() "ptr",
524 RtsFlags_GcFlags_initialStkSize(RtsFlags),
527 /* start blocked if the current thread is blocked */
528 StgTSO_flags(threadid) =
529 StgTSO_flags(threadid) | (StgTSO_flags(CurrentTSO) &
530 (TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32));
532 foreign "C" scheduleThread(MyCapability() "ptr", threadid "ptr") [];
534 // context switch soon, but not immediately: we don't want every
535 // forkIO to force a context-switch.
536 Capability_context_switch(MyCapability()) = 1 :: CInt;
543 /* args: R1 = cpu, R2 = closure to spark */
545 MAYBE_GC(R2_PTR, stg_forkOnzh);
553 ("ptr" threadid) = foreign "C" createIOThread( MyCapability() "ptr",
554 RtsFlags_GcFlags_initialStkSize(RtsFlags),
557 /* start blocked if the current thread is blocked */
558 StgTSO_flags(threadid) =
559 StgTSO_flags(threadid) | (StgTSO_flags(CurrentTSO) &
560 (TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32));
562 foreign "C" scheduleThreadOn(MyCapability() "ptr", cpu, threadid "ptr") [];
564 // context switch soon, but not immediately: we don't want every
565 // forkIO to force a context-switch.
566 Capability_context_switch(MyCapability()) = 1 :: CInt;
573 jump stg_yield_noregs;
588 foreign "C" labelThread(R1 "ptr", R2 "ptr") [];
590 jump %ENTRY_CODE(Sp(0));
593 stg_isCurrentThreadBoundzh
597 (r) = foreign "C" isThreadBound(CurrentTSO) [];
603 /* args: R1 :: ThreadId# */
611 if (TO_W_(StgTSO_what_next(tso)) == ThreadRelocated) {
612 tso = StgTSO__link(tso);
616 what_next = TO_W_(StgTSO_what_next(tso));
617 why_blocked = TO_W_(StgTSO_why_blocked(tso));
618 // Note: these two reads are not atomic, so they might end up
619 // being inconsistent. It doesn't matter, since we
620 // only return one or the other. If we wanted to return the
621 // contents of block_info too, then we'd have to do some synchronisation.
623 if (what_next == ThreadComplete) {
624 ret = 16; // NB. magic, matches up with GHC.Conc.threadStatus
626 if (what_next == ThreadKilled) {
635 /* -----------------------------------------------------------------------------
637 * -------------------------------------------------------------------------- */
641 // Catch retry frame ------------------------------------------------------------
643 INFO_TABLE_RET(stg_catch_retry_frame, CATCH_RETRY_FRAME,
644 #if defined(PROFILING)
645 W_ unused1, W_ unused2,
647 W_ unused3, P_ unused4, P_ unused5)
649 W_ r, frame, trec, outer;
652 trec = StgTSO_trec(CurrentTSO);
653 outer = StgTRecHeader_enclosing_trec(trec);
654 (r) = foreign "C" stmCommitNestedTransaction(MyCapability() "ptr", trec "ptr") [];
656 /* Succeeded (either first branch or second branch) */
657 StgTSO_trec(CurrentTSO) = outer;
658 Sp = Sp + SIZEOF_StgCatchRetryFrame;
659 jump %ENTRY_CODE(Sp(SP_OFF));
661 /* Did not commit: re-execute */
663 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
664 StgTSO_trec(CurrentTSO) = new_trec;
665 if (StgCatchRetryFrame_running_alt_code(frame) != 0::I32) {
666 R1 = StgCatchRetryFrame_alt_code(frame);
668 R1 = StgCatchRetryFrame_first_code(frame);
675 // Atomically frame ------------------------------------------------------------
677 INFO_TABLE_RET(stg_atomically_frame, ATOMICALLY_FRAME,
678 #if defined(PROFILING)
679 W_ unused1, W_ unused2,
681 P_ code, P_ next_invariant_to_check, P_ result)
683 W_ frame, trec, valid, next_invariant, q, outer;
686 trec = StgTSO_trec(CurrentTSO);
688 outer = StgTRecHeader_enclosing_trec(trec);
690 if (outer == NO_TREC) {
691 /* First time back at the atomically frame -- pick up invariants */
692 ("ptr" q) = foreign "C" stmGetInvariantsToCheck(MyCapability() "ptr", trec "ptr") [];
693 StgAtomicallyFrame_next_invariant_to_check(frame) = q;
694 StgAtomicallyFrame_result(frame) = result;
697 /* Second/subsequent time back at the atomically frame -- abort the
698 * tx that's checking the invariant and move on to the next one */
699 StgTSO_trec(CurrentTSO) = outer;
700 q = StgAtomicallyFrame_next_invariant_to_check(frame);
701 StgInvariantCheckQueue_my_execution(q) = trec;
702 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
703 /* Don't free trec -- it's linked from q and will be stashed in the
704 * invariant if we eventually commit. */
705 q = StgInvariantCheckQueue_next_queue_entry(q);
706 StgAtomicallyFrame_next_invariant_to_check(frame) = q;
710 q = StgAtomicallyFrame_next_invariant_to_check(frame);
712 if (q != END_INVARIANT_CHECK_QUEUE) {
713 /* We can't commit yet: another invariant to check */
714 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", trec "ptr") [];
715 StgTSO_trec(CurrentTSO) = trec;
717 next_invariant = StgInvariantCheckQueue_invariant(q);
718 R1 = StgAtomicInvariant_code(next_invariant);
723 /* We've got no more invariants to check, try to commit */
724 (valid) = foreign "C" stmCommitTransaction(MyCapability() "ptr", trec "ptr") [];
726 /* Transaction was valid: commit succeeded */
727 StgTSO_trec(CurrentTSO) = NO_TREC;
728 R1 = StgAtomicallyFrame_result(frame);
729 Sp = Sp + SIZEOF_StgAtomicallyFrame;
730 jump %ENTRY_CODE(Sp(SP_OFF));
732 /* Transaction was not valid: try again */
733 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", NO_TREC "ptr") [];
734 StgTSO_trec(CurrentTSO) = trec;
735 StgAtomicallyFrame_next_invariant_to_check(frame) = END_INVARIANT_CHECK_QUEUE;
736 R1 = StgAtomicallyFrame_code(frame);
742 INFO_TABLE_RET(stg_atomically_waiting_frame, ATOMICALLY_FRAME,
743 #if defined(PROFILING)
744 W_ unused1, W_ unused2,
746 P_ code, P_ next_invariant_to_check, P_ result)
748 W_ frame, trec, valid;
752 /* The TSO is currently waiting: should we stop waiting? */
753 (valid) = foreign "C" stmReWait(MyCapability() "ptr", CurrentTSO "ptr") [];
755 /* Previous attempt is still valid: no point trying again yet */
756 jump stg_block_noregs;
758 /* Previous attempt is no longer valid: try again */
759 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", NO_TREC "ptr") [];
760 StgTSO_trec(CurrentTSO) = trec;
761 StgHeader_info(frame) = stg_atomically_frame_info;
762 R1 = StgAtomicallyFrame_code(frame);
767 // STM catch frame --------------------------------------------------------------
771 /* Catch frames are very similar to update frames, but when entering
772 * one we just pop the frame off the stack and perform the correct
773 * kind of return to the activation record underneath us on the stack.
776 INFO_TABLE_RET(stg_catch_stm_frame, CATCH_STM_FRAME,
777 #if defined(PROFILING)
778 W_ unused1, W_ unused2,
780 P_ unused3, P_ unused4)
782 W_ r, frame, trec, outer;
784 trec = StgTSO_trec(CurrentTSO);
785 outer = StgTRecHeader_enclosing_trec(trec);
786 (r) = foreign "C" stmCommitNestedTransaction(MyCapability() "ptr", trec "ptr") [];
788 /* Commit succeeded */
789 StgTSO_trec(CurrentTSO) = outer;
790 Sp = Sp + SIZEOF_StgCatchSTMFrame;
795 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
796 StgTSO_trec(CurrentTSO) = new_trec;
797 R1 = StgCatchSTMFrame_code(frame);
803 // Primop definition ------------------------------------------------------------
811 // stmStartTransaction may allocate
812 MAYBE_GC (R1_PTR, stg_atomicallyzh);
814 /* Args: R1 = m :: STM a */
815 STK_CHK_GEN(SIZEOF_StgAtomicallyFrame + WDS(1), R1_PTR, stg_atomicallyzh);
817 old_trec = StgTSO_trec(CurrentTSO);
819 /* Nested transactions are not allowed; raise an exception */
820 if (old_trec != NO_TREC) {
821 R1 = base_ControlziExceptionziBase_nestedAtomically_closure;
825 /* Set up the atomically frame */
826 Sp = Sp - SIZEOF_StgAtomicallyFrame;
829 SET_HDR(frame,stg_atomically_frame_info, W_[CCCS]);
830 StgAtomicallyFrame_code(frame) = R1;
831 StgAtomicallyFrame_result(frame) = NO_TREC;
832 StgAtomicallyFrame_next_invariant_to_check(frame) = END_INVARIANT_CHECK_QUEUE;
834 /* Start the memory transcation */
835 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", old_trec "ptr") [R1];
836 StgTSO_trec(CurrentTSO) = new_trec;
838 /* Apply R1 to the realworld token */
847 /* Args: R1 :: STM a */
848 /* Args: R2 :: Exception -> STM a */
849 STK_CHK_GEN(SIZEOF_StgCatchSTMFrame + WDS(1), R1_PTR & R2_PTR, stg_catchSTMzh);
851 /* Set up the catch frame */
852 Sp = Sp - SIZEOF_StgCatchSTMFrame;
855 SET_HDR(frame, stg_catch_stm_frame_info, W_[CCCS]);
856 StgCatchSTMFrame_handler(frame) = R2;
857 StgCatchSTMFrame_code(frame) = R1;
859 /* Start a nested transaction to run the body of the try block in */
862 cur_trec = StgTSO_trec(CurrentTSO);
863 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", cur_trec "ptr");
864 StgTSO_trec(CurrentTSO) = new_trec;
866 /* Apply R1 to the realworld token */
877 // stmStartTransaction may allocate
878 MAYBE_GC (R1_PTR & R2_PTR, stg_catchRetryzh);
880 /* Args: R1 :: STM a */
881 /* Args: R2 :: STM a */
882 STK_CHK_GEN(SIZEOF_StgCatchRetryFrame + WDS(1), R1_PTR & R2_PTR, stg_catchRetryzh);
884 /* Start a nested transaction within which to run the first code */
885 trec = StgTSO_trec(CurrentTSO);
886 ("ptr" new_trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", trec "ptr") [R1,R2];
887 StgTSO_trec(CurrentTSO) = new_trec;
889 /* Set up the catch-retry frame */
890 Sp = Sp - SIZEOF_StgCatchRetryFrame;
893 SET_HDR(frame, stg_catch_retry_frame_info, W_[CCCS]);
894 StgCatchRetryFrame_running_alt_code(frame) = 0 :: CInt; // false;
895 StgCatchRetryFrame_first_code(frame) = R1;
896 StgCatchRetryFrame_alt_code(frame) = R2;
898 /* Apply R1 to the realworld token */
911 MAYBE_GC (NO_PTRS, stg_retryzh); // STM operations may allocate
913 // Find the enclosing ATOMICALLY_FRAME or CATCH_RETRY_FRAME
915 StgTSO_sp(CurrentTSO) = Sp;
916 (frame_type) = foreign "C" findRetryFrameHelper(CurrentTSO "ptr") [];
917 Sp = StgTSO_sp(CurrentTSO);
919 trec = StgTSO_trec(CurrentTSO);
920 outer = StgTRecHeader_enclosing_trec(trec);
922 if (frame_type == CATCH_RETRY_FRAME) {
923 // The retry reaches a CATCH_RETRY_FRAME before the atomic frame
924 ASSERT(outer != NO_TREC);
925 // Abort the transaction attempting the current branch
926 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
927 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
928 if (!StgCatchRetryFrame_running_alt_code(frame) != 0::I32) {
929 // Retry in the first branch: try the alternative
930 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
931 StgTSO_trec(CurrentTSO) = trec;
932 StgCatchRetryFrame_running_alt_code(frame) = 1 :: CInt; // true;
933 R1 = StgCatchRetryFrame_alt_code(frame);
936 // Retry in the alternative code: propagate the retry
937 StgTSO_trec(CurrentTSO) = outer;
938 Sp = Sp + SIZEOF_StgCatchRetryFrame;
939 goto retry_pop_stack;
943 // We've reached the ATOMICALLY_FRAME: attempt to wait
944 ASSERT(frame_type == ATOMICALLY_FRAME);
945 if (outer != NO_TREC) {
946 // We called retry while checking invariants, so abort the current
947 // invariant check (merging its TVar accesses into the parents read
948 // set so we'll wait on them)
949 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
950 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
952 StgTSO_trec(CurrentTSO) = trec;
953 outer = StgTRecHeader_enclosing_trec(trec);
955 ASSERT(outer == NO_TREC);
957 (r) = foreign "C" stmWait(MyCapability() "ptr", CurrentTSO "ptr", trec "ptr") [];
959 // Transaction was valid: stmWait put us on the TVars' queues, we now block
960 StgHeader_info(frame) = stg_atomically_waiting_frame_info;
962 // Fix up the stack in the unregisterised case: the return convention is different.
963 R3 = trec; // passing to stmWaitUnblock()
964 jump stg_block_stmwait;
966 // Transaction was not valid: retry immediately
967 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
968 StgTSO_trec(CurrentTSO) = trec;
969 R1 = StgAtomicallyFrame_code(frame);
980 /* Args: R1 = invariant closure */
981 MAYBE_GC (R1_PTR, stg_checkzh);
983 trec = StgTSO_trec(CurrentTSO);
985 foreign "C" stmAddInvariantToCheck(MyCapability() "ptr",
989 jump %ENTRY_CODE(Sp(0));
998 /* Args: R1 = initialisation value */
1000 MAYBE_GC (R1_PTR, stg_newTVarzh);
1002 ("ptr" tv) = foreign "C" stmNewTVar(MyCapability() "ptr", new_value "ptr") [];
1013 /* Args: R1 = TVar closure */
1015 MAYBE_GC (R1_PTR, stg_readTVarzh); // Call to stmReadTVar may allocate
1016 trec = StgTSO_trec(CurrentTSO);
1018 ("ptr" result) = foreign "C" stmReadTVar(MyCapability() "ptr", trec "ptr", tvar "ptr") [];
1028 result = StgTVar_current_value(R1);
1029 if (%INFO_PTR(result) == stg_TREC_HEADER_info) {
1041 /* Args: R1 = TVar closure */
1042 /* R2 = New value */
1044 MAYBE_GC (R1_PTR & R2_PTR, stg_writeTVarzh); // Call to stmWriteTVar may allocate
1045 trec = StgTSO_trec(CurrentTSO);
1048 foreign "C" stmWriteTVar(MyCapability() "ptr", trec "ptr", tvar "ptr", new_value "ptr") [];
1050 jump %ENTRY_CODE(Sp(0));
1054 /* -----------------------------------------------------------------------------
1057 * take & putMVar work as follows. Firstly, an important invariant:
1059 * If the MVar is full, then the blocking queue contains only
1060 * threads blocked on putMVar, and if the MVar is empty then the
1061 * blocking queue contains only threads blocked on takeMVar.
1064 * MVar empty : then add ourselves to the blocking queue
1065 * MVar full : remove the value from the MVar, and
1066 * blocking queue empty : return
1067 * blocking queue non-empty : perform the first blocked putMVar
1068 * from the queue, and wake up the
1069 * thread (MVar is now full again)
1071 * putMVar is just the dual of the above algorithm.
1073 * How do we "perform a putMVar"? Well, we have to fiddle around with
1074 * the stack of the thread waiting to do the putMVar. See
1075 * stg_block_putmvar and stg_block_takemvar in HeapStackCheck.c for
1076 * the stack layout, and the PerformPut and PerformTake macros below.
1078 * It is important that a blocked take or put is woken up with the
1079 * take/put already performed, because otherwise there would be a
1080 * small window of vulnerability where the thread could receive an
1081 * exception and never perform its take or put, and we'd end up with a
1084 * -------------------------------------------------------------------------- */
1088 /* args: R1 = MVar closure */
1090 if (StgMVar_value(R1) == stg_END_TSO_QUEUE_closure) {
1102 ALLOC_PRIM ( SIZEOF_StgMVar, NO_PTRS, stg_newMVarzh );
1104 mvar = Hp - SIZEOF_StgMVar + WDS(1);
1105 SET_HDR(mvar,stg_MVAR_DIRTY_info,W_[CCCS]);
1106 // MVARs start dirty: generation 0 has no mutable list
1107 StgMVar_head(mvar) = stg_END_TSO_QUEUE_closure;
1108 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1109 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1114 #define PerformTake(tso, value) \
1115 W_[StgTSO_sp(tso) + WDS(1)] = value; \
1116 W_[StgTSO_sp(tso) + WDS(0)] = stg_gc_unpt_r1_info;
1118 #define PerformPut(tso,lval) \
1119 StgTSO_sp(tso) = StgTSO_sp(tso) + WDS(3); \
1120 lval = W_[StgTSO_sp(tso) - WDS(1)];
1124 W_ mvar, val, info, tso;
1126 /* args: R1 = MVar closure */
1129 #if defined(THREADED_RTS)
1130 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [];
1132 info = GET_INFO(mvar);
1135 if (info == stg_MVAR_CLEAN_info) {
1136 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr") [];
1139 /* If the MVar is empty, put ourselves on its blocking queue,
1140 * and wait until we're woken up.
1142 if (StgMVar_value(mvar) == stg_END_TSO_QUEUE_closure) {
1143 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1144 StgMVar_head(mvar) = CurrentTSO;
1146 foreign "C" setTSOLink(MyCapability() "ptr",
1147 StgMVar_tail(mvar) "ptr",
1150 StgTSO__link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1151 StgTSO_block_info(CurrentTSO) = mvar;
1152 // write barrier for throwTo(), which looks at block_info
1153 // if why_blocked==BlockedOnMVar.
1154 prim %write_barrier() [];
1155 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1156 StgMVar_tail(mvar) = CurrentTSO;
1159 jump stg_block_takemvar;
1162 /* we got the value... */
1163 val = StgMVar_value(mvar);
1165 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure)
1167 /* There are putMVar(s) waiting...
1168 * wake up the first thread on the queue
1170 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1172 /* actually perform the putMVar for the thread that we just woke up */
1173 tso = StgMVar_head(mvar);
1174 PerformPut(tso,StgMVar_value(mvar));
1176 if (TO_W_(StgTSO_dirty(tso)) == 0) {
1177 foreign "C" dirty_TSO(MyCapability() "ptr", tso "ptr") [];
1180 ("ptr" tso) = foreign "C" unblockOne_(MyCapability() "ptr",
1181 StgMVar_head(mvar) "ptr", 1) [];
1182 StgMVar_head(mvar) = tso;
1184 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1185 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1188 #if defined(THREADED_RTS)
1189 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1191 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1197 /* No further putMVars, MVar is now empty */
1198 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1200 #if defined(THREADED_RTS)
1201 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1203 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1213 W_ mvar, val, info, tso;
1215 /* args: R1 = MVar closure */
1219 #if defined(THREADED_RTS)
1220 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [];
1222 info = GET_INFO(mvar);
1225 if (StgMVar_value(mvar) == stg_END_TSO_QUEUE_closure) {
1226 #if defined(THREADED_RTS)
1227 unlockClosure(mvar, info);
1229 /* HACK: we need a pointer to pass back,
1230 * so we abuse NO_FINALIZER_closure
1232 RET_NP(0, stg_NO_FINALIZER_closure);
1235 if (info == stg_MVAR_CLEAN_info) {
1236 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1239 /* we got the value... */
1240 val = StgMVar_value(mvar);
1242 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1244 /* There are putMVar(s) waiting...
1245 * wake up the first thread on the queue
1247 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1249 /* actually perform the putMVar for the thread that we just woke up */
1250 tso = StgMVar_head(mvar);
1251 PerformPut(tso,StgMVar_value(mvar));
1252 if (TO_W_(StgTSO_dirty(tso)) == 0) {
1253 foreign "C" dirty_TSO(MyCapability() "ptr", tso "ptr") [];
1256 ("ptr" tso) = foreign "C" unblockOne_(MyCapability() "ptr",
1257 StgMVar_head(mvar) "ptr", 1) [];
1258 StgMVar_head(mvar) = tso;
1260 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1261 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1263 #if defined(THREADED_RTS)
1264 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1266 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1271 /* No further putMVars, MVar is now empty */
1272 StgMVar_value(mvar) = stg_END_TSO_QUEUE_closure;
1273 #if defined(THREADED_RTS)
1274 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1276 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1286 W_ mvar, val, info, tso;
1288 /* args: R1 = MVar, R2 = value */
1292 #if defined(THREADED_RTS)
1293 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [];
1295 info = GET_INFO(mvar);
1298 if (info == stg_MVAR_CLEAN_info) {
1299 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1302 if (StgMVar_value(mvar) != stg_END_TSO_QUEUE_closure) {
1303 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1304 StgMVar_head(mvar) = CurrentTSO;
1306 foreign "C" setTSOLink(MyCapability() "ptr",
1307 StgMVar_tail(mvar) "ptr",
1310 StgTSO__link(CurrentTSO) = stg_END_TSO_QUEUE_closure;
1311 StgTSO_block_info(CurrentTSO) = mvar;
1312 // write barrier for throwTo(), which looks at block_info
1313 // if why_blocked==BlockedOnMVar.
1314 prim %write_barrier() [];
1315 StgTSO_why_blocked(CurrentTSO) = BlockedOnMVar::I16;
1316 StgMVar_tail(mvar) = CurrentTSO;
1320 jump stg_block_putmvar;
1323 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1325 /* There are takeMVar(s) waiting: wake up the first one
1327 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1329 /* actually perform the takeMVar */
1330 tso = StgMVar_head(mvar);
1331 PerformTake(tso, val);
1332 if (TO_W_(StgTSO_dirty(tso)) == 0) {
1333 foreign "C" dirty_TSO(MyCapability() "ptr", tso "ptr") [];
1336 ("ptr" tso) = foreign "C" unblockOne_(MyCapability() "ptr",
1337 StgMVar_head(mvar) "ptr", 1) [];
1338 StgMVar_head(mvar) = tso;
1340 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1341 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1344 #if defined(THREADED_RTS)
1345 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1347 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1349 jump %ENTRY_CODE(Sp(0));
1353 /* No further takes, the MVar is now full. */
1354 StgMVar_value(mvar) = val;
1356 #if defined(THREADED_RTS)
1357 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1359 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1361 jump %ENTRY_CODE(Sp(0));
1364 /* ToDo: yield afterward for better communication performance? */
1372 /* args: R1 = MVar, R2 = value */
1375 #if defined(THREADED_RTS)
1376 ("ptr" info) = foreign "C" lockClosure(mvar "ptr") [R2];
1378 info = GET_INFO(mvar);
1381 if (StgMVar_value(mvar) != stg_END_TSO_QUEUE_closure) {
1382 #if defined(THREADED_RTS)
1383 unlockClosure(mvar, info);
1388 if (info == stg_MVAR_CLEAN_info) {
1389 foreign "C" dirty_MVAR(BaseReg "ptr", mvar "ptr");
1392 if (StgMVar_head(mvar) != stg_END_TSO_QUEUE_closure) {
1394 /* There are takeMVar(s) waiting: wake up the first one
1396 ASSERT(StgTSO_why_blocked(StgMVar_head(mvar)) == BlockedOnMVar::I16);
1398 /* actually perform the takeMVar */
1399 tso = StgMVar_head(mvar);
1400 PerformTake(tso, R2);
1401 if (TO_W_(StgTSO_dirty(tso)) == 0) {
1402 foreign "C" dirty_TSO(MyCapability() "ptr", tso "ptr") [];
1405 ("ptr" tso) = foreign "C" unblockOne_(MyCapability() "ptr",
1406 StgMVar_head(mvar) "ptr", 1) [];
1407 StgMVar_head(mvar) = tso;
1409 if (StgMVar_head(mvar) == stg_END_TSO_QUEUE_closure) {
1410 StgMVar_tail(mvar) = stg_END_TSO_QUEUE_closure;
1413 #if defined(THREADED_RTS)
1414 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1416 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1421 /* No further takes, the MVar is now full. */
1422 StgMVar_value(mvar) = R2;
1424 #if defined(THREADED_RTS)
1425 unlockClosure(mvar, stg_MVAR_DIRTY_info);
1427 SET_INFO(mvar,stg_MVAR_DIRTY_info);
1432 /* ToDo: yield afterward for better communication performance? */
1436 /* -----------------------------------------------------------------------------
1437 Stable pointer primitives
1438 ------------------------------------------------------------------------- */
1440 stg_makeStableNamezh
1444 ALLOC_PRIM( SIZEOF_StgStableName, R1_PTR, stg_makeStableNamezh );
1446 (index) = foreign "C" lookupStableName(R1 "ptr") [];
1448 /* Is there already a StableName for this heap object?
1449 * stable_ptr_table is a pointer to an array of snEntry structs.
1451 if ( snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry) == NULL ) {
1452 sn_obj = Hp - SIZEOF_StgStableName + WDS(1);
1453 SET_HDR(sn_obj, stg_STABLE_NAME_info, W_[CCCS]);
1454 StgStableName_sn(sn_obj) = index;
1455 snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry) = sn_obj;
1457 sn_obj = snEntry_sn_obj(W_[stable_ptr_table] + index*SIZEOF_snEntry);
1468 MAYBE_GC(R1_PTR, stg_makeStablePtrzh);
1469 ("ptr" sp) = foreign "C" getStablePtr(R1 "ptr") [];
1473 stg_deRefStablePtrzh
1475 /* Args: R1 = the stable ptr */
1478 r = snEntry_addr(W_[stable_ptr_table] + sp*SIZEOF_snEntry);
1482 /* -----------------------------------------------------------------------------
1483 Bytecode object primitives
1484 ------------------------------------------------------------------------- */
1494 W_ bco, bitmap_arr, bytes, words;
1498 words = BYTES_TO_WDS(SIZEOF_StgBCO) + StgArrWords_words(bitmap_arr);
1501 ALLOC_PRIM( bytes, R1_PTR&R2_PTR&R3_PTR&R5_PTR, stg_newBCOzh );
1503 bco = Hp - bytes + WDS(1);
1504 SET_HDR(bco, stg_BCO_info, W_[CCCS]);
1506 StgBCO_instrs(bco) = R1;
1507 StgBCO_literals(bco) = R2;
1508 StgBCO_ptrs(bco) = R3;
1509 StgBCO_arity(bco) = HALF_W_(R4);
1510 StgBCO_size(bco) = HALF_W_(words);
1512 // Copy the arity/bitmap info into the BCO
1516 if (i < StgArrWords_words(bitmap_arr)) {
1517 StgBCO_bitmap(bco,i) = StgArrWords_payload(bitmap_arr,i);
1528 // R1 = the BCO# for the AP
1532 // This function is *only* used to wrap zero-arity BCOs in an
1533 // updatable wrapper (see ByteCodeLink.lhs). An AP thunk is always
1534 // saturated and always points directly to a FUN or BCO.
1535 ASSERT(%INFO_TYPE(%GET_STD_INFO(R1)) == HALF_W_(BCO) &&
1536 StgBCO_arity(R1) == HALF_W_(0));
1538 HP_CHK_GEN_TICKY(SIZEOF_StgAP, R1_PTR, stg_mkApUpd0zh);
1539 TICK_ALLOC_UP_THK(0, 0);
1540 CCCS_ALLOC(SIZEOF_StgAP);
1542 ap = Hp - SIZEOF_StgAP + WDS(1);
1543 SET_HDR(ap, stg_AP_info, W_[CCCS]);
1545 StgAP_n_args(ap) = HALF_W_(0);
1553 /* args: R1 = closure to analyze */
1554 // TODO: Consider the absence of ptrs or nonptrs as a special case ?
1556 W_ info, ptrs, nptrs, p, ptrs_arr, nptrs_arr;
1557 info = %GET_STD_INFO(UNTAG(R1));
1559 // Some closures have non-standard layout, so we omit those here.
1561 type = TO_W_(%INFO_TYPE(info));
1562 switch [0 .. N_CLOSURE_TYPES] type {
1563 case THUNK_SELECTOR : {
1568 case THUNK, THUNK_1_0, THUNK_0_1, THUNK_2_0, THUNK_1_1,
1569 THUNK_0_2, THUNK_STATIC, AP, PAP, AP_STACK, BCO : {
1575 ptrs = TO_W_(%INFO_PTRS(info));
1576 nptrs = TO_W_(%INFO_NPTRS(info));
1581 W_ ptrs_arr_sz, nptrs_arr_sz;
1582 nptrs_arr_sz = SIZEOF_StgArrWords + WDS(nptrs);
1583 ptrs_arr_sz = SIZEOF_StgMutArrPtrs + WDS(ptrs);
1585 ALLOC_PRIM (ptrs_arr_sz + nptrs_arr_sz, R1_PTR, stg_unpackClosurezh);
1590 ptrs_arr = Hp - nptrs_arr_sz - ptrs_arr_sz + WDS(1);
1591 nptrs_arr = Hp - nptrs_arr_sz + WDS(1);
1593 SET_HDR(ptrs_arr, stg_MUT_ARR_PTRS_FROZEN_info, W_[CCCS]);
1594 StgMutArrPtrs_ptrs(ptrs_arr) = ptrs;
1598 W_[ptrs_arr + SIZEOF_StgMutArrPtrs + WDS(p)] = StgClosure_payload(clos,p);
1603 SET_HDR(nptrs_arr, stg_ARR_WORDS_info, W_[CCCS]);
1604 StgArrWords_words(nptrs_arr) = nptrs;
1608 W_[BYTE_ARR_CTS(nptrs_arr) + WDS(p)] = StgClosure_payload(clos, p+ptrs);
1612 RET_NPP(info, ptrs_arr, nptrs_arr);
1615 /* -----------------------------------------------------------------------------
1616 Thread I/O blocking primitives
1617 -------------------------------------------------------------------------- */
1619 /* Add a thread to the end of the blocked queue. (C-- version of the C
1620 * macro in Schedule.h).
1622 #define APPEND_TO_BLOCKED_QUEUE(tso) \
1623 ASSERT(StgTSO__link(tso) == END_TSO_QUEUE); \
1624 if (W_[blocked_queue_hd] == END_TSO_QUEUE) { \
1625 W_[blocked_queue_hd] = tso; \
1627 foreign "C" setTSOLink(MyCapability() "ptr", W_[blocked_queue_tl] "ptr", tso) []; \
1629 W_[blocked_queue_tl] = tso;
1635 foreign "C" barf("waitRead# on threaded RTS") never returns;
1638 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1639 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
1640 StgTSO_block_info(CurrentTSO) = R1;
1641 // No locking - we're not going to use this interface in the
1642 // threaded RTS anyway.
1643 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1644 jump stg_block_noregs;
1652 foreign "C" barf("waitWrite# on threaded RTS") never returns;
1655 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1656 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
1657 StgTSO_block_info(CurrentTSO) = R1;
1658 // No locking - we're not going to use this interface in the
1659 // threaded RTS anyway.
1660 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1661 jump stg_block_noregs;
1666 STRING(stg_delayzh_malloc_str, "stg_delayzh")
1669 #ifdef mingw32_HOST_OS
1677 foreign "C" barf("delay# on threaded RTS") never returns;
1680 /* args: R1 (microsecond delay amount) */
1681 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1682 StgTSO_why_blocked(CurrentTSO) = BlockedOnDelay::I16;
1684 #ifdef mingw32_HOST_OS
1686 /* could probably allocate this on the heap instead */
1687 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1688 stg_delayzh_malloc_str);
1689 (reqID) = foreign "C" addDelayRequest(R1);
1690 StgAsyncIOResult_reqID(ares) = reqID;
1691 StgAsyncIOResult_len(ares) = 0;
1692 StgAsyncIOResult_errCode(ares) = 0;
1693 StgTSO_block_info(CurrentTSO) = ares;
1695 /* Having all async-blocked threads reside on the blocked_queue
1696 * simplifies matters, so change the status to OnDoProc put the
1697 * delayed thread on the blocked_queue.
1699 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
1700 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1701 jump stg_block_async_void;
1707 (time) = foreign "C" getourtimeofday() [R1];
1708 divisor = TO_W_(RtsFlags_MiscFlags_tickInterval(RtsFlags));
1712 divisor = divisor * 1000;
1713 target = ((R1 + divisor - 1) / divisor) /* divide rounding up */
1714 + time + 1; /* Add 1 as getourtimeofday rounds down */
1715 StgTSO_block_info(CurrentTSO) = target;
1717 /* Insert the new thread in the sleeping queue. */
1719 t = W_[sleeping_queue];
1721 if (t != END_TSO_QUEUE && StgTSO_block_info(t) < target) {
1723 t = StgTSO__link(t);
1727 StgTSO__link(CurrentTSO) = t;
1729 W_[sleeping_queue] = CurrentTSO;
1731 foreign "C" setTSOLink(MyCapability() "ptr", prev "ptr", CurrentTSO) [];
1733 jump stg_block_noregs;
1735 #endif /* !THREADED_RTS */
1739 #ifdef mingw32_HOST_OS
1740 STRING(stg_asyncReadzh_malloc_str, "stg_asyncReadzh")
1747 foreign "C" barf("asyncRead# on threaded RTS") never returns;
1750 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
1751 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1752 StgTSO_why_blocked(CurrentTSO) = BlockedOnRead::I16;
1754 /* could probably allocate this on the heap instead */
1755 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1756 stg_asyncReadzh_malloc_str)
1758 (reqID) = foreign "C" addIORequest(R1, 0/*FALSE*/,R2,R3,R4 "ptr") [];
1759 StgAsyncIOResult_reqID(ares) = reqID;
1760 StgAsyncIOResult_len(ares) = 0;
1761 StgAsyncIOResult_errCode(ares) = 0;
1762 StgTSO_block_info(CurrentTSO) = ares;
1763 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1764 jump stg_block_async;
1768 STRING(stg_asyncWritezh_malloc_str, "stg_asyncWritezh")
1775 foreign "C" barf("asyncWrite# on threaded RTS") never returns;
1778 /* args: R1 = fd, R2 = isSock, R3 = len, R4 = buf */
1779 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1780 StgTSO_why_blocked(CurrentTSO) = BlockedOnWrite::I16;
1782 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1783 stg_asyncWritezh_malloc_str)
1785 (reqID) = foreign "C" addIORequest(R1, 1/*TRUE*/,R2,R3,R4 "ptr") [];
1787 StgAsyncIOResult_reqID(ares) = reqID;
1788 StgAsyncIOResult_len(ares) = 0;
1789 StgAsyncIOResult_errCode(ares) = 0;
1790 StgTSO_block_info(CurrentTSO) = ares;
1791 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1792 jump stg_block_async;
1796 STRING(stg_asyncDoProczh_malloc_str, "stg_asyncDoProczh")
1803 foreign "C" barf("asyncDoProc# on threaded RTS") never returns;
1806 /* args: R1 = proc, R2 = param */
1807 ASSERT(StgTSO_why_blocked(CurrentTSO) == NotBlocked::I16);
1808 StgTSO_why_blocked(CurrentTSO) = BlockedOnDoProc::I16;
1810 /* could probably allocate this on the heap instead */
1811 ("ptr" ares) = foreign "C" stgMallocBytes(SIZEOF_StgAsyncIOResult,
1812 stg_asyncDoProczh_malloc_str)
1814 (reqID) = foreign "C" addDoProcRequest(R1 "ptr",R2 "ptr") [];
1815 StgAsyncIOResult_reqID(ares) = reqID;
1816 StgAsyncIOResult_len(ares) = 0;
1817 StgAsyncIOResult_errCode(ares) = 0;
1818 StgTSO_block_info(CurrentTSO) = ares;
1819 APPEND_TO_BLOCKED_QUEUE(CurrentTSO);
1820 jump stg_block_async;
1825 // noDuplicate# tries to ensure that none of the thunks under
1826 // evaluation by the current thread are also under evaluation by
1827 // another thread. It relies on *both* threads doing noDuplicate#;
1828 // the second one will get blocked if they are duplicating some work.
1831 SAVE_THREAD_STATE();
1832 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
1833 foreign "C" threadPaused (MyCapability() "ptr", CurrentTSO "ptr") [];
1835 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
1836 jump stg_threadFinished;
1838 LOAD_THREAD_STATE();
1839 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
1840 jump %ENTRY_CODE(Sp(0));
1846 W_ ap_stack, offset, val, ok;
1848 /* args: R1 = AP_STACK, R2 = offset */
1852 if (%INFO_PTR(ap_stack) == stg_AP_STACK_info) {
1854 val = StgAP_STACK_payload(ap_stack,offset);
1862 /* -----------------------------------------------------------------------------
1864 -------------------------------------------------------------------------- */
1866 // Write the cost center stack of the first argument on stderr; return
1867 // the second. Possibly only makes sense for already evaluated
1874 ccs = StgHeader_ccs(UNTAG(R1));
1875 foreign "C" fprintCCS_stderr(ccs "ptr") [R2];
1886 #ifndef THREADED_RTS
1887 RET_NP(0,ghczmprim_GHCziBool_False_closure);
1889 (spark) = foreign "C" findSpark(MyCapability());
1893 RET_NP(0,ghczmprim_GHCziBool_False_closure);
1902 #if defined(TRACING) || defined(DEBUG)
1903 foreign "C" traceUserMsg(MyCapability() "ptr", msg "ptr") [];
1905 jump %ENTRY_CODE(Sp(0));