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.
17 #ifdef USING_SPLIT_STACK
19 /* FIXME: These are not declared anywhere. */
21 extern void __splitstack_getcontext(void *context[10]);
23 extern void __splitstack_setcontext(void *context[10]);
25 extern void *__splitstack_makecontext(size_t, void *context[10], size_t *);
27 extern void * __splitstack_resetcontext(void *context[10], size_t *);
29 extern void *__splitstack_find(void *, void *, size_t *, void **, void **,
32 extern void __splitstack_block_signals (int *, int *);
34 extern void __splitstack_block_signals_context (void *context[10], int *,
39 #if defined(USING_SPLIT_STACK) && defined(LINKER_SUPPORTS_SPLIT_STACK)
40 # ifdef PTHREAD_STACK_MIN
41 # define StackMin PTHREAD_STACK_MIN
43 # define StackMin 8192
46 # define StackMin 2 * 1024 * 1024
49 uintptr runtime_stacks_sys;
51 static void schedule(G*);
53 typedef struct Sched Sched;
56 G runtime_g0; // idle goroutine for m0
65 #ifndef SETCONTEXT_CLOBBERS_TLS
73 fixcontext(ucontext_t *c __attribute__ ((unused)))
79 # if defined(__x86_64__) && defined(__sun__)
81 // x86_64 Solaris 10 and 11 have a bug: setcontext switches the %fs
82 // register to that of the thread which called getcontext. The effect
83 // is that the address of all __thread variables changes. This bug
84 // also affects pthread_self() and pthread_getspecific. We work
85 // around it by clobbering the context field directly to keep %fs the
88 static __thread greg_t fs;
96 fs = c.uc_mcontext.gregs[REG_FSBASE];
100 fixcontext(ucontext_t* c)
102 c->uc_mcontext.gregs[REG_FSBASE] = fs;
107 # error unknown case for SETCONTEXT_CLOBBERS_TLS
113 // We can not always refer to the TLS variables directly. The
114 // compiler will call tls_get_addr to get the address of the variable,
115 // and it may hold it in a register across a call to schedule. When
116 // we get back from the call we may be running in a different thread,
117 // in which case the register now points to the TLS variable for a
118 // different thread. We use non-inlinable functions to avoid this
121 G* runtime_g(void) __attribute__ ((noinline, no_split_stack));
129 M* runtime_m(void) __attribute__ ((noinline, no_split_stack));
137 int32 runtime_gcwaiting;
141 // The go scheduler's job is to match ready-to-run goroutines (`g's)
142 // with waiting-for-work schedulers (`m's). If there are ready g's
143 // and no waiting m's, ready() will start a new m running in a new
144 // OS thread, so that all ready g's can run simultaneously, up to a limit.
145 // For now, m's never go away.
147 // By default, Go keeps only one kernel thread (m) running user code
148 // at a single time; other threads may be blocked in the operating system.
149 // Setting the environment variable $GOMAXPROCS or calling
150 // runtime.GOMAXPROCS() will change the number of user threads
151 // allowed to execute simultaneously. $GOMAXPROCS is thus an
152 // approximation of the maximum number of cores to use.
154 // Even a program that can run without deadlock in a single process
155 // might use more m's if given the chance. For example, the prime
156 // sieve will use as many m's as there are primes (up to runtime_sched.mmax),
157 // allowing different stages of the pipeline to execute in parallel.
158 // We could revisit this choice, only kicking off new m's for blocking
159 // system calls, but that would limit the amount of parallel computation
160 // that go would try to do.
162 // In general, one could imagine all sorts of refinements to the
163 // scheduler, but the goal now is just to get something working on
169 G *gfree; // available g's (status == Gdead)
172 G *ghead; // g's waiting to run
174 int32 gwait; // number of g's waiting to run
175 int32 gcount; // number of g's that are alive
176 int32 grunning; // number of g's running on cpu or in syscall
178 M *mhead; // m's waiting for work
179 int32 mwait; // number of m's waiting for work
180 int32 mcount; // number of m's that have been created
182 volatile uint32 atomic; // atomic scheduling word (see below)
184 int32 profilehz; // cpu profiling rate
186 bool init; // running initialization
187 bool lockmain; // init called runtime.LockOSThread
189 Note stopped; // one g can set waitstop and wait here for m's to stop
192 // The atomic word in sched is an atomic uint32 that
193 // holds these fields.
195 // [15 bits] mcpu number of m's executing on cpu
196 // [15 bits] mcpumax max number of m's allowed on cpu
197 // [1 bit] waitstop some g is waiting on stopped
198 // [1 bit] gwaiting gwait != 0
200 // These fields are the information needed by entersyscall
201 // and exitsyscall to decide whether to coordinate with the
202 // scheduler. Packing them into a single machine word lets
203 // them use a fast path with a single atomic read/write and
204 // no lock/unlock. This greatly reduces contention in
205 // syscall- or cgo-heavy multithreaded programs.
207 // Except for entersyscall and exitsyscall, the manipulations
208 // to these fields only happen while holding the schedlock,
209 // so the routines holding schedlock only need to worry about
210 // what entersyscall and exitsyscall do, not the other routines
211 // (which also use the schedlock).
213 // In particular, entersyscall and exitsyscall only read mcpumax,
214 // waitstop, and gwaiting. They never write them. Thus, writes to those
215 // fields can be done (holding schedlock) without fear of write conflicts.
216 // There may still be logic conflicts: for example, the set of waitstop must
217 // be conditioned on mcpu >= mcpumax or else the wait may be a
218 // spurious sleep. The Promela model in proc.p verifies these accesses.
221 mcpuMask = (1<<mcpuWidth) - 1,
223 mcpumaxShift = mcpuShift + mcpuWidth,
224 waitstopShift = mcpumaxShift + mcpuWidth,
225 gwaitingShift = waitstopShift+1,
227 // The max value of GOMAXPROCS is constrained
228 // by the max value we can store in the bit fields
229 // of the atomic word. Reserve a few high values
230 // so that we can detect accidental decrement
232 maxgomaxprocs = mcpuMask - 10,
235 #define atomic_mcpu(v) (((v)>>mcpuShift)&mcpuMask)
236 #define atomic_mcpumax(v) (((v)>>mcpumaxShift)&mcpuMask)
237 #define atomic_waitstop(v) (((v)>>waitstopShift)&1)
238 #define atomic_gwaiting(v) (((v)>>gwaitingShift)&1)
241 int32 runtime_gomaxprocs;
242 bool runtime_singleproc;
244 static bool canaddmcpu(void);
246 // An m that is waiting for notewakeup(&m->havenextg). This may
247 // only be accessed while the scheduler lock is held. This is used to
248 // minimize the number of times we call notewakeup while the scheduler
249 // lock is held, since the m will normally move quickly to lock the
250 // scheduler itself, producing lock contention.
253 // Scheduling helpers. Sched must be locked.
254 static void gput(G*); // put/get on ghead/gtail
255 static G* gget(void);
256 static void mput(M*); // put/get on mhead
258 static void gfput(G*); // put/get on gfree
259 static G* gfget(void);
260 static void matchmg(void); // match m's to g's
261 static void readylocked(G*); // ready, but sched is locked
262 static void mnextg(M*, G*);
263 static void mcommoninit(M*);
271 v = runtime_sched.atomic;
273 w &= ~(mcpuMask<<mcpumaxShift);
274 w |= n<<mcpumaxShift;
275 if(runtime_cas(&runtime_sched.atomic, v, w))
280 // First function run by a new goroutine. This replaces gogocall.
286 fn = (void (*)(void*))(g->entry);
291 // Switch context to a different goroutine. This is like longjmp.
292 static void runtime_gogo(G*) __attribute__ ((noinline));
294 runtime_gogo(G* newg)
296 #ifdef USING_SPLIT_STACK
297 __splitstack_setcontext(&newg->stack_context[0]);
300 newg->fromgogo = true;
301 fixcontext(&newg->context);
302 setcontext(&newg->context);
303 runtime_throw("gogo setcontext returned");
306 // Save context and call fn passing g as a parameter. This is like
307 // setjmp. Because getcontext always returns 0, unlike setjmp, we use
308 // g->fromgogo as a code. It will be true if we got here via
309 // setcontext. g == nil the first time this is called in a new m.
310 static void runtime_mcall(void (*)(G*)) __attribute__ ((noinline));
312 runtime_mcall(void (*pfn)(G*))
316 #ifndef USING_SPLIT_STACK
320 // Ensure that all registers are on the stack for the garbage
322 __builtin_unwind_init();
327 runtime_throw("runtime: mcall called on m->g0 stack");
331 #ifdef USING_SPLIT_STACK
332 __splitstack_getcontext(&g->stack_context[0]);
336 gp->fromgogo = false;
337 getcontext(&gp->context);
339 // When we return from getcontext, we may be running
340 // in a new thread. That means that m and g may have
341 // changed. They are global variables so we will
342 // reload them, but the addresses of m and g may be
343 // cached in our local stack frame, and those
344 // addresses may be wrong. Call functions to reload
345 // the values for this thread.
349 if (gp == nil || !gp->fromgogo) {
350 #ifdef USING_SPLIT_STACK
351 __splitstack_setcontext(&mp->g0->stack_context[0]);
353 mp->g0->entry = (byte*)pfn;
356 // It's OK to set g directly here because this case
357 // can not occur if we got here via a setcontext to
358 // the getcontext call just above.
361 fixcontext(&mp->g0->context);
362 setcontext(&mp->g0->context);
363 runtime_throw("runtime: mcall function returned");
367 // Keep trace of scavenger's goroutine for deadlock detection.
370 // The bootstrap sequence is:
374 // make & queue new G
375 // call runtime_mstart
377 // The new G calls runtime_main.
379 runtime_schedinit(void)
393 runtime_mallocinit();
400 // Allocate internal symbol table representation now,
401 // so that we don't need to call malloc when we crash.
402 // runtime_findfunc(0);
404 runtime_gomaxprocs = 1;
405 p = runtime_getenv("GOMAXPROCS");
406 if(p != nil && (n = runtime_atoi(p)) != 0) {
407 if(n > maxgomaxprocs)
409 runtime_gomaxprocs = n;
411 // wait for the main goroutine to start before taking
412 // GOMAXPROCS into account.
414 runtime_singleproc = runtime_gomaxprocs == 1;
416 canaddmcpu(); // mcpu++ to account for bootstrap m
417 m->helpgc = 1; // flag to tell schedule() to mcpu--
418 runtime_sched.grunning++;
420 // Can not enable GC until all roots are registered.
421 // mstats.enablegc = 1;
425 extern void main_init(void) __asm__ ("__go_init_main");
426 extern void main_main(void) __asm__ ("main.main");
428 // The main goroutine.
432 // Lock the main goroutine onto this, the main OS thread,
433 // during initialization. Most programs won't care, but a few
434 // do require certain calls to be made by the main thread.
435 // Those can arrange for main.main to run in the main thread
436 // by calling runtime.LockOSThread during initialization
437 // to preserve the lock.
438 runtime_LockOSThread();
439 // From now on, newgoroutines may use non-main threads.
440 setmcpumax(runtime_gomaxprocs);
441 runtime_sched.init = true;
442 scvg = __go_go(runtime_MHeap_Scavenger, nil);
444 runtime_sched.init = false;
445 if(!runtime_sched.lockmain)
446 runtime_UnlockOSThread();
448 // For gccgo we have to wait until after main is initialized
449 // to enable GC, because initializing main registers the GC
453 // The deadlock detection has false negatives.
454 // Let scvg start up, to eliminate the false negative
455 // for the trivial program func main() { select{} }.
464 // Lock the scheduler.
468 runtime_lock(&runtime_sched);
471 // Unlock the scheduler.
479 runtime_unlock(&runtime_sched);
481 runtime_notewakeup(&m->havenextg);
487 g->status = Gmoribund;
492 runtime_goroutineheader(G *g)
511 status = g->waitreason;
522 runtime_printf("goroutine %d [%s]:\n", g->goid, status);
526 runtime_tracebackothers(G *me)
530 for(g = runtime_allg; g != nil; g = g->alllink) {
531 if(g == me || g->status == Gdead)
533 runtime_printf("\n");
534 runtime_goroutineheader(g);
535 // runtime_traceback(g->sched.pc, g->sched.sp, 0, g);
539 // Mark this g as m's idle goroutine.
540 // This functionality might be used in environments where programs
541 // are limited to a single thread, to simulate a select-driven
542 // network server. It is not exposed via the standard runtime API.
544 runtime_idlegoroutine(void)
547 runtime_throw("g is already an idle goroutine");
554 m->id = runtime_sched.mcount++;
555 m->fastrand = 0x49f6428aUL + m->id + runtime_cputicks();
558 m->mcache = runtime_allocmcache();
560 runtime_callers(1, m->createstack, nelem(m->createstack));
562 // Add to runtime_allm so garbage collector doesn't free m
563 // when it is just in a register or thread-local storage.
564 m->alllink = runtime_allm;
565 // runtime_NumCgoCall() iterates over allm w/o schedlock,
566 // so we need to publish it safely.
567 runtime_atomicstorep(&runtime_allm, m);
570 // Try to increment mcpu. Report whether succeeded.
577 v = runtime_sched.atomic;
578 if(atomic_mcpu(v) >= atomic_mcpumax(v))
580 if(runtime_cas(&runtime_sched.atomic, v, v+(1<<mcpuShift)))
585 // Put on `g' queue. Sched must be locked.
591 // If g is wired, hand it off directly.
592 if((m = g->lockedm) != nil && canaddmcpu()) {
597 // If g is the idle goroutine for an m, hand it off.
598 if(g->idlem != nil) {
599 if(g->idlem->idleg != nil) {
600 runtime_printf("m%d idle out of sync: g%d g%d\n",
602 g->idlem->idleg->goid, g->goid);
603 runtime_throw("runtime: double idle");
610 if(runtime_sched.ghead == nil)
611 runtime_sched.ghead = g;
613 runtime_sched.gtail->schedlink = g;
614 runtime_sched.gtail = g;
617 // if it transitions to nonzero, set atomic gwaiting bit.
618 if(runtime_sched.gwait++ == 0)
619 runtime_xadd(&runtime_sched.atomic, 1<<gwaitingShift);
622 // Report whether gget would return something.
626 return runtime_sched.ghead != nil || m->idleg != nil;
629 // Get from `g' queue. Sched must be locked.
635 g = runtime_sched.ghead;
637 runtime_sched.ghead = g->schedlink;
638 if(runtime_sched.ghead == nil)
639 runtime_sched.gtail = nil;
641 // if it transitions to zero, clear atomic gwaiting bit.
642 if(--runtime_sched.gwait == 0)
643 runtime_xadd(&runtime_sched.atomic, -1<<gwaitingShift);
644 } else if(m->idleg != nil) {
651 // Put on `m' list. Sched must be locked.
655 m->schedlink = runtime_sched.mhead;
656 runtime_sched.mhead = m;
657 runtime_sched.mwait++;
660 // Get an `m' to run `g'. Sched must be locked.
666 // if g has its own m, use it.
667 if(g && (m = g->lockedm) != nil)
670 // otherwise use general m pool.
671 if((m = runtime_sched.mhead) != nil){
672 runtime_sched.mhead = m->schedlink;
673 runtime_sched.mwait--;
678 // Mark g ready to run.
687 // Mark g ready to run. Sched is already locked.
688 // G might be running already and about to stop.
689 // The sched lock protects g->status from changing underfoot.
694 // Running on another machine.
695 // Ready it when it stops.
701 if(g->status == Grunnable || g->status == Grunning) {
702 runtime_printf("goroutine %d has status %d\n", g->goid, g->status);
703 runtime_throw("bad g->status in ready");
705 g->status = Grunnable;
711 // Same as readylocked but a different symbol so that
712 // debuggers can set a breakpoint here and catch all
715 newprocreadylocked(G *g)
720 // Pass g to m for running.
721 // Caller has already incremented mcpu.
725 runtime_sched.grunning++;
730 runtime_notewakeup(&mwakeup->havenextg);
735 // Get the next goroutine that m should run.
736 // Sched must be locked on entry, is unlocked on exit.
737 // Makes sure that at most $GOMAXPROCS g's are
738 // running on cpus (not in system calls) at any given time.
746 if(atomic_mcpu(runtime_sched.atomic) >= maxgomaxprocs)
747 runtime_throw("negative mcpu");
749 // If there is a g waiting as m->nextg, the mcpu++
750 // happened before it was passed to mnextg.
751 if(m->nextg != nil) {
758 if(m->lockedg != nil) {
759 // We can only run one g, and it's not available.
760 // Make sure some other cpu is running to handle
761 // the ordinary run queue.
762 if(runtime_sched.gwait != 0) {
764 // m->lockedg might have been on the queue.
765 if(m->nextg != nil) {
773 // Look for work on global queue.
774 while(haveg() && canaddmcpu()) {
777 runtime_throw("gget inconsistency");
780 mnextg(gp->lockedm, gp);
783 runtime_sched.grunning++;
788 // The while loop ended either because the g queue is empty
789 // or because we have maxed out our m procs running go
790 // code (mcpu >= mcpumax). We need to check that
791 // concurrent actions by entersyscall/exitsyscall cannot
792 // invalidate the decision to end the loop.
794 // We hold the sched lock, so no one else is manipulating the
795 // g queue or changing mcpumax. Entersyscall can decrement
796 // mcpu, but if does so when there is something on the g queue,
797 // the gwait bit will be set, so entersyscall will take the slow path
798 // and use the sched lock. So it cannot invalidate our decision.
800 // Wait on global m queue.
804 // Look for deadlock situation.
805 // There is a race with the scavenger that causes false negatives:
806 // if the scavenger is just starting, then we have
807 // scvg != nil && grunning == 0 && gwait == 0
808 // and we do not detect a deadlock. It is possible that we should
809 // add that case to the if statement here, but it is too close to Go 1
810 // to make such a subtle change. Instead, we work around the
811 // false negative in trivial programs by calling runtime.gosched
812 // from the main goroutine just before main.main.
813 // See runtime_main above.
815 // On a related note, it is also possible that the scvg == nil case is
816 // wrong and should include gwait, but that does not happen in
817 // standard Go programs, which all start the scavenger.
819 if((scvg == nil && runtime_sched.grunning == 0) ||
820 (scvg != nil && runtime_sched.grunning == 1 && runtime_sched.gwait == 0 &&
821 (scvg->status == Grunning || scvg->status == Gsyscall))) {
822 runtime_throw("all goroutines are asleep - deadlock!");
827 runtime_noteclear(&m->havenextg);
829 // Stoptheworld is waiting for all but its cpu to go to stop.
830 // Entersyscall might have decremented mcpu too, but if so
831 // it will see the waitstop and take the slow path.
832 // Exitsyscall never increments mcpu beyond mcpumax.
833 v = runtime_atomicload(&runtime_sched.atomic);
834 if(atomic_waitstop(v) && atomic_mcpu(v) <= atomic_mcpumax(v)) {
835 // set waitstop = 0 (known to be 1)
836 runtime_xadd(&runtime_sched.atomic, -1<<waitstopShift);
837 runtime_notewakeup(&runtime_sched.stopped);
841 runtime_notesleep(&m->havenextg);
845 runtime_lock(&runtime_sched);
848 if((gp = m->nextg) == nil)
849 runtime_throw("bad m->nextg in nextgoroutine");
855 runtime_helpgc(bool *extra)
860 // Figure out how many CPUs to use.
861 // Limited by gomaxprocs, number of actual CPUs, and MaxGcproc.
862 max = runtime_gomaxprocs;
863 if(max > runtime_ncpu)
864 max = runtime_ncpu > 0 ? runtime_ncpu : 1;
868 // We're going to use one CPU no matter what.
869 // Figure out the max number of additional CPUs.
872 runtime_lock(&runtime_sched);
874 while(n < max && (mp = mget(nil)) != nil) {
878 runtime_notewakeup(&mp->havenextg);
880 runtime_unlock(&runtime_sched);
887 runtime_stoptheworld(void)
892 runtime_gcwaiting = 1;
898 v = runtime_sched.atomic;
899 if(atomic_mcpu(v) <= 1)
902 // It would be unsafe for multiple threads to be using
903 // the stopped note at once, but there is only
904 // ever one thread doing garbage collection.
905 runtime_noteclear(&runtime_sched.stopped);
906 if(atomic_waitstop(v))
907 runtime_throw("invalid waitstop");
909 // atomic { waitstop = 1 }, predicated on mcpu <= 1 check above
911 if(!runtime_cas(&runtime_sched.atomic, v, v+(1<<waitstopShift)))
915 runtime_notesleep(&runtime_sched.stopped);
918 runtime_singleproc = runtime_gomaxprocs == 1;
923 runtime_starttheworld(bool extra)
928 runtime_gcwaiting = 0;
929 setmcpumax(runtime_gomaxprocs);
931 if(extra && canaddmcpu()) {
932 // Start a new m that will (we hope) be idle
933 // and so available to help when the next
934 // garbage collection happens.
935 // canaddmcpu above did mcpu++
936 // (necessary, because m will be doing various
937 // initialization work so is definitely running),
938 // but m is not running a specific goroutine,
939 // so set the helpgc flag as a signal to m's
940 // first schedule(nil) to mcpu-- and grunning--.
943 runtime_sched.grunning++;
948 // Called to start an M.
950 runtime_mstart(void* mp)
960 // Record top of stack for use by mcall.
961 // Once we call schedule we're never coming back,
962 // so other calls can reuse this stack space.
963 #ifdef USING_SPLIT_STACK
964 __splitstack_getcontext(&g->stack_context[0]);
966 g->gcinitial_sp = ∓
967 // Setting gcstack_size to 0 is a marker meaning that gcinitial_sp
968 // is the top of the stack, not the bottom.
972 getcontext(&g->context);
974 if(g->entry != nil) {
975 // Got here from mcall.
976 void (*pfn)(G*) = (void (*)(G*))g->entry;
977 G* gp = (G*)g->param;
983 #ifdef USING_SPLIT_STACK
985 int dont_block_signals = 0;
986 __splitstack_block_signals(&dont_block_signals, nil);
990 // Install signal handlers; after minit so that minit can
991 // prepare the thread to be able to handle the signals.
999 typedef struct CgoThreadStart CgoThreadStart;
1000 struct CgoThreadStart
1007 // Kick off new m's as needed (up to mcpumax).
1015 if(m->mallocing || m->gcing)
1018 while(haveg() && canaddmcpu()) {
1021 runtime_throw("gget inconsistency");
1023 // Find the m that will run gp.
1024 if((mp = mget(gp)) == nil)
1025 mp = runtime_newm();
1030 // Create a new m. It will start off with a call to runtime_mstart.
1035 pthread_attr_t attr;
1038 m = runtime_malloc(sizeof(M));
1040 m->g0 = runtime_malg(-1, nil, nil);
1042 if(pthread_attr_init(&attr) != 0)
1043 runtime_throw("pthread_attr_init");
1044 if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
1045 runtime_throw("pthread_attr_setdetachstate");
1047 #ifndef PTHREAD_STACK_MIN
1048 #define PTHREAD_STACK_MIN 8192
1050 if(pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
1051 runtime_throw("pthread_attr_setstacksize");
1053 if(pthread_create(&tid, &attr, runtime_mstart, m) != 0)
1054 runtime_throw("pthread_create");
1059 // One round of scheduler: find a goroutine and run it.
1060 // The argument is the goroutine that was running before
1061 // schedule was called, or nil if this is the first call.
1071 // Just finished running gp.
1073 runtime_sched.grunning--;
1075 // atomic { mcpu-- }
1076 v = runtime_xadd(&runtime_sched.atomic, -1<<mcpuShift);
1077 if(atomic_mcpu(v) > maxgomaxprocs)
1078 runtime_throw("negative mcpu in scheduler");
1083 // Shouldn't have been running!
1084 runtime_throw("bad gp->status in sched");
1086 gp->status = Grunnable;
1096 runtime_memclr(&gp->context, sizeof gp->context);
1098 if(--runtime_sched.gcount == 0)
1102 if(gp->readyonstop){
1103 gp->readyonstop = 0;
1106 } else if(m->helpgc) {
1107 // Bootstrap m or new m started by starttheworld.
1108 // atomic { mcpu-- }
1109 v = runtime_xadd(&runtime_sched.atomic, -1<<mcpuShift);
1110 if(atomic_mcpu(v) > maxgomaxprocs)
1111 runtime_throw("negative mcpu in scheduler");
1112 // Compensate for increment in starttheworld().
1113 runtime_sched.grunning--;
1115 } else if(m->nextg != nil) {
1116 // New m started by matchmg.
1118 runtime_throw("invalid m state in scheduler");
1121 // Find (or wait for) g to run. Unlocks runtime_sched.
1122 gp = nextgandunlock();
1123 gp->readyonstop = 0;
1124 gp->status = Grunning;
1128 // Check whether the profiler needs to be turned on or off.
1129 hz = runtime_sched.profilehz;
1130 if(m->profilehz != hz)
1131 runtime_resetcpuprofiler(hz);
1136 // Enter scheduler. If g->status is Grunning,
1137 // re-queues g and runs everyone else who is waiting
1138 // before running g again. If g->status is Gmoribund,
1141 runtime_gosched(void)
1144 runtime_throw("gosched holding locks");
1146 runtime_throw("gosched of g0");
1147 runtime_mcall(schedule);
1150 // The goroutine g is about to enter a system call.
1151 // Record that it's not using the cpu anymore.
1152 // This is called only from the go syscall library and cgocall,
1153 // not from the low-level system calls used by the runtime.
1155 // Entersyscall cannot split the stack: the runtime_gosave must
1156 // make g->sched refer to the caller's stack segment, because
1157 // entersyscall is going to return immediately after.
1158 // It's okay to call matchmg and notewakeup even after
1159 // decrementing mcpu, because we haven't released the
1160 // sched lock yet, so the garbage collector cannot be running.
1162 void runtime_entersyscall(void) __attribute__ ((no_split_stack));
1165 runtime_entersyscall(void)
1169 if(m->profilehz > 0)
1170 runtime_setprof(false);
1172 // Leave SP around for gc and traceback.
1173 #ifdef USING_SPLIT_STACK
1174 g->gcstack = __splitstack_find(NULL, NULL, &g->gcstack_size,
1175 &g->gcnext_segment, &g->gcnext_sp,
1178 g->gcnext_sp = (byte *) &v;
1181 // Save the registers in the g structure so that any pointers
1182 // held in registers will be seen by the garbage collector.
1183 // We could use getcontext here, but setjmp is more efficient
1184 // because it doesn't need to save the signal mask.
1187 g->status = Gsyscall;
1190 // The slow path inside the schedlock/schedunlock will get
1191 // through without stopping if it does:
1194 // waitstop && mcpu <= mcpumax not true
1195 // If we can do the same with a single atomic add,
1196 // then we can skip the locks.
1197 v = runtime_xadd(&runtime_sched.atomic, -1<<mcpuShift);
1198 if(!atomic_gwaiting(v) && (!atomic_waitstop(v) || atomic_mcpu(v) > atomic_mcpumax(v)))
1202 v = runtime_atomicload(&runtime_sched.atomic);
1203 if(atomic_gwaiting(v)) {
1205 v = runtime_atomicload(&runtime_sched.atomic);
1207 if(atomic_waitstop(v) && atomic_mcpu(v) <= atomic_mcpumax(v)) {
1208 runtime_xadd(&runtime_sched.atomic, -1<<waitstopShift);
1209 runtime_notewakeup(&runtime_sched.stopped);
1215 // The goroutine g exited its system call.
1216 // Arrange for it to run on a cpu again.
1217 // This is called only from the go syscall library, not
1218 // from the low-level system calls used by the runtime.
1220 runtime_exitsyscall(void)
1226 // If we can do the mcpu++ bookkeeping and
1227 // find that we still have mcpu <= mcpumax, then we can
1228 // start executing Go code immediately, without having to
1229 // schedlock/schedunlock.
1231 v = runtime_xadd(&runtime_sched.atomic, (1<<mcpuShift));
1232 if(m->profilehz == runtime_sched.profilehz && atomic_mcpu(v) <= atomic_mcpumax(v)) {
1233 // There's a cpu for us, so we can run.
1234 gp->status = Grunning;
1235 // Garbage collector isn't running (since we are),
1236 // so okay to clear gcstack.
1237 #ifdef USING_SPLIT_STACK
1240 gp->gcnext_sp = nil;
1241 runtime_memclr(gp->gcregs, sizeof gp->gcregs);
1243 if(m->profilehz > 0)
1244 runtime_setprof(true);
1248 // Tell scheduler to put g back on the run queue:
1249 // mostly equivalent to g->status = Grunning,
1250 // but keeps the garbage collector from thinking
1251 // that g is running right now, which it's not.
1252 gp->readyonstop = 1;
1254 // All the cpus are taken.
1255 // The scheduler will ready g and put this m to sleep.
1256 // When the scheduler takes g away from m,
1257 // it will undo the runtime_sched.mcpu++ above.
1260 // Gosched returned, so we're allowed to run now.
1261 // Delete the gcstack information that we left for
1262 // the garbage collector during the system call.
1263 // Must wait until now because until gosched returns
1264 // we don't know for sure that the garbage collector
1266 #ifdef USING_SPLIT_STACK
1269 gp->gcnext_sp = nil;
1270 runtime_memclr(gp->gcregs, sizeof gp->gcregs);
1273 // Allocate a new g, with a stack big enough for stacksize bytes.
1275 runtime_malg(int32 stacksize, byte** ret_stack, size_t* ret_stacksize)
1279 newg = runtime_malloc(sizeof(G));
1280 if(stacksize >= 0) {
1281 #if USING_SPLIT_STACK
1282 int dont_block_signals = 0;
1284 *ret_stack = __splitstack_makecontext(stacksize,
1285 &newg->stack_context[0],
1287 __splitstack_block_signals_context(&newg->stack_context[0],
1288 &dont_block_signals, nil);
1290 *ret_stack = runtime_mallocgc(stacksize, FlagNoProfiling|FlagNoGC, 0, 0);
1291 *ret_stacksize = stacksize;
1292 newg->gcinitial_sp = *ret_stack;
1293 newg->gcstack_size = stacksize;
1294 runtime_xadd(&runtime_stacks_sys, stacksize);
1300 /* For runtime package testing. */
1302 void runtime_testing_entersyscall(void)
1303 __asm__("libgo_runtime.runtime.entersyscall");
1306 runtime_testing_entersyscall()
1308 runtime_entersyscall();
1311 void runtime_testing_exitsyscall(void)
1312 __asm__("libgo_runtime.runtime.exitsyscall");
1315 runtime_testing_exitsyscall()
1317 runtime_exitsyscall();
1321 __go_go(void (*fn)(void*), void* arg)
1325 G * volatile newg; // volatile to avoid longjmp warning
1329 if((newg = gfget()) != nil){
1330 #ifdef USING_SPLIT_STACK
1331 int dont_block_signals = 0;
1333 sp = __splitstack_resetcontext(&newg->stack_context[0],
1335 __splitstack_block_signals_context(&newg->stack_context[0],
1336 &dont_block_signals, nil);
1338 sp = newg->gcinitial_sp;
1339 spsize = newg->gcstack_size;
1341 runtime_throw("bad spsize in __go_go");
1342 newg->gcnext_sp = sp;
1345 newg = runtime_malg(StackMin, &sp, &spsize);
1346 if(runtime_lastg == nil)
1347 runtime_allg = newg;
1349 runtime_lastg->alllink = newg;
1350 runtime_lastg = newg;
1352 newg->status = Gwaiting;
1353 newg->waitreason = "new goroutine";
1355 newg->entry = (byte*)fn;
1357 newg->gopc = (uintptr)__builtin_return_address(0);
1359 runtime_sched.gcount++;
1360 runtime_sched.goidgen++;
1361 newg->goid = runtime_sched.goidgen;
1364 runtime_throw("nil g->stack0");
1366 getcontext(&newg->context);
1367 newg->context.uc_stack.ss_sp = sp;
1368 #ifdef MAKECONTEXT_STACK_TOP
1369 newg->context.uc_stack.ss_sp += spsize;
1371 newg->context.uc_stack.ss_size = spsize;
1372 makecontext(&newg->context, kickoff, 0);
1374 newprocreadylocked(newg);
1378 //printf(" goid=%d\n", newg->goid);
1381 // Put on gfree list. Sched must be locked.
1385 g->schedlink = runtime_sched.gfree;
1386 runtime_sched.gfree = g;
1389 // Get from gfree list. Sched must be locked.
1395 g = runtime_sched.gfree;
1397 runtime_sched.gfree = g->schedlink;
1401 // Run all deferred functions for the current goroutine.
1407 while((d = g->defer) != nil) {
1414 g->defer = d->__next;
1419 void runtime_Goexit (void) asm ("libgo_runtime.runtime.Goexit");
1422 runtime_Goexit(void)
1428 void runtime_Gosched (void) asm ("libgo_runtime.runtime.Gosched");
1431 runtime_Gosched(void)
1436 // Implementation of runtime.GOMAXPROCS.
1437 // delete when scheduler is stronger
1439 runtime_gomaxprocsfunc(int32 n)
1445 ret = runtime_gomaxprocs;
1448 if(n > maxgomaxprocs)
1450 runtime_gomaxprocs = n;
1451 if(runtime_gomaxprocs > 1)
1452 runtime_singleproc = false;
1453 if(runtime_gcwaiting != 0) {
1454 if(atomic_mcpumax(runtime_sched.atomic) != 1)
1455 runtime_throw("invalid mcpumax during gc");
1462 // If there are now fewer allowed procs
1463 // than procs running, stop.
1464 v = runtime_atomicload(&runtime_sched.atomic);
1465 if((int32)atomic_mcpu(v) > n) {
1470 // handle more procs
1477 runtime_LockOSThread(void)
1479 if(m == &runtime_m0 && runtime_sched.init) {
1480 runtime_sched.lockmain = true;
1488 runtime_UnlockOSThread(void)
1490 if(m == &runtime_m0 && runtime_sched.init) {
1491 runtime_sched.lockmain = false;
1499 runtime_lockedOSThread(void)
1501 return g->lockedm != nil && m->lockedg != nil;
1504 // for testing of callbacks
1506 _Bool runtime_golockedOSThread(void)
1507 asm("libgo_runtime.runtime.golockedOSThread");
1510 runtime_golockedOSThread(void)
1512 return runtime_lockedOSThread();
1515 // for testing of wire, unwire
1522 int32 runtime_NumGoroutine (void)
1523 __asm__ ("libgo_runtime.runtime.NumGoroutine");
1526 runtime_NumGoroutine()
1528 return runtime_sched.gcount;
1532 runtime_gcount(void)
1534 return runtime_sched.gcount;
1538 runtime_mcount(void)
1540 return runtime_sched.mcount;
1545 void (*fn)(uintptr*, int32);
1550 // Called if we receive a SIGPROF signal.
1552 runtime_sigprof(uint8 *pc __attribute__ ((unused)),
1553 uint8 *sp __attribute__ ((unused)),
1554 uint8 *lr __attribute__ ((unused)),
1555 G *gp __attribute__ ((unused)))
1559 if(prof.fn == nil || prof.hz == 0)
1562 runtime_lock(&prof);
1563 if(prof.fn == nil) {
1564 runtime_unlock(&prof);
1567 // n = runtime_gentraceback(pc, sp, lr, gp, 0, prof.pcbuf, nelem(prof.pcbuf));
1569 // prof.fn(prof.pcbuf, n);
1570 runtime_unlock(&prof);
1573 // Arrange to call fn with a traceback hz times a second.
1575 runtime_setcpuprofilerate(void (*fn)(uintptr*, int32), int32 hz)
1577 // Force sane arguments.
1585 // Stop profiler on this cpu so that it is safe to lock prof.
1586 // if a profiling signal came in while we had prof locked,
1587 // it would deadlock.
1588 runtime_resetcpuprofiler(0);
1590 runtime_lock(&prof);
1593 runtime_unlock(&prof);
1594 runtime_lock(&runtime_sched);
1595 runtime_sched.profilehz = hz;
1596 runtime_unlock(&runtime_sched);
1599 runtime_resetcpuprofiler(hz);