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.
12 #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 2 * 1024 * 1024
56 uintptr runtime_stacks_sys
;
58 static void schedule(G
*);
60 static void gtraceback(G
*);
62 typedef struct Sched Sched
;
65 G runtime_g0
; // idle goroutine for m0
74 #ifndef SETCONTEXT_CLOBBERS_TLS
82 fixcontext(ucontext_t
*c
__attribute__ ((unused
)))
88 # if defined(__x86_64__) && defined(__sun__)
90 // x86_64 Solaris 10 and 11 have a bug: setcontext switches the %fs
91 // register to that of the thread which called getcontext. The effect
92 // is that the address of all __thread variables changes. This bug
93 // also affects pthread_self() and pthread_getspecific. We work
94 // around it by clobbering the context field directly to keep %fs the
97 static __thread greg_t fs
;
105 fs
= c
.uc_mcontext
.gregs
[REG_FSBASE
];
109 fixcontext(ucontext_t
* c
)
111 c
->uc_mcontext
.gregs
[REG_FSBASE
] = fs
;
114 # elif defined(__NetBSD__)
116 // NetBSD has a bug: setcontext clobbers tlsbase, we need to save
117 // and restore it ourselves.
119 static __thread __greg_t tlsbase
;
127 tlsbase
= c
.uc_mcontext
._mc_tlsbase
;
131 fixcontext(ucontext_t
* c
)
133 c
->uc_mcontext
._mc_tlsbase
= tlsbase
;
138 # error unknown case for SETCONTEXT_CLOBBERS_TLS
144 // We can not always refer to the TLS variables directly. The
145 // compiler will call tls_get_addr to get the address of the variable,
146 // and it may hold it in a register across a call to schedule. When
147 // we get back from the call we may be running in a different thread,
148 // in which case the register now points to the TLS variable for a
149 // different thread. We use non-inlinable functions to avoid this
152 G
* runtime_g(void) __attribute__ ((noinline
, no_split_stack
));
160 M
* runtime_m(void) __attribute__ ((noinline
, no_split_stack
));
168 int32 runtime_gcwaiting
;
177 // The static TLS size. See runtime_newm.
180 #ifdef HAVE_DL_ITERATE_PHDR
182 // Called via dl_iterate_phdr.
185 addtls(struct dl_phdr_info
* info
, size_t size
__attribute__ ((unused
)), void *data
)
187 size_t *total
= (size_t *)data
;
190 for(i
= 0; i
< info
->dlpi_phnum
; ++i
) {
191 if(info
->dlpi_phdr
[i
].p_type
== PT_TLS
)
192 *total
+= info
->dlpi_phdr
[i
].p_memsz
;
197 // Set the total TLS size.
204 dl_iterate_phdr(addtls
, (void *)&total
);
219 // The go scheduler's job is to match ready-to-run goroutines (`g's)
220 // with waiting-for-work schedulers (`m's). If there are ready g's
221 // and no waiting m's, ready() will start a new m running in a new
222 // OS thread, so that all ready g's can run simultaneously, up to a limit.
223 // For now, m's never go away.
225 // By default, Go keeps only one kernel thread (m) running user code
226 // at a single time; other threads may be blocked in the operating system.
227 // Setting the environment variable $GOMAXPROCS or calling
228 // runtime.GOMAXPROCS() will change the number of user threads
229 // allowed to execute simultaneously. $GOMAXPROCS is thus an
230 // approximation of the maximum number of cores to use.
232 // Even a program that can run without deadlock in a single process
233 // might use more m's if given the chance. For example, the prime
234 // sieve will use as many m's as there are primes (up to runtime_sched.mmax),
235 // allowing different stages of the pipeline to execute in parallel.
236 // We could revisit this choice, only kicking off new m's for blocking
237 // system calls, but that would limit the amount of parallel computation
238 // that go would try to do.
240 // In general, one could imagine all sorts of refinements to the
241 // scheduler, but the goal now is just to get something working on
247 G
*gfree
; // available g's (status == Gdead)
250 G
*ghead
; // g's waiting to run
252 int32 gwait
; // number of g's waiting to run
253 int32 gcount
; // number of g's that are alive
254 int32 grunning
; // number of g's running on cpu or in syscall
256 M
*mhead
; // m's waiting for work
257 int32 mwait
; // number of m's waiting for work
258 int32 mcount
; // number of m's that have been created
260 volatile uint32 atomic
; // atomic scheduling word (see below)
262 int32 profilehz
; // cpu profiling rate
264 bool init
; // running initialization
265 bool lockmain
; // init called runtime.LockOSThread
267 Note stopped
; // one g can set waitstop and wait here for m's to stop
270 // The atomic word in sched is an atomic uint32 that
271 // holds these fields.
273 // [15 bits] mcpu number of m's executing on cpu
274 // [15 bits] mcpumax max number of m's allowed on cpu
275 // [1 bit] waitstop some g is waiting on stopped
276 // [1 bit] gwaiting gwait != 0
278 // These fields are the information needed by entersyscall
279 // and exitsyscall to decide whether to coordinate with the
280 // scheduler. Packing them into a single machine word lets
281 // them use a fast path with a single atomic read/write and
282 // no lock/unlock. This greatly reduces contention in
283 // syscall- or cgo-heavy multithreaded programs.
285 // Except for entersyscall and exitsyscall, the manipulations
286 // to these fields only happen while holding the schedlock,
287 // so the routines holding schedlock only need to worry about
288 // what entersyscall and exitsyscall do, not the other routines
289 // (which also use the schedlock).
291 // In particular, entersyscall and exitsyscall only read mcpumax,
292 // waitstop, and gwaiting. They never write them. Thus, writes to those
293 // fields can be done (holding schedlock) without fear of write conflicts.
294 // There may still be logic conflicts: for example, the set of waitstop must
295 // be conditioned on mcpu >= mcpumax or else the wait may be a
296 // spurious sleep. The Promela model in proc.p verifies these accesses.
299 mcpuMask
= (1<<mcpuWidth
) - 1,
301 mcpumaxShift
= mcpuShift
+ mcpuWidth
,
302 waitstopShift
= mcpumaxShift
+ mcpuWidth
,
303 gwaitingShift
= waitstopShift
+1,
305 // The max value of GOMAXPROCS is constrained
306 // by the max value we can store in the bit fields
307 // of the atomic word. Reserve a few high values
308 // so that we can detect accidental decrement
310 maxgomaxprocs
= mcpuMask
- 10,
313 #define atomic_mcpu(v) (((v)>>mcpuShift)&mcpuMask)
314 #define atomic_mcpumax(v) (((v)>>mcpumaxShift)&mcpuMask)
315 #define atomic_waitstop(v) (((v)>>waitstopShift)&1)
316 #define atomic_gwaiting(v) (((v)>>gwaitingShift)&1)
319 int32 runtime_gomaxprocs
;
320 bool runtime_singleproc
;
322 static bool canaddmcpu(void);
324 // An m that is waiting for notewakeup(&m->havenextg). This may
325 // only be accessed while the scheduler lock is held. This is used to
326 // minimize the number of times we call notewakeup while the scheduler
327 // lock is held, since the m will normally move quickly to lock the
328 // scheduler itself, producing lock contention.
331 // Scheduling helpers. Sched must be locked.
332 static void gput(G
*); // put/get on ghead/gtail
333 static G
* gget(void);
334 static void mput(M
*); // put/get on mhead
336 static void gfput(G
*); // put/get on gfree
337 static G
* gfget(void);
338 static void matchmg(void); // match m's to g's
339 static void readylocked(G
*); // ready, but sched is locked
340 static void mnextg(M
*, G
*);
341 static void mcommoninit(M
*);
349 v
= runtime_sched
.atomic
;
351 w
&= ~(mcpuMask
<<mcpumaxShift
);
352 w
|= n
<<mcpumaxShift
;
353 if(runtime_cas(&runtime_sched
.atomic
, v
, w
))
358 // First function run by a new goroutine. This replaces gogocall.
364 if(g
->traceback
!= nil
)
367 fn
= (void (*)(void*))(g
->entry
);
372 // Switch context to a different goroutine. This is like longjmp.
373 static void runtime_gogo(G
*) __attribute__ ((noinline
));
375 runtime_gogo(G
* newg
)
377 #ifdef USING_SPLIT_STACK
378 __splitstack_setcontext(&newg
->stack_context
[0]);
381 newg
->fromgogo
= true;
382 fixcontext(&newg
->context
);
383 setcontext(&newg
->context
);
384 runtime_throw("gogo setcontext returned");
387 // Save context and call fn passing g as a parameter. This is like
388 // setjmp. Because getcontext always returns 0, unlike setjmp, we use
389 // g->fromgogo as a code. It will be true if we got here via
390 // setcontext. g == nil the first time this is called in a new m.
391 static void runtime_mcall(void (*)(G
*)) __attribute__ ((noinline
));
393 runtime_mcall(void (*pfn
)(G
*))
397 #ifndef USING_SPLIT_STACK
401 // Ensure that all registers are on the stack for the garbage
403 __builtin_unwind_init();
408 runtime_throw("runtime: mcall called on m->g0 stack");
412 #ifdef USING_SPLIT_STACK
413 __splitstack_getcontext(&g
->stack_context
[0]);
417 gp
->fromgogo
= false;
418 getcontext(&gp
->context
);
420 // When we return from getcontext, we may be running
421 // in a new thread. That means that m and g may have
422 // changed. They are global variables so we will
423 // reload them, but the addresses of m and g may be
424 // cached in our local stack frame, and those
425 // addresses may be wrong. Call functions to reload
426 // the values for this thread.
430 if(gp
->traceback
!= nil
)
433 if (gp
== nil
|| !gp
->fromgogo
) {
434 #ifdef USING_SPLIT_STACK
435 __splitstack_setcontext(&mp
->g0
->stack_context
[0]);
437 mp
->g0
->entry
= (byte
*)pfn
;
440 // It's OK to set g directly here because this case
441 // can not occur if we got here via a setcontext to
442 // the getcontext call just above.
445 fixcontext(&mp
->g0
->context
);
446 setcontext(&mp
->g0
->context
);
447 runtime_throw("runtime: mcall function returned");
451 // Keep trace of scavenger's goroutine for deadlock detection.
454 // The bootstrap sequence is:
458 // make & queue new G
459 // call runtime_mstart
461 // The new G calls runtime_main.
463 runtime_schedinit(void)
478 runtime_mallocinit();
485 // Allocate internal symbol table representation now,
486 // so that we don't need to call malloc when we crash.
487 // runtime_findfunc(0);
489 runtime_gomaxprocs
= 1;
490 p
= runtime_getenv("GOMAXPROCS");
491 if(p
!= nil
&& (n
= runtime_atoi(p
)) != 0) {
492 if(n
> maxgomaxprocs
)
494 runtime_gomaxprocs
= n
;
496 // wait for the main goroutine to start before taking
497 // GOMAXPROCS into account.
499 runtime_singleproc
= runtime_gomaxprocs
== 1;
501 canaddmcpu(); // mcpu++ to account for bootstrap m
502 m
->helpgc
= 1; // flag to tell schedule() to mcpu--
503 runtime_sched
.grunning
++;
505 // Can not enable GC until all roots are registered.
506 // mstats.enablegc = 1;
513 extern void main_init(void) __asm__ (GOSYM_PREFIX
"__go_init_main");
514 extern void main_main(void) __asm__ (GOSYM_PREFIX
"main.main");
516 // The main goroutine.
520 // Lock the main goroutine onto this, the main OS thread,
521 // during initialization. Most programs won't care, but a few
522 // do require certain calls to be made by the main thread.
523 // Those can arrange for main.main to run in the main thread
524 // by calling runtime.LockOSThread during initialization
525 // to preserve the lock.
526 runtime_LockOSThread();
527 // From now on, newgoroutines may use non-main threads.
528 setmcpumax(runtime_gomaxprocs
);
529 runtime_sched
.init
= true;
530 scvg
= __go_go(runtime_MHeap_Scavenger
, nil
);
532 runtime_sched
.init
= false;
533 if(!runtime_sched
.lockmain
)
534 runtime_UnlockOSThread();
536 // For gccgo we have to wait until after main is initialized
537 // to enable GC, because initializing main registers the GC
541 // The deadlock detection has false negatives.
542 // Let scvg start up, to eliminate the false negative
543 // for the trivial program func main() { select{} }.
554 // Lock the scheduler.
558 runtime_lock(&runtime_sched
);
561 // Unlock the scheduler.
569 runtime_unlock(&runtime_sched
);
571 runtime_notewakeup(&mp
->havenextg
);
577 g
->status
= Gmoribund
;
582 runtime_goroutineheader(G
*gp
)
601 status
= gp
->waitreason
;
612 runtime_printf("goroutine %D [%s]:\n", gp
->goid
, status
);
616 runtime_goroutinetrailer(G
*g
)
618 if(g
!= nil
&& g
->gopc
!= 0 && g
->goid
!= 1) {
623 if(__go_file_line(g
->gopc
- 1, &fn
, &file
, &line
)) {
624 runtime_printf("created by %S\n", fn
);
625 runtime_printf("\t%S:%D\n", file
, (int64
) line
);
638 runtime_tracebackothers(G
* volatile me
)
644 for(gp
= runtime_allg
; gp
!= nil
; gp
= gp
->alllink
) {
645 if(gp
== me
|| gp
->status
== Gdead
)
647 runtime_printf("\n");
648 runtime_goroutineheader(gp
);
650 // Our only mechanism for doing a stack trace is
651 // _Unwind_Backtrace. And that only works for the
652 // current thread, not for other random goroutines.
653 // So we need to switch context to the goroutine, get
654 // the backtrace, and then switch back.
656 // This means that if g is running or in a syscall, we
657 // can't reliably print a stack trace. FIXME.
658 if(gp
->status
== Gsyscall
|| gp
->status
== Grunning
) {
659 runtime_printf("no stack trace available\n");
660 runtime_goroutinetrailer(gp
);
664 gp
->traceback
= &traceback
;
666 #ifdef USING_SPLIT_STACK
667 __splitstack_getcontext(&me
->stack_context
[0]);
669 getcontext(&me
->context
);
671 if(gp
->traceback
!= nil
) {
675 runtime_printtrace(traceback
.pcbuf
, traceback
.c
);
676 runtime_goroutinetrailer(gp
);
680 // Do a stack trace of gp, and then restore the context to
686 Traceback
* traceback
;
688 traceback
= gp
->traceback
;
690 traceback
->c
= runtime_callers(1, traceback
->pcbuf
,
691 sizeof traceback
->pcbuf
/ sizeof traceback
->pcbuf
[0]);
692 runtime_gogo(traceback
->gp
);
695 // Mark this g as m's idle goroutine.
696 // This functionality might be used in environments where programs
697 // are limited to a single thread, to simulate a select-driven
698 // network server. It is not exposed via the standard runtime API.
700 runtime_idlegoroutine(void)
703 runtime_throw("g is already an idle goroutine");
710 mp
->id
= runtime_sched
.mcount
++;
711 mp
->fastrand
= 0x49f6428aUL
+ mp
->id
+ runtime_cputicks();
713 if(mp
->mcache
== nil
)
714 mp
->mcache
= runtime_allocmcache();
716 runtime_callers(1, mp
->createstack
, nelem(mp
->createstack
));
718 // Add to runtime_allm so garbage collector doesn't free m
719 // when it is just in a register or thread-local storage.
720 mp
->alllink
= runtime_allm
;
721 // runtime_NumCgoCall() iterates over allm w/o schedlock,
722 // so we need to publish it safely.
723 runtime_atomicstorep(&runtime_allm
, mp
);
726 // Try to increment mcpu. Report whether succeeded.
733 v
= runtime_sched
.atomic
;
734 if(atomic_mcpu(v
) >= atomic_mcpumax(v
))
736 if(runtime_cas(&runtime_sched
.atomic
, v
, v
+(1<<mcpuShift
)))
741 // Put on `g' queue. Sched must be locked.
747 // If g is wired, hand it off directly.
748 if((mp
= gp
->lockedm
) != nil
&& canaddmcpu()) {
753 // If g is the idle goroutine for an m, hand it off.
754 if(gp
->idlem
!= nil
) {
755 if(gp
->idlem
->idleg
!= nil
) {
756 runtime_printf("m%d idle out of sync: g%D g%D\n",
758 gp
->idlem
->idleg
->goid
, gp
->goid
);
759 runtime_throw("runtime: double idle");
761 gp
->idlem
->idleg
= gp
;
766 if(runtime_sched
.ghead
== nil
)
767 runtime_sched
.ghead
= gp
;
769 runtime_sched
.gtail
->schedlink
= gp
;
770 runtime_sched
.gtail
= gp
;
773 // if it transitions to nonzero, set atomic gwaiting bit.
774 if(runtime_sched
.gwait
++ == 0)
775 runtime_xadd(&runtime_sched
.atomic
, 1<<gwaitingShift
);
778 // Report whether gget would return something.
782 return runtime_sched
.ghead
!= nil
|| m
->idleg
!= nil
;
785 // Get from `g' queue. Sched must be locked.
791 gp
= runtime_sched
.ghead
;
793 runtime_sched
.ghead
= gp
->schedlink
;
794 if(runtime_sched
.ghead
== nil
)
795 runtime_sched
.gtail
= nil
;
797 // if it transitions to zero, clear atomic gwaiting bit.
798 if(--runtime_sched
.gwait
== 0)
799 runtime_xadd(&runtime_sched
.atomic
, -1<<gwaitingShift
);
800 } else if(m
->idleg
!= nil
) {
807 // Put on `m' list. Sched must be locked.
811 mp
->schedlink
= runtime_sched
.mhead
;
812 runtime_sched
.mhead
= mp
;
813 runtime_sched
.mwait
++;
816 // Get an `m' to run `g'. Sched must be locked.
822 // if g has its own m, use it.
823 if(gp
&& (mp
= gp
->lockedm
) != nil
)
826 // otherwise use general m pool.
827 if((mp
= runtime_sched
.mhead
) != nil
) {
828 runtime_sched
.mhead
= mp
->schedlink
;
829 runtime_sched
.mwait
--;
834 // Mark g ready to run.
843 // Mark g ready to run. Sched is already locked.
844 // G might be running already and about to stop.
845 // The sched lock protects g->status from changing underfoot.
850 // Running on another machine.
851 // Ready it when it stops.
857 if(gp
->status
== Grunnable
|| gp
->status
== Grunning
) {
858 runtime_printf("goroutine %D has status %d\n", gp
->goid
, gp
->status
);
859 runtime_throw("bad g->status in ready");
861 gp
->status
= Grunnable
;
867 // Same as readylocked but a different symbol so that
868 // debuggers can set a breakpoint here and catch all
871 newprocreadylocked(G
*gp
)
876 // Pass g to m for running.
877 // Caller has already incremented mcpu.
881 runtime_sched
.grunning
++;
886 runtime_notewakeup(&mwakeup
->havenextg
);
891 // Get the next goroutine that m should run.
892 // Sched must be locked on entry, is unlocked on exit.
893 // Makes sure that at most $GOMAXPROCS g's are
894 // running on cpus (not in system calls) at any given time.
902 if(atomic_mcpu(runtime_sched
.atomic
) >= maxgomaxprocs
)
903 runtime_throw("negative mcpu");
905 // If there is a g waiting as m->nextg, the mcpu++
906 // happened before it was passed to mnextg.
907 if(m
->nextg
!= nil
) {
914 if(m
->lockedg
!= nil
) {
915 // We can only run one g, and it's not available.
916 // Make sure some other cpu is running to handle
917 // the ordinary run queue.
918 if(runtime_sched
.gwait
!= 0) {
920 // m->lockedg might have been on the queue.
921 if(m
->nextg
!= nil
) {
929 // Look for work on global queue.
930 while(haveg() && canaddmcpu()) {
933 runtime_throw("gget inconsistency");
936 mnextg(gp
->lockedm
, gp
);
939 runtime_sched
.grunning
++;
944 // The while loop ended either because the g queue is empty
945 // or because we have maxed out our m procs running go
946 // code (mcpu >= mcpumax). We need to check that
947 // concurrent actions by entersyscall/exitsyscall cannot
948 // invalidate the decision to end the loop.
950 // We hold the sched lock, so no one else is manipulating the
951 // g queue or changing mcpumax. Entersyscall can decrement
952 // mcpu, but if does so when there is something on the g queue,
953 // the gwait bit will be set, so entersyscall will take the slow path
954 // and use the sched lock. So it cannot invalidate our decision.
956 // Wait on global m queue.
960 // Look for deadlock situation.
961 // There is a race with the scavenger that causes false negatives:
962 // if the scavenger is just starting, then we have
963 // scvg != nil && grunning == 0 && gwait == 0
964 // and we do not detect a deadlock. It is possible that we should
965 // add that case to the if statement here, but it is too close to Go 1
966 // to make such a subtle change. Instead, we work around the
967 // false negative in trivial programs by calling runtime.gosched
968 // from the main goroutine just before main.main.
969 // See runtime_main above.
971 // On a related note, it is also possible that the scvg == nil case is
972 // wrong and should include gwait, but that does not happen in
973 // standard Go programs, which all start the scavenger.
975 if((scvg
== nil
&& runtime_sched
.grunning
== 0) ||
976 (scvg
!= nil
&& runtime_sched
.grunning
== 1 && runtime_sched
.gwait
== 0 &&
977 (scvg
->status
== Grunning
|| scvg
->status
== Gsyscall
))) {
978 runtime_throw("all goroutines are asleep - deadlock!");
983 runtime_noteclear(&m
->havenextg
);
985 // Stoptheworld is waiting for all but its cpu to go to stop.
986 // Entersyscall might have decremented mcpu too, but if so
987 // it will see the waitstop and take the slow path.
988 // Exitsyscall never increments mcpu beyond mcpumax.
989 v
= runtime_atomicload(&runtime_sched
.atomic
);
990 if(atomic_waitstop(v
) && atomic_mcpu(v
) <= atomic_mcpumax(v
)) {
991 // set waitstop = 0 (known to be 1)
992 runtime_xadd(&runtime_sched
.atomic
, -1<<waitstopShift
);
993 runtime_notewakeup(&runtime_sched
.stopped
);
997 runtime_notesleep(&m
->havenextg
);
1001 runtime_lock(&runtime_sched
);
1004 if((gp
= m
->nextg
) == nil
)
1005 runtime_throw("bad m->nextg in nextgoroutine");
1011 runtime_gcprocs(void)
1015 // Figure out how many CPUs to use during GC.
1016 // Limited by gomaxprocs, number of actual CPUs, and MaxGcproc.
1017 n
= runtime_gomaxprocs
;
1018 if(n
> runtime_ncpu
)
1019 n
= runtime_ncpu
> 0 ? runtime_ncpu
: 1;
1022 if(n
> runtime_sched
.mwait
+1) // one M is currently running
1023 n
= runtime_sched
.mwait
+1;
1028 runtime_helpgc(int32 nproc
)
1033 runtime_lock(&runtime_sched
);
1034 for(n
= 1; n
< nproc
; n
++) { // one M is currently running
1037 runtime_throw("runtime_gcprocs inconsistency");
1040 runtime_notewakeup(&mp
->havenextg
);
1042 runtime_unlock(&runtime_sched
);
1046 runtime_stoptheworld(void)
1051 runtime_gcwaiting
= 1;
1057 v
= runtime_sched
.atomic
;
1058 if(atomic_mcpu(v
) <= 1)
1061 // It would be unsafe for multiple threads to be using
1062 // the stopped note at once, but there is only
1063 // ever one thread doing garbage collection.
1064 runtime_noteclear(&runtime_sched
.stopped
);
1065 if(atomic_waitstop(v
))
1066 runtime_throw("invalid waitstop");
1068 // atomic { waitstop = 1 }, predicated on mcpu <= 1 check above
1069 // still being true.
1070 if(!runtime_cas(&runtime_sched
.atomic
, v
, v
+(1<<waitstopShift
)))
1074 runtime_notesleep(&runtime_sched
.stopped
);
1077 runtime_singleproc
= runtime_gomaxprocs
== 1;
1082 runtime_starttheworld(void)
1087 // Figure out how many CPUs GC could possibly use.
1088 max
= runtime_gomaxprocs
;
1089 if(max
> runtime_ncpu
)
1090 max
= runtime_ncpu
> 0 ? runtime_ncpu
: 1;
1095 runtime_gcwaiting
= 0;
1096 setmcpumax(runtime_gomaxprocs
);
1098 if(runtime_gcprocs() < max
&& canaddmcpu()) {
1099 // If GC could have used another helper proc, start one now,
1100 // in the hope that it will be available next time.
1101 // It would have been even better to start it before the collection,
1102 // but doing so requires allocating memory, so it's tricky to
1103 // coordinate. This lazy approach works out in practice:
1104 // we don't mind if the first couple gc rounds don't have quite
1105 // the maximum number of procs.
1106 // canaddmcpu above did mcpu++
1107 // (necessary, because m will be doing various
1108 // initialization work so is definitely running),
1109 // but m is not running a specific goroutine,
1110 // so set the helpgc flag as a signal to m's
1111 // first schedule(nil) to mcpu-- and grunning--.
1112 mp
= runtime_newm();
1114 runtime_sched
.grunning
++;
1119 // Called to start an M.
1121 runtime_mstart(void* mp
)
1131 // Record top of stack for use by mcall.
1132 // Once we call schedule we're never coming back,
1133 // so other calls can reuse this stack space.
1134 #ifdef USING_SPLIT_STACK
1135 __splitstack_getcontext(&g
->stack_context
[0]);
1137 g
->gcinitial_sp
= &mp
;
1138 // Setting gcstack_size to 0 is a marker meaning that gcinitial_sp
1139 // is the top of the stack, not the bottom.
1140 g
->gcstack_size
= 0;
1143 getcontext(&g
->context
);
1145 if(g
->entry
!= nil
) {
1146 // Got here from mcall.
1147 void (*pfn
)(G
*) = (void (*)(G
*))g
->entry
;
1148 G
* gp
= (G
*)g
->param
;
1154 #ifdef USING_SPLIT_STACK
1156 int dont_block_signals
= 0;
1157 __splitstack_block_signals(&dont_block_signals
, nil
);
1161 // Install signal handlers; after minit so that minit can
1162 // prepare the thread to be able to handle the signals.
1163 if(m
== &runtime_m0
)
1168 // TODO(brainman): This point is never reached, because scheduler
1169 // does not release os threads at the moment. But once this path
1170 // is enabled, we must remove our seh here.
1175 typedef struct CgoThreadStart CgoThreadStart
;
1176 struct CgoThreadStart
1183 // Kick off new m's as needed (up to mcpumax).
1191 if(m
->mallocing
|| m
->gcing
)
1194 while(haveg() && canaddmcpu()) {
1197 runtime_throw("gget inconsistency");
1199 // Find the m that will run gp.
1200 if((mp
= mget(gp
)) == nil
)
1201 mp
= runtime_newm();
1206 // Create a new m. It will start off with a call to runtime_mstart.
1211 pthread_attr_t attr
;
1216 static const Type
*mtype
; // The Go type M
1219 runtime_gc_m_ptr(&e
);
1220 mtype
= ((const PtrType
*)e
.__type_descriptor
)->__element_type
;
1224 mp
= runtime_mal(sizeof *mp
);
1226 mp
->g0
= runtime_malg(-1, nil
, nil
);
1228 if(pthread_attr_init(&attr
) != 0)
1229 runtime_throw("pthread_attr_init");
1230 if(pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
) != 0)
1231 runtime_throw("pthread_attr_setdetachstate");
1233 stacksize
= PTHREAD_STACK_MIN
;
1235 // With glibc before version 2.16 the static TLS size is taken
1236 // out of the stack size, and we get an error or a crash if
1237 // there is not enough stack space left. Add it back in if we
1238 // can, in case the program uses a lot of TLS space. FIXME:
1239 // This can be disabled in glibc 2.16 and later, if the bug is
1240 // indeed fixed then.
1241 stacksize
+= tlssize
;
1243 if(pthread_attr_setstacksize(&attr
, stacksize
) != 0)
1244 runtime_throw("pthread_attr_setstacksize");
1246 if(pthread_create(&tid
, &attr
, runtime_mstart
, mp
) != 0)
1247 runtime_throw("pthread_create");
1252 // One round of scheduler: find a goroutine and run it.
1253 // The argument is the goroutine that was running before
1254 // schedule was called, or nil if this is the first call.
1264 // Just finished running gp.
1266 runtime_sched
.grunning
--;
1268 // atomic { mcpu-- }
1269 v
= runtime_xadd(&runtime_sched
.atomic
, -1<<mcpuShift
);
1270 if(atomic_mcpu(v
) > maxgomaxprocs
)
1271 runtime_throw("negative mcpu in scheduler");
1273 switch(gp
->status
) {
1276 // Shouldn't have been running!
1277 runtime_throw("bad gp->status in sched");
1279 gp
->status
= Grunnable
;
1284 runtime_racegoend(gp
->goid
);
1291 runtime_memclr(&gp
->context
, sizeof gp
->context
);
1293 if(--runtime_sched
.gcount
== 0)
1297 if(gp
->readyonstop
) {
1298 gp
->readyonstop
= 0;
1301 } else if(m
->helpgc
) {
1302 // Bootstrap m or new m started by starttheworld.
1303 // atomic { mcpu-- }
1304 v
= runtime_xadd(&runtime_sched
.atomic
, -1<<mcpuShift
);
1305 if(atomic_mcpu(v
) > maxgomaxprocs
)
1306 runtime_throw("negative mcpu in scheduler");
1307 // Compensate for increment in starttheworld().
1308 runtime_sched
.grunning
--;
1310 } else if(m
->nextg
!= nil
) {
1311 // New m started by matchmg.
1313 runtime_throw("invalid m state in scheduler");
1316 // Find (or wait for) g to run. Unlocks runtime_sched.
1317 gp
= nextgandunlock();
1318 gp
->readyonstop
= 0;
1319 gp
->status
= Grunning
;
1323 // Check whether the profiler needs to be turned on or off.
1324 hz
= runtime_sched
.profilehz
;
1325 if(m
->profilehz
!= hz
)
1326 runtime_resetcpuprofiler(hz
);
1331 // Enter scheduler. If g->status is Grunning,
1332 // re-queues g and runs everyone else who is waiting
1333 // before running g again. If g->status is Gmoribund,
1336 runtime_gosched(void)
1339 runtime_throw("gosched holding locks");
1341 runtime_throw("gosched of g0");
1342 runtime_mcall(schedule
);
1345 // Puts the current goroutine into a waiting state and unlocks the lock.
1346 // The goroutine can be made runnable again by calling runtime_ready(gp).
1348 runtime_park(void (*unlockf
)(Lock
*), Lock
*lock
, const char *reason
)
1350 g
->status
= Gwaiting
;
1351 g
->waitreason
= reason
;
1357 // The goroutine g is about to enter a system call.
1358 // Record that it's not using the cpu anymore.
1359 // This is called only from the go syscall library and cgocall,
1360 // not from the low-level system calls used by the runtime.
1362 // Entersyscall cannot split the stack: the runtime_gosave must
1363 // make g->sched refer to the caller's stack segment, because
1364 // entersyscall is going to return immediately after.
1365 // It's okay to call matchmg and notewakeup even after
1366 // decrementing mcpu, because we haven't released the
1367 // sched lock yet, so the garbage collector cannot be running.
1369 void runtime_entersyscall(void) __attribute__ ((no_split_stack
));
1372 runtime_entersyscall(void)
1376 if(m
->profilehz
> 0)
1377 runtime_setprof(false);
1379 // Leave SP around for gc and traceback.
1380 #ifdef USING_SPLIT_STACK
1381 g
->gcstack
= __splitstack_find(nil
, nil
, &g
->gcstack_size
,
1382 &g
->gcnext_segment
, &g
->gcnext_sp
,
1385 g
->gcnext_sp
= (byte
*) &v
;
1388 // Save the registers in the g structure so that any pointers
1389 // held in registers will be seen by the garbage collector.
1390 getcontext(&g
->gcregs
);
1392 g
->status
= Gsyscall
;
1395 // The slow path inside the schedlock/schedunlock will get
1396 // through without stopping if it does:
1399 // waitstop && mcpu <= mcpumax not true
1400 // If we can do the same with a single atomic add,
1401 // then we can skip the locks.
1402 v
= runtime_xadd(&runtime_sched
.atomic
, -1<<mcpuShift
);
1403 if(!atomic_gwaiting(v
) && (!atomic_waitstop(v
) || atomic_mcpu(v
) > atomic_mcpumax(v
)))
1407 v
= runtime_atomicload(&runtime_sched
.atomic
);
1408 if(atomic_gwaiting(v
)) {
1410 v
= runtime_atomicload(&runtime_sched
.atomic
);
1412 if(atomic_waitstop(v
) && atomic_mcpu(v
) <= atomic_mcpumax(v
)) {
1413 runtime_xadd(&runtime_sched
.atomic
, -1<<waitstopShift
);
1414 runtime_notewakeup(&runtime_sched
.stopped
);
1420 // The goroutine g exited its system call.
1421 // Arrange for it to run on a cpu again.
1422 // This is called only from the go syscall library, not
1423 // from the low-level system calls used by the runtime.
1425 runtime_exitsyscall(void)
1431 // If we can do the mcpu++ bookkeeping and
1432 // find that we still have mcpu <= mcpumax, then we can
1433 // start executing Go code immediately, without having to
1434 // schedlock/schedunlock.
1435 // Also do fast return if any locks are held, so that
1436 // panic code can use syscalls to open a file.
1438 v
= runtime_xadd(&runtime_sched
.atomic
, (1<<mcpuShift
));
1439 if((m
->profilehz
== runtime_sched
.profilehz
&& atomic_mcpu(v
) <= atomic_mcpumax(v
)) || m
->locks
> 0) {
1440 // There's a cpu for us, so we can run.
1441 gp
->status
= Grunning
;
1442 // Garbage collector isn't running (since we are),
1443 // so okay to clear gcstack.
1444 #ifdef USING_SPLIT_STACK
1447 gp
->gcnext_sp
= nil
;
1448 runtime_memclr(&gp
->gcregs
, sizeof gp
->gcregs
);
1450 if(m
->profilehz
> 0)
1451 runtime_setprof(true);
1455 // Tell scheduler to put g back on the run queue:
1456 // mostly equivalent to g->status = Grunning,
1457 // but keeps the garbage collector from thinking
1458 // that g is running right now, which it's not.
1459 gp
->readyonstop
= 1;
1461 // All the cpus are taken.
1462 // The scheduler will ready g and put this m to sleep.
1463 // When the scheduler takes g away from m,
1464 // it will undo the runtime_sched.mcpu++ above.
1467 // Gosched returned, so we're allowed to run now.
1468 // Delete the gcstack information that we left for
1469 // the garbage collector during the system call.
1470 // Must wait until now because until gosched returns
1471 // we don't know for sure that the garbage collector
1473 #ifdef USING_SPLIT_STACK
1476 gp
->gcnext_sp
= nil
;
1477 runtime_memclr(&gp
->gcregs
, sizeof gp
->gcregs
);
1480 // Allocate a new g, with a stack big enough for stacksize bytes.
1482 runtime_malg(int32 stacksize
, byte
** ret_stack
, size_t* ret_stacksize
)
1486 newg
= runtime_malloc(sizeof(G
));
1487 if(stacksize
>= 0) {
1488 #if USING_SPLIT_STACK
1489 int dont_block_signals
= 0;
1491 *ret_stack
= __splitstack_makecontext(stacksize
,
1492 &newg
->stack_context
[0],
1494 __splitstack_block_signals_context(&newg
->stack_context
[0],
1495 &dont_block_signals
, nil
);
1497 *ret_stack
= runtime_mallocgc(stacksize
, FlagNoProfiling
|FlagNoGC
, 0, 0);
1498 *ret_stacksize
= stacksize
;
1499 newg
->gcinitial_sp
= *ret_stack
;
1500 newg
->gcstack_size
= stacksize
;
1501 runtime_xadd(&runtime_stacks_sys
, stacksize
);
1507 /* For runtime package testing. */
1509 void runtime_testing_entersyscall(void)
1510 __asm__ (GOSYM_PREFIX
"runtime.entersyscall");
1513 runtime_testing_entersyscall()
1515 runtime_entersyscall();
1518 void runtime_testing_exitsyscall(void)
1519 __asm__ (GOSYM_PREFIX
"runtime.exitsyscall");
1522 runtime_testing_exitsyscall()
1524 runtime_exitsyscall();
1528 __go_go(void (*fn
)(void*), void* arg
)
1535 goid
= runtime_xadd64((uint64
*)&runtime_sched
.goidgen
, 1);
1537 runtime_racegostart(goid
, runtime_getcallerpc(&fn
));
1541 if((newg
= gfget()) != nil
) {
1542 #ifdef USING_SPLIT_STACK
1543 int dont_block_signals
= 0;
1545 sp
= __splitstack_resetcontext(&newg
->stack_context
[0],
1547 __splitstack_block_signals_context(&newg
->stack_context
[0],
1548 &dont_block_signals
, nil
);
1550 sp
= newg
->gcinitial_sp
;
1551 spsize
= newg
->gcstack_size
;
1553 runtime_throw("bad spsize in __go_go");
1554 newg
->gcnext_sp
= sp
;
1557 newg
= runtime_malg(StackMin
, &sp
, &spsize
);
1558 if(runtime_lastg
== nil
)
1559 runtime_allg
= newg
;
1561 runtime_lastg
->alllink
= newg
;
1562 runtime_lastg
= newg
;
1564 newg
->status
= Gwaiting
;
1565 newg
->waitreason
= "new goroutine";
1567 newg
->entry
= (byte
*)fn
;
1569 newg
->gopc
= (uintptr
)__builtin_return_address(0);
1571 runtime_sched
.gcount
++;
1575 runtime_throw("nil g->stack0");
1578 // Avoid warnings about variables clobbered by
1580 byte
* volatile vsp
= sp
;
1581 size_t volatile vspsize
= spsize
;
1582 G
* volatile vnewg
= newg
;
1584 getcontext(&vnewg
->context
);
1585 vnewg
->context
.uc_stack
.ss_sp
= vsp
;
1586 #ifdef MAKECONTEXT_STACK_TOP
1587 vnewg
->context
.uc_stack
.ss_sp
+= vspsize
;
1589 vnewg
->context
.uc_stack
.ss_size
= vspsize
;
1590 makecontext(&vnewg
->context
, kickoff
, 0);
1592 newprocreadylocked(vnewg
);
1599 // Put on gfree list. Sched must be locked.
1603 gp
->schedlink
= runtime_sched
.gfree
;
1604 runtime_sched
.gfree
= gp
;
1607 // Get from gfree list. Sched must be locked.
1613 gp
= runtime_sched
.gfree
;
1615 runtime_sched
.gfree
= gp
->schedlink
;
1619 void runtime_Gosched (void) __asm__ (GOSYM_PREFIX
"runtime.Gosched");
1622 runtime_Gosched(void)
1627 // Implementation of runtime.GOMAXPROCS.
1628 // delete when scheduler is stronger
1630 runtime_gomaxprocsfunc(int32 n
)
1636 ret
= runtime_gomaxprocs
;
1639 if(n
> maxgomaxprocs
)
1641 runtime_gomaxprocs
= n
;
1642 if(runtime_gomaxprocs
> 1)
1643 runtime_singleproc
= false;
1644 if(runtime_gcwaiting
!= 0) {
1645 if(atomic_mcpumax(runtime_sched
.atomic
) != 1)
1646 runtime_throw("invalid mcpumax during gc");
1653 // If there are now fewer allowed procs
1654 // than procs running, stop.
1655 v
= runtime_atomicload(&runtime_sched
.atomic
);
1656 if((int32
)atomic_mcpu(v
) > n
) {
1661 // handle more procs
1668 runtime_LockOSThread(void)
1670 if(m
== &runtime_m0
&& runtime_sched
.init
) {
1671 runtime_sched
.lockmain
= true;
1679 runtime_UnlockOSThread(void)
1681 if(m
== &runtime_m0
&& runtime_sched
.init
) {
1682 runtime_sched
.lockmain
= false;
1690 runtime_lockedOSThread(void)
1692 return g
->lockedm
!= nil
&& m
->lockedg
!= nil
;
1695 // for testing of callbacks
1697 _Bool
runtime_golockedOSThread(void)
1698 __asm__ (GOSYM_PREFIX
"runtime.golockedOSThread");
1701 runtime_golockedOSThread(void)
1703 return runtime_lockedOSThread();
1706 // for testing of wire, unwire
1713 intgo
runtime_NumGoroutine (void)
1714 __asm__ (GOSYM_PREFIX
"runtime.NumGoroutine");
1717 runtime_NumGoroutine()
1719 return runtime_sched
.gcount
;
1723 runtime_gcount(void)
1725 return runtime_sched
.gcount
;
1729 runtime_mcount(void)
1731 return runtime_sched
.mcount
;
1736 void (*fn
)(uintptr
*, int32
);
1741 // Called if we receive a SIGPROF signal.
1747 if(prof
.fn
== nil
|| prof
.hz
== 0)
1750 runtime_lock(&prof
);
1751 if(prof
.fn
== nil
) {
1752 runtime_unlock(&prof
);
1755 n
= runtime_callers(0, prof
.pcbuf
, nelem(prof
.pcbuf
));
1757 prof
.fn(prof
.pcbuf
, n
);
1758 runtime_unlock(&prof
);
1761 // Arrange to call fn with a traceback hz times a second.
1763 runtime_setcpuprofilerate(void (*fn
)(uintptr
*, int32
), int32 hz
)
1765 // Force sane arguments.
1773 // Stop profiler on this cpu so that it is safe to lock prof.
1774 // if a profiling signal came in while we had prof locked,
1775 // it would deadlock.
1776 runtime_resetcpuprofiler(0);
1778 runtime_lock(&prof
);
1781 runtime_unlock(&prof
);
1782 runtime_lock(&runtime_sched
);
1783 runtime_sched
.profilehz
= hz
;
1784 runtime_unlock(&runtime_sched
);
1787 runtime_resetcpuprofiler(hz
);