1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // The main goroutine.
17 // Racectx of m0->g0 is used only as the parent of the main goroutine.
18 // It must not be used for anything else.
21 // Max stack size is 1 GB on 64-bit, 250 MB on 32-bit.
22 // Using decimal instead of binary GB and MB because
23 // they look nicer in the stack overflow failure message.
25 maxstacksize = 1000000000
27 maxstacksize = 250000000
30 systemstack(newsysmon)
32 // Lock the main goroutine onto this, the main OS thread,
33 // during initialization. Most programs won't care, but a few
34 // do require certain calls to be made by the main thread.
35 // Those can arrange for main.main to run in the main thread
36 // by calling runtime.LockOSThread during initialization
37 // to preserve the lock.
41 gothrow("runtime.main not on m0")
44 runtime_init() // must be before defer
46 // Defer unlock so that runtime.Goexit during init does the unlock too.
54 memstats.enablegc = true // now that runtime is initialized, GC is okay
57 if _cgo_thread_start == nil {
58 gothrow("_cgo_thread_start missing")
60 if _cgo_malloc == nil {
61 gothrow("_cgo_malloc missing")
64 gothrow("_cgo_free missing")
66 if _cgo_setenv == nil {
67 gothrow("_cgo_setenv missing")
69 if _cgo_unsetenv == nil {
70 gothrow("_cgo_unsetenv missing")
84 // Make racy client program work: if panicking on
85 // another goroutine at the same time as main returns,
86 // let the other goroutine finish printing the panic trace.
87 // Once it does, it will exit. See issue 3934.
89 gopark(nil, nil, "panicwait")
99 // start forcegc helper goroutine
104 func forcegchelper() {
106 forcegc.g.issystem = true
109 if forcegc.idle != 0 {
110 gothrow("forcegc: phase error")
112 atomicstore(&forcegc.idle, 1)
113 goparkunlock(&forcegc.lock, "force gc (idle)")
114 // this goroutine is explicitly resumed by sysmon
115 if debug.gctrace > 0 {
124 // Gosched yields the processor, allowing other goroutines to run. It does not
125 // suspend the current goroutine, so execution resumes automatically.
130 // Puts the current goroutine into a waiting state and calls unlockf.
131 // If unlockf returns false, the goroutine is resumed.
132 func gopark(unlockf func(*g, unsafe.Pointer) bool, lock unsafe.Pointer, reason string) {
135 status := readgstatus(gp)
136 if status != _Grunning && status != _Gscanrunning {
137 gothrow("gopark: bad g status")
140 mp.waitunlockf = *(*unsafe.Pointer)(unsafe.Pointer(&unlockf))
141 gp.waitreason = reason
143 // can't do anything that might move the G between Ms here.
147 // Puts the current goroutine into a waiting state and unlocks the lock.
148 // The goroutine can be made runnable again by calling goready(gp).
149 func goparkunlock(lock *mutex, reason string) {
150 gopark(parkunlock_c, unsafe.Pointer(lock), reason)
153 func goready(gp *g) {
160 func acquireSudog() *sudog {
165 gothrow("acquireSudog: found s.elem != nil in cache")
167 c.sudogcache = s.next
171 // Delicate dance: the semaphore implementation calls
172 // acquireSudog, acquireSudog calls new(sudog),
173 // new calls malloc, malloc can call the garbage collector,
174 // and the garbage collector calls the semaphore implementation
176 // Break the cycle by doing acquirem/releasem around new(sudog).
177 // The acquirem/releasem increments m.locks during new(sudog),
178 // which keeps the garbage collector from being invoked.
182 gothrow("acquireSudog: found p.elem != nil after new")
189 func releaseSudog(s *sudog) {
191 gothrow("runtime: sudog with non-nil elem")
193 if s.selectdone != nil {
194 gothrow("runtime: sudog with non-nil selectdone")
198 gothrow("runtime: releaseSudog with non-nil gp.param")
201 s.next = c.sudogcache
205 // funcPC returns the entry PC of the function f.
206 // It assumes that f is a func value. Otherwise the behavior is undefined.
208 func funcPC(f interface{}) uintptr {
209 return **(**uintptr)(add(unsafe.Pointer(&f), ptrSize))
212 // called from assembly
213 func badmcall(fn func(*g)) {
214 gothrow("runtime: mcall called on m->g0 stack")
217 func badmcall2(fn func(*g)) {
218 gothrow("runtime: mcall function returned")
221 func badreflectcall() {
222 panic("runtime: arg size to reflect.call more than 1GB")
225 func lockedOSThread() bool {
227 return gp.lockedm != nil && gp.m.lockedg != nil
247 func allgadd(gp *g) {
248 if readgstatus(gp) == _Gidle {
249 gothrow("allgadd: bad status Gidle")
253 allgs = append(allgs, gp)
255 allglen = uintptr(len(allgs))