in another package that refers to <code>B</code>.
</p>
<p>
-Initialization code may contain "go" statements, but the functions
-they invoke do not begin execution until initialization of the entire
-program is complete. Therefore, all initialization code is run in a single
-goroutine.
-</p>
-<p>
An <code>init</code> function cannot be referred to from anywhere
in a program. In particular, <code>init</code> cannot be called explicitly,
nor can a pointer to <code>init</code> be assigned to a function variable.
TEXT runtime·mainstart(SB),7,$0
CALL main·init(SB)
- CALL runtime·initdone(SB)
CALL main·main(SB)
PUSHL $0
CALL runtime·exit(SB)
TEXT runtime·mainstart(SB),7,$0
CALL main·init(SB)
- CALL runtime·initdone(SB)
CALL main·main(SB)
PUSHQ $0
CALL runtime·exit(SB)
TEXT runtime·mainstart(SB),7,$4
BL main·init(SB)
- BL runtime·initdone(SB)
EOR R0, R0
MOVW R0, 0(R13)
BL main·main(SB)
volatile uint32 atomic; // atomic scheduling word (see below)
- int32 predawn; // running initialization, don't run new g's.
int32 profilehz; // cpu profiling rate
Note stopped; // one g can set waitstop and wait here for m's to stop
}
setmcpumax(runtime·gomaxprocs);
runtime·singleproc = runtime·gomaxprocs == 1;
- runtime·sched.predawn = 1;
+ mstats.enablegc = 1;
m->nomemprof--;
}
runtime·notewakeup(&m->havenextg);
}
-// Called after main·init_function; main·main will be called on return.
-void
-runtime·initdone(void)
-{
- // Let's go.
- runtime·sched.predawn = 0;
- mstats.enablegc = 1;
-
- // If main·init_function started other goroutines,
- // kick off new m's to handle them, like ready
- // would have, had it not been pre-dawn.
- schedlock();
- matchmg();
- schedunlock();
-}
-
void
runtime·goexit(void)
{
g->status = Grunnable;
gput(g);
- if(!runtime·sched.predawn)
- matchmg();
+ matchmg();
}
static void
schedlock();
if(gp != nil) {
- if(runtime·sched.predawn)
- runtime·throw("init rescheduling");
-
// Just finished running gp.
gp->m = nil;
runtime·sched.grunning--;
{
uint32 v;
- if(runtime·sched.predawn)
- return;
-
// Leave SP around for gc and traceback.
runtime·gosave(&g->sched);
g->gcsp = g->sched.sp;
{
uint32 v;
- if(runtime·sched.predawn)
- return;
-
// Fast path.
// If we can do the mcpu++ bookkeeping and
// find that we still have mcpu <= mcpumax, then we can
void
runtime·LockOSThread(void)
{
- if(runtime·sched.predawn)
- runtime·throw("cannot wire during init");
m->lockedg = g;
g->lockedm = m;
}
--- /dev/null
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test that goroutines and garbage collection run during init.
+
+package main
+
+import "runtime"
+
+var x []byte
+
+func init() {
+ c := make(chan int)
+ go send(c)
+ <-c
+
+ const chunk = 1<<20
+ runtime.UpdateMemStats()
+ sys := runtime.MemStats.Sys
+ b := make([]byte, chunk)
+ for i := range b {
+ b[i] = byte(i%10 + '0')
+ }
+ s := string(b)
+ for i := 0; i < 1000; i++ {
+ x = []byte(s)
+ }
+ runtime.UpdateMemStats()
+ sys1 := runtime.MemStats.Sys
+ if sys1-sys > chunk*50 {
+ println("allocated 1000 chunks of", chunk, "and used ", sys1-sys, "memory")
+ }
+}
+
+func send(c chan int) {
+ c <- 1
+}
+
+func main() {
+}
+