libgo: Update Go library to master revision 15489/921e53d4863c.
[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 main_init();
532 runtime_sched.init = false;
533 if(!runtime_sched.lockmain)
534 runtime_UnlockOSThread();
535
536 // For gccgo we have to wait until after main is initialized
537 // to enable GC, because initializing main registers the GC
538 // roots.
539 mstats.enablegc = 1;
540
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{} }.
544 runtime_gosched();
545
546 main_main();
547 if(raceenabled)
548 runtime_racefini();
549 runtime_exit(0);
550 for(;;)
551 *(int32*)0 = 0;
552 }
553
554 // Lock the scheduler.
555 static void
556 schedlock(void)
557 {
558 runtime_lock(&runtime_sched);
559 }
560
561 // Unlock the scheduler.
562 static void
563 schedunlock(void)
564 {
565 M *mp;
566
567 mp = mwakeup;
568 mwakeup = nil;
569 runtime_unlock(&runtime_sched);
570 if(mp != nil)
571 runtime_notewakeup(&mp->havenextg);
572 }
573
574 void
575 runtime_goexit(void)
576 {
577 g->status = Gmoribund;
578 runtime_gosched();
579 }
580
581 void
582 runtime_goroutineheader(G *gp)
583 {
584 const char *status;
585
586 switch(gp->status) {
587 case Gidle:
588 status = "idle";
589 break;
590 case Grunnable:
591 status = "runnable";
592 break;
593 case Grunning:
594 status = "running";
595 break;
596 case Gsyscall:
597 status = "syscall";
598 break;
599 case Gwaiting:
600 if(gp->waitreason)
601 status = gp->waitreason;
602 else
603 status = "waiting";
604 break;
605 case Gmoribund:
606 status = "moribund";
607 break;
608 default:
609 status = "???";
610 break;
611 }
612 runtime_printf("goroutine %D [%s]:\n", gp->goid, status);
613 }
614
615 void
616 runtime_goroutinetrailer(G *g)
617 {
618 if(g != nil && g->gopc != 0 && g->goid != 1) {
619 String fn;
620 String file;
621 intgo line;
622
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);
626 }
627 }
628 }
629
630 struct Traceback
631 {
632 G* gp;
633 uintptr pcbuf[100];
634 int32 c;
635 };
636
637 void
638 runtime_tracebackothers(G * volatile me)
639 {
640 G * volatile gp;
641 Traceback traceback;
642
643 traceback.gp = me;
644 for(gp = runtime_allg; gp != nil; gp = gp->alllink) {
645 if(gp == me || gp->status == Gdead)
646 continue;
647 runtime_printf("\n");
648 runtime_goroutineheader(gp);
649
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.
655
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);
661 continue;
662 }
663
664 gp->traceback = &traceback;
665
666 #ifdef USING_SPLIT_STACK
667 __splitstack_getcontext(&me->stack_context[0]);
668 #endif
669 getcontext(&me->context);
670
671 if(gp->traceback != nil) {
672 runtime_gogo(gp);
673 }
674
675 runtime_printtrace(traceback.pcbuf, traceback.c);
676 runtime_goroutinetrailer(gp);
677 }
678 }
679
680 // Do a stack trace of gp, and then restore the context to
681 // gp->dotraceback.
682
683 static void
684 gtraceback(G* gp)
685 {
686 Traceback* traceback;
687
688 traceback = gp->traceback;
689 gp->traceback = nil;
690 traceback->c = runtime_callers(1, traceback->pcbuf,
691 sizeof traceback->pcbuf / sizeof traceback->pcbuf[0]);
692 runtime_gogo(traceback->gp);
693 }
694
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.
699 void
700 runtime_idlegoroutine(void)
701 {
702 if(g->idlem != nil)
703 runtime_throw("g is already an idle goroutine");
704 g->idlem = m;
705 }
706
707 static void
708 mcommoninit(M *mp)
709 {
710 mp->id = runtime_sched.mcount++;
711 mp->fastrand = 0x49f6428aUL + mp->id + runtime_cputicks();
712
713 if(mp->mcache == nil)
714 mp->mcache = runtime_allocmcache();
715
716 runtime_callers(1, mp->createstack, nelem(mp->createstack));
717
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);
724 }
725
726 // Try to increment mcpu. Report whether succeeded.
727 static bool
728 canaddmcpu(void)
729 {
730 uint32 v;
731
732 for(;;) {
733 v = runtime_sched.atomic;
734 if(atomic_mcpu(v) >= atomic_mcpumax(v))
735 return 0;
736 if(runtime_cas(&runtime_sched.atomic, v, v+(1<<mcpuShift)))
737 return 1;
738 }
739 }
740
741 // Put on `g' queue. Sched must be locked.
742 static void
743 gput(G *gp)
744 {
745 M *mp;
746
747 // If g is wired, hand it off directly.
748 if((mp = gp->lockedm) != nil && canaddmcpu()) {
749 mnextg(mp, gp);
750 return;
751 }
752
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",
757 gp->idlem->id,
758 gp->idlem->idleg->goid, gp->goid);
759 runtime_throw("runtime: double idle");
760 }
761 gp->idlem->idleg = gp;
762 return;
763 }
764
765 gp->schedlink = nil;
766 if(runtime_sched.ghead == nil)
767 runtime_sched.ghead = gp;
768 else
769 runtime_sched.gtail->schedlink = gp;
770 runtime_sched.gtail = gp;
771
772 // increment gwait.
773 // if it transitions to nonzero, set atomic gwaiting bit.
774 if(runtime_sched.gwait++ == 0)
775 runtime_xadd(&runtime_sched.atomic, 1<<gwaitingShift);
776 }
777
778 // Report whether gget would return something.
779 static bool
780 haveg(void)
781 {
782 return runtime_sched.ghead != nil || m->idleg != nil;
783 }
784
785 // Get from `g' queue. Sched must be locked.
786 static G*
787 gget(void)
788 {
789 G *gp;
790
791 gp = runtime_sched.ghead;
792 if(gp) {
793 runtime_sched.ghead = gp->schedlink;
794 if(runtime_sched.ghead == nil)
795 runtime_sched.gtail = nil;
796 // decrement gwait.
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) {
801 gp = m->idleg;
802 m->idleg = nil;
803 }
804 return gp;
805 }
806
807 // Put on `m' list. Sched must be locked.
808 static void
809 mput(M *mp)
810 {
811 mp->schedlink = runtime_sched.mhead;
812 runtime_sched.mhead = mp;
813 runtime_sched.mwait++;
814 }
815
816 // Get an `m' to run `g'. Sched must be locked.
817 static M*
818 mget(G *gp)
819 {
820 M *mp;
821
822 // if g has its own m, use it.
823 if(gp && (mp = gp->lockedm) != nil)
824 return mp;
825
826 // otherwise use general m pool.
827 if((mp = runtime_sched.mhead) != nil) {
828 runtime_sched.mhead = mp->schedlink;
829 runtime_sched.mwait--;
830 }
831 return mp;
832 }
833
834 // Mark g ready to run.
835 void
836 runtime_ready(G *gp)
837 {
838 schedlock();
839 readylocked(gp);
840 schedunlock();
841 }
842
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.
846 static void
847 readylocked(G *gp)
848 {
849 if(gp->m) {
850 // Running on another machine.
851 // Ready it when it stops.
852 gp->readyonstop = 1;
853 return;
854 }
855
856 // Mark runnable.
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");
860 }
861 gp->status = Grunnable;
862
863 gput(gp);
864 matchmg();
865 }
866
867 // Same as readylocked but a different symbol so that
868 // debuggers can set a breakpoint here and catch all
869 // new goroutines.
870 static void
871 newprocreadylocked(G *gp)
872 {
873 readylocked(gp);
874 }
875
876 // Pass g to m for running.
877 // Caller has already incremented mcpu.
878 static void
879 mnextg(M *mp, G *gp)
880 {
881 runtime_sched.grunning++;
882 mp->nextg = gp;
883 if(mp->waitnextg) {
884 mp->waitnextg = 0;
885 if(mwakeup != nil)
886 runtime_notewakeup(&mwakeup->havenextg);
887 mwakeup = mp;
888 }
889 }
890
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.
895 static G*
896 nextgandunlock(void)
897 {
898 G *gp;
899 uint32 v;
900
901 top:
902 if(atomic_mcpu(runtime_sched.atomic) >= maxgomaxprocs)
903 runtime_throw("negative mcpu");
904
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) {
908 gp = m->nextg;
909 m->nextg = nil;
910 schedunlock();
911 return gp;
912 }
913
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) {
919 matchmg();
920 // m->lockedg might have been on the queue.
921 if(m->nextg != nil) {
922 gp = m->nextg;
923 m->nextg = nil;
924 schedunlock();
925 return gp;
926 }
927 }
928 } else {
929 // Look for work on global queue.
930 while(haveg() && canaddmcpu()) {
931 gp = gget();
932 if(gp == nil)
933 runtime_throw("gget inconsistency");
934
935 if(gp->lockedm) {
936 mnextg(gp->lockedm, gp);
937 continue;
938 }
939 runtime_sched.grunning++;
940 schedunlock();
941 return gp;
942 }
943
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.
949 //
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.
955 //
956 // Wait on global m queue.
957 mput(m);
958 }
959
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.
970 //
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.
974 //
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!");
979 }
980
981 m->nextg = nil;
982 m->waitnextg = 1;
983 runtime_noteclear(&m->havenextg);
984
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);
994 }
995 schedunlock();
996
997 runtime_notesleep(&m->havenextg);
998 if(m->helpgc) {
999 runtime_gchelper();
1000 m->helpgc = 0;
1001 runtime_lock(&runtime_sched);
1002 goto top;
1003 }
1004 if((gp = m->nextg) == nil)
1005 runtime_throw("bad m->nextg in nextgoroutine");
1006 m->nextg = nil;
1007 return gp;
1008 }
1009
1010 int32
1011 runtime_gcprocs(void)
1012 {
1013 int32 n;
1014
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;
1020 if(n > MaxGcproc)
1021 n = MaxGcproc;
1022 if(n > runtime_sched.mwait+1) // one M is currently running
1023 n = runtime_sched.mwait+1;
1024 return n;
1025 }
1026
1027 void
1028 runtime_helpgc(int32 nproc)
1029 {
1030 M *mp;
1031 int32 n;
1032
1033 runtime_lock(&runtime_sched);
1034 for(n = 1; n < nproc; n++) { // one M is currently running
1035 mp = mget(nil);
1036 if(mp == nil)
1037 runtime_throw("runtime_gcprocs inconsistency");
1038 mp->helpgc = 1;
1039 mp->waitnextg = 0;
1040 runtime_notewakeup(&mp->havenextg);
1041 }
1042 runtime_unlock(&runtime_sched);
1043 }
1044
1045 void
1046 runtime_stoptheworld(void)
1047 {
1048 uint32 v;
1049
1050 schedlock();
1051 runtime_gcwaiting = 1;
1052
1053 setmcpumax(1);
1054
1055 // while mcpu > 1
1056 for(;;) {
1057 v = runtime_sched.atomic;
1058 if(atomic_mcpu(v) <= 1)
1059 break;
1060
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");
1067
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)))
1071 continue;
1072
1073 schedunlock();
1074 runtime_notesleep(&runtime_sched.stopped);
1075 schedlock();
1076 }
1077 runtime_singleproc = runtime_gomaxprocs == 1;
1078 schedunlock();
1079 }
1080
1081 void
1082 runtime_starttheworld(void)
1083 {
1084 M *mp;
1085 int32 max;
1086
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;
1091 if(max > MaxGcproc)
1092 max = MaxGcproc;
1093
1094 schedlock();
1095 runtime_gcwaiting = 0;
1096 setmcpumax(runtime_gomaxprocs);
1097 matchmg();
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();
1113 mp->helpgc = 1;
1114 runtime_sched.grunning++;
1115 }
1116 schedunlock();
1117 }
1118
1119 // Called to start an M.
1120 void*
1121 runtime_mstart(void* mp)
1122 {
1123 m = (M*)mp;
1124 g = m->g0;
1125
1126 initcontext();
1127
1128 g->entry = nil;
1129 g->param = nil;
1130
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]);
1136 #else
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;
1141 g->gcnext_sp = &mp;
1142 #endif
1143 getcontext(&g->context);
1144
1145 if(g->entry != nil) {
1146 // Got here from mcall.
1147 void (*pfn)(G*) = (void (*)(G*))g->entry;
1148 G* gp = (G*)g->param;
1149 pfn(gp);
1150 *(int*)0x21 = 0x21;
1151 }
1152 runtime_minit();
1153
1154 #ifdef USING_SPLIT_STACK
1155 {
1156 int dont_block_signals = 0;
1157 __splitstack_block_signals(&dont_block_signals, nil);
1158 }
1159 #endif
1160
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)
1164 runtime_initsig();
1165
1166 schedule(nil);
1167
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.
1171
1172 return nil;
1173 }
1174
1175 typedef struct CgoThreadStart CgoThreadStart;
1176 struct CgoThreadStart
1177 {
1178 M *m;
1179 G *g;
1180 void (*fn)(void);
1181 };
1182
1183 // Kick off new m's as needed (up to mcpumax).
1184 // Sched is locked.
1185 static void
1186 matchmg(void)
1187 {
1188 G *gp;
1189 M *mp;
1190
1191 if(m->mallocing || m->gcing)
1192 return;
1193
1194 while(haveg() && canaddmcpu()) {
1195 gp = gget();
1196 if(gp == nil)
1197 runtime_throw("gget inconsistency");
1198
1199 // Find the m that will run gp.
1200 if((mp = mget(gp)) == nil)
1201 mp = runtime_newm();
1202 mnextg(mp, gp);
1203 }
1204 }
1205
1206 // Create a new m. It will start off with a call to runtime_mstart.
1207 M*
1208 runtime_newm(void)
1209 {
1210 M *mp;
1211 pthread_attr_t attr;
1212 pthread_t tid;
1213 size_t stacksize;
1214
1215 #if 0
1216 static const Type *mtype; // The Go type M
1217 if(mtype == nil) {
1218 Eface e;
1219 runtime_gc_m_ptr(&e);
1220 mtype = ((const PtrType*)e.__type_descriptor)->__element_type;
1221 }
1222 #endif
1223
1224 mp = runtime_mal(sizeof *mp);
1225 mcommoninit(mp);
1226 mp->g0 = runtime_malg(-1, nil, nil);
1227
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");
1232
1233 stacksize = PTHREAD_STACK_MIN;
1234
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;
1242
1243 if(pthread_attr_setstacksize(&attr, stacksize) != 0)
1244 runtime_throw("pthread_attr_setstacksize");
1245
1246 if(pthread_create(&tid, &attr, runtime_mstart, mp) != 0)
1247 runtime_throw("pthread_create");
1248
1249 return mp;
1250 }
1251
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.
1255 // Never returns.
1256 static void
1257 schedule(G *gp)
1258 {
1259 int32 hz;
1260 uint32 v;
1261
1262 schedlock();
1263 if(gp != nil) {
1264 // Just finished running gp.
1265 gp->m = nil;
1266 runtime_sched.grunning--;
1267
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");
1272
1273 switch(gp->status) {
1274 case Grunnable:
1275 case Gdead:
1276 // Shouldn't have been running!
1277 runtime_throw("bad gp->status in sched");
1278 case Grunning:
1279 gp->status = Grunnable;
1280 gput(gp);
1281 break;
1282 case Gmoribund:
1283 if(raceenabled)
1284 runtime_racegoend(gp->goid);
1285 gp->status = Gdead;
1286 if(gp->lockedm) {
1287 gp->lockedm = nil;
1288 m->lockedg = nil;
1289 }
1290 gp->idlem = nil;
1291 runtime_memclr(&gp->context, sizeof gp->context);
1292 gfput(gp);
1293 if(--runtime_sched.gcount == 0)
1294 runtime_exit(0);
1295 break;
1296 }
1297 if(gp->readyonstop) {
1298 gp->readyonstop = 0;
1299 readylocked(gp);
1300 }
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--;
1309 m->helpgc = 0;
1310 } else if(m->nextg != nil) {
1311 // New m started by matchmg.
1312 } else {
1313 runtime_throw("invalid m state in scheduler");
1314 }
1315
1316 // Find (or wait for) g to run. Unlocks runtime_sched.
1317 gp = nextgandunlock();
1318 gp->readyonstop = 0;
1319 gp->status = Grunning;
1320 m->curg = gp;
1321 gp->m = m;
1322
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);
1327
1328 runtime_gogo(gp);
1329 }
1330
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,
1334 // kills off g.
1335 void
1336 runtime_gosched(void)
1337 {
1338 if(m->locks != 0)
1339 runtime_throw("gosched holding locks");
1340 if(g == m->g0)
1341 runtime_throw("gosched of g0");
1342 runtime_mcall(schedule);
1343 }
1344
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).
1347 void
1348 runtime_park(void (*unlockf)(Lock*), Lock *lock, const char *reason)
1349 {
1350 g->status = Gwaiting;
1351 g->waitreason = reason;
1352 if(unlockf)
1353 unlockf(lock);
1354 runtime_gosched();
1355 }
1356
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.
1361 //
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.
1368
1369 void runtime_entersyscall(void) __attribute__ ((no_split_stack));
1370
1371 void
1372 runtime_entersyscall(void)
1373 {
1374 uint32 v;
1375
1376 if(m->profilehz > 0)
1377 runtime_setprof(false);
1378
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,
1383 &g->gcinitial_sp);
1384 #else
1385 g->gcnext_sp = (byte *) &v;
1386 #endif
1387
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);
1391
1392 g->status = Gsyscall;
1393
1394 // Fast path.
1395 // The slow path inside the schedlock/schedunlock will get
1396 // through without stopping if it does:
1397 // mcpu--
1398 // gwait not true
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)))
1404 return;
1405
1406 schedlock();
1407 v = runtime_atomicload(&runtime_sched.atomic);
1408 if(atomic_gwaiting(v)) {
1409 matchmg();
1410 v = runtime_atomicload(&runtime_sched.atomic);
1411 }
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);
1415 }
1416
1417 schedunlock();
1418 }
1419
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.
1424 void
1425 runtime_exitsyscall(void)
1426 {
1427 G *gp;
1428 uint32 v;
1429
1430 // Fast path.
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.
1437 gp = g;
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
1445 gp->gcstack = nil;
1446 #endif
1447 gp->gcnext_sp = nil;
1448 runtime_memclr(&gp->gcregs, sizeof gp->gcregs);
1449
1450 if(m->profilehz > 0)
1451 runtime_setprof(true);
1452 return;
1453 }
1454
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;
1460
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.
1465 runtime_gosched();
1466
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
1472 // is not running.
1473 #ifdef USING_SPLIT_STACK
1474 gp->gcstack = nil;
1475 #endif
1476 gp->gcnext_sp = nil;
1477 runtime_memclr(&gp->gcregs, sizeof gp->gcregs);
1478 }
1479
1480 // Allocate a new g, with a stack big enough for stacksize bytes.
1481 G*
1482 runtime_malg(int32 stacksize, byte** ret_stack, size_t* ret_stacksize)
1483 {
1484 G *newg;
1485
1486 newg = runtime_malloc(sizeof(G));
1487 if(stacksize >= 0) {
1488 #if USING_SPLIT_STACK
1489 int dont_block_signals = 0;
1490
1491 *ret_stack = __splitstack_makecontext(stacksize,
1492 &newg->stack_context[0],
1493 ret_stacksize);
1494 __splitstack_block_signals_context(&newg->stack_context[0],
1495 &dont_block_signals, nil);
1496 #else
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);
1502 #endif
1503 }
1504 return newg;
1505 }
1506
1507 /* For runtime package testing. */
1508
1509 void runtime_testing_entersyscall(void)
1510 __asm__ (GOSYM_PREFIX "runtime.entersyscall");
1511
1512 void
1513 runtime_testing_entersyscall()
1514 {
1515 runtime_entersyscall();
1516 }
1517
1518 void runtime_testing_exitsyscall(void)
1519 __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
1520
1521 void
1522 runtime_testing_exitsyscall()
1523 {
1524 runtime_exitsyscall();
1525 }
1526
1527 G*
1528 __go_go(void (*fn)(void*), void* arg)
1529 {
1530 byte *sp;
1531 size_t spsize;
1532 G *newg;
1533 int64 goid;
1534
1535 goid = runtime_xadd64((uint64*)&runtime_sched.goidgen, 1);
1536 if(raceenabled)
1537 runtime_racegostart(goid, runtime_getcallerpc(&fn));
1538
1539 schedlock();
1540
1541 if((newg = gfget()) != nil) {
1542 #ifdef USING_SPLIT_STACK
1543 int dont_block_signals = 0;
1544
1545 sp = __splitstack_resetcontext(&newg->stack_context[0],
1546 &spsize);
1547 __splitstack_block_signals_context(&newg->stack_context[0],
1548 &dont_block_signals, nil);
1549 #else
1550 sp = newg->gcinitial_sp;
1551 spsize = newg->gcstack_size;
1552 if(spsize == 0)
1553 runtime_throw("bad spsize in __go_go");
1554 newg->gcnext_sp = sp;
1555 #endif
1556 } else {
1557 newg = runtime_malg(StackMin, &sp, &spsize);
1558 if(runtime_lastg == nil)
1559 runtime_allg = newg;
1560 else
1561 runtime_lastg->alllink = newg;
1562 runtime_lastg = newg;
1563 }
1564 newg->status = Gwaiting;
1565 newg->waitreason = "new goroutine";
1566
1567 newg->entry = (byte*)fn;
1568 newg->param = arg;
1569 newg->gopc = (uintptr)__builtin_return_address(0);
1570
1571 runtime_sched.gcount++;
1572 newg->goid = goid;
1573
1574 if(sp == nil)
1575 runtime_throw("nil g->stack0");
1576
1577 {
1578 // Avoid warnings about variables clobbered by
1579 // longjmp.
1580 byte * volatile vsp = sp;
1581 size_t volatile vspsize = spsize;
1582 G * volatile vnewg = newg;
1583
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;
1588 #endif
1589 vnewg->context.uc_stack.ss_size = vspsize;
1590 makecontext(&vnewg->context, kickoff, 0);
1591
1592 newprocreadylocked(vnewg);
1593 schedunlock();
1594
1595 return vnewg;
1596 }
1597 }
1598
1599 // Put on gfree list. Sched must be locked.
1600 static void
1601 gfput(G *gp)
1602 {
1603 gp->schedlink = runtime_sched.gfree;
1604 runtime_sched.gfree = gp;
1605 }
1606
1607 // Get from gfree list. Sched must be locked.
1608 static G*
1609 gfget(void)
1610 {
1611 G *gp;
1612
1613 gp = runtime_sched.gfree;
1614 if(gp)
1615 runtime_sched.gfree = gp->schedlink;
1616 return gp;
1617 }
1618
1619 void runtime_Gosched (void) __asm__ (GOSYM_PREFIX "runtime.Gosched");
1620
1621 void
1622 runtime_Gosched(void)
1623 {
1624 runtime_gosched();
1625 }
1626
1627 // Implementation of runtime.GOMAXPROCS.
1628 // delete when scheduler is stronger
1629 int32
1630 runtime_gomaxprocsfunc(int32 n)
1631 {
1632 int32 ret;
1633 uint32 v;
1634
1635 schedlock();
1636 ret = runtime_gomaxprocs;
1637 if(n <= 0)
1638 n = ret;
1639 if(n > maxgomaxprocs)
1640 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");
1647 schedunlock();
1648 return ret;
1649 }
1650
1651 setmcpumax(n);
1652
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) {
1657 schedunlock();
1658 runtime_gosched();
1659 return ret;
1660 }
1661 // handle more procs
1662 matchmg();
1663 schedunlock();
1664 return ret;
1665 }
1666
1667 void
1668 runtime_LockOSThread(void)
1669 {
1670 if(m == &runtime_m0 && runtime_sched.init) {
1671 runtime_sched.lockmain = true;
1672 return;
1673 }
1674 m->lockedg = g;
1675 g->lockedm = m;
1676 }
1677
1678 void
1679 runtime_UnlockOSThread(void)
1680 {
1681 if(m == &runtime_m0 && runtime_sched.init) {
1682 runtime_sched.lockmain = false;
1683 return;
1684 }
1685 m->lockedg = nil;
1686 g->lockedm = nil;
1687 }
1688
1689 bool
1690 runtime_lockedOSThread(void)
1691 {
1692 return g->lockedm != nil && m->lockedg != nil;
1693 }
1694
1695 // for testing of callbacks
1696
1697 _Bool runtime_golockedOSThread(void)
1698 __asm__ (GOSYM_PREFIX "runtime.golockedOSThread");
1699
1700 _Bool
1701 runtime_golockedOSThread(void)
1702 {
1703 return runtime_lockedOSThread();
1704 }
1705
1706 // for testing of wire, unwire
1707 uint32
1708 runtime_mid()
1709 {
1710 return m->id;
1711 }
1712
1713 intgo runtime_NumGoroutine (void)
1714 __asm__ (GOSYM_PREFIX "runtime.NumGoroutine");
1715
1716 intgo
1717 runtime_NumGoroutine()
1718 {
1719 return runtime_sched.gcount;
1720 }
1721
1722 int32
1723 runtime_gcount(void)
1724 {
1725 return runtime_sched.gcount;
1726 }
1727
1728 int32
1729 runtime_mcount(void)
1730 {
1731 return runtime_sched.mcount;
1732 }
1733
1734 static struct {
1735 Lock;
1736 void (*fn)(uintptr*, int32);
1737 int32 hz;
1738 uintptr pcbuf[100];
1739 } prof;
1740
1741 // Called if we receive a SIGPROF signal.
1742 void
1743 runtime_sigprof()
1744 {
1745 int32 n;
1746
1747 if(prof.fn == nil || prof.hz == 0)
1748 return;
1749
1750 runtime_lock(&prof);
1751 if(prof.fn == nil) {
1752 runtime_unlock(&prof);
1753 return;
1754 }
1755 n = runtime_callers(0, prof.pcbuf, nelem(prof.pcbuf));
1756 if(n > 0)
1757 prof.fn(prof.pcbuf, n);
1758 runtime_unlock(&prof);
1759 }
1760
1761 // Arrange to call fn with a traceback hz times a second.
1762 void
1763 runtime_setcpuprofilerate(void (*fn)(uintptr*, int32), int32 hz)
1764 {
1765 // Force sane arguments.
1766 if(hz < 0)
1767 hz = 0;
1768 if(hz == 0)
1769 fn = nil;
1770 if(fn == nil)
1771 hz = 0;
1772
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);
1777
1778 runtime_lock(&prof);
1779 prof.fn = fn;
1780 prof.hz = hz;
1781 runtime_unlock(&prof);
1782 runtime_lock(&runtime_sched);
1783 runtime_sched.profilehz = hz;
1784 runtime_unlock(&runtime_sched);
1785
1786 if(hz != 0)
1787 runtime_resetcpuprofiler(hz);
1788 }