runtime: In backtraces, get inline functions, skip split-stack fns.
[gcc.git] / libgo / runtime / proc.c
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.
4
5 #include <limits.h>
6 #include <stdlib.h>
7 #include <pthread.h>
8 #include <unistd.h>
9
10 #include "config.h"
11
12 #ifdef HAVE_DL_ITERATE_PHDR
13 #include <link.h>
14 #endif
15
16 #include "runtime.h"
17 #include "arch.h"
18 #include "defs.h"
19 #include "malloc.h"
20 #include "race.h"
21 #include "go-type.h"
22 #include "go-defer.h"
23
24 #ifdef USING_SPLIT_STACK
25
26 /* FIXME: These are not declared anywhere. */
27
28 extern void __splitstack_getcontext(void *context[10]);
29
30 extern void __splitstack_setcontext(void *context[10]);
31
32 extern void *__splitstack_makecontext(size_t, void *context[10], size_t *);
33
34 extern void * __splitstack_resetcontext(void *context[10], size_t *);
35
36 extern void *__splitstack_find(void *, void *, size_t *, void **, void **,
37 void **);
38
39 extern void __splitstack_block_signals (int *, int *);
40
41 extern void __splitstack_block_signals_context (void *context[10], int *,
42 int *);
43
44 #endif
45
46 #ifndef PTHREAD_STACK_MIN
47 # define PTHREAD_STACK_MIN 8192
48 #endif
49
50 #if defined(USING_SPLIT_STACK) && defined(LINKER_SUPPORTS_SPLIT_STACK)
51 # define StackMin PTHREAD_STACK_MIN
52 #else
53 # define StackMin 2 * 1024 * 1024
54 #endif
55
56 uintptr runtime_stacks_sys;
57
58 static void schedule(G*);
59
60 static void gtraceback(G*);
61
62 typedef struct Sched Sched;
63
64 M runtime_m0;
65 G runtime_g0; // idle goroutine for m0
66
67 #ifdef __rtems__
68 #define __thread
69 #endif
70
71 static __thread G *g;
72 static __thread M *m;
73
74 #ifndef SETCONTEXT_CLOBBERS_TLS
75
76 static inline void
77 initcontext(void)
78 {
79 }
80
81 static inline void
82 fixcontext(ucontext_t *c __attribute__ ((unused)))
83 {
84 }
85
86 #else
87
88 # if defined(__x86_64__) && defined(__sun__)
89
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
95 // same.
96
97 static __thread greg_t fs;
98
99 static inline void
100 initcontext(void)
101 {
102 ucontext_t c;
103
104 getcontext(&c);
105 fs = c.uc_mcontext.gregs[REG_FSBASE];
106 }
107
108 static inline void
109 fixcontext(ucontext_t* c)
110 {
111 c->uc_mcontext.gregs[REG_FSBASE] = fs;
112 }
113
114 # elif defined(__NetBSD__)
115
116 // NetBSD has a bug: setcontext clobbers tlsbase, we need to save
117 // and restore it ourselves.
118
119 static __thread __greg_t tlsbase;
120
121 static inline void
122 initcontext(void)
123 {
124 ucontext_t c;
125
126 getcontext(&c);
127 tlsbase = c.uc_mcontext._mc_tlsbase;
128 }
129
130 static inline void
131 fixcontext(ucontext_t* c)
132 {
133 c->uc_mcontext._mc_tlsbase = tlsbase;
134 }
135
136 # else
137
138 # error unknown case for SETCONTEXT_CLOBBERS_TLS
139
140 # endif
141
142 #endif
143
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
150 // when necessary.
151
152 G* runtime_g(void) __attribute__ ((noinline, no_split_stack));
153
154 G*
155 runtime_g(void)
156 {
157 return g;
158 }
159
160 M* runtime_m(void) __attribute__ ((noinline, no_split_stack));
161
162 M*
163 runtime_m(void)
164 {
165 return m;
166 }
167
168 int32 runtime_gcwaiting;
169
170 G* runtime_allg;
171 G* runtime_lastg;
172 M* runtime_allm;
173
174 int8* runtime_goos;
175 int32 runtime_ncpu;
176
177 // The static TLS size. See runtime_newm.
178 static int tlssize;
179
180 #ifdef HAVE_DL_ITERATE_PHDR
181
182 // Called via dl_iterate_phdr.
183
184 static int
185 addtls(struct dl_phdr_info* info, size_t size __attribute__ ((unused)), void *data)
186 {
187 size_t *total = (size_t *)data;
188 unsigned int i;
189
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;
193 }
194 return 0;
195 }
196
197 // Set the total TLS size.
198
199 static void
200 inittlssize()
201 {
202 size_t total = 0;
203
204 dl_iterate_phdr(addtls, (void *)&total);
205 tlssize = total;
206 }
207
208 #else
209
210 static void
211 inittlssize()
212 {
213 }
214
215 #endif
216
217 // Go scheduler
218 //
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.
224 //
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.
231 //
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.
239 //
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
242 // Linux and OS X.
243
244 struct Sched {
245 Lock;
246
247 G *gfree; // available g's (status == Gdead)
248 int64 goidgen;
249
250 G *ghead; // g's waiting to run
251 G *gtail;
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
255
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
259
260 volatile uint32 atomic; // atomic scheduling word (see below)
261
262 int32 profilehz; // cpu profiling rate
263
264 bool init; // running initialization
265 bool lockmain; // init called runtime.LockOSThread
266
267 Note stopped; // one g can set waitstop and wait here for m's to stop
268 };
269
270 // The atomic word in sched is an atomic uint32 that
271 // holds these fields.
272 //
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
277 //
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.
284 //
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).
290 //
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.
297 enum {
298 mcpuWidth = 15,
299 mcpuMask = (1<<mcpuWidth) - 1,
300 mcpuShift = 0,
301 mcpumaxShift = mcpuShift + mcpuWidth,
302 waitstopShift = mcpumaxShift + mcpuWidth,
303 gwaitingShift = waitstopShift+1,
304
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
309 // beyond zero.
310 maxgomaxprocs = mcpuMask - 10,
311 };
312
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)
317
318 Sched runtime_sched;
319 int32 runtime_gomaxprocs;
320 bool runtime_singleproc;
321
322 static bool canaddmcpu(void);
323
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.
329 static M* mwakeup;
330
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
335 static M* mget(G*);
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*);
342
343 void
344 setmcpumax(uint32 n)
345 {
346 uint32 v, w;
347
348 for(;;) {
349 v = runtime_sched.atomic;
350 w = v;
351 w &= ~(mcpuMask<<mcpumaxShift);
352 w |= n<<mcpumaxShift;
353 if(runtime_cas(&runtime_sched.atomic, v, w))
354 break;
355 }
356 }
357
358 // First function run by a new goroutine. This replaces gogocall.
359 static void
360 kickoff(void)
361 {
362 void (*fn)(void*);
363
364 if(g->traceback != nil)
365 gtraceback(g);
366
367 fn = (void (*)(void*))(g->entry);
368 fn(g->param);
369 runtime_goexit();
370 }
371
372 // Switch context to a different goroutine. This is like longjmp.
373 static void runtime_gogo(G*) __attribute__ ((noinline));
374 static void
375 runtime_gogo(G* newg)
376 {
377 #ifdef USING_SPLIT_STACK
378 __splitstack_setcontext(&newg->stack_context[0]);
379 #endif
380 g = newg;
381 newg->fromgogo = true;
382 fixcontext(&newg->context);
383 setcontext(&newg->context);
384 runtime_throw("gogo setcontext returned");
385 }
386
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));
392 static void
393 runtime_mcall(void (*pfn)(G*))
394 {
395 M *mp;
396 G *gp;
397 #ifndef USING_SPLIT_STACK
398 int i;
399 #endif
400
401 // Ensure that all registers are on the stack for the garbage
402 // collector.
403 __builtin_unwind_init();
404
405 mp = m;
406 gp = g;
407 if(gp == mp->g0)
408 runtime_throw("runtime: mcall called on m->g0 stack");
409
410 if(gp != nil) {
411
412 #ifdef USING_SPLIT_STACK
413 __splitstack_getcontext(&g->stack_context[0]);
414 #else
415 gp->gcnext_sp = &i;
416 #endif
417 gp->fromgogo = false;
418 getcontext(&gp->context);
419
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.
427 mp = runtime_m();
428 gp = runtime_g();
429
430 if(gp->traceback != nil)
431 gtraceback(gp);
432 }
433 if (gp == nil || !gp->fromgogo) {
434 #ifdef USING_SPLIT_STACK
435 __splitstack_setcontext(&mp->g0->stack_context[0]);
436 #endif
437 mp->g0->entry = (byte*)pfn;
438 mp->g0->param = gp;
439
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.
443 g = mp->g0;
444
445 fixcontext(&mp->g0->context);
446 setcontext(&mp->g0->context);
447 runtime_throw("runtime: mcall function returned");
448 }
449 }
450
451 // Keep trace of scavenger's goroutine for deadlock detection.
452 static G *scvg;
453
454 // The bootstrap sequence is:
455 //
456 // call osinit
457 // call schedinit
458 // make & queue new G
459 // call runtime_mstart
460 //
461 // The new G calls runtime_main.
462 void
463 runtime_schedinit(void)
464 {
465 int32 n;
466 const byte *p;
467
468 m = &runtime_m0;
469 g = &runtime_g0;
470 m->g0 = g;
471 m->curg = g;
472 g->m = m;
473
474 initcontext();
475 inittlssize();
476
477 m->nomemprof++;
478 runtime_mallocinit();
479 mcommoninit(m);
480
481 runtime_goargs();
482 runtime_goenvs();
483
484 // For debugging:
485 // Allocate internal symbol table representation now,
486 // so that we don't need to call malloc when we crash.
487 // runtime_findfunc(0);
488
489 runtime_gomaxprocs = 1;
490 p = runtime_getenv("GOMAXPROCS");
491 if(p != nil && (n = runtime_atoi(p)) != 0) {
492 if(n > maxgomaxprocs)
493 n = maxgomaxprocs;
494 runtime_gomaxprocs = n;
495 }
496 // wait for the main goroutine to start before taking
497 // GOMAXPROCS into account.
498 setmcpumax(1);
499 runtime_singleproc = runtime_gomaxprocs == 1;
500
501 canaddmcpu(); // mcpu++ to account for bootstrap m
502 m->helpgc = 1; // flag to tell schedule() to mcpu--
503 runtime_sched.grunning++;
504
505 // Can not enable GC until all roots are registered.
506 // mstats.enablegc = 1;
507 m->nomemprof--;
508
509 if(raceenabled)
510 runtime_raceinit();
511 }
512
513 extern void main_init(void) __asm__ (GOSYM_PREFIX "__go_init_main");
514 extern void main_main(void) __asm__ (GOSYM_PREFIX "main.main");
515
516 // The main goroutine.
517 void
518 runtime_main(void)
519 {
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);
531 scvg->issystem = true;
532 main_init();
533 runtime_sched.init = false;
534 if(!runtime_sched.lockmain)
535 runtime_UnlockOSThread();
536
537 // For gccgo we have to wait until after main is initialized
538 // to enable GC, because initializing main registers the GC
539 // roots.
540 mstats.enablegc = 1;
541
542 // The deadlock detection has false negatives.
543 // Let scvg start up, to eliminate the false negative
544 // for the trivial program func main() { select{} }.
545 runtime_gosched();
546
547 main_main();
548 if(raceenabled)
549 runtime_racefini();
550 runtime_exit(0);
551 for(;;)
552 *(int32*)0 = 0;
553 }
554
555 // Lock the scheduler.
556 static void
557 schedlock(void)
558 {
559 runtime_lock(&runtime_sched);
560 }
561
562 // Unlock the scheduler.
563 static void
564 schedunlock(void)
565 {
566 M *mp;
567
568 mp = mwakeup;
569 mwakeup = nil;
570 runtime_unlock(&runtime_sched);
571 if(mp != nil)
572 runtime_notewakeup(&mp->havenextg);
573 }
574
575 void
576 runtime_goexit(void)
577 {
578 g->status = Gmoribund;
579 runtime_gosched();
580 }
581
582 void
583 runtime_goroutineheader(G *gp)
584 {
585 const char *status;
586
587 switch(gp->status) {
588 case Gidle:
589 status = "idle";
590 break;
591 case Grunnable:
592 status = "runnable";
593 break;
594 case Grunning:
595 status = "running";
596 break;
597 case Gsyscall:
598 status = "syscall";
599 break;
600 case Gwaiting:
601 if(gp->waitreason)
602 status = gp->waitreason;
603 else
604 status = "waiting";
605 break;
606 case Gmoribund:
607 status = "moribund";
608 break;
609 default:
610 status = "???";
611 break;
612 }
613 runtime_printf("goroutine %D [%s]:\n", gp->goid, status);
614 }
615
616 void
617 runtime_goroutinetrailer(G *g)
618 {
619 if(g != nil && g->gopc != 0 && g->goid != 1) {
620 String fn;
621 String file;
622 intgo line;
623
624 if(__go_file_line(g->gopc - 1, &fn, &file, &line)) {
625 runtime_printf("created by %S\n", fn);
626 runtime_printf("\t%S:%D\n", file, (int64) line);
627 }
628 }
629 }
630
631 struct Traceback
632 {
633 G* gp;
634 Location locbuf[100];
635 int32 c;
636 };
637
638 void
639 runtime_tracebackothers(G * volatile me)
640 {
641 G * volatile gp;
642 Traceback tb;
643 int32 traceback;
644
645 tb.gp = me;
646 traceback = runtime_gotraceback();
647 for(gp = runtime_allg; gp != nil; gp = gp->alllink) {
648 if(gp == me || gp->status == Gdead)
649 continue;
650 if(gp->issystem && traceback < 2)
651 continue;
652 runtime_printf("\n");
653 runtime_goroutineheader(gp);
654
655 // Our only mechanism for doing a stack trace is
656 // _Unwind_Backtrace. And that only works for the
657 // current thread, not for other random goroutines.
658 // So we need to switch context to the goroutine, get
659 // the backtrace, and then switch back.
660
661 // This means that if g is running or in a syscall, we
662 // can't reliably print a stack trace. FIXME.
663 if(gp->status == Gsyscall || gp->status == Grunning) {
664 runtime_printf("no stack trace available\n");
665 runtime_goroutinetrailer(gp);
666 continue;
667 }
668
669 gp->traceback = &tb;
670
671 #ifdef USING_SPLIT_STACK
672 __splitstack_getcontext(&me->stack_context[0]);
673 #endif
674 getcontext(&me->context);
675
676 if(gp->traceback != nil) {
677 runtime_gogo(gp);
678 }
679
680 runtime_printtrace(tb.locbuf, tb.c, false);
681 runtime_goroutinetrailer(gp);
682 }
683 }
684
685 // Do a stack trace of gp, and then restore the context to
686 // gp->dotraceback.
687
688 static void
689 gtraceback(G* gp)
690 {
691 Traceback* traceback;
692
693 traceback = gp->traceback;
694 gp->traceback = nil;
695 traceback->c = runtime_callers(1, traceback->locbuf,
696 sizeof traceback->locbuf / sizeof traceback->locbuf[0]);
697 runtime_gogo(traceback->gp);
698 }
699
700 // Mark this g as m's idle goroutine.
701 // This functionality might be used in environments where programs
702 // are limited to a single thread, to simulate a select-driven
703 // network server. It is not exposed via the standard runtime API.
704 void
705 runtime_idlegoroutine(void)
706 {
707 if(g->idlem != nil)
708 runtime_throw("g is already an idle goroutine");
709 g->idlem = m;
710 }
711
712 static void
713 mcommoninit(M *mp)
714 {
715 mp->id = runtime_sched.mcount++;
716 mp->fastrand = 0x49f6428aUL + mp->id + runtime_cputicks();
717
718 if(mp->mcache == nil)
719 mp->mcache = runtime_allocmcache();
720
721 runtime_callers(1, mp->createstack, nelem(mp->createstack));
722
723 // Add to runtime_allm so garbage collector doesn't free m
724 // when it is just in a register or thread-local storage.
725 mp->alllink = runtime_allm;
726 // runtime_NumCgoCall() iterates over allm w/o schedlock,
727 // so we need to publish it safely.
728 runtime_atomicstorep(&runtime_allm, mp);
729 }
730
731 // Try to increment mcpu. Report whether succeeded.
732 static bool
733 canaddmcpu(void)
734 {
735 uint32 v;
736
737 for(;;) {
738 v = runtime_sched.atomic;
739 if(atomic_mcpu(v) >= atomic_mcpumax(v))
740 return 0;
741 if(runtime_cas(&runtime_sched.atomic, v, v+(1<<mcpuShift)))
742 return 1;
743 }
744 }
745
746 // Put on `g' queue. Sched must be locked.
747 static void
748 gput(G *gp)
749 {
750 M *mp;
751
752 // If g is wired, hand it off directly.
753 if((mp = gp->lockedm) != nil && canaddmcpu()) {
754 mnextg(mp, gp);
755 return;
756 }
757
758 // If g is the idle goroutine for an m, hand it off.
759 if(gp->idlem != nil) {
760 if(gp->idlem->idleg != nil) {
761 runtime_printf("m%d idle out of sync: g%D g%D\n",
762 gp->idlem->id,
763 gp->idlem->idleg->goid, gp->goid);
764 runtime_throw("runtime: double idle");
765 }
766 gp->idlem->idleg = gp;
767 return;
768 }
769
770 gp->schedlink = nil;
771 if(runtime_sched.ghead == nil)
772 runtime_sched.ghead = gp;
773 else
774 runtime_sched.gtail->schedlink = gp;
775 runtime_sched.gtail = gp;
776
777 // increment gwait.
778 // if it transitions to nonzero, set atomic gwaiting bit.
779 if(runtime_sched.gwait++ == 0)
780 runtime_xadd(&runtime_sched.atomic, 1<<gwaitingShift);
781 }
782
783 // Report whether gget would return something.
784 static bool
785 haveg(void)
786 {
787 return runtime_sched.ghead != nil || m->idleg != nil;
788 }
789
790 // Get from `g' queue. Sched must be locked.
791 static G*
792 gget(void)
793 {
794 G *gp;
795
796 gp = runtime_sched.ghead;
797 if(gp) {
798 runtime_sched.ghead = gp->schedlink;
799 if(runtime_sched.ghead == nil)
800 runtime_sched.gtail = nil;
801 // decrement gwait.
802 // if it transitions to zero, clear atomic gwaiting bit.
803 if(--runtime_sched.gwait == 0)
804 runtime_xadd(&runtime_sched.atomic, -1<<gwaitingShift);
805 } else if(m->idleg != nil) {
806 gp = m->idleg;
807 m->idleg = nil;
808 }
809 return gp;
810 }
811
812 // Put on `m' list. Sched must be locked.
813 static void
814 mput(M *mp)
815 {
816 mp->schedlink = runtime_sched.mhead;
817 runtime_sched.mhead = mp;
818 runtime_sched.mwait++;
819 }
820
821 // Get an `m' to run `g'. Sched must be locked.
822 static M*
823 mget(G *gp)
824 {
825 M *mp;
826
827 // if g has its own m, use it.
828 if(gp && (mp = gp->lockedm) != nil)
829 return mp;
830
831 // otherwise use general m pool.
832 if((mp = runtime_sched.mhead) != nil) {
833 runtime_sched.mhead = mp->schedlink;
834 runtime_sched.mwait--;
835 }
836 return mp;
837 }
838
839 // Mark g ready to run.
840 void
841 runtime_ready(G *gp)
842 {
843 schedlock();
844 readylocked(gp);
845 schedunlock();
846 }
847
848 // Mark g ready to run. Sched is already locked.
849 // G might be running already and about to stop.
850 // The sched lock protects g->status from changing underfoot.
851 static void
852 readylocked(G *gp)
853 {
854 if(gp->m) {
855 // Running on another machine.
856 // Ready it when it stops.
857 gp->readyonstop = 1;
858 return;
859 }
860
861 // Mark runnable.
862 if(gp->status == Grunnable || gp->status == Grunning) {
863 runtime_printf("goroutine %D has status %d\n", gp->goid, gp->status);
864 runtime_throw("bad g->status in ready");
865 }
866 gp->status = Grunnable;
867
868 gput(gp);
869 matchmg();
870 }
871
872 // Same as readylocked but a different symbol so that
873 // debuggers can set a breakpoint here and catch all
874 // new goroutines.
875 static void
876 newprocreadylocked(G *gp)
877 {
878 readylocked(gp);
879 }
880
881 // Pass g to m for running.
882 // Caller has already incremented mcpu.
883 static void
884 mnextg(M *mp, G *gp)
885 {
886 runtime_sched.grunning++;
887 mp->nextg = gp;
888 if(mp->waitnextg) {
889 mp->waitnextg = 0;
890 if(mwakeup != nil)
891 runtime_notewakeup(&mwakeup->havenextg);
892 mwakeup = mp;
893 }
894 }
895
896 // Get the next goroutine that m should run.
897 // Sched must be locked on entry, is unlocked on exit.
898 // Makes sure that at most $GOMAXPROCS g's are
899 // running on cpus (not in system calls) at any given time.
900 static G*
901 nextgandunlock(void)
902 {
903 G *gp;
904 uint32 v;
905
906 top:
907 if(atomic_mcpu(runtime_sched.atomic) >= maxgomaxprocs)
908 runtime_throw("negative mcpu");
909
910 // If there is a g waiting as m->nextg, the mcpu++
911 // happened before it was passed to mnextg.
912 if(m->nextg != nil) {
913 gp = m->nextg;
914 m->nextg = nil;
915 schedunlock();
916 return gp;
917 }
918
919 if(m->lockedg != nil) {
920 // We can only run one g, and it's not available.
921 // Make sure some other cpu is running to handle
922 // the ordinary run queue.
923 if(runtime_sched.gwait != 0) {
924 matchmg();
925 // m->lockedg might have been on the queue.
926 if(m->nextg != nil) {
927 gp = m->nextg;
928 m->nextg = nil;
929 schedunlock();
930 return gp;
931 }
932 }
933 } else {
934 // Look for work on global queue.
935 while(haveg() && canaddmcpu()) {
936 gp = gget();
937 if(gp == nil)
938 runtime_throw("gget inconsistency");
939
940 if(gp->lockedm) {
941 mnextg(gp->lockedm, gp);
942 continue;
943 }
944 runtime_sched.grunning++;
945 schedunlock();
946 return gp;
947 }
948
949 // The while loop ended either because the g queue is empty
950 // or because we have maxed out our m procs running go
951 // code (mcpu >= mcpumax). We need to check that
952 // concurrent actions by entersyscall/exitsyscall cannot
953 // invalidate the decision to end the loop.
954 //
955 // We hold the sched lock, so no one else is manipulating the
956 // g queue or changing mcpumax. Entersyscall can decrement
957 // mcpu, but if does so when there is something on the g queue,
958 // the gwait bit will be set, so entersyscall will take the slow path
959 // and use the sched lock. So it cannot invalidate our decision.
960 //
961 // Wait on global m queue.
962 mput(m);
963 }
964
965 // Look for deadlock situation.
966 // There is a race with the scavenger that causes false negatives:
967 // if the scavenger is just starting, then we have
968 // scvg != nil && grunning == 0 && gwait == 0
969 // and we do not detect a deadlock. It is possible that we should
970 // add that case to the if statement here, but it is too close to Go 1
971 // to make such a subtle change. Instead, we work around the
972 // false negative in trivial programs by calling runtime.gosched
973 // from the main goroutine just before main.main.
974 // See runtime_main above.
975 //
976 // On a related note, it is also possible that the scvg == nil case is
977 // wrong and should include gwait, but that does not happen in
978 // standard Go programs, which all start the scavenger.
979 //
980 if((scvg == nil && runtime_sched.grunning == 0) ||
981 (scvg != nil && runtime_sched.grunning == 1 && runtime_sched.gwait == 0 &&
982 (scvg->status == Grunning || scvg->status == Gsyscall))) {
983 m->throwing = -1; // do not dump full stacks
984 runtime_throw("all goroutines are asleep - deadlock!");
985 }
986
987 m->nextg = nil;
988 m->waitnextg = 1;
989 runtime_noteclear(&m->havenextg);
990
991 // Stoptheworld is waiting for all but its cpu to go to stop.
992 // Entersyscall might have decremented mcpu too, but if so
993 // it will see the waitstop and take the slow path.
994 // Exitsyscall never increments mcpu beyond mcpumax.
995 v = runtime_atomicload(&runtime_sched.atomic);
996 if(atomic_waitstop(v) && atomic_mcpu(v) <= atomic_mcpumax(v)) {
997 // set waitstop = 0 (known to be 1)
998 runtime_xadd(&runtime_sched.atomic, -1<<waitstopShift);
999 runtime_notewakeup(&runtime_sched.stopped);
1000 }
1001 schedunlock();
1002
1003 runtime_notesleep(&m->havenextg);
1004 if(m->helpgc) {
1005 runtime_gchelper();
1006 m->helpgc = 0;
1007 runtime_lock(&runtime_sched);
1008 goto top;
1009 }
1010 if((gp = m->nextg) == nil)
1011 runtime_throw("bad m->nextg in nextgoroutine");
1012 m->nextg = nil;
1013 return gp;
1014 }
1015
1016 int32
1017 runtime_gcprocs(void)
1018 {
1019 int32 n;
1020
1021 // Figure out how many CPUs to use during GC.
1022 // Limited by gomaxprocs, number of actual CPUs, and MaxGcproc.
1023 n = runtime_gomaxprocs;
1024 if(n > runtime_ncpu)
1025 n = runtime_ncpu > 0 ? runtime_ncpu : 1;
1026 if(n > MaxGcproc)
1027 n = MaxGcproc;
1028 if(n > runtime_sched.mwait+1) // one M is currently running
1029 n = runtime_sched.mwait+1;
1030 return n;
1031 }
1032
1033 void
1034 runtime_helpgc(int32 nproc)
1035 {
1036 M *mp;
1037 int32 n;
1038
1039 runtime_lock(&runtime_sched);
1040 for(n = 1; n < nproc; n++) { // one M is currently running
1041 mp = mget(nil);
1042 if(mp == nil)
1043 runtime_throw("runtime_gcprocs inconsistency");
1044 mp->helpgc = 1;
1045 mp->waitnextg = 0;
1046 runtime_notewakeup(&mp->havenextg);
1047 }
1048 runtime_unlock(&runtime_sched);
1049 }
1050
1051 void
1052 runtime_stoptheworld(void)
1053 {
1054 uint32 v;
1055
1056 schedlock();
1057 runtime_gcwaiting = 1;
1058
1059 setmcpumax(1);
1060
1061 // while mcpu > 1
1062 for(;;) {
1063 v = runtime_sched.atomic;
1064 if(atomic_mcpu(v) <= 1)
1065 break;
1066
1067 // It would be unsafe for multiple threads to be using
1068 // the stopped note at once, but there is only
1069 // ever one thread doing garbage collection.
1070 runtime_noteclear(&runtime_sched.stopped);
1071 if(atomic_waitstop(v))
1072 runtime_throw("invalid waitstop");
1073
1074 // atomic { waitstop = 1 }, predicated on mcpu <= 1 check above
1075 // still being true.
1076 if(!runtime_cas(&runtime_sched.atomic, v, v+(1<<waitstopShift)))
1077 continue;
1078
1079 schedunlock();
1080 runtime_notesleep(&runtime_sched.stopped);
1081 schedlock();
1082 }
1083 runtime_singleproc = runtime_gomaxprocs == 1;
1084 schedunlock();
1085 }
1086
1087 void
1088 runtime_starttheworld(void)
1089 {
1090 M *mp;
1091 int32 max;
1092
1093 // Figure out how many CPUs GC could possibly use.
1094 max = runtime_gomaxprocs;
1095 if(max > runtime_ncpu)
1096 max = runtime_ncpu > 0 ? runtime_ncpu : 1;
1097 if(max > MaxGcproc)
1098 max = MaxGcproc;
1099
1100 schedlock();
1101 runtime_gcwaiting = 0;
1102 setmcpumax(runtime_gomaxprocs);
1103 matchmg();
1104 if(runtime_gcprocs() < max && canaddmcpu()) {
1105 // If GC could have used another helper proc, start one now,
1106 // in the hope that it will be available next time.
1107 // It would have been even better to start it before the collection,
1108 // but doing so requires allocating memory, so it's tricky to
1109 // coordinate. This lazy approach works out in practice:
1110 // we don't mind if the first couple gc rounds don't have quite
1111 // the maximum number of procs.
1112 // canaddmcpu above did mcpu++
1113 // (necessary, because m will be doing various
1114 // initialization work so is definitely running),
1115 // but m is not running a specific goroutine,
1116 // so set the helpgc flag as a signal to m's
1117 // first schedule(nil) to mcpu-- and grunning--.
1118 mp = runtime_newm();
1119 mp->helpgc = 1;
1120 runtime_sched.grunning++;
1121 }
1122 schedunlock();
1123 }
1124
1125 // Called to start an M.
1126 void*
1127 runtime_mstart(void* mp)
1128 {
1129 m = (M*)mp;
1130 g = m->g0;
1131
1132 initcontext();
1133
1134 g->entry = nil;
1135 g->param = nil;
1136
1137 // Record top of stack for use by mcall.
1138 // Once we call schedule we're never coming back,
1139 // so other calls can reuse this stack space.
1140 #ifdef USING_SPLIT_STACK
1141 __splitstack_getcontext(&g->stack_context[0]);
1142 #else
1143 g->gcinitial_sp = &mp;
1144 // Setting gcstack_size to 0 is a marker meaning that gcinitial_sp
1145 // is the top of the stack, not the bottom.
1146 g->gcstack_size = 0;
1147 g->gcnext_sp = &mp;
1148 #endif
1149 getcontext(&g->context);
1150
1151 if(g->entry != nil) {
1152 // Got here from mcall.
1153 void (*pfn)(G*) = (void (*)(G*))g->entry;
1154 G* gp = (G*)g->param;
1155 pfn(gp);
1156 *(int*)0x21 = 0x21;
1157 }
1158 runtime_minit();
1159
1160 #ifdef USING_SPLIT_STACK
1161 {
1162 int dont_block_signals = 0;
1163 __splitstack_block_signals(&dont_block_signals, nil);
1164 }
1165 #endif
1166
1167 // Install signal handlers; after minit so that minit can
1168 // prepare the thread to be able to handle the signals.
1169 if(m == &runtime_m0)
1170 runtime_initsig();
1171
1172 schedule(nil);
1173
1174 // TODO(brainman): This point is never reached, because scheduler
1175 // does not release os threads at the moment. But once this path
1176 // is enabled, we must remove our seh here.
1177
1178 return nil;
1179 }
1180
1181 typedef struct CgoThreadStart CgoThreadStart;
1182 struct CgoThreadStart
1183 {
1184 M *m;
1185 G *g;
1186 void (*fn)(void);
1187 };
1188
1189 // Kick off new m's as needed (up to mcpumax).
1190 // Sched is locked.
1191 static void
1192 matchmg(void)
1193 {
1194 G *gp;
1195 M *mp;
1196
1197 if(m->mallocing || m->gcing)
1198 return;
1199
1200 while(haveg() && canaddmcpu()) {
1201 gp = gget();
1202 if(gp == nil)
1203 runtime_throw("gget inconsistency");
1204
1205 // Find the m that will run gp.
1206 if((mp = mget(gp)) == nil)
1207 mp = runtime_newm();
1208 mnextg(mp, gp);
1209 }
1210 }
1211
1212 // Create a new m. It will start off with a call to runtime_mstart.
1213 M*
1214 runtime_newm(void)
1215 {
1216 M *mp;
1217 pthread_attr_t attr;
1218 pthread_t tid;
1219 size_t stacksize;
1220
1221 #if 0
1222 static const Type *mtype; // The Go type M
1223 if(mtype == nil) {
1224 Eface e;
1225 runtime_gc_m_ptr(&e);
1226 mtype = ((const PtrType*)e.__type_descriptor)->__element_type;
1227 }
1228 #endif
1229
1230 mp = runtime_mal(sizeof *mp);
1231 mcommoninit(mp);
1232 mp->g0 = runtime_malg(-1, nil, nil);
1233
1234 if(pthread_attr_init(&attr) != 0)
1235 runtime_throw("pthread_attr_init");
1236 if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
1237 runtime_throw("pthread_attr_setdetachstate");
1238
1239 stacksize = PTHREAD_STACK_MIN;
1240
1241 // With glibc before version 2.16 the static TLS size is taken
1242 // out of the stack size, and we get an error or a crash if
1243 // there is not enough stack space left. Add it back in if we
1244 // can, in case the program uses a lot of TLS space. FIXME:
1245 // This can be disabled in glibc 2.16 and later, if the bug is
1246 // indeed fixed then.
1247 stacksize += tlssize;
1248
1249 if(pthread_attr_setstacksize(&attr, stacksize) != 0)
1250 runtime_throw("pthread_attr_setstacksize");
1251
1252 if(pthread_create(&tid, &attr, runtime_mstart, mp) != 0)
1253 runtime_throw("pthread_create");
1254
1255 return mp;
1256 }
1257
1258 // One round of scheduler: find a goroutine and run it.
1259 // The argument is the goroutine that was running before
1260 // schedule was called, or nil if this is the first call.
1261 // Never returns.
1262 static void
1263 schedule(G *gp)
1264 {
1265 int32 hz;
1266 uint32 v;
1267
1268 schedlock();
1269 if(gp != nil) {
1270 // Just finished running gp.
1271 gp->m = nil;
1272 runtime_sched.grunning--;
1273
1274 // atomic { mcpu-- }
1275 v = runtime_xadd(&runtime_sched.atomic, -1<<mcpuShift);
1276 if(atomic_mcpu(v) > maxgomaxprocs)
1277 runtime_throw("negative mcpu in scheduler");
1278
1279 switch(gp->status) {
1280 case Grunnable:
1281 case Gdead:
1282 // Shouldn't have been running!
1283 runtime_throw("bad gp->status in sched");
1284 case Grunning:
1285 gp->status = Grunnable;
1286 gput(gp);
1287 break;
1288 case Gmoribund:
1289 if(raceenabled)
1290 runtime_racegoend(gp->goid);
1291 gp->status = Gdead;
1292 if(gp->lockedm) {
1293 gp->lockedm = nil;
1294 m->lockedg = nil;
1295 }
1296 gp->idlem = nil;
1297 runtime_memclr(&gp->context, sizeof gp->context);
1298 gfput(gp);
1299 if(--runtime_sched.gcount == 0)
1300 runtime_exit(0);
1301 break;
1302 }
1303 if(gp->readyonstop) {
1304 gp->readyonstop = 0;
1305 readylocked(gp);
1306 }
1307 } else if(m->helpgc) {
1308 // Bootstrap m or new m started by starttheworld.
1309 // atomic { mcpu-- }
1310 v = runtime_xadd(&runtime_sched.atomic, -1<<mcpuShift);
1311 if(atomic_mcpu(v) > maxgomaxprocs)
1312 runtime_throw("negative mcpu in scheduler");
1313 // Compensate for increment in starttheworld().
1314 runtime_sched.grunning--;
1315 m->helpgc = 0;
1316 } else if(m->nextg != nil) {
1317 // New m started by matchmg.
1318 } else {
1319 runtime_throw("invalid m state in scheduler");
1320 }
1321
1322 // Find (or wait for) g to run. Unlocks runtime_sched.
1323 gp = nextgandunlock();
1324 gp->readyonstop = 0;
1325 gp->status = Grunning;
1326 m->curg = gp;
1327 gp->m = m;
1328
1329 // Check whether the profiler needs to be turned on or off.
1330 hz = runtime_sched.profilehz;
1331 if(m->profilehz != hz)
1332 runtime_resetcpuprofiler(hz);
1333
1334 runtime_gogo(gp);
1335 }
1336
1337 // Enter scheduler. If g->status is Grunning,
1338 // re-queues g and runs everyone else who is waiting
1339 // before running g again. If g->status is Gmoribund,
1340 // kills off g.
1341 void
1342 runtime_gosched(void)
1343 {
1344 if(m->locks != 0)
1345 runtime_throw("gosched holding locks");
1346 if(g == m->g0)
1347 runtime_throw("gosched of g0");
1348 runtime_mcall(schedule);
1349 }
1350
1351 // Puts the current goroutine into a waiting state and unlocks the lock.
1352 // The goroutine can be made runnable again by calling runtime_ready(gp).
1353 void
1354 runtime_park(void (*unlockf)(Lock*), Lock *lock, const char *reason)
1355 {
1356 g->status = Gwaiting;
1357 g->waitreason = reason;
1358 if(unlockf)
1359 unlockf(lock);
1360 runtime_gosched();
1361 }
1362
1363 // The goroutine g is about to enter a system call.
1364 // Record that it's not using the cpu anymore.
1365 // This is called only from the go syscall library and cgocall,
1366 // not from the low-level system calls used by the runtime.
1367 //
1368 // Entersyscall cannot split the stack: the runtime_gosave must
1369 // make g->sched refer to the caller's stack segment, because
1370 // entersyscall is going to return immediately after.
1371 // It's okay to call matchmg and notewakeup even after
1372 // decrementing mcpu, because we haven't released the
1373 // sched lock yet, so the garbage collector cannot be running.
1374
1375 void runtime_entersyscall(void) __attribute__ ((no_split_stack));
1376
1377 void
1378 runtime_entersyscall(void)
1379 {
1380 uint32 v;
1381
1382 if(m->profilehz > 0)
1383 runtime_setprof(false);
1384
1385 // Leave SP around for gc and traceback.
1386 #ifdef USING_SPLIT_STACK
1387 g->gcstack = __splitstack_find(nil, nil, &g->gcstack_size,
1388 &g->gcnext_segment, &g->gcnext_sp,
1389 &g->gcinitial_sp);
1390 #else
1391 g->gcnext_sp = (byte *) &v;
1392 #endif
1393
1394 // Save the registers in the g structure so that any pointers
1395 // held in registers will be seen by the garbage collector.
1396 getcontext(&g->gcregs);
1397
1398 g->status = Gsyscall;
1399
1400 // Fast path.
1401 // The slow path inside the schedlock/schedunlock will get
1402 // through without stopping if it does:
1403 // mcpu--
1404 // gwait not true
1405 // waitstop && mcpu <= mcpumax not true
1406 // If we can do the same with a single atomic add,
1407 // then we can skip the locks.
1408 v = runtime_xadd(&runtime_sched.atomic, -1<<mcpuShift);
1409 if(!atomic_gwaiting(v) && (!atomic_waitstop(v) || atomic_mcpu(v) > atomic_mcpumax(v)))
1410 return;
1411
1412 schedlock();
1413 v = runtime_atomicload(&runtime_sched.atomic);
1414 if(atomic_gwaiting(v)) {
1415 matchmg();
1416 v = runtime_atomicload(&runtime_sched.atomic);
1417 }
1418 if(atomic_waitstop(v) && atomic_mcpu(v) <= atomic_mcpumax(v)) {
1419 runtime_xadd(&runtime_sched.atomic, -1<<waitstopShift);
1420 runtime_notewakeup(&runtime_sched.stopped);
1421 }
1422
1423 schedunlock();
1424 }
1425
1426 // The goroutine g exited its system call.
1427 // Arrange for it to run on a cpu again.
1428 // This is called only from the go syscall library, not
1429 // from the low-level system calls used by the runtime.
1430 void
1431 runtime_exitsyscall(void)
1432 {
1433 G *gp;
1434 uint32 v;
1435
1436 // Fast path.
1437 // If we can do the mcpu++ bookkeeping and
1438 // find that we still have mcpu <= mcpumax, then we can
1439 // start executing Go code immediately, without having to
1440 // schedlock/schedunlock.
1441 // Also do fast return if any locks are held, so that
1442 // panic code can use syscalls to open a file.
1443 gp = g;
1444 v = runtime_xadd(&runtime_sched.atomic, (1<<mcpuShift));
1445 if((m->profilehz == runtime_sched.profilehz && atomic_mcpu(v) <= atomic_mcpumax(v)) || m->locks > 0) {
1446 // There's a cpu for us, so we can run.
1447 gp->status = Grunning;
1448 // Garbage collector isn't running (since we are),
1449 // so okay to clear gcstack.
1450 #ifdef USING_SPLIT_STACK
1451 gp->gcstack = nil;
1452 #endif
1453 gp->gcnext_sp = nil;
1454 runtime_memclr(&gp->gcregs, sizeof gp->gcregs);
1455
1456 if(m->profilehz > 0)
1457 runtime_setprof(true);
1458 return;
1459 }
1460
1461 // Tell scheduler to put g back on the run queue:
1462 // mostly equivalent to g->status = Grunning,
1463 // but keeps the garbage collector from thinking
1464 // that g is running right now, which it's not.
1465 gp->readyonstop = 1;
1466
1467 // All the cpus are taken.
1468 // The scheduler will ready g and put this m to sleep.
1469 // When the scheduler takes g away from m,
1470 // it will undo the runtime_sched.mcpu++ above.
1471 runtime_gosched();
1472
1473 // Gosched returned, so we're allowed to run now.
1474 // Delete the gcstack information that we left for
1475 // the garbage collector during the system call.
1476 // Must wait until now because until gosched returns
1477 // we don't know for sure that the garbage collector
1478 // is not running.
1479 #ifdef USING_SPLIT_STACK
1480 gp->gcstack = nil;
1481 #endif
1482 gp->gcnext_sp = nil;
1483 runtime_memclr(&gp->gcregs, sizeof gp->gcregs);
1484 }
1485
1486 // Allocate a new g, with a stack big enough for stacksize bytes.
1487 G*
1488 runtime_malg(int32 stacksize, byte** ret_stack, size_t* ret_stacksize)
1489 {
1490 G *newg;
1491
1492 newg = runtime_malloc(sizeof(G));
1493 if(stacksize >= 0) {
1494 #if USING_SPLIT_STACK
1495 int dont_block_signals = 0;
1496
1497 *ret_stack = __splitstack_makecontext(stacksize,
1498 &newg->stack_context[0],
1499 ret_stacksize);
1500 __splitstack_block_signals_context(&newg->stack_context[0],
1501 &dont_block_signals, nil);
1502 #else
1503 *ret_stack = runtime_mallocgc(stacksize, FlagNoProfiling|FlagNoGC, 0, 0);
1504 *ret_stacksize = stacksize;
1505 newg->gcinitial_sp = *ret_stack;
1506 newg->gcstack_size = stacksize;
1507 runtime_xadd(&runtime_stacks_sys, stacksize);
1508 #endif
1509 }
1510 return newg;
1511 }
1512
1513 /* For runtime package testing. */
1514
1515 void runtime_testing_entersyscall(void)
1516 __asm__ (GOSYM_PREFIX "runtime.entersyscall");
1517
1518 void
1519 runtime_testing_entersyscall()
1520 {
1521 runtime_entersyscall();
1522 }
1523
1524 void runtime_testing_exitsyscall(void)
1525 __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
1526
1527 void
1528 runtime_testing_exitsyscall()
1529 {
1530 runtime_exitsyscall();
1531 }
1532
1533 G*
1534 __go_go(void (*fn)(void*), void* arg)
1535 {
1536 byte *sp;
1537 size_t spsize;
1538 G *newg;
1539 int64 goid;
1540
1541 goid = runtime_xadd64((uint64*)&runtime_sched.goidgen, 1);
1542 if(raceenabled)
1543 runtime_racegostart(goid, runtime_getcallerpc(&fn));
1544
1545 schedlock();
1546
1547 if((newg = gfget()) != nil) {
1548 #ifdef USING_SPLIT_STACK
1549 int dont_block_signals = 0;
1550
1551 sp = __splitstack_resetcontext(&newg->stack_context[0],
1552 &spsize);
1553 __splitstack_block_signals_context(&newg->stack_context[0],
1554 &dont_block_signals, nil);
1555 #else
1556 sp = newg->gcinitial_sp;
1557 spsize = newg->gcstack_size;
1558 if(spsize == 0)
1559 runtime_throw("bad spsize in __go_go");
1560 newg->gcnext_sp = sp;
1561 #endif
1562 } else {
1563 newg = runtime_malg(StackMin, &sp, &spsize);
1564 if(runtime_lastg == nil)
1565 runtime_allg = newg;
1566 else
1567 runtime_lastg->alllink = newg;
1568 runtime_lastg = newg;
1569 }
1570 newg->status = Gwaiting;
1571 newg->waitreason = "new goroutine";
1572
1573 newg->entry = (byte*)fn;
1574 newg->param = arg;
1575 newg->gopc = (uintptr)__builtin_return_address(0);
1576
1577 runtime_sched.gcount++;
1578 newg->goid = goid;
1579
1580 if(sp == nil)
1581 runtime_throw("nil g->stack0");
1582
1583 {
1584 // Avoid warnings about variables clobbered by
1585 // longjmp.
1586 byte * volatile vsp = sp;
1587 size_t volatile vspsize = spsize;
1588 G * volatile vnewg = newg;
1589
1590 getcontext(&vnewg->context);
1591 vnewg->context.uc_stack.ss_sp = vsp;
1592 #ifdef MAKECONTEXT_STACK_TOP
1593 vnewg->context.uc_stack.ss_sp += vspsize;
1594 #endif
1595 vnewg->context.uc_stack.ss_size = vspsize;
1596 makecontext(&vnewg->context, kickoff, 0);
1597
1598 newprocreadylocked(vnewg);
1599 schedunlock();
1600
1601 return vnewg;
1602 }
1603 }
1604
1605 // Put on gfree list. Sched must be locked.
1606 static void
1607 gfput(G *gp)
1608 {
1609 gp->schedlink = runtime_sched.gfree;
1610 runtime_sched.gfree = gp;
1611 }
1612
1613 // Get from gfree list. Sched must be locked.
1614 static G*
1615 gfget(void)
1616 {
1617 G *gp;
1618
1619 gp = runtime_sched.gfree;
1620 if(gp)
1621 runtime_sched.gfree = gp->schedlink;
1622 return gp;
1623 }
1624
1625 void runtime_Gosched (void) __asm__ (GOSYM_PREFIX "runtime.Gosched");
1626
1627 void
1628 runtime_Gosched(void)
1629 {
1630 runtime_gosched();
1631 }
1632
1633 // Implementation of runtime.GOMAXPROCS.
1634 // delete when scheduler is stronger
1635 int32
1636 runtime_gomaxprocsfunc(int32 n)
1637 {
1638 int32 ret;
1639 uint32 v;
1640
1641 schedlock();
1642 ret = runtime_gomaxprocs;
1643 if(n <= 0)
1644 n = ret;
1645 if(n > maxgomaxprocs)
1646 n = maxgomaxprocs;
1647 runtime_gomaxprocs = n;
1648 if(runtime_gomaxprocs > 1)
1649 runtime_singleproc = false;
1650 if(runtime_gcwaiting != 0) {
1651 if(atomic_mcpumax(runtime_sched.atomic) != 1)
1652 runtime_throw("invalid mcpumax during gc");
1653 schedunlock();
1654 return ret;
1655 }
1656
1657 setmcpumax(n);
1658
1659 // If there are now fewer allowed procs
1660 // than procs running, stop.
1661 v = runtime_atomicload(&runtime_sched.atomic);
1662 if((int32)atomic_mcpu(v) > n) {
1663 schedunlock();
1664 runtime_gosched();
1665 return ret;
1666 }
1667 // handle more procs
1668 matchmg();
1669 schedunlock();
1670 return ret;
1671 }
1672
1673 void
1674 runtime_LockOSThread(void)
1675 {
1676 if(m == &runtime_m0 && runtime_sched.init) {
1677 runtime_sched.lockmain = true;
1678 return;
1679 }
1680 m->lockedg = g;
1681 g->lockedm = m;
1682 }
1683
1684 void
1685 runtime_UnlockOSThread(void)
1686 {
1687 if(m == &runtime_m0 && runtime_sched.init) {
1688 runtime_sched.lockmain = false;
1689 return;
1690 }
1691 m->lockedg = nil;
1692 g->lockedm = nil;
1693 }
1694
1695 bool
1696 runtime_lockedOSThread(void)
1697 {
1698 return g->lockedm != nil && m->lockedg != nil;
1699 }
1700
1701 // for testing of callbacks
1702
1703 _Bool runtime_golockedOSThread(void)
1704 __asm__ (GOSYM_PREFIX "runtime.golockedOSThread");
1705
1706 _Bool
1707 runtime_golockedOSThread(void)
1708 {
1709 return runtime_lockedOSThread();
1710 }
1711
1712 // for testing of wire, unwire
1713 uint32
1714 runtime_mid()
1715 {
1716 return m->id;
1717 }
1718
1719 intgo runtime_NumGoroutine (void)
1720 __asm__ (GOSYM_PREFIX "runtime.NumGoroutine");
1721
1722 intgo
1723 runtime_NumGoroutine()
1724 {
1725 return runtime_sched.gcount;
1726 }
1727
1728 int32
1729 runtime_gcount(void)
1730 {
1731 return runtime_sched.gcount;
1732 }
1733
1734 int32
1735 runtime_mcount(void)
1736 {
1737 return runtime_sched.mcount;
1738 }
1739
1740 static struct {
1741 Lock;
1742 void (*fn)(uintptr*, int32);
1743 int32 hz;
1744 uintptr pcbuf[100];
1745 Location locbuf[100];
1746 } prof;
1747
1748 // Called if we receive a SIGPROF signal.
1749 void
1750 runtime_sigprof()
1751 {
1752 int32 n, i;
1753
1754 if(prof.fn == nil || prof.hz == 0)
1755 return;
1756
1757 runtime_lock(&prof);
1758 if(prof.fn == nil) {
1759 runtime_unlock(&prof);
1760 return;
1761 }
1762 n = runtime_callers(0, prof.locbuf, nelem(prof.locbuf));
1763 for(i = 0; i < n; i++)
1764 prof.pcbuf[i] = prof.locbuf[i].pc;
1765 if(n > 0)
1766 prof.fn(prof.pcbuf, n);
1767 runtime_unlock(&prof);
1768 }
1769
1770 // Arrange to call fn with a traceback hz times a second.
1771 void
1772 runtime_setcpuprofilerate(void (*fn)(uintptr*, int32), int32 hz)
1773 {
1774 // Force sane arguments.
1775 if(hz < 0)
1776 hz = 0;
1777 if(hz == 0)
1778 fn = nil;
1779 if(fn == nil)
1780 hz = 0;
1781
1782 // Stop profiler on this cpu so that it is safe to lock prof.
1783 // if a profiling signal came in while we had prof locked,
1784 // it would deadlock.
1785 runtime_resetcpuprofiler(0);
1786
1787 runtime_lock(&prof);
1788 prof.fn = fn;
1789 prof.hz = hz;
1790 runtime_unlock(&prof);
1791 runtime_lock(&runtime_sched);
1792 runtime_sched.profilehz = hz;
1793 runtime_unlock(&runtime_sched);
1794
1795 if(hz != 0)
1796 runtime_resetcpuprofiler(hz);
1797 }