1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 #ifdef HAVE_DL_ITERATE_PHDR
24 #ifdef USING_SPLIT_STACK
26 /* FIXME: These are not declared anywhere. */
28 extern void __splitstack_getcontext(void *context
[10]);
30 extern void __splitstack_setcontext(void *context
[10]);
32 extern void *__splitstack_makecontext(size_t, void *context
[10], size_t *);
34 extern void * __splitstack_resetcontext(void *context
[10], size_t *);
36 extern void *__splitstack_find(void *, void *, size_t *, void **, void **,
39 extern void __splitstack_block_signals (int *, int *);
41 extern void __splitstack_block_signals_context (void *context
[10], int *,
46 #ifndef PTHREAD_STACK_MIN
47 # define PTHREAD_STACK_MIN 8192
50 #if defined(USING_SPLIT_STACK) && defined(LINKER_SUPPORTS_SPLIT_STACK)
51 # define StackMin PTHREAD_STACK_MIN
53 # define StackMin ((sizeof(char *) < 8) ? 2 * 1024 * 1024 : 4 * 1024 * 1024)
56 uintptr runtime_stacks_sys
;
58 static void gtraceback(G
*);
67 #ifndef SETCONTEXT_CLOBBERS_TLS
75 fixcontext(ucontext_t
*c
__attribute__ ((unused
)))
81 # if defined(__x86_64__) && defined(__sun__)
83 // x86_64 Solaris 10 and 11 have a bug: setcontext switches the %fs
84 // register to that of the thread which called getcontext. The effect
85 // is that the address of all __thread variables changes. This bug
86 // also affects pthread_self() and pthread_getspecific. We work
87 // around it by clobbering the context field directly to keep %fs the
90 static __thread greg_t fs
;
98 fs
= c
.uc_mcontext
.gregs
[REG_FSBASE
];
102 fixcontext(ucontext_t
* c
)
104 c
->uc_mcontext
.gregs
[REG_FSBASE
] = fs
;
107 # elif defined(__NetBSD__)
109 // NetBSD has a bug: setcontext clobbers tlsbase, we need to save
110 // and restore it ourselves.
112 static __thread __greg_t tlsbase
;
120 tlsbase
= c
.uc_mcontext
._mc_tlsbase
;
124 fixcontext(ucontext_t
* c
)
126 c
->uc_mcontext
._mc_tlsbase
= tlsbase
;
131 # error unknown case for SETCONTEXT_CLOBBERS_TLS
137 // We can not always refer to the TLS variables directly. The
138 // compiler will call tls_get_addr to get the address of the variable,
139 // and it may hold it in a register across a call to schedule. When
140 // we get back from the call we may be running in a different thread,
141 // in which case the register now points to the TLS variable for a
142 // different thread. We use non-inlinable functions to avoid this
145 G
* runtime_g(void) __attribute__ ((noinline
, no_split_stack
));
153 M
* runtime_m(void) __attribute__ ((noinline
, no_split_stack
));
163 runtime_setmg(M
* mp
, G
* gp
)
169 // Start a new thread.
171 runtime_newosproc(M
*mp
)
178 if(pthread_attr_init(&attr
) != 0)
179 runtime_throw("pthread_attr_init");
180 if(pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
) != 0)
181 runtime_throw("pthread_attr_setdetachstate");
183 // Block signals during pthread_create so that the new thread
184 // starts with signals disabled. It will enable them in minit.
188 // Blocking SIGTRAP reportedly breaks gdb on Alpha GNU/Linux.
189 sigdelset(&clear
, SIGTRAP
);
193 pthread_sigmask(SIG_BLOCK
, &clear
, &old
);
194 ret
= pthread_create(&tid
, &attr
, runtime_mstart
, mp
);
195 pthread_sigmask(SIG_SETMASK
, &old
, nil
);
198 runtime_throw("pthread_create");
201 // First function run by a new goroutine. This replaces gogocall.
207 if(g
->traceback
!= nil
)
210 fn
= (void (*)(void*))(g
->entry
);
215 // Switch context to a different goroutine. This is like longjmp.
216 void runtime_gogo(G
*) __attribute__ ((noinline
));
218 runtime_gogo(G
* newg
)
220 #ifdef USING_SPLIT_STACK
221 __splitstack_setcontext(&newg
->stack_context
[0]);
224 newg
->fromgogo
= true;
225 fixcontext(&newg
->context
);
226 setcontext(&newg
->context
);
227 runtime_throw("gogo setcontext returned");
230 // Save context and call fn passing g as a parameter. This is like
231 // setjmp. Because getcontext always returns 0, unlike setjmp, we use
232 // g->fromgogo as a code. It will be true if we got here via
233 // setcontext. g == nil the first time this is called in a new m.
234 void runtime_mcall(void (*)(G
*)) __attribute__ ((noinline
));
236 runtime_mcall(void (*pfn
)(G
*))
241 // Ensure that all registers are on the stack for the garbage
243 __builtin_unwind_init();
248 runtime_throw("runtime: mcall called on m->g0 stack");
252 #ifdef USING_SPLIT_STACK
253 __splitstack_getcontext(&g
->stack_context
[0]);
255 gp
->gcnext_sp
= &pfn
;
257 gp
->fromgogo
= false;
258 getcontext(&gp
->context
);
260 // When we return from getcontext, we may be running
261 // in a new thread. That means that m and g may have
262 // changed. They are global variables so we will
263 // reload them, but the addresses of m and g may be
264 // cached in our local stack frame, and those
265 // addresses may be wrong. Call functions to reload
266 // the values for this thread.
270 if(gp
->traceback
!= nil
)
273 if (gp
== nil
|| !gp
->fromgogo
) {
274 #ifdef USING_SPLIT_STACK
275 __splitstack_setcontext(&mp
->g0
->stack_context
[0]);
277 mp
->g0
->entry
= (byte
*)pfn
;
280 // It's OK to set g directly here because this case
281 // can not occur if we got here via a setcontext to
282 // the getcontext call just above.
285 fixcontext(&mp
->g0
->context
);
286 setcontext(&mp
->g0
->context
);
287 runtime_throw("runtime: mcall function returned");
291 // Goroutine scheduler
292 // The scheduler's job is to distribute ready-to-run goroutines over worker threads.
294 // The main concepts are:
296 // M - worker thread, or machine.
297 // P - processor, a resource that is required to execute Go code.
298 // M must have an associated P to execute Go code, however it can be
299 // blocked or in a syscall w/o an associated P.
301 // Design doc at http://golang.org/s/go11sched.
303 typedef struct Sched Sched
;
308 M
* midle
; // idle m's waiting for work
309 int32 nmidle
; // number of idle m's waiting for work
310 int32 nmidlelocked
; // number of locked m's waiting for work
311 int32 mcount
; // number of m's that have been created
312 int32 maxmcount
; // maximum number of m's allowed (or die)
314 P
* pidle
; // idle P's
318 // Global runnable queue.
323 // Global cache of dead G's.
327 uint32 gcwaiting
; // gc is waiting to run
334 int32 profilehz
; // cpu profiling rate
339 // The max value of GOMAXPROCS.
340 // There are no fundamental restrictions on the value.
341 MaxGomaxprocs
= 1<<8,
343 // Number of goroutine ids to grab from runtime_sched.goidgen to local per-P cache at once.
344 // 16 seems to provide enough amortization, but other than that it's mostly arbitrary number.
349 int32 runtime_gomaxprocs
;
350 uint32 runtime_needextram
= 1;
351 bool runtime_iscgo
= true;
353 G runtime_g0
; // idle goroutine for m0
360 bool runtime_precisestack
;
361 static int32 newprocs
;
363 static Lock allglock
; // the following vars are protected by this lock or by stoptheworld
365 uintptr runtime_allglen
;
366 static uintptr allgcap
;
368 void* runtime_mstart(void*);
369 static void runqput(P
*, G
*);
370 static G
* runqget(P
*);
371 static bool runqputslow(P
*, G
*, uint32
, uint32
);
372 static G
* runqsteal(P
*, P
*);
373 static void mput(M
*);
374 static M
* mget(void);
375 static void mcommoninit(M
*);
376 static void schedule(void);
377 static void procresize(int32
);
378 static void acquirep(P
*);
379 static P
* releasep(void);
380 static void newm(void(*)(void), P
*);
381 static void stopm(void);
382 static void startm(P
*, bool);
383 static void handoffp(P
*);
384 static void wakep(void);
385 static void stoplockedm(void);
386 static void startlockedm(G
*);
387 static void sysmon(void);
388 static uint32
retake(int64
);
389 static void incidlelocked(int32
);
390 static void checkdead(void);
391 static void exitsyscall0(G
*);
392 static void park0(G
*);
393 static void goexit0(G
*);
394 static void gfput(P
*, G
*);
396 static void gfpurge(P
*);
397 static void globrunqput(G
*);
398 static void globrunqputbatch(G
*, G
*, int32
);
399 static G
* globrunqget(P
*, int32
);
400 static P
* pidleget(void);
401 static void pidleput(P
*);
402 static void injectglist(G
*);
403 static bool preemptall(void);
404 static bool exitsyscallfast(void);
405 static void allgadd(G
*);
407 // The bootstrap sequence is:
411 // make & queue new G
412 // call runtime_mstart
414 // The new G calls runtime_main.
416 runtime_schedinit(void)
430 runtime_sched
.maxmcount
= 10000;
431 runtime_precisestack
= 0;
433 // runtime_symtabinit();
434 runtime_mallocinit();
437 // Initialize the itable value for newErrorCString,
438 // so that the next time it gets called, possibly
439 // in a fault during a garbage collection, it will not
440 // need to allocated memory.
441 runtime_newErrorCString(0, &i
);
443 // Initialize the cached gotraceback value, since
444 // gotraceback calls getenv, which mallocs on Plan 9.
445 runtime_gotraceback(nil
);
449 runtime_parsedebugvars();
451 runtime_sched
.lastpoll
= runtime_nanotime();
453 p
= runtime_getenv("GOMAXPROCS");
454 if(p
!= nil
&& (n
= runtime_atoi(p
)) > 0) {
455 if(n
> MaxGomaxprocs
)
459 runtime_allp
= runtime_malloc((MaxGomaxprocs
+1)*sizeof(runtime_allp
[0]));
462 // Can not enable GC until all roots are registered.
463 // mstats.enablegc = 1;
466 extern void main_init(void) __asm__ (GOSYM_PREFIX
"__go_init_main");
467 extern void main_main(void) __asm__ (GOSYM_PREFIX
"main.main");
470 initDone(void *arg
__attribute__ ((unused
))) {
471 runtime_unlockOSThread();
474 // The main goroutine.
475 // Note: C frames in general are not copyable during stack growth, for two reasons:
476 // 1) We don't know where in a frame to find pointers to other stack locations.
477 // 2) There's no guarantee that globals or heap values do not point into the frame.
479 // The C frame for runtime.main is copyable, because:
480 // 1) There are no pointers to other stack locations in the frame
481 // (d.fn points at a global, d.link is nil, d.argp is -1).
482 // 2) The only pointer into this frame is from the defer chain,
483 // which is explicitly handled during stack copying.
485 runtime_main(void* dummy
__attribute__((unused
)))
492 // Lock the main goroutine onto this, the main OS thread,
493 // during initialization. Most programs won't care, but a few
494 // do require certain calls to be made by the main thread.
495 // Those can arrange for main.main to run in the main thread
496 // by calling runtime.LockOSThread during initialization
497 // to preserve the lock.
498 runtime_lockOSThread();
500 // Defer unlock so that runtime.Goexit during init does the unlock too.
504 d
.__panic
= g
->panic
;
506 d
.__makefunc_can_recover
= 0;
512 runtime_throw("runtime_main not on m0");
513 __go_go(runtime_MHeap_Scavenger
, nil
);
516 if(g
->defer
!= &d
|| d
.__pfn
!= initDone
)
517 runtime_throw("runtime: bad defer entry after init");
519 runtime_unlockOSThread();
521 // For gccgo we have to wait until after main is initialized
522 // to enable GC, because initializing main registers the GC
528 // Make racy client program work: if panicking on
529 // another goroutine at the same time as main returns,
530 // let the other goroutine finish printing the panic trace.
531 // Once it does, it will exit. See issue 3934.
532 if(runtime_panicking
)
533 runtime_park(nil
, nil
, "panicwait");
541 runtime_goroutineheader(G
*gp
)
561 status
= gp
->waitreason
;
570 // approx time the G is blocked, in minutes
572 if((gp
->status
== Gwaiting
|| gp
->status
== Gsyscall
) && gp
->waitsince
!= 0)
573 waitfor
= (runtime_nanotime() - gp
->waitsince
) / (60LL*1000*1000*1000);
576 runtime_printf("goroutine %D [%s]:\n", gp
->goid
, status
);
578 runtime_printf("goroutine %D [%s, %D minutes]:\n", gp
->goid
, status
, waitfor
);
582 runtime_printcreatedby(G
*g
)
584 if(g
!= nil
&& g
->gopc
!= 0 && g
->goid
!= 1) {
589 if(__go_file_line(g
->gopc
- 1, &fn
, &file
, &line
)) {
590 runtime_printf("created by %S\n", fn
);
591 runtime_printf("\t%S:%D\n", file
, (int64
) line
);
599 Location locbuf
[TracebackMaxFrames
];
604 runtime_tracebackothers(G
* volatile me
)
612 traceback
= runtime_gotraceback(nil
);
614 // Show the current goroutine first, if we haven't already.
615 if((gp
= m
->curg
) != nil
&& gp
!= me
) {
616 runtime_printf("\n");
617 runtime_goroutineheader(gp
);
620 #ifdef USING_SPLIT_STACK
621 __splitstack_getcontext(&me
->stack_context
[0]);
623 getcontext(&me
->context
);
625 if(gp
->traceback
!= nil
) {
629 runtime_printtrace(tb
.locbuf
, tb
.c
, false);
630 runtime_printcreatedby(gp
);
633 runtime_lock(&allglock
);
634 for(i
= 0; i
< runtime_allglen
; i
++) {
635 gp
= runtime_allg
[i
];
636 if(gp
== me
|| gp
== m
->curg
|| gp
->status
== Gdead
)
638 if(gp
->issystem
&& traceback
< 2)
640 runtime_printf("\n");
641 runtime_goroutineheader(gp
);
643 // Our only mechanism for doing a stack trace is
644 // _Unwind_Backtrace. And that only works for the
645 // current thread, not for other random goroutines.
646 // So we need to switch context to the goroutine, get
647 // the backtrace, and then switch back.
649 // This means that if g is running or in a syscall, we
650 // can't reliably print a stack trace. FIXME.
652 if(gp
->status
== Grunning
) {
653 runtime_printf("\tgoroutine running on other thread; stack unavailable\n");
654 runtime_printcreatedby(gp
);
655 } else if(gp
->status
== Gsyscall
) {
656 runtime_printf("\tgoroutine in C code; stack unavailable\n");
657 runtime_printcreatedby(gp
);
661 #ifdef USING_SPLIT_STACK
662 __splitstack_getcontext(&me
->stack_context
[0]);
664 getcontext(&me
->context
);
666 if(gp
->traceback
!= nil
) {
670 runtime_printtrace(tb
.locbuf
, tb
.c
, false);
671 runtime_printcreatedby(gp
);
674 runtime_unlock(&allglock
);
680 // sched lock is held
681 if(runtime_sched
.mcount
> runtime_sched
.maxmcount
) {
682 runtime_printf("runtime: program exceeds %d-thread limit\n", runtime_sched
.maxmcount
);
683 runtime_throw("thread exhaustion");
687 // Do a stack trace of gp, and then restore the context to
693 Traceback
* traceback
;
695 traceback
= gp
->traceback
;
697 traceback
->c
= runtime_callers(1, traceback
->locbuf
,
698 sizeof traceback
->locbuf
/ sizeof traceback
->locbuf
[0], false);
699 runtime_gogo(traceback
->gp
);
705 // If there is no mcache runtime_callers() will crash,
706 // and we are most likely in sysmon thread so the stack is senseless anyway.
708 runtime_callers(1, mp
->createstack
, nelem(mp
->createstack
), false);
710 mp
->fastrand
= 0x49f6428aUL
+ mp
->id
+ runtime_cputicks();
712 runtime_lock(&runtime_sched
);
713 mp
->id
= runtime_sched
.mcount
++;
715 runtime_mpreinit(mp
);
717 // Add to runtime_allm so garbage collector doesn't free m
718 // when it is just in a register or thread-local storage.
719 mp
->alllink
= runtime_allm
;
720 // runtime_NumCgoCall() iterates over allm w/o schedlock,
721 // so we need to publish it safely.
722 runtime_atomicstorep(&runtime_allm
, mp
);
723 runtime_unlock(&runtime_sched
);
726 // Mark gp ready to run.
731 m
->locks
++; // disable preemption because it can be holding p in a local var
732 if(gp
->status
!= Gwaiting
) {
733 runtime_printf("goroutine %D has status %d\n", gp
->goid
, gp
->status
);
734 runtime_throw("bad g->status in ready");
736 gp
->status
= Grunnable
;
738 if(runtime_atomicload(&runtime_sched
.npidle
) != 0 && runtime_atomicload(&runtime_sched
.nmspinning
) == 0) // TODO: fast atomic
744 runtime_gcprocs(void)
748 // Figure out how many CPUs to use during GC.
749 // Limited by gomaxprocs, number of actual CPUs, and MaxGcproc.
750 runtime_lock(&runtime_sched
);
751 n
= runtime_gomaxprocs
;
753 n
= runtime_ncpu
> 0 ? runtime_ncpu
: 1;
756 if(n
> runtime_sched
.nmidle
+1) // one M is currently running
757 n
= runtime_sched
.nmidle
+1;
758 runtime_unlock(&runtime_sched
);
767 runtime_lock(&runtime_sched
);
768 n
= runtime_gomaxprocs
;
773 n
-= runtime_sched
.nmidle
+1; // one M is currently running
774 runtime_unlock(&runtime_sched
);
779 runtime_helpgc(int32 nproc
)
784 runtime_lock(&runtime_sched
);
786 for(n
= 1; n
< nproc
; n
++) { // one M is currently running
787 if(runtime_allp
[pos
]->mcache
== m
->mcache
)
791 runtime_throw("runtime_gcprocs inconsistency");
793 mp
->mcache
= runtime_allp
[pos
]->mcache
;
795 runtime_notewakeup(&mp
->park
);
797 runtime_unlock(&runtime_sched
);
800 // Similar to stoptheworld but best-effort and can be called several times.
801 // There is no reverse operation, used during crashing.
802 // This function must not lock any mutexes.
804 runtime_freezetheworld(void)
808 if(runtime_gomaxprocs
== 1)
810 // stopwait and preemption requests can be lost
811 // due to races with concurrently executing threads,
812 // so try several times
813 for(i
= 0; i
< 5; i
++) {
814 // this should tell the scheduler to not start any new goroutines
815 runtime_sched
.stopwait
= 0x7fffffff;
816 runtime_atomicstore((uint32
*)&runtime_sched
.gcwaiting
, 1);
817 // this should stop running goroutines
819 break; // no running goroutines
820 runtime_usleep(1000);
823 runtime_usleep(1000);
825 runtime_usleep(1000);
829 runtime_stoptheworld(void)
836 runtime_lock(&runtime_sched
);
837 runtime_sched
.stopwait
= runtime_gomaxprocs
;
838 runtime_atomicstore((uint32
*)&runtime_sched
.gcwaiting
, 1);
841 m
->p
->status
= Pgcstop
;
842 runtime_sched
.stopwait
--;
843 // try to retake all P's in Psyscall status
844 for(i
= 0; i
< runtime_gomaxprocs
; i
++) {
847 if(s
== Psyscall
&& runtime_cas(&p
->status
, s
, Pgcstop
))
848 runtime_sched
.stopwait
--;
851 while((p
= pidleget()) != nil
) {
853 runtime_sched
.stopwait
--;
855 wait
= runtime_sched
.stopwait
> 0;
856 runtime_unlock(&runtime_sched
);
858 // wait for remaining P's to stop voluntarily
860 runtime_notesleep(&runtime_sched
.stopnote
);
861 runtime_noteclear(&runtime_sched
.stopnote
);
863 if(runtime_sched
.stopwait
)
864 runtime_throw("stoptheworld: not stopped");
865 for(i
= 0; i
< runtime_gomaxprocs
; i
++) {
867 if(p
->status
!= Pgcstop
)
868 runtime_throw("stoptheworld: not stopped");
879 runtime_starttheworld(void)
886 m
->locks
++; // disable preemption because it can be holding p in a local var
887 gp
= runtime_netpoll(false); // non-blocking
889 add
= needaddgcproc();
890 runtime_lock(&runtime_sched
);
892 procresize(newprocs
);
895 procresize(runtime_gomaxprocs
);
896 runtime_sched
.gcwaiting
= 0;
899 while((p
= pidleget()) != nil
) {
900 // procresize() puts p's with work at the beginning of the list.
901 // Once we reach a p without a run queue, the rest don't have one either.
902 if(p
->runqhead
== p
->runqtail
) {
910 if(runtime_sched
.sysmonwait
) {
911 runtime_sched
.sysmonwait
= false;
912 runtime_notewakeup(&runtime_sched
.sysmonnote
);
914 runtime_unlock(&runtime_sched
);
923 runtime_throw("starttheworld: inconsistent mp->nextp");
925 runtime_notewakeup(&mp
->park
);
927 // Start M to run P. Do not start another M below.
934 // If GC could have used another helper proc, start one now,
935 // in the hope that it will be available next time.
936 // It would have been even better to start it before the collection,
937 // but doing so requires allocating memory, so it's tricky to
938 // coordinate. This lazy approach works out in practice:
939 // we don't mind if the first couple gc rounds don't have quite
940 // the maximum number of procs.
946 // Called to start an M.
948 runtime_mstart(void* mp
)
958 // Record top of stack for use by mcall.
959 // Once we call schedule we're never coming back,
960 // so other calls can reuse this stack space.
961 #ifdef USING_SPLIT_STACK
962 __splitstack_getcontext(&g
->stack_context
[0]);
964 g
->gcinitial_sp
= &mp
;
965 // Setting gcstack_size to 0 is a marker meaning that gcinitial_sp
966 // is the top of the stack, not the bottom.
970 getcontext(&g
->context
);
972 if(g
->entry
!= nil
) {
973 // Got here from mcall.
974 void (*pfn
)(G
*) = (void (*)(G
*))g
->entry
;
975 G
* gp
= (G
*)g
->param
;
981 #ifdef USING_SPLIT_STACK
983 int dont_block_signals
= 0;
984 __splitstack_block_signals(&dont_block_signals
, nil
);
988 // Install signal handlers; after minit so that minit can
989 // prepare the thread to be able to handle the signals.
999 } else if(m
!= &runtime_m0
) {
1005 // TODO(brainman): This point is never reached, because scheduler
1006 // does not release os threads at the moment. But once this path
1007 // is enabled, we must remove our seh here.
1012 typedef struct CgoThreadStart CgoThreadStart
;
1013 struct CgoThreadStart
1021 // Allocate a new m unassociated with any thread.
1022 // Can use p for allocation context if needed.
1024 runtime_allocm(P
*p
, int32 stacksize
, byte
** ret_g0_stack
, size_t* ret_g0_stacksize
)
1028 m
->locks
++; // disable GC because it can be called from sysmon
1030 acquirep(p
); // temporarily borrow p for mallocs in this function
1034 runtime_gc_m_ptr(&e
);
1035 mtype
= ((const PtrType
*)e
.__type_descriptor
)->__element_type
;
1039 mp
= runtime_mal(sizeof *mp
);
1041 mp
->g0
= runtime_malg(stacksize
, ret_g0_stack
, ret_g0_stacksize
);
1054 // static Type *gtype;
1056 // if(gtype == nil) {
1058 // runtime_gc_g_ptr(&e);
1059 // gtype = ((PtrType*)e.__type_descriptor)->__element_type;
1061 // gp = runtime_cnew(gtype);
1062 gp
= runtime_malloc(sizeof(G
));
1066 static M
* lockextra(bool nilokay
);
1067 static void unlockextra(M
*);
1069 // needm is called when a cgo callback happens on a
1070 // thread without an m (a thread not created by Go).
1071 // In this case, needm is expected to find an m to use
1072 // and return with m, g initialized correctly.
1073 // Since m and g are not set now (likely nil, but see below)
1074 // needm is limited in what routines it can call. In particular
1075 // it can only call nosplit functions (textflag 7) and cannot
1076 // do any scheduling that requires an m.
1078 // In order to avoid needing heavy lifting here, we adopt
1079 // the following strategy: there is a stack of available m's
1080 // that can be stolen. Using compare-and-swap
1081 // to pop from the stack has ABA races, so we simulate
1082 // a lock by doing an exchange (via casp) to steal the stack
1083 // head and replace the top pointer with MLOCKED (1).
1084 // This serves as a simple spin lock that we can use even
1085 // without an m. The thread that locks the stack in this way
1086 // unlocks the stack by storing a valid stack head pointer.
1088 // In order to make sure that there is always an m structure
1089 // available to be stolen, we maintain the invariant that there
1090 // is always one more than needed. At the beginning of the
1091 // program (if cgo is in use) the list is seeded with a single m.
1092 // If needm finds that it has taken the last m off the list, its job
1093 // is - once it has installed its own m so that it can do things like
1094 // allocate memory - to create a spare m and put it on the list.
1096 // Each of these extra m's also has a g0 and a curg that are
1097 // pressed into service as the scheduling stack and current
1098 // goroutine for the duration of the cgo callback.
1100 // When the callback is done with the m, it calls dropm to
1101 // put the m back on the list.
1103 // Unlike the gc toolchain, we start running on curg, since we are
1104 // just going to return and let the caller continue.
1110 if(runtime_needextram
) {
1111 // Can happen if C/C++ code calls Go from a global ctor.
1112 // Can not throw, because scheduler is not initialized yet.
1113 int rv
__attribute__((unused
));
1114 rv
= runtime_write(2, "fatal error: cgo callback before cgo call\n",
1115 sizeof("fatal error: cgo callback before cgo call\n")-1);
1119 // Lock extra list, take head, unlock popped list.
1120 // nilokay=false is safe here because of the invariant above,
1121 // that the extra list always contains or will soon contain
1123 mp
= lockextra(false);
1125 // Set needextram when we've just emptied the list,
1126 // so that the eventual call into cgocallbackg will
1127 // allocate a new m for the extra list. We delay the
1128 // allocation until then so that it can be done
1129 // after exitsyscall makes sure it is okay to be
1130 // running at all (that is, there's no garbage collection
1131 // running right now).
1132 mp
->needextram
= mp
->schedlink
== nil
;
1133 unlockextra(mp
->schedlink
);
1135 // Install m and g (= m->curg).
1136 runtime_setmg(mp
, mp
->curg
);
1138 // Initialize g's context as in mstart.
1140 g
->status
= Gsyscall
;
1143 #ifdef USING_SPLIT_STACK
1144 __splitstack_getcontext(&g
->stack_context
[0]);
1146 g
->gcinitial_sp
= &mp
;
1148 g
->gcstack_size
= 0;
1151 getcontext(&g
->context
);
1153 if(g
->entry
!= nil
) {
1154 // Got here from mcall.
1155 void (*pfn
)(G
*) = (void (*)(G
*))g
->entry
;
1156 G
* gp
= (G
*)g
->param
;
1161 // Initialize this thread to use the m.
1164 #ifdef USING_SPLIT_STACK
1166 int dont_block_signals
= 0;
1167 __splitstack_block_signals(&dont_block_signals
, nil
);
1172 // newextram allocates an m and puts it on the extra list.
1173 // It is called with a working local m, so that it can do things
1174 // like call schedlock and allocate.
1176 runtime_newextram(void)
1181 size_t g0_spsize
, spsize
;
1183 // Create extra goroutine locked to extra m.
1184 // The goroutine is the context in which the cgo callback will run.
1185 // The sched.pc will never be returned to, but setting it to
1186 // runtime.goexit makes clear to the traceback routines where
1187 // the goroutine stack ends.
1188 mp
= runtime_allocm(nil
, StackMin
, &g0_sp
, &g0_spsize
);
1189 gp
= runtime_malg(StackMin
, &sp
, &spsize
);
1192 mp
->locked
= LockInternal
;
1195 gp
->goid
= runtime_xadd64(&runtime_sched
.goidgen
, 1);
1196 // put on allg for garbage collector
1199 // The context for gp will be set up in runtime_needm. But
1200 // here we need to set up the context for g0.
1201 getcontext(&mp
->g0
->context
);
1202 mp
->g0
->context
.uc_stack
.ss_sp
= g0_sp
;
1203 mp
->g0
->context
.uc_stack
.ss_size
= g0_spsize
;
1204 makecontext(&mp
->g0
->context
, kickoff
, 0);
1206 // Add m to the extra list.
1207 mnext
= lockextra(true);
1208 mp
->schedlink
= mnext
;
1212 // dropm is called when a cgo callback has called needm but is now
1213 // done with the callback and returning back into the non-Go thread.
1214 // It puts the current m back onto the extra list.
1216 // The main expense here is the call to signalstack to release the
1217 // m's signal stack, and then the call to needm on the next callback
1218 // from this thread. It is tempting to try to save the m for next time,
1219 // which would eliminate both these costs, but there might not be
1220 // a next time: the current thread (which Go does not control) might exit.
1221 // If we saved the m for that thread, there would be an m leak each time
1222 // such a thread exited. Instead, we acquire and release an m on each
1223 // call. These should typically not be scheduling operations, just a few
1224 // atomics, so the cost should be small.
1226 // TODO(rsc): An alternative would be to allocate a dummy pthread per-thread
1227 // variable using pthread_key_create. Unlike the pthread keys we already use
1228 // on OS X, this dummy key would never be read by Go code. It would exist
1229 // only so that we could register at thread-exit-time destructor.
1230 // That destructor would put the m back onto the extra list.
1231 // This is purely a performance optimization. The current version,
1232 // in which dropm happens on each cgo call, is still correct too.
1233 // We may have to keep the current version on systems with cgo
1234 // but without pthreads, like Windows.
1240 // Undo whatever initialization minit did during needm.
1243 // Clear m and g, and return m to the extra list.
1244 // After the call to setmg we can only call nosplit functions.
1246 runtime_setmg(nil
, nil
);
1248 mp
->curg
->status
= Gdead
;
1249 mp
->curg
->gcstack
= nil
;
1250 mp
->curg
->gcnext_sp
= nil
;
1252 mnext
= lockextra(true);
1253 mp
->schedlink
= mnext
;
1257 #define MLOCKED ((M*)1)
1259 // lockextra locks the extra list and returns the list head.
1260 // The caller must unlock the list by storing a new list head
1261 // to runtime.extram. If nilokay is true, then lockextra will
1262 // return a nil list head if that's what it finds. If nilokay is false,
1263 // lockextra will keep waiting until the list head is no longer nil.
1265 lockextra(bool nilokay
)
1268 void (*yield
)(void);
1271 mp
= runtime_atomicloadp(&runtime_extram
);
1273 yield
= runtime_osyield
;
1277 if(mp
== nil
&& !nilokay
) {
1281 if(!runtime_casp(&runtime_extram
, mp
, MLOCKED
)) {
1282 yield
= runtime_osyield
;
1294 runtime_atomicstorep(&runtime_extram
, mp
);
1304 mp
= runtime_atomicloadp(&runtime_extram
);
1309 if(!runtime_casp(&runtime_extram
, mp
, MLOCKED
)) {
1314 for(mc
= mp
; mc
!= nil
; mc
= mc
->schedlink
)
1316 runtime_atomicstorep(&runtime_extram
, mp
);
1321 // Create a new m. It will start off with a call to fn, or else the scheduler.
1323 newm(void(*fn
)(void), P
*p
)
1327 mp
= runtime_allocm(p
, -1, nil
, nil
);
1331 runtime_newosproc(mp
);
1334 // Stops execution of the current m until new work is available.
1335 // Returns with acquired P.
1340 runtime_throw("stopm holding locks");
1342 runtime_throw("stopm holding p");
1344 m
->spinning
= false;
1345 runtime_xadd(&runtime_sched
.nmspinning
, -1);
1349 runtime_lock(&runtime_sched
);
1351 runtime_unlock(&runtime_sched
);
1352 runtime_notesleep(&m
->park
);
1353 runtime_noteclear(&m
->park
);
1370 // Schedules some M to run the p (creates an M if necessary).
1371 // If p==nil, tries to get an idle P, if no idle P's does nothing.
1373 startm(P
*p
, bool spinning
)
1378 runtime_lock(&runtime_sched
);
1382 runtime_unlock(&runtime_sched
);
1384 runtime_xadd(&runtime_sched
.nmspinning
, -1);
1389 runtime_unlock(&runtime_sched
);
1398 runtime_throw("startm: m is spinning");
1400 runtime_throw("startm: m has p");
1401 mp
->spinning
= spinning
;
1403 runtime_notewakeup(&mp
->park
);
1406 // Hands off P from syscall or locked M.
1410 // if it has local work, start it straight away
1411 if(p
->runqhead
!= p
->runqtail
|| runtime_sched
.runqsize
) {
1415 // no local work, check that there are no spinning/idle M's,
1416 // otherwise our help is not required
1417 if(runtime_atomicload(&runtime_sched
.nmspinning
) + runtime_atomicload(&runtime_sched
.npidle
) == 0 && // TODO: fast atomic
1418 runtime_cas(&runtime_sched
.nmspinning
, 0, 1)) {
1422 runtime_lock(&runtime_sched
);
1423 if(runtime_sched
.gcwaiting
) {
1424 p
->status
= Pgcstop
;
1425 if(--runtime_sched
.stopwait
== 0)
1426 runtime_notewakeup(&runtime_sched
.stopnote
);
1427 runtime_unlock(&runtime_sched
);
1430 if(runtime_sched
.runqsize
) {
1431 runtime_unlock(&runtime_sched
);
1435 // If this is the last running P and nobody is polling network,
1436 // need to wakeup another M to poll network.
1437 if(runtime_sched
.npidle
== (uint32
)runtime_gomaxprocs
-1 && runtime_atomicload64(&runtime_sched
.lastpoll
) != 0) {
1438 runtime_unlock(&runtime_sched
);
1443 runtime_unlock(&runtime_sched
);
1446 // Tries to add one more P to execute G's.
1447 // Called when a G is made runnable (newproc, ready).
1451 // be conservative about spinning threads
1452 if(!runtime_cas(&runtime_sched
.nmspinning
, 0, 1))
1457 // Stops execution of the current m that is locked to a g until the g is runnable again.
1458 // Returns with acquired P.
1464 if(m
->lockedg
== nil
|| m
->lockedg
->lockedm
!= m
)
1465 runtime_throw("stoplockedm: inconsistent locking");
1467 // Schedule another M to run this p.
1472 // Wait until another thread schedules lockedg again.
1473 runtime_notesleep(&m
->park
);
1474 runtime_noteclear(&m
->park
);
1475 if(m
->lockedg
->status
!= Grunnable
)
1476 runtime_throw("stoplockedm: not runnable");
1481 // Schedules the locked m to run the locked gp.
1490 runtime_throw("startlockedm: locked to me");
1492 runtime_throw("startlockedm: m has p");
1493 // directly handoff current P to the locked m
1497 runtime_notewakeup(&mp
->park
);
1501 // Stops the current m for stoptheworld.
1502 // Returns when the world is restarted.
1508 if(!runtime_sched
.gcwaiting
)
1509 runtime_throw("gcstopm: not waiting for gc");
1511 m
->spinning
= false;
1512 runtime_xadd(&runtime_sched
.nmspinning
, -1);
1515 runtime_lock(&runtime_sched
);
1516 p
->status
= Pgcstop
;
1517 if(--runtime_sched
.stopwait
== 0)
1518 runtime_notewakeup(&runtime_sched
.stopnote
);
1519 runtime_unlock(&runtime_sched
);
1523 // Schedules gp to run on the current M.
1530 if(gp
->status
!= Grunnable
) {
1531 runtime_printf("execute: bad g status %d\n", gp
->status
);
1532 runtime_throw("execute: bad g status");
1534 gp
->status
= Grunning
;
1540 // Check whether the profiler needs to be turned on or off.
1541 hz
= runtime_sched
.profilehz
;
1542 if(m
->profilehz
!= hz
)
1543 runtime_resetcpuprofiler(hz
);
1548 // Finds a runnable goroutine to execute.
1549 // Tries to steal from other P's, get g from global queue, poll network.
1558 if(runtime_sched
.gcwaiting
) {
1562 if(runtime_fingwait
&& runtime_fingwake
&& (gp
= runtime_wakefing()) != nil
)
1569 if(runtime_sched
.runqsize
) {
1570 runtime_lock(&runtime_sched
);
1571 gp
= globrunqget(m
->p
, 0);
1572 runtime_unlock(&runtime_sched
);
1577 gp
= runtime_netpoll(false); // non-blocking
1579 injectglist(gp
->schedlink
);
1580 gp
->status
= Grunnable
;
1583 // If number of spinning M's >= number of busy P's, block.
1584 // This is necessary to prevent excessive CPU consumption
1585 // when GOMAXPROCS>>1 but the program parallelism is low.
1586 if(!m
->spinning
&& 2 * runtime_atomicload(&runtime_sched
.nmspinning
) >= runtime_gomaxprocs
- runtime_atomicload(&runtime_sched
.npidle
)) // TODO: fast atomic
1590 runtime_xadd(&runtime_sched
.nmspinning
, 1);
1592 // random steal from other P's
1593 for(i
= 0; i
< 2*runtime_gomaxprocs
; i
++) {
1594 if(runtime_sched
.gcwaiting
)
1596 p
= runtime_allp
[runtime_fastrand1()%runtime_gomaxprocs
];
1600 gp
= runqsteal(m
->p
, p
);
1605 // return P and block
1606 runtime_lock(&runtime_sched
);
1607 if(runtime_sched
.gcwaiting
) {
1608 runtime_unlock(&runtime_sched
);
1611 if(runtime_sched
.runqsize
) {
1612 gp
= globrunqget(m
->p
, 0);
1613 runtime_unlock(&runtime_sched
);
1618 runtime_unlock(&runtime_sched
);
1620 m
->spinning
= false;
1621 runtime_xadd(&runtime_sched
.nmspinning
, -1);
1623 // check all runqueues once again
1624 for(i
= 0; i
< runtime_gomaxprocs
; i
++) {
1625 p
= runtime_allp
[i
];
1626 if(p
&& p
->runqhead
!= p
->runqtail
) {
1627 runtime_lock(&runtime_sched
);
1629 runtime_unlock(&runtime_sched
);
1638 if(runtime_xchg64(&runtime_sched
.lastpoll
, 0) != 0) {
1640 runtime_throw("findrunnable: netpoll with p");
1642 runtime_throw("findrunnable: netpoll with spinning");
1643 gp
= runtime_netpoll(true); // block until new work is available
1644 runtime_atomicstore64(&runtime_sched
.lastpoll
, runtime_nanotime());
1646 runtime_lock(&runtime_sched
);
1648 runtime_unlock(&runtime_sched
);
1651 injectglist(gp
->schedlink
);
1652 gp
->status
= Grunnable
;
1668 m
->spinning
= false;
1669 nmspinning
= runtime_xadd(&runtime_sched
.nmspinning
, -1);
1671 runtime_throw("findrunnable: negative nmspinning");
1673 nmspinning
= runtime_atomicload(&runtime_sched
.nmspinning
);
1675 // M wakeup policy is deliberately somewhat conservative (see nmspinning handling),
1676 // so see if we need to wakeup another P here.
1677 if (nmspinning
== 0 && runtime_atomicload(&runtime_sched
.npidle
) > 0)
1681 // Injects the list of runnable G's into the scheduler.
1682 // Can run concurrently with GC.
1684 injectglist(G
*glist
)
1691 runtime_lock(&runtime_sched
);
1692 for(n
= 0; glist
; n
++) {
1694 glist
= gp
->schedlink
;
1695 gp
->status
= Grunnable
;
1698 runtime_unlock(&runtime_sched
);
1700 for(; n
&& runtime_sched
.npidle
; n
--)
1704 // One round of scheduler: find a runnable goroutine and execute it.
1713 runtime_throw("schedule: holding locks");
1716 if(runtime_sched
.gcwaiting
) {
1722 // Check the global runnable queue once in a while to ensure fairness.
1723 // Otherwise two goroutines can completely occupy the local runqueue
1724 // by constantly respawning each other.
1725 tick
= m
->p
->schedtick
;
1726 // This is a fancy way to say tick%61==0,
1727 // it uses 2 MUL instructions instead of a single DIV and so is faster on modern processors.
1728 if(tick
- (((uint64
)tick
*0x4325c53fu
)>>36)*61 == 0 && runtime_sched
.runqsize
> 0) {
1729 runtime_lock(&runtime_sched
);
1730 gp
= globrunqget(m
->p
, 1);
1731 runtime_unlock(&runtime_sched
);
1737 if(gp
&& m
->spinning
)
1738 runtime_throw("schedule: spinning with local work");
1741 gp
= findrunnable(); // blocks until work is available
1746 // Hands off own p to the locked m,
1747 // then blocks waiting for a new p.
1755 // Puts the current goroutine into a waiting state and calls unlockf.
1756 // If unlockf returns false, the goroutine is resumed.
1758 runtime_park(bool(*unlockf
)(G
*, void*), void *lock
, const char *reason
)
1760 if(g
->status
!= Grunning
)
1761 runtime_throw("bad g status");
1763 m
->waitunlockf
= unlockf
;
1764 g
->waitreason
= reason
;
1765 runtime_mcall(park0
);
1769 parkunlock(G
*gp
, void *lock
)
1772 runtime_unlock(lock
);
1776 // Puts the current goroutine into a waiting state and unlocks the lock.
1777 // The goroutine can be made runnable again by calling runtime_ready(gp).
1779 runtime_parkunlock(Lock
*lock
, const char *reason
)
1781 runtime_park(parkunlock
, lock
, reason
);
1784 // runtime_park continuation on g0.
1790 gp
->status
= Gwaiting
;
1793 if(m
->waitunlockf
) {
1794 ok
= m
->waitunlockf(gp
, m
->waitlock
);
1795 m
->waitunlockf
= nil
;
1798 gp
->status
= Grunnable
;
1799 execute(gp
); // Schedule it back, never returns.
1804 execute(gp
); // Never returns.
1811 runtime_gosched(void)
1813 if(g
->status
!= Grunning
)
1814 runtime_throw("bad g status");
1815 runtime_mcall(runtime_gosched0
);
1818 // runtime_gosched continuation on g0.
1820 runtime_gosched0(G
*gp
)
1822 gp
->status
= Grunnable
;
1825 runtime_lock(&runtime_sched
);
1827 runtime_unlock(&runtime_sched
);
1830 execute(gp
); // Never returns.
1835 // Finishes execution of the current goroutine.
1836 // Need to mark it as nosplit, because it runs with sp > stackbase (as runtime_lessstack).
1837 // Since it does not return it does not matter. But if it is preempted
1838 // at the split stack check, GC will complain about inconsistent sp.
1839 void runtime_goexit(void) __attribute__ ((noinline
));
1841 runtime_goexit(void)
1843 if(g
->status
!= Grunning
)
1844 runtime_throw("bad g status");
1845 runtime_mcall(goexit0
);
1848 // runtime_goexit continuation on g0.
1856 gp
->paniconfault
= 0;
1857 gp
->defer
= nil
; // should be true already but just in case.
1858 gp
->panic
= nil
; // non-nil for Goexit during panic. points at stack-allocated data.
1861 gp
->waitreason
= nil
;
1865 if(m
->locked
& ~LockExternal
) {
1866 runtime_printf("invalid m->locked = %d\n", m
->locked
);
1867 runtime_throw("internal lockOSThread error");
1874 // The goroutine g is about to enter a system call.
1875 // Record that it's not using the cpu anymore.
1876 // This is called only from the go syscall library and cgocall,
1877 // not from the low-level system calls used by the runtime.
1879 // Entersyscall cannot split the stack: the runtime_gosave must
1880 // make g->sched refer to the caller's stack segment, because
1881 // entersyscall is going to return immediately after.
1883 void runtime_entersyscall(void) __attribute__ ((no_split_stack
));
1884 static void doentersyscall(void) __attribute__ ((no_split_stack
, noinline
));
1887 runtime_entersyscall()
1889 // Save the registers in the g structure so that any pointers
1890 // held in registers will be seen by the garbage collector.
1891 getcontext(&g
->gcregs
);
1893 // Do the work in a separate function, so that this function
1894 // doesn't save any registers on its own stack. If this
1895 // function does save any registers, we might store the wrong
1896 // value in the call to getcontext.
1898 // FIXME: This assumes that we do not need to save any
1899 // callee-saved registers to access the TLS variable g. We
1900 // don't want to put the ucontext_t on the stack because it is
1901 // large and we can not split the stack here.
1908 // Disable preemption because during this function g is in Gsyscall status,
1909 // but can have inconsistent g->sched, do not let GC observe it.
1912 // Leave SP around for GC and traceback.
1913 #ifdef USING_SPLIT_STACK
1914 g
->gcstack
= __splitstack_find(nil
, nil
, &g
->gcstack_size
,
1915 &g
->gcnext_segment
, &g
->gcnext_sp
,
1921 g
->gcnext_sp
= (byte
*) &v
;
1925 g
->status
= Gsyscall
;
1927 if(runtime_atomicload(&runtime_sched
.sysmonwait
)) { // TODO: fast atomic
1928 runtime_lock(&runtime_sched
);
1929 if(runtime_atomicload(&runtime_sched
.sysmonwait
)) {
1930 runtime_atomicstore(&runtime_sched
.sysmonwait
, 0);
1931 runtime_notewakeup(&runtime_sched
.sysmonnote
);
1933 runtime_unlock(&runtime_sched
);
1938 runtime_atomicstore(&m
->p
->status
, Psyscall
);
1939 if(runtime_sched
.gcwaiting
) {
1940 runtime_lock(&runtime_sched
);
1941 if (runtime_sched
.stopwait
> 0 && runtime_cas(&m
->p
->status
, Psyscall
, Pgcstop
)) {
1942 if(--runtime_sched
.stopwait
== 0)
1943 runtime_notewakeup(&runtime_sched
.stopnote
);
1945 runtime_unlock(&runtime_sched
);
1951 // The same as runtime_entersyscall(), but with a hint that the syscall is blocking.
1953 runtime_entersyscallblock(void)
1957 m
->locks
++; // see comment in entersyscall
1959 // Leave SP around for GC and traceback.
1960 #ifdef USING_SPLIT_STACK
1961 g
->gcstack
= __splitstack_find(nil
, nil
, &g
->gcstack_size
,
1962 &g
->gcnext_segment
, &g
->gcnext_sp
,
1965 g
->gcnext_sp
= (byte
*) &p
;
1968 // Save the registers in the g structure so that any pointers
1969 // held in registers will be seen by the garbage collector.
1970 getcontext(&g
->gcregs
);
1972 g
->status
= Gsyscall
;
1976 if(g
->isbackground
) // do not consider blocked scavenger for deadlock detection
1982 // The goroutine g exited its system call.
1983 // Arrange for it to run on a cpu again.
1984 // This is called only from the go syscall library, not
1985 // from the low-level system calls used by the runtime.
1987 runtime_exitsyscall(void)
1991 m
->locks
++; // see comment in entersyscall
1994 if(gp
->isbackground
) // do not consider blocked scavenger for deadlock detection
1998 if(exitsyscallfast()) {
1999 // There's a cpu for us, so we can run.
2000 m
->p
->syscalltick
++;
2001 gp
->status
= Grunning
;
2002 // Garbage collector isn't running (since we are),
2003 // so okay to clear gcstack and gcsp.
2004 #ifdef USING_SPLIT_STACK
2007 gp
->gcnext_sp
= nil
;
2008 runtime_memclr(&gp
->gcregs
, sizeof gp
->gcregs
);
2015 // Call the scheduler.
2016 runtime_mcall(exitsyscall0
);
2018 // Scheduler returned, so we're allowed to run now.
2019 // Delete the gcstack information that we left for
2020 // the garbage collector during the system call.
2021 // Must wait until now because until gosched returns
2022 // we don't know for sure that the garbage collector
2024 #ifdef USING_SPLIT_STACK
2027 gp
->gcnext_sp
= nil
;
2028 runtime_memclr(&gp
->gcregs
, sizeof gp
->gcregs
);
2030 // Don't refer to m again, we might be running on a different
2031 // thread after returning from runtime_mcall.
2032 runtime_m()->p
->syscalltick
++;
2036 exitsyscallfast(void)
2040 // Freezetheworld sets stopwait but does not retake P's.
2041 if(runtime_sched
.stopwait
) {
2046 // Try to re-acquire the last P.
2047 if(m
->p
&& m
->p
->status
== Psyscall
&& runtime_cas(&m
->p
->status
, Psyscall
, Prunning
)) {
2048 // There's a cpu for us, so we can run.
2049 m
->mcache
= m
->p
->mcache
;
2053 // Try to get any other idle P.
2055 if(runtime_sched
.pidle
) {
2056 runtime_lock(&runtime_sched
);
2058 if(p
&& runtime_atomicload(&runtime_sched
.sysmonwait
)) {
2059 runtime_atomicstore(&runtime_sched
.sysmonwait
, 0);
2060 runtime_notewakeup(&runtime_sched
.sysmonnote
);
2062 runtime_unlock(&runtime_sched
);
2071 // runtime_exitsyscall slow path on g0.
2072 // Failed to acquire P, enqueue gp as runnable.
2078 gp
->status
= Grunnable
;
2081 runtime_lock(&runtime_sched
);
2085 else if(runtime_atomicload(&runtime_sched
.sysmonwait
)) {
2086 runtime_atomicstore(&runtime_sched
.sysmonwait
, 0);
2087 runtime_notewakeup(&runtime_sched
.sysmonnote
);
2089 runtime_unlock(&runtime_sched
);
2092 execute(gp
); // Never returns.
2095 // Wait until another thread schedules gp and so m again.
2097 execute(gp
); // Never returns.
2100 schedule(); // Never returns.
2103 // Called from syscall package before fork.
2104 void syscall_runtime_BeforeFork(void)
2105 __asm__(GOSYM_PREFIX
"syscall.runtime_BeforeFork");
2107 syscall_runtime_BeforeFork(void)
2109 // Fork can hang if preempted with signals frequently enough (see issue 5517).
2110 // Ensure that we stay on the same M where we disable profiling.
2111 runtime_m()->locks
++;
2112 if(runtime_m()->profilehz
!= 0)
2113 runtime_resetcpuprofiler(0);
2116 // Called from syscall package after fork in parent.
2117 void syscall_runtime_AfterFork(void)
2118 __asm__(GOSYM_PREFIX
"syscall.runtime_AfterFork");
2120 syscall_runtime_AfterFork(void)
2124 hz
= runtime_sched
.profilehz
;
2126 runtime_resetcpuprofiler(hz
);
2127 runtime_m()->locks
--;
2130 // Allocate a new g, with a stack big enough for stacksize bytes.
2132 runtime_malg(int32 stacksize
, byte
** ret_stack
, size_t* ret_stacksize
)
2137 if(stacksize
>= 0) {
2138 #if USING_SPLIT_STACK
2139 int dont_block_signals
= 0;
2141 *ret_stack
= __splitstack_makecontext(stacksize
,
2142 &newg
->stack_context
[0],
2144 __splitstack_block_signals_context(&newg
->stack_context
[0],
2145 &dont_block_signals
, nil
);
2147 *ret_stack
= runtime_mallocgc(stacksize
, 0, FlagNoProfiling
|FlagNoGC
);
2148 *ret_stacksize
= stacksize
;
2149 newg
->gcinitial_sp
= *ret_stack
;
2150 newg
->gcstack_size
= stacksize
;
2151 runtime_xadd(&runtime_stacks_sys
, stacksize
);
2157 /* For runtime package testing. */
2160 // Create a new g running fn with siz bytes of arguments.
2161 // Put it on the queue of g's waiting to run.
2162 // The compiler turns a go statement into a call to this.
2163 // Cannot split the stack because it assumes that the arguments
2164 // are available sequentially after &fn; they would not be
2165 // copied if a stack split occurred. It's OK for this to call
2166 // functions that split the stack.
2167 void runtime_testing_entersyscall(void)
2168 __asm__ (GOSYM_PREFIX
"runtime.entersyscall");
2170 runtime_testing_entersyscall()
2172 runtime_entersyscall();
2175 void runtime_testing_exitsyscall(void)
2176 __asm__ (GOSYM_PREFIX
"runtime.exitsyscall");
2179 runtime_testing_exitsyscall()
2181 runtime_exitsyscall();
2185 __go_go(void (*fn
)(void*), void* arg
)
2192 //runtime_printf("newproc1 %p %p narg=%d nret=%d\n", fn->fn, argp, narg, nret);
2194 m
->throwing
= -1; // do not dump full stacks
2195 runtime_throw("go of nil func value");
2197 m
->locks
++; // disable preemption because it can be holding p in a local var
2200 if((newg
= gfget(p
)) != nil
) {
2201 #ifdef USING_SPLIT_STACK
2202 int dont_block_signals
= 0;
2204 sp
= __splitstack_resetcontext(&newg
->stack_context
[0],
2206 __splitstack_block_signals_context(&newg
->stack_context
[0],
2207 &dont_block_signals
, nil
);
2209 sp
= newg
->gcinitial_sp
;
2210 spsize
= newg
->gcstack_size
;
2212 runtime_throw("bad spsize in __go_go");
2213 newg
->gcnext_sp
= sp
;
2216 newg
= runtime_malg(StackMin
, &sp
, &spsize
);
2220 newg
->entry
= (byte
*)fn
;
2222 newg
->gopc
= (uintptr
)__builtin_return_address(0);
2223 newg
->status
= Grunnable
;
2224 if(p
->goidcache
== p
->goidcacheend
) {
2225 p
->goidcache
= runtime_xadd64(&runtime_sched
.goidgen
, GoidCacheBatch
);
2226 p
->goidcacheend
= p
->goidcache
+ GoidCacheBatch
;
2228 newg
->goid
= p
->goidcache
++;
2231 // Avoid warnings about variables clobbered by
2233 byte
* volatile vsp
= sp
;
2234 size_t volatile vspsize
= spsize
;
2235 G
* volatile vnewg
= newg
;
2237 getcontext(&vnewg
->context
);
2238 vnewg
->context
.uc_stack
.ss_sp
= vsp
;
2239 #ifdef MAKECONTEXT_STACK_TOP
2240 vnewg
->context
.uc_stack
.ss_sp
+= vspsize
;
2242 vnewg
->context
.uc_stack
.ss_size
= vspsize
;
2243 makecontext(&vnewg
->context
, kickoff
, 0);
2247 if(runtime_atomicload(&runtime_sched
.npidle
) != 0 && runtime_atomicload(&runtime_sched
.nmspinning
) == 0 && fn
!= runtime_main
) // TODO: fast atomic
2260 runtime_lock(&allglock
);
2261 if(runtime_allglen
>= allgcap
) {
2262 cap
= 4096/sizeof(new[0]);
2265 new = runtime_malloc(cap
*sizeof(new[0]));
2267 runtime_throw("runtime: cannot allocate memory");
2268 if(runtime_allg
!= nil
) {
2269 runtime_memmove(new, runtime_allg
, runtime_allglen
*sizeof(new[0]));
2270 runtime_free(runtime_allg
);
2275 runtime_allg
[runtime_allglen
++] = gp
;
2276 runtime_unlock(&allglock
);
2279 // Put on gfree list.
2280 // If local list is too long, transfer a batch to the global list.
2284 gp
->schedlink
= p
->gfree
;
2287 if(p
->gfreecnt
>= 64) {
2288 runtime_lock(&runtime_sched
.gflock
);
2289 while(p
->gfreecnt
>= 32) {
2292 p
->gfree
= gp
->schedlink
;
2293 gp
->schedlink
= runtime_sched
.gfree
;
2294 runtime_sched
.gfree
= gp
;
2296 runtime_unlock(&runtime_sched
.gflock
);
2300 // Get from gfree list.
2301 // If local list is empty, grab a batch from global list.
2309 if(gp
== nil
&& runtime_sched
.gfree
) {
2310 runtime_lock(&runtime_sched
.gflock
);
2311 while(p
->gfreecnt
< 32 && runtime_sched
.gfree
) {
2313 gp
= runtime_sched
.gfree
;
2314 runtime_sched
.gfree
= gp
->schedlink
;
2315 gp
->schedlink
= p
->gfree
;
2318 runtime_unlock(&runtime_sched
.gflock
);
2322 p
->gfree
= gp
->schedlink
;
2328 // Purge all cached G's from gfree list to the global list.
2334 runtime_lock(&runtime_sched
.gflock
);
2335 while(p
->gfreecnt
) {
2338 p
->gfree
= gp
->schedlink
;
2339 gp
->schedlink
= runtime_sched
.gfree
;
2340 runtime_sched
.gfree
= gp
;
2342 runtime_unlock(&runtime_sched
.gflock
);
2346 runtime_Breakpoint(void)
2348 runtime_breakpoint();
2351 void runtime_Gosched (void) __asm__ (GOSYM_PREFIX
"runtime.Gosched");
2354 runtime_Gosched(void)
2359 // Implementation of runtime.GOMAXPROCS.
2360 // delete when scheduler is even stronger
2362 runtime_gomaxprocsfunc(int32 n
)
2366 if(n
> MaxGomaxprocs
)
2368 runtime_lock(&runtime_sched
);
2369 ret
= runtime_gomaxprocs
;
2370 if(n
<= 0 || n
== ret
) {
2371 runtime_unlock(&runtime_sched
);
2374 runtime_unlock(&runtime_sched
);
2376 runtime_semacquire(&runtime_worldsema
, false);
2378 runtime_stoptheworld();
2381 runtime_semrelease(&runtime_worldsema
);
2382 runtime_starttheworld();
2387 // lockOSThread is called by runtime.LockOSThread and runtime.lockOSThread below
2388 // after they modify m->locked. Do not allow preemption during this call,
2389 // or else the m might be different in this function than in the caller.
2397 void runtime_LockOSThread(void) __asm__ (GOSYM_PREFIX
"runtime.LockOSThread");
2399 runtime_LockOSThread(void)
2401 m
->locked
|= LockExternal
;
2406 runtime_lockOSThread(void)
2408 m
->locked
+= LockInternal
;
2413 // unlockOSThread is called by runtime.UnlockOSThread and runtime.unlockOSThread below
2414 // after they update m->locked. Do not allow preemption during this call,
2415 // or else the m might be in different in this function than in the caller.
2417 unlockOSThread(void)
2425 void runtime_UnlockOSThread(void) __asm__ (GOSYM_PREFIX
"runtime.UnlockOSThread");
2428 runtime_UnlockOSThread(void)
2430 m
->locked
&= ~LockExternal
;
2435 runtime_unlockOSThread(void)
2437 if(m
->locked
< LockInternal
)
2438 runtime_throw("runtime: internal error: misuse of lockOSThread/unlockOSThread");
2439 m
->locked
-= LockInternal
;
2444 runtime_lockedOSThread(void)
2446 return g
->lockedm
!= nil
&& m
->lockedg
!= nil
;
2450 runtime_gcount(void)
2457 runtime_lock(&allglock
);
2458 // TODO(dvyukov): runtime.NumGoroutine() is O(N).
2459 // We do not want to increment/decrement centralized counter in newproc/goexit,
2460 // just to make runtime.NumGoroutine() faster.
2461 // Compromise solution is to introduce per-P counters of active goroutines.
2462 for(i
= 0; i
< runtime_allglen
; i
++) {
2463 gp
= runtime_allg
[i
];
2465 if(s
== Grunnable
|| s
== Grunning
|| s
== Gsyscall
|| s
== Gwaiting
)
2468 runtime_unlock(&allglock
);
2473 runtime_mcount(void)
2475 return runtime_sched
.mcount
;
2480 void (*fn
)(uintptr
*, int32
);
2482 uintptr pcbuf
[TracebackMaxFrames
];
2483 Location locbuf
[TracebackMaxFrames
];
2486 static void System(void) {}
2487 static void GC(void) {}
2489 // Called if we receive a SIGPROF signal.
2497 if(prof
.fn
== nil
|| prof
.hz
== 0)
2503 // Profiling runs concurrently with GC, so it must not allocate.
2508 if(mp
->mcache
== nil
)
2511 runtime_lock(&prof
);
2512 if(prof
.fn
== nil
) {
2513 runtime_unlock(&prof
);
2519 if(runtime_atomicload(&runtime_in_callers
) > 0) {
2520 // If SIGPROF arrived while already fetching runtime
2521 // callers we can have trouble on older systems
2522 // because the unwind library calls dl_iterate_phdr
2523 // which was not recursive in the past.
2528 n
= runtime_callers(0, prof
.locbuf
, nelem(prof
.locbuf
), false);
2529 for(i
= 0; i
< n
; i
++)
2530 prof
.pcbuf
[i
] = prof
.locbuf
[i
].pc
;
2532 if(!traceback
|| n
<= 0) {
2534 prof
.pcbuf
[0] = (uintptr
)runtime_getcallerpc(&n
);
2535 if(mp
->gcing
|| mp
->helpgc
)
2536 prof
.pcbuf
[1] = (uintptr
)GC
;
2538 prof
.pcbuf
[1] = (uintptr
)System
;
2540 prof
.fn(prof
.pcbuf
, n
);
2541 runtime_unlock(&prof
);
2545 // Arrange to call fn with a traceback hz times a second.
2547 runtime_setcpuprofilerate(void (*fn
)(uintptr
*, int32
), int32 hz
)
2549 // Force sane arguments.
2557 // Disable preemption, otherwise we can be rescheduled to another thread
2558 // that has profiling enabled.
2561 // Stop profiler on this thread so that it is safe to lock prof.
2562 // if a profiling signal came in while we had prof locked,
2563 // it would deadlock.
2564 runtime_resetcpuprofiler(0);
2566 runtime_lock(&prof
);
2569 runtime_unlock(&prof
);
2570 runtime_lock(&runtime_sched
);
2571 runtime_sched
.profilehz
= hz
;
2572 runtime_unlock(&runtime_sched
);
2575 runtime_resetcpuprofiler(hz
);
2580 // Change number of processors. The world is stopped, sched is locked.
2582 procresize(int32
new)
2589 old
= runtime_gomaxprocs
;
2590 if(old
< 0 || old
> MaxGomaxprocs
|| new <= 0 || new >MaxGomaxprocs
)
2591 runtime_throw("procresize: invalid arg");
2592 // initialize new P's
2593 for(i
= 0; i
< new; i
++) {
2594 p
= runtime_allp
[i
];
2596 p
= (P
*)runtime_mallocgc(sizeof(*p
), 0, FlagNoInvokeGC
);
2598 p
->status
= Pgcstop
;
2599 runtime_atomicstorep(&runtime_allp
[i
], p
);
2601 if(p
->mcache
== nil
) {
2603 p
->mcache
= m
->mcache
; // bootstrap
2605 p
->mcache
= runtime_allocmcache();
2609 // redistribute runnable G's evenly
2610 // collect all runnable goroutines in global queue preserving FIFO order
2611 // FIFO order is required to ensure fairness even during frequent GCs
2612 // see http://golang.org/issue/7126
2616 for(i
= 0; i
< old
; i
++) {
2617 p
= runtime_allp
[i
];
2618 if(p
->runqhead
== p
->runqtail
)
2621 // pop from tail of local queue
2623 gp
= p
->runq
[p
->runqtail
%nelem(p
->runq
)];
2624 // push onto head of global queue
2625 gp
->schedlink
= runtime_sched
.runqhead
;
2626 runtime_sched
.runqhead
= gp
;
2627 if(runtime_sched
.runqtail
== nil
)
2628 runtime_sched
.runqtail
= gp
;
2629 runtime_sched
.runqsize
++;
2632 // fill local queues with at most nelem(p->runq)/2 goroutines
2633 // start at 1 because current M already executes some G and will acquire allp[0] below,
2634 // so if we have a spare G we want to put it into allp[1].
2635 for(i
= 1; (uint32
)i
< (uint32
)new * nelem(p
->runq
)/2 && runtime_sched
.runqsize
> 0; i
++) {
2636 gp
= runtime_sched
.runqhead
;
2637 runtime_sched
.runqhead
= gp
->schedlink
;
2638 if(runtime_sched
.runqhead
== nil
)
2639 runtime_sched
.runqtail
= nil
;
2640 runtime_sched
.runqsize
--;
2641 runqput(runtime_allp
[i
%new], gp
);
2645 for(i
= new; i
< old
; i
++) {
2646 p
= runtime_allp
[i
];
2647 runtime_freemcache(p
->mcache
);
2651 // can't free P itself because it can be referenced by an M in syscall
2658 p
= runtime_allp
[0];
2662 for(i
= new-1; i
> 0; i
--) {
2663 p
= runtime_allp
[i
];
2667 runtime_atomicstore((uint32
*)&runtime_gomaxprocs
, new);
2670 // Associate p and the current m.
2674 if(m
->p
|| m
->mcache
)
2675 runtime_throw("acquirep: already in go");
2676 if(p
->m
|| p
->status
!= Pidle
) {
2677 runtime_printf("acquirep: p->m=%p(%d) p->status=%d\n", p
->m
, p
->m
? p
->m
->id
: 0, p
->status
);
2678 runtime_throw("acquirep: invalid p state");
2680 m
->mcache
= p
->mcache
;
2683 p
->status
= Prunning
;
2686 // Disassociate p and the current m.
2692 if(m
->p
== nil
|| m
->mcache
== nil
)
2693 runtime_throw("releasep: invalid arg");
2695 if(p
->m
!= m
|| p
->mcache
!= m
->mcache
|| p
->status
!= Prunning
) {
2696 runtime_printf("releasep: m=%p m->p=%p p->m=%p m->mcache=%p p->mcache=%p p->status=%d\n",
2697 m
, m
->p
, p
->m
, m
->mcache
, p
->mcache
, p
->status
);
2698 runtime_throw("releasep: invalid p state");
2708 incidlelocked(int32 v
)
2710 runtime_lock(&runtime_sched
);
2711 runtime_sched
.nmidlelocked
+= v
;
2714 runtime_unlock(&runtime_sched
);
2717 // Check for deadlock situation.
2718 // The check is based on number of running M's, if 0 -> deadlock.
2723 int32 run
, grunning
, s
;
2727 run
= runtime_sched
.mcount
- runtime_sched
.nmidle
- runtime_sched
.nmidlelocked
- 1 - countextra();
2730 // If we are dying because of a signal caught on an already idle thread,
2731 // freezetheworld will cause all running threads to block.
2732 // And runtime will essentially enter into deadlock state,
2733 // except that there is a thread that will call runtime_exit soon.
2734 if(runtime_panicking
> 0)
2737 runtime_printf("runtime: checkdead: nmidle=%d nmidlelocked=%d mcount=%d\n",
2738 runtime_sched
.nmidle
, runtime_sched
.nmidlelocked
, runtime_sched
.mcount
);
2739 runtime_throw("checkdead: inconsistent counts");
2742 runtime_lock(&allglock
);
2743 for(i
= 0; i
< runtime_allglen
; i
++) {
2744 gp
= runtime_allg
[i
];
2745 if(gp
->isbackground
)
2750 else if(s
== Grunnable
|| s
== Grunning
|| s
== Gsyscall
) {
2751 runtime_unlock(&allglock
);
2752 runtime_printf("runtime: checkdead: find g %D in status %d\n", gp
->goid
, s
);
2753 runtime_throw("checkdead: runnable g");
2756 runtime_unlock(&allglock
);
2757 if(grunning
== 0) // possible if main goroutine calls runtime_Goexit()
2758 runtime_throw("no goroutines (main called runtime.Goexit) - deadlock!");
2759 m
->throwing
= -1; // do not dump full stacks
2760 runtime_throw("all goroutines are asleep - deadlock!");
2767 int64 now
, lastpoll
, lasttrace
;
2771 idle
= 0; // how many cycles in succession we had not wokeup somebody
2774 if(idle
== 0) // start with 20us sleep...
2776 else if(idle
> 50) // start doubling the sleep after 1ms...
2778 if(delay
> 10*1000) // up to 10ms
2780 runtime_usleep(delay
);
2781 if(runtime_debug
.schedtrace
<= 0 &&
2782 (runtime_sched
.gcwaiting
|| runtime_atomicload(&runtime_sched
.npidle
) == (uint32
)runtime_gomaxprocs
)) { // TODO: fast atomic
2783 runtime_lock(&runtime_sched
);
2784 if(runtime_atomicload(&runtime_sched
.gcwaiting
) || runtime_atomicload(&runtime_sched
.npidle
) == (uint32
)runtime_gomaxprocs
) {
2785 runtime_atomicstore(&runtime_sched
.sysmonwait
, 1);
2786 runtime_unlock(&runtime_sched
);
2787 runtime_notesleep(&runtime_sched
.sysmonnote
);
2788 runtime_noteclear(&runtime_sched
.sysmonnote
);
2792 runtime_unlock(&runtime_sched
);
2794 // poll network if not polled for more than 10ms
2795 lastpoll
= runtime_atomicload64(&runtime_sched
.lastpoll
);
2796 now
= runtime_nanotime();
2797 if(lastpoll
!= 0 && lastpoll
+ 10*1000*1000 < now
) {
2798 runtime_cas64(&runtime_sched
.lastpoll
, lastpoll
, now
);
2799 gp
= runtime_netpoll(false); // non-blocking
2801 // Need to decrement number of idle locked M's
2802 // (pretending that one more is running) before injectglist.
2803 // Otherwise it can lead to the following situation:
2804 // injectglist grabs all P's but before it starts M's to run the P's,
2805 // another M returns from syscall, finishes running its G,
2806 // observes that there is no work to do and no other running M's
2807 // and reports deadlock.
2813 // retake P's blocked in syscalls
2814 // and preempt long running G's
2820 if(runtime_debug
.schedtrace
> 0 && lasttrace
+ runtime_debug
.schedtrace
*1000000ll <= now
) {
2822 runtime_schedtrace(runtime_debug
.scheddetail
);
2827 typedef struct Pdesc Pdesc
;
2835 static Pdesc pdesc
[MaxGomaxprocs
];
2846 for(i
= 0; i
< (uint32
)runtime_gomaxprocs
; i
++) {
2847 p
= runtime_allp
[i
];
2853 // Retake P from syscall if it's there for more than 1 sysmon tick (at least 20us).
2855 if(pd
->syscalltick
!= t
) {
2856 pd
->syscalltick
= t
;
2857 pd
->syscallwhen
= now
;
2860 // On the one hand we don't want to retake Ps if there is no other work to do,
2861 // but on the other hand we want to retake them eventually
2862 // because they can prevent the sysmon thread from deep sleep.
2863 if(p
->runqhead
== p
->runqtail
&&
2864 runtime_atomicload(&runtime_sched
.nmspinning
) + runtime_atomicload(&runtime_sched
.npidle
) > 0 &&
2865 pd
->syscallwhen
+ 10*1000*1000 > now
)
2867 // Need to decrement number of idle locked M's
2868 // (pretending that one more is running) before the CAS.
2869 // Otherwise the M from which we retake can exit the syscall,
2870 // increment nmidle and report deadlock.
2872 if(runtime_cas(&p
->status
, s
, Pidle
)) {
2877 } else if(s
== Prunning
) {
2878 // Preempt G if it's running for more than 10ms.
2880 if(pd
->schedtick
!= t
) {
2882 pd
->schedwhen
= now
;
2885 if(pd
->schedwhen
+ 10*1000*1000 > now
)
2893 // Tell all goroutines that they have been preempted and they should stop.
2894 // This function is purely best-effort. It can fail to inform a goroutine if a
2895 // processor just started running it.
2896 // No locks need to be held.
2897 // Returns true if preemption request was issued to at least one goroutine.
2905 runtime_schedtrace(bool detailed
)
2907 static int64 starttime
;
2909 int64 id1
, id2
, id3
;
2917 now
= runtime_nanotime();
2921 runtime_lock(&runtime_sched
);
2922 runtime_printf("SCHED %Dms: gomaxprocs=%d idleprocs=%d threads=%d idlethreads=%d runqueue=%d",
2923 (now
-starttime
)/1000000, runtime_gomaxprocs
, runtime_sched
.npidle
, runtime_sched
.mcount
,
2924 runtime_sched
.nmidle
, runtime_sched
.runqsize
);
2926 runtime_printf(" gcwaiting=%d nmidlelocked=%d nmspinning=%d stopwait=%d sysmonwait=%d\n",
2927 runtime_sched
.gcwaiting
, runtime_sched
.nmidlelocked
, runtime_sched
.nmspinning
,
2928 runtime_sched
.stopwait
, runtime_sched
.sysmonwait
);
2930 // We must be careful while reading data from P's, M's and G's.
2931 // Even if we hold schedlock, most data can be changed concurrently.
2932 // E.g. (p->m ? p->m->id : -1) can crash if p->m changes from non-nil to nil.
2933 for(i
= 0; i
< runtime_gomaxprocs
; i
++) {
2934 p
= runtime_allp
[i
];
2938 h
= runtime_atomicload(&p
->runqhead
);
2939 t
= runtime_atomicload(&p
->runqtail
);
2941 runtime_printf(" P%d: status=%d schedtick=%d syscalltick=%d m=%d runqsize=%d gfreecnt=%d\n",
2942 i
, p
->status
, p
->schedtick
, p
->syscalltick
, mp
? mp
->id
: -1, t
-h
, p
->gfreecnt
);
2944 // In non-detailed mode format lengths of per-P run queues as:
2945 // [len1 len2 len3 len4]
2947 if(runtime_gomaxprocs
== 1)
2951 else if(i
== runtime_gomaxprocs
-1)
2953 runtime_printf(fmt
, t
-h
);
2957 runtime_unlock(&runtime_sched
);
2960 for(mp
= runtime_allm
; mp
; mp
= mp
->alllink
) {
2963 lockedg
= mp
->lockedg
;
2972 id3
= lockedg
->goid
;
2973 runtime_printf(" M%d: p=%D curg=%D mallocing=%d throwing=%d gcing=%d"
2974 " locks=%d dying=%d helpgc=%d spinning=%d blocked=%d lockedg=%D\n",
2976 mp
->mallocing
, mp
->throwing
, mp
->gcing
, mp
->locks
, mp
->dying
, mp
->helpgc
,
2977 mp
->spinning
, m
->blocked
, id3
);
2979 runtime_lock(&allglock
);
2980 for(gi
= 0; gi
< runtime_allglen
; gi
++) {
2981 gp
= runtime_allg
[gi
];
2983 lockedm
= gp
->lockedm
;
2984 runtime_printf(" G%D: status=%d(%s) m=%d lockedm=%d\n",
2985 gp
->goid
, gp
->status
, gp
->waitreason
, mp
? mp
->id
: -1,
2986 lockedm
? lockedm
->id
: -1);
2988 runtime_unlock(&allglock
);
2989 runtime_unlock(&runtime_sched
);
2992 // Put mp on midle list.
2993 // Sched must be locked.
2997 mp
->schedlink
= runtime_sched
.midle
;
2998 runtime_sched
.midle
= mp
;
2999 runtime_sched
.nmidle
++;
3003 // Try to get an m from midle list.
3004 // Sched must be locked.
3010 if((mp
= runtime_sched
.midle
) != nil
){
3011 runtime_sched
.midle
= mp
->schedlink
;
3012 runtime_sched
.nmidle
--;
3017 // Put gp on the global runnable queue.
3018 // Sched must be locked.
3022 gp
->schedlink
= nil
;
3023 if(runtime_sched
.runqtail
)
3024 runtime_sched
.runqtail
->schedlink
= gp
;
3026 runtime_sched
.runqhead
= gp
;
3027 runtime_sched
.runqtail
= gp
;
3028 runtime_sched
.runqsize
++;
3031 // Put a batch of runnable goroutines on the global runnable queue.
3032 // Sched must be locked.
3034 globrunqputbatch(G
*ghead
, G
*gtail
, int32 n
)
3036 gtail
->schedlink
= nil
;
3037 if(runtime_sched
.runqtail
)
3038 runtime_sched
.runqtail
->schedlink
= ghead
;
3040 runtime_sched
.runqhead
= ghead
;
3041 runtime_sched
.runqtail
= gtail
;
3042 runtime_sched
.runqsize
+= n
;
3045 // Try get a batch of G's from the global runnable queue.
3046 // Sched must be locked.
3048 globrunqget(P
*p
, int32 max
)
3053 if(runtime_sched
.runqsize
== 0)
3055 n
= runtime_sched
.runqsize
/runtime_gomaxprocs
+1;
3056 if(n
> runtime_sched
.runqsize
)
3057 n
= runtime_sched
.runqsize
;
3058 if(max
> 0 && n
> max
)
3060 if((uint32
)n
> nelem(p
->runq
)/2)
3061 n
= nelem(p
->runq
)/2;
3062 runtime_sched
.runqsize
-= n
;
3063 if(runtime_sched
.runqsize
== 0)
3064 runtime_sched
.runqtail
= nil
;
3065 gp
= runtime_sched
.runqhead
;
3066 runtime_sched
.runqhead
= gp
->schedlink
;
3069 gp1
= runtime_sched
.runqhead
;
3070 runtime_sched
.runqhead
= gp1
->schedlink
;
3076 // Put p to on pidle list.
3077 // Sched must be locked.
3081 p
->link
= runtime_sched
.pidle
;
3082 runtime_sched
.pidle
= p
;
3083 runtime_xadd(&runtime_sched
.npidle
, 1); // TODO: fast atomic
3086 // Try get a p from pidle list.
3087 // Sched must be locked.
3093 p
= runtime_sched
.pidle
;
3095 runtime_sched
.pidle
= p
->link
;
3096 runtime_xadd(&runtime_sched
.npidle
, -1); // TODO: fast atomic
3101 // Try to put g on local runnable queue.
3102 // If it's full, put onto global queue.
3103 // Executed only by the owner P.
3105 runqput(P
*p
, G
*gp
)
3110 h
= runtime_atomicload(&p
->runqhead
); // load-acquire, synchronize with consumers
3112 if(t
- h
< nelem(p
->runq
)) {
3113 p
->runq
[t
%nelem(p
->runq
)] = gp
;
3114 runtime_atomicstore(&p
->runqtail
, t
+1); // store-release, makes the item available for consumption
3117 if(runqputslow(p
, gp
, h
, t
))
3119 // the queue is not full, now the put above must suceed
3123 // Put g and a batch of work from local runnable queue on global queue.
3124 // Executed only by the owner P.
3126 runqputslow(P
*p
, G
*gp
, uint32 h
, uint32 t
)
3128 G
*batch
[nelem(p
->runq
)/2+1];
3131 // First, grab a batch from local queue.
3134 if(n
!= nelem(p
->runq
)/2)
3135 runtime_throw("runqputslow: queue is not full");
3137 batch
[i
] = p
->runq
[(h
+i
)%nelem(p
->runq
)];
3138 if(!runtime_cas(&p
->runqhead
, h
, h
+n
)) // cas-release, commits consume
3141 // Link the goroutines.
3143 batch
[i
]->schedlink
= batch
[i
+1];
3144 // Now put the batch on global queue.
3145 runtime_lock(&runtime_sched
);
3146 globrunqputbatch(batch
[0], batch
[n
], n
+1);
3147 runtime_unlock(&runtime_sched
);
3151 // Get g from local runnable queue.
3152 // Executed only by the owner P.
3160 h
= runtime_atomicload(&p
->runqhead
); // load-acquire, synchronize with other consumers
3164 gp
= p
->runq
[h
%nelem(p
->runq
)];
3165 if(runtime_cas(&p
->runqhead
, h
, h
+1)) // cas-release, commits consume
3170 // Grabs a batch of goroutines from local runnable queue.
3171 // batch array must be of size nelem(p->runq)/2. Returns number of grabbed goroutines.
3172 // Can be executed by any P.
3174 runqgrab(P
*p
, G
**batch
)
3179 h
= runtime_atomicload(&p
->runqhead
); // load-acquire, synchronize with other consumers
3180 t
= runtime_atomicload(&p
->runqtail
); // load-acquire, synchronize with the producer
3185 if(n
> nelem(p
->runq
)/2) // read inconsistent h and t
3188 batch
[i
] = p
->runq
[(h
+i
)%nelem(p
->runq
)];
3189 if(runtime_cas(&p
->runqhead
, h
, h
+n
)) // cas-release, commits consume
3195 // Steal half of elements from local runnable queue of p2
3196 // and put onto local runnable queue of p.
3197 // Returns one of the stolen elements (or nil if failed).
3199 runqsteal(P
*p
, P
*p2
)
3202 G
*batch
[nelem(p
->runq
)/2];
3205 n
= runqgrab(p2
, batch
);
3212 h
= runtime_atomicload(&p
->runqhead
); // load-acquire, synchronize with consumers
3214 if(t
- h
+ n
>= nelem(p
->runq
))
3215 runtime_throw("runqsteal: runq overflow");
3216 for(i
=0; i
<n
; i
++, t
++)
3217 p
->runq
[t
%nelem(p
->runq
)] = batch
[i
];
3218 runtime_atomicstore(&p
->runqtail
, t
); // store-release, makes the item available for consumption
3222 void runtime_testSchedLocalQueue(void)
3223 __asm__("runtime.testSchedLocalQueue");
3226 runtime_testSchedLocalQueue(void)
3229 G gs
[nelem(p
.runq
)];
3232 runtime_memclr((byte
*)&p
, sizeof(p
));
3234 for(i
= 0; i
< (int32
)nelem(gs
); i
++) {
3235 if(runqget(&p
) != nil
)
3236 runtime_throw("runq is not empty initially");
3237 for(j
= 0; j
< i
; j
++)
3238 runqput(&p
, &gs
[i
]);
3239 for(j
= 0; j
< i
; j
++) {
3240 if(runqget(&p
) != &gs
[i
]) {
3241 runtime_printf("bad element at iter %d/%d\n", i
, j
);
3242 runtime_throw("bad element");
3245 if(runqget(&p
) != nil
)
3246 runtime_throw("runq is not empty afterwards");
3250 void runtime_testSchedLocalQueueSteal(void)
3251 __asm__("runtime.testSchedLocalQueueSteal");
3254 runtime_testSchedLocalQueueSteal(void)
3257 G gs
[nelem(p1
.runq
)], *gp
;
3260 runtime_memclr((byte
*)&p1
, sizeof(p1
));
3261 runtime_memclr((byte
*)&p2
, sizeof(p2
));
3263 for(i
= 0; i
< (int32
)nelem(gs
); i
++) {
3264 for(j
= 0; j
< i
; j
++) {
3266 runqput(&p1
, &gs
[j
]);
3268 gp
= runqsteal(&p2
, &p1
);
3274 while((gp
= runqget(&p2
)) != nil
) {
3278 while((gp
= runqget(&p1
)) != nil
)
3280 for(j
= 0; j
< i
; j
++) {
3281 if(gs
[j
].sig
!= 1) {
3282 runtime_printf("bad element %d(%d) at iter %d\n", j
, gs
[j
].sig
, i
);
3283 runtime_throw("bad element");
3286 if(s
!= i
/2 && s
!= i
/2+1) {
3287 runtime_printf("bad steal %d, want %d or %d, iter %d\n",
3289 runtime_throw("bad steal");
3295 runtime_setmaxthreads(int32 in
)
3299 runtime_lock(&runtime_sched
);
3300 out
= runtime_sched
.maxmcount
;
3301 runtime_sched
.maxmcount
= in
;
3303 runtime_unlock(&runtime_sched
);
3308 runtime_proc_scan(struct Workbuf
** wbufp
, void (*enqueue1
)(struct Workbuf
**, Obj
))
3310 enqueue1(wbufp
, (Obj
){(byte
*)&runtime_sched
, sizeof runtime_sched
, 0});
3313 // When a function calls a closure, it passes the closure value to
3314 // __go_set_closure immediately before the function call. When a
3315 // function uses a closure, it calls __go_get_closure immediately on
3316 // function entry. This is a hack, but it will work on any system.
3317 // It would be better to use the static chain register when there is
3318 // one. It is also worth considering expanding these functions
3319 // directly in the compiler.
3322 __go_set_closure(void* v
)
3328 __go_get_closure(void)
3333 // Return whether we are waiting for a GC. This gc toolchain uses
3334 // preemption instead.
3336 runtime_gcwaiting(void)
3338 return runtime_sched
.gcwaiting
;