Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 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 "runtime.h" |
Russ Cox | 9b15ad8 | 2010-10-14 10:45:32 -0400 | [diff] [blame] | 6 | #include "arch.h" |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 7 | #include "defs.h" |
Russ Cox | 1ce1791 | 2009-01-26 17:37:05 -0800 | [diff] [blame] | 8 | #include "malloc.h" |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 9 | #include "os.h" |
Russ Cox | d9fd114 | 2011-02-22 17:40:40 -0500 | [diff] [blame] | 10 | #include "stack.h" |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 11 | |
Russ Cox | 9042c2c | 2010-12-08 13:53:30 -0500 | [diff] [blame] | 12 | bool runtime·iscgo; |
| 13 | |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 14 | static void unwindstack(G*, byte*); |
| 15 | |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 16 | typedef struct Sched Sched; |
| 17 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 18 | M runtime·m0; |
| 19 | G runtime·g0; // idle goroutine for m0 |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 20 | |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 21 | static int32 debug = 0; |
| 22 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 23 | int32 runtime·gcwaiting; |
Russ Cox | 5328df6 | 2010-01-09 09:47:45 -0800 | [diff] [blame] | 24 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 25 | // Go scheduler |
| 26 | // |
| 27 | // The go scheduler's job is to match ready-to-run goroutines (`g's) |
| 28 | // with waiting-for-work schedulers (`m's). If there are ready gs |
| 29 | // and no waiting ms, ready() will start a new m running in a new |
| 30 | // OS thread, so that all ready gs can run simultaneously, up to a limit. |
| 31 | // For now, ms never go away. |
| 32 | // |
Russ Cox | fe1e492 | 2009-11-10 19:59:22 -0800 | [diff] [blame] | 33 | // By default, Go keeps only one kernel thread (m) running user code |
| 34 | // at a single time; other threads may be blocked in the operating system. |
| 35 | // Setting the environment variable $GOMAXPROCS or calling |
| 36 | // runtime.GOMAXPROCS() will change the number of user threads |
| 37 | // allowed to execute simultaneously. $GOMAXPROCS is thus an |
| 38 | // approximation of the maximum number of cores to use. |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 39 | // |
| 40 | // Even a program that can run without deadlock in a single process |
| 41 | // might use more ms if given the chance. For example, the prime |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 42 | // sieve will use as many ms as there are primes (up to runtime·sched.mmax), |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 43 | // allowing different stages of the pipeline to execute in parallel. |
| 44 | // We could revisit this choice, only kicking off new ms for blocking |
| 45 | // system calls, but that would limit the amount of parallel computation |
| 46 | // that go would try to do. |
| 47 | // |
| 48 | // In general, one could imagine all sorts of refinements to the |
| 49 | // scheduler, but the goal now is just to get something working on |
| 50 | // Linux and OS X. |
| 51 | |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 52 | struct Sched { |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 53 | Lock; |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 54 | |
| 55 | G *gfree; // available gs (status == Gdead) |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 56 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 57 | G *ghead; // gs waiting to run |
| 58 | G *gtail; |
| 59 | int32 gwait; // number of gs waiting to run |
| 60 | int32 gcount; // number of gs that are alive |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 61 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 62 | M *mhead; // ms waiting for work |
| 63 | int32 mwait; // number of ms waiting for work |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 64 | int32 mcount; // number of ms that have been created |
| 65 | int32 mcpu; // number of ms executing on cpu |
| 66 | int32 mcpumax; // max number of ms allowed on cpu |
| 67 | int32 msyscall; // number of ms in system calls |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 68 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 69 | int32 predawn; // running initialization, don't run new gs. |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 70 | |
| 71 | Note stopped; // one g can wait here for ms to stop |
Russ Cox | be62913 | 2008-12-08 17:14:08 -0800 | [diff] [blame] | 72 | int32 waitstop; // after setting this flag |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 75 | Sched runtime·sched; |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 76 | int32 gomaxprocs; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 77 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 78 | // Scheduling helpers. Sched must be locked. |
| 79 | static void gput(G*); // put/get on ghead/gtail |
| 80 | static G* gget(void); |
| 81 | static void mput(M*); // put/get on mhead |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 82 | static M* mget(G*); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 83 | static void gfput(G*); // put/get on gfree |
| 84 | static G* gfget(void); |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 85 | static void matchmg(void); // match ms to gs |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 86 | static void readylocked(G*); // ready, but sched is locked |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 87 | static void mnextg(M*, G*); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 88 | |
| 89 | // Scheduler loop. |
| 90 | static void scheduler(void); |
| 91 | |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 92 | // The bootstrap sequence is: |
| 93 | // |
| 94 | // call osinit |
| 95 | // call schedinit |
| 96 | // make & queue new G |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 97 | // call runtime·mstart |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 98 | // |
| 99 | // The new G does: |
| 100 | // |
| 101 | // call main·init_function |
| 102 | // call initdone |
| 103 | // call main·main |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 104 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 105 | runtime·schedinit(void) |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 106 | { |
| 107 | int32 n; |
| 108 | byte *p; |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 109 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 110 | runtime·allm = m; |
Russ Cox | 6eb251f | 2010-03-24 09:40:09 -0700 | [diff] [blame] | 111 | m->nomemprof++; |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 112 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 113 | runtime·mallocinit(); |
| 114 | runtime·goargs(); |
Alex Brainman | a41d854 | 2011-01-12 11:48:15 +1100 | [diff] [blame] | 115 | runtime·goenvs(); |
Russ Cox | 3609624 | 2009-01-16 14:58:14 -0800 | [diff] [blame] | 116 | |
Russ Cox | 0d3301a | 2009-12-07 15:52:14 -0800 | [diff] [blame] | 117 | // For debugging: |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 118 | // Allocate internal symbol table representation now, |
| 119 | // so that we don't need to call malloc when we crash. |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 120 | // runtime·findfunc(0); |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 121 | |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 122 | runtime·gomaxprocs = 1; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 123 | p = runtime·getenv("GOMAXPROCS"); |
| 124 | if(p != nil && (n = runtime·atoi(p)) != 0) |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 125 | runtime·gomaxprocs = n; |
| 126 | runtime·sched.mcpumax = runtime·gomaxprocs; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 127 | runtime·sched.mcount = 1; |
| 128 | runtime·sched.predawn = 1; |
Russ Cox | 6eb251f | 2010-03-24 09:40:09 -0700 | [diff] [blame] | 129 | |
| 130 | m->nomemprof--; |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 133 | // Called after main·init_function; main·main will be called on return. |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 134 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 135 | runtime·initdone(void) |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 136 | { |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 137 | // Let's go. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 138 | runtime·sched.predawn = 0; |
Russ Cox | 1ce1791 | 2009-01-26 17:37:05 -0800 | [diff] [blame] | 139 | mstats.enablegc = 1; |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 140 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 141 | // If main·init_function started other goroutines, |
| 142 | // kick off new ms to handle them, like ready |
| 143 | // would have, had it not been pre-dawn. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 144 | runtime·lock(&runtime·sched); |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 145 | matchmg(); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 146 | runtime·unlock(&runtime·sched); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 149 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 150 | runtime·goexit(void) |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 151 | { |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 152 | g->status = Gmoribund; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 153 | runtime·gosched(); |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ken Thompson | 1e1cc4e | 2009-01-27 12:03:53 -0800 | [diff] [blame] | 156 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 157 | runtime·tracebackothers(G *me) |
Rob Pike | 3835e01 | 2008-07-28 11:29:41 -0700 | [diff] [blame] | 158 | { |
| 159 | G *g; |
| 160 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 161 | for(g = runtime·allg; g != nil; g = g->alllink) { |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 162 | if(g == me || g->status == Gdead) |
Rob Pike | 3835e01 | 2008-07-28 11:29:41 -0700 | [diff] [blame] | 163 | continue; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 164 | runtime·printf("\ngoroutine %d [%d]:\n", g->goid, g->status); |
| 165 | runtime·traceback(g->sched.pc, g->sched.sp, 0, g); |
Rob Pike | 3835e01 | 2008-07-28 11:29:41 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 169 | // Put on `g' queue. Sched must be locked. |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 170 | static void |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 171 | gput(G *g) |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 172 | { |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 173 | M *m; |
| 174 | |
| 175 | // If g is wired, hand it off directly. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 176 | if(runtime·sched.mcpu < runtime·sched.mcpumax && (m = g->lockedm) != nil) { |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 177 | mnextg(m, g); |
| 178 | return; |
| 179 | } |
| 180 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 181 | g->schedlink = nil; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 182 | if(runtime·sched.ghead == nil) |
| 183 | runtime·sched.ghead = g; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 184 | else |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 185 | runtime·sched.gtail->schedlink = g; |
| 186 | runtime·sched.gtail = g; |
| 187 | runtime·sched.gwait++; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 190 | // Get from `g' queue. Sched must be locked. |
| 191 | static G* |
| 192 | gget(void) |
| 193 | { |
| 194 | G *g; |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 195 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 196 | g = runtime·sched.ghead; |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 197 | if(g){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 198 | runtime·sched.ghead = g->schedlink; |
| 199 | if(runtime·sched.ghead == nil) |
| 200 | runtime·sched.gtail = nil; |
| 201 | runtime·sched.gwait--; |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 202 | } |
| 203 | return g; |
| 204 | } |
| 205 | |
| 206 | // Put on `m' list. Sched must be locked. |
| 207 | static void |
| 208 | mput(M *m) |
| 209 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 210 | m->schedlink = runtime·sched.mhead; |
| 211 | runtime·sched.mhead = m; |
| 212 | runtime·sched.mwait++; |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 215 | // Get an `m' to run `g'. Sched must be locked. |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 216 | static M* |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 217 | mget(G *g) |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 218 | { |
| 219 | M *m; |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 220 | |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 221 | // if g has its own m, use it. |
| 222 | if((m = g->lockedm) != nil) |
| 223 | return m; |
| 224 | |
| 225 | // otherwise use general m pool. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 226 | if((m = runtime·sched.mhead) != nil){ |
| 227 | runtime·sched.mhead = m->schedlink; |
| 228 | runtime·sched.mwait--; |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 229 | } |
| 230 | return m; |
| 231 | } |
| 232 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 233 | // Mark g ready to run. |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 234 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 235 | runtime·ready(G *g) |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 236 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 237 | runtime·lock(&runtime·sched); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 238 | readylocked(g); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 239 | runtime·unlock(&runtime·sched); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Russ Cox | a61bb95 | 2008-09-24 14:13:07 -0700 | [diff] [blame] | 242 | // Mark g ready to run. Sched is already locked. |
| 243 | // G might be running already and about to stop. |
| 244 | // The sched lock protects g->status from changing underfoot. |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 245 | static void |
| 246 | readylocked(G *g) |
| 247 | { |
Russ Cox | a61bb95 | 2008-09-24 14:13:07 -0700 | [diff] [blame] | 248 | if(g->m){ |
| 249 | // Running on another machine. |
| 250 | // Ready it when it stops. |
| 251 | g->readyonstop = 1; |
| 252 | return; |
| 253 | } |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 254 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 255 | // Mark runnable. |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 256 | if(g->status == Grunnable || g->status == Grunning || g->status == Grecovery || g->status == Gstackalloc) { |
| 257 | runtime·printf("goroutine %d has status %d\n", g->goid, g->status); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 258 | runtime·throw("bad g->status in ready"); |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 259 | } |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 260 | g->status = Grunnable; |
| 261 | |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 262 | gput(g); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 263 | if(!runtime·sched.predawn) |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 264 | matchmg(); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Austin Clements | ad9c6f7 | 2009-09-18 09:11:19 -0700 | [diff] [blame] | 267 | static void |
| 268 | nop(void) |
| 269 | { |
| 270 | } |
| 271 | |
Russ Cox | fe8ff95 | 2009-08-31 18:10:11 -0700 | [diff] [blame] | 272 | // Same as readylocked but a different symbol so that |
| 273 | // debuggers can set a breakpoint here and catch all |
| 274 | // new goroutines. |
| 275 | static void |
| 276 | newprocreadylocked(G *g) |
| 277 | { |
Austin Clements | ad9c6f7 | 2009-09-18 09:11:19 -0700 | [diff] [blame] | 278 | nop(); // avoid inlining in 6l |
Russ Cox | fe8ff95 | 2009-08-31 18:10:11 -0700 | [diff] [blame] | 279 | readylocked(g); |
| 280 | } |
| 281 | |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 282 | // Pass g to m for running. |
| 283 | static void |
| 284 | mnextg(M *m, G *g) |
| 285 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 286 | runtime·sched.mcpu++; |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 287 | m->nextg = g; |
| 288 | if(m->waitnextg) { |
| 289 | m->waitnextg = 0; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 290 | runtime·notewakeup(&m->havenextg); |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 294 | // Get the next goroutine that m should run. |
| 295 | // Sched must be locked on entry, is unlocked on exit. |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 296 | // Makes sure that at most $GOMAXPROCS gs are |
| 297 | // running on cpus (not in system calls) at any given time. |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 298 | static G* |
| 299 | nextgandunlock(void) |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 300 | { |
Ken Thompson | e7d549f | 2008-07-16 13:50:23 -0700 | [diff] [blame] | 301 | G *gp; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 302 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 303 | if(runtime·sched.mcpu < 0) |
| 304 | runtime·throw("negative runtime·sched.mcpu"); |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 305 | |
| 306 | // If there is a g waiting as m->nextg, |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 307 | // mnextg took care of the runtime·sched.mcpu++. |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 308 | if(m->nextg != nil) { |
| 309 | gp = m->nextg; |
| 310 | m->nextg = nil; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 311 | runtime·unlock(&runtime·sched); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 312 | return gp; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 313 | } |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 314 | |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 315 | if(m->lockedg != nil) { |
| 316 | // We can only run one g, and it's not available. |
| 317 | // Make sure some other cpu is running to handle |
| 318 | // the ordinary run queue. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 319 | if(runtime·sched.gwait != 0) |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 320 | matchmg(); |
| 321 | } else { |
| 322 | // Look for work on global queue. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 323 | while(runtime·sched.mcpu < runtime·sched.mcpumax && (gp=gget()) != nil) { |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 324 | if(gp->lockedm) { |
| 325 | mnextg(gp->lockedm, gp); |
| 326 | continue; |
| 327 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 328 | runtime·sched.mcpu++; // this m will run gp |
| 329 | runtime·unlock(&runtime·sched); |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 330 | return gp; |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 331 | } |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 332 | // Otherwise, wait on global m queue. |
| 333 | mput(m); |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 334 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 335 | if(runtime·sched.mcpu == 0 && runtime·sched.msyscall == 0) |
| 336 | runtime·throw("all goroutines are asleep - deadlock!"); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 337 | m->nextg = nil; |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 338 | m->waitnextg = 1; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 339 | runtime·noteclear(&m->havenextg); |
| 340 | if(runtime·sched.waitstop && runtime·sched.mcpu <= runtime·sched.mcpumax) { |
| 341 | runtime·sched.waitstop = 0; |
| 342 | runtime·notewakeup(&runtime·sched.stopped); |
Russ Cox | be62913 | 2008-12-08 17:14:08 -0800 | [diff] [blame] | 343 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 344 | runtime·unlock(&runtime·sched); |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 345 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 346 | runtime·notesleep(&m->havenextg); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 347 | if((gp = m->nextg) == nil) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 348 | runtime·throw("bad m->nextg in nextgoroutine"); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 349 | m->nextg = nil; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 350 | return gp; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 353 | // TODO(rsc): Remove. This is only temporary, |
| 354 | // for the mark and sweep collector. |
| 355 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 356 | runtime·stoptheworld(void) |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 357 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 358 | runtime·lock(&runtime·sched); |
| 359 | runtime·gcwaiting = 1; |
| 360 | runtime·sched.mcpumax = 1; |
| 361 | while(runtime·sched.mcpu > 1) { |
Russ Cox | 88ce9ce | 2010-04-06 13:48:31 -0700 | [diff] [blame] | 362 | // It would be unsafe for multiple threads to be using |
| 363 | // the stopped note at once, but there is only |
| 364 | // ever one thread doing garbage collection, |
| 365 | // so this is okay. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 366 | runtime·noteclear(&runtime·sched.stopped); |
| 367 | runtime·sched.waitstop = 1; |
| 368 | runtime·unlock(&runtime·sched); |
| 369 | runtime·notesleep(&runtime·sched.stopped); |
| 370 | runtime·lock(&runtime·sched); |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 371 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 372 | runtime·unlock(&runtime·sched); |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // TODO(rsc): Remove. This is only temporary, |
| 376 | // for the mark and sweep collector. |
| 377 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 378 | runtime·starttheworld(void) |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 379 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 380 | runtime·lock(&runtime·sched); |
| 381 | runtime·gcwaiting = 0; |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 382 | runtime·sched.mcpumax = runtime·gomaxprocs; |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 383 | matchmg(); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 384 | runtime·unlock(&runtime·sched); |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 387 | // Called to start an M. |
| 388 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 389 | runtime·mstart(void) |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 390 | { |
Russ Cox | 74a9fc1 | 2010-01-06 19:24:11 -0800 | [diff] [blame] | 391 | if(g != m->g0) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 392 | runtime·throw("bad runtime·mstart"); |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 393 | if(m->mcache == nil) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 394 | m->mcache = runtime·allocmcache(); |
| 395 | runtime·minit(); |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 396 | scheduler(); |
| 397 | } |
| 398 | |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 399 | // When running with cgo, we call libcgo_thread_start |
| 400 | // to start threads for us so that we can play nicely with |
| 401 | // foreign code. |
| 402 | void (*libcgo_thread_start)(void*); |
| 403 | |
| 404 | typedef struct CgoThreadStart CgoThreadStart; |
| 405 | struct CgoThreadStart |
| 406 | { |
| 407 | M *m; |
| 408 | G *g; |
| 409 | void (*fn)(void); |
| 410 | }; |
| 411 | |
| 412 | // Kick off new ms as needed (up to mcpumax). |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 413 | // There are already `other' other cpus that will |
| 414 | // start looking for goroutines shortly. |
| 415 | // Sched is locked. |
| 416 | static void |
| 417 | matchmg(void) |
| 418 | { |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 419 | G *g; |
| 420 | |
Russ Cox | 41554e2 | 2009-11-17 22:00:30 -0800 | [diff] [blame] | 421 | if(m->mallocing || m->gcing) |
Russ Cox | 4dfd7fd | 2009-11-17 14:42:08 -0800 | [diff] [blame] | 422 | return; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 423 | while(runtime·sched.mcpu < runtime·sched.mcpumax && (g = gget()) != nil){ |
Russ Cox | 4dfd7fd | 2009-11-17 14:42:08 -0800 | [diff] [blame] | 424 | M *m; |
| 425 | |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 426 | // Find the m that will run g. |
| 427 | if((m = mget(g)) == nil){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 428 | m = runtime·malloc(sizeof(M)); |
| 429 | // Add to runtime·allm so garbage collector doesn't free m |
Russ Cox | 93689d8 | 2009-10-09 15:35:33 -0700 | [diff] [blame] | 430 | // when it is just in a register (R14 on amd64). |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 431 | m->alllink = runtime·allm; |
| 432 | runtime·allm = m; |
| 433 | m->id = runtime·sched.mcount++; |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 434 | |
Russ Cox | 9042c2c | 2010-12-08 13:53:30 -0500 | [diff] [blame] | 435 | if(runtime·iscgo) { |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 436 | CgoThreadStart ts; |
Russ Cox | 9042c2c | 2010-12-08 13:53:30 -0500 | [diff] [blame] | 437 | |
| 438 | if(libcgo_thread_start == nil) |
| 439 | runtime·throw("libcgo_thread_start missing"); |
Russ Cox | f25586a | 2010-02-10 00:00:12 -0800 | [diff] [blame] | 440 | // pthread_create will make us a stack. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 441 | m->g0 = runtime·malg(-1); |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 442 | ts.m = m; |
| 443 | ts.g = m->g0; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 444 | ts.fn = runtime·mstart; |
| 445 | runtime·runcgo(libcgo_thread_start, &ts); |
Russ Cox | f25586a | 2010-02-10 00:00:12 -0800 | [diff] [blame] | 446 | } else { |
Alex Brainman | f95a2f2 | 2010-09-12 11:45:16 +1000 | [diff] [blame] | 447 | if(Windows) |
| 448 | // windows will layout sched stack on os stack |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 449 | m->g0 = runtime·malg(-1); |
Alex Brainman | f95a2f2 | 2010-09-12 11:45:16 +1000 | [diff] [blame] | 450 | else |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 451 | m->g0 = runtime·malg(8192); |
| 452 | runtime·newosproc(m, m->g0, m->g0->stackbase, runtime·mstart); |
Russ Cox | f25586a | 2010-02-10 00:00:12 -0800 | [diff] [blame] | 453 | } |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 454 | } |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 455 | mnextg(m, g); |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 459 | // Scheduler loop: find g to run, run it, repeat. |
| 460 | static void |
Russ Cox | 4feda71 | 2008-08-02 22:34:04 -0700 | [diff] [blame] | 461 | scheduler(void) |
| 462 | { |
| 463 | G* gp; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 464 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 465 | runtime·lock(&runtime·sched); |
| 466 | if(runtime·gosave(&m->sched) != 0){ |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 467 | gp = m->curg; |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 468 | if(gp->status == Grecovery) { |
| 469 | // switched to scheduler to get stack unwound. |
| 470 | // don't go through the full scheduling logic. |
| 471 | Defer *d; |
| 472 | |
| 473 | d = gp->defer; |
| 474 | gp->defer = d->link; |
| 475 | |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 476 | // unwind to the stack frame with d's arguments in it. |
| 477 | unwindstack(gp, d->argp); |
Russ Cox | 72157c3 | 2010-04-08 13:24:53 -0700 | [diff] [blame] | 478 | |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 479 | // make the deferproc for this d return again, |
| 480 | // this time returning 1. function will jump to |
| 481 | // standard return epilogue. |
| 482 | // the -2*sizeof(uintptr) makes up for the |
| 483 | // two extra words that are on the stack at |
| 484 | // each call to deferproc. |
| 485 | // (the pc we're returning to does pop pop |
| 486 | // before it tests the return value.) |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 487 | // on the arm there are 2 saved LRs mixed in too. |
| 488 | if(thechar == '5') |
| 489 | gp->sched.sp = (byte*)d->argp - 4*sizeof(uintptr); |
| 490 | else |
| 491 | gp->sched.sp = (byte*)d->argp - 2*sizeof(uintptr); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 492 | gp->sched.pc = d->pc; |
Russ Cox | 214a55b | 2010-04-21 16:27:41 -0700 | [diff] [blame] | 493 | gp->status = Grunning; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 494 | runtime·free(d); |
| 495 | runtime·gogo(&gp->sched, 1); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 496 | } |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 497 | |
| 498 | if(gp->status == Gstackalloc) { |
| 499 | // switched to scheduler stack to call stackalloc. |
| 500 | gp->param = runtime·stackalloc((uintptr)gp->param); |
| 501 | gp->status = Grunning; |
| 502 | runtime·gogo(&gp->sched, 1); |
| 503 | } |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 504 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 505 | // Jumped here via runtime·gosave/gogo, so didn't |
| 506 | // execute lock(&runtime·sched) above. |
| 507 | runtime·lock(&runtime·sched); |
Russ Cox | 72154b0 | 2008-09-26 14:10:26 -0700 | [diff] [blame] | 508 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 509 | if(runtime·sched.predawn) |
| 510 | runtime·throw("init sleeping"); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 511 | |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 512 | // Just finished running gp. |
Russ Cox | a61bb95 | 2008-09-24 14:13:07 -0700 | [diff] [blame] | 513 | gp->m = nil; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 514 | runtime·sched.mcpu--; |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 515 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 516 | if(runtime·sched.mcpu < 0) |
| 517 | runtime·throw("runtime·sched.mcpu < 0 in scheduler"); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 518 | switch(gp->status){ |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 519 | case Grunnable: |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 520 | case Gdead: |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 521 | case Grecovery: |
| 522 | case Gstackalloc: |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 523 | // Shouldn't have been running! |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 524 | runtime·throw("bad gp->status in sched"); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 525 | case Grunning: |
| 526 | gp->status = Grunnable; |
| 527 | gput(gp); |
| 528 | break; |
| 529 | case Gmoribund: |
| 530 | gp->status = Gdead; |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 531 | if(gp->lockedm) { |
| 532 | gp->lockedm = nil; |
| 533 | m->lockedg = nil; |
| 534 | } |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 535 | unwindstack(gp, nil); |
Russ Cox | 2aea4a0 | 2009-08-26 15:26:09 -0700 | [diff] [blame] | 536 | gfput(gp); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 537 | if(--runtime·sched.gcount == 0) |
| 538 | runtime·exit(0); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 539 | break; |
| 540 | } |
Russ Cox | a61bb95 | 2008-09-24 14:13:07 -0700 | [diff] [blame] | 541 | if(gp->readyonstop){ |
| 542 | gp->readyonstop = 0; |
| 543 | readylocked(gp); |
| 544 | } |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 547 | // Find (or wait for) g to run. Unlocks runtime·sched. |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 548 | gp = nextgandunlock(); |
Russ Cox | a61bb95 | 2008-09-24 14:13:07 -0700 | [diff] [blame] | 549 | gp->readyonstop = 0; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 550 | gp->status = Grunning; |
Russ Cox | 4feda71 | 2008-08-02 22:34:04 -0700 | [diff] [blame] | 551 | m->curg = gp; |
Russ Cox | a61bb95 | 2008-09-24 14:13:07 -0700 | [diff] [blame] | 552 | gp->m = m; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 553 | if(gp->sched.pc == (byte*)runtime·goexit) { // kickoff |
| 554 | runtime·gogocall(&gp->sched, (void(*)(void))gp->entry); |
Ken Thompson | b33f5d5 | 2010-10-13 13:24:14 -0700 | [diff] [blame] | 555 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 556 | runtime·gogo(&gp->sched, 1); |
Russ Cox | 4feda71 | 2008-08-02 22:34:04 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 559 | // Enter scheduler. If g->status is Grunning, |
| 560 | // re-queues g and runs everyone else who is waiting |
| 561 | // before running g again. If g->status is Gmoribund, |
| 562 | // kills off g. |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 563 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 564 | runtime·gosched(void) |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 565 | { |
Russ Cox | 7110881 | 2010-01-12 10:03:02 -0800 | [diff] [blame] | 566 | if(m->locks != 0) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 567 | runtime·throw("gosched holding locks"); |
Russ Cox | 74a9fc1 | 2010-01-06 19:24:11 -0800 | [diff] [blame] | 568 | if(g == m->g0) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 569 | runtime·throw("gosched of g0"); |
| 570 | if(runtime·gosave(&g->sched) == 0) |
| 571 | runtime·gogo(&m->sched, 1); |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 574 | // The goroutine g is about to enter a system call. |
| 575 | // Record that it's not using the cpu anymore. |
Ian Lance Taylor | 2e20386 | 2010-04-09 13:30:35 -0700 | [diff] [blame] | 576 | // This is called only from the go syscall library and cgocall, |
| 577 | // not from the low-level system calls used by the runtime. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 578 | // Entersyscall cannot split the stack: the runtime·gosave must |
Russ Cox | e2bde5c | 2010-08-12 23:26:54 -0700 | [diff] [blame] | 579 | // make g->sched refer to the caller's stack pointer. |
| 580 | #pragma textflag 7 |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 581 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 582 | runtime·entersyscall(void) |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 583 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 584 | runtime·lock(&runtime·sched); |
Ian Lance Taylor | e1b4715 | 2010-05-06 22:07:03 -0700 | [diff] [blame] | 585 | // Leave SP around for gc and traceback. |
| 586 | // Do before notewakeup so that gc |
| 587 | // never sees Gsyscall with wrong stack. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 588 | runtime·gosave(&g->sched); |
| 589 | if(runtime·sched.predawn) { |
| 590 | runtime·unlock(&runtime·sched); |
Russ Cox | 052a66b | 2009-07-21 19:43:27 -0700 | [diff] [blame] | 591 | return; |
| 592 | } |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 593 | g->status = Gsyscall; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 594 | runtime·sched.mcpu--; |
| 595 | runtime·sched.msyscall++; |
| 596 | if(runtime·sched.gwait != 0) |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 597 | matchmg(); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 598 | if(runtime·sched.waitstop && runtime·sched.mcpu <= runtime·sched.mcpumax) { |
| 599 | runtime·sched.waitstop = 0; |
| 600 | runtime·notewakeup(&runtime·sched.stopped); |
Russ Cox | 53e69e1 | 2009-01-27 14:01:20 -0800 | [diff] [blame] | 601 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 602 | runtime·unlock(&runtime·sched); |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // The goroutine g exited its system call. |
| 606 | // Arrange for it to run on a cpu again. |
| 607 | // This is called only from the go syscall library, not |
| 608 | // from the low-level system calls used by the runtime. |
| 609 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 610 | runtime·exitsyscall(void) |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 611 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 612 | runtime·lock(&runtime·sched); |
| 613 | if(runtime·sched.predawn) { |
| 614 | runtime·unlock(&runtime·sched); |
Russ Cox | 052a66b | 2009-07-21 19:43:27 -0700 | [diff] [blame] | 615 | return; |
| 616 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 617 | runtime·sched.msyscall--; |
| 618 | runtime·sched.mcpu++; |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 619 | // Fast path - if there's room for this m, we're done. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 620 | if(runtime·sched.mcpu <= runtime·sched.mcpumax) { |
Russ Cox | 19c1835 | 2009-12-14 19:06:20 -0800 | [diff] [blame] | 621 | g->status = Grunning; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 622 | runtime·unlock(&runtime·sched); |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 623 | return; |
| 624 | } |
Russ Cox | 19c1835 | 2009-12-14 19:06:20 -0800 | [diff] [blame] | 625 | // Tell scheduler to put g back on the run queue: |
| 626 | // mostly equivalent to g->status = Grunning, |
| 627 | // but keeps the garbage collector from thinking |
| 628 | // that g is running right now, which it's not. |
| 629 | g->readyonstop = 1; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 630 | runtime·unlock(&runtime·sched); |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 631 | |
| 632 | // Slow path - all the cpus are taken. |
| 633 | // The scheduler will ready g and put this m to sleep. |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 634 | // When the scheduler takes g away from m, |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 635 | // it will undo the runtime·sched.mcpu++ above. |
| 636 | runtime·gosched(); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Ian Lance Taylor | 2d39303 | 2011-01-08 10:22:37 -0800 | [diff] [blame] | 639 | // Restore the position of m's scheduler stack if we unwind the stack |
| 640 | // through a cgo callback. |
| 641 | static void |
| 642 | runtime·unwindcgocallback(void **spaddr, void *sp) |
| 643 | { |
| 644 | *spaddr = sp; |
| 645 | } |
| 646 | |
Ian Lance Taylor | 2e20386 | 2010-04-09 13:30:35 -0700 | [diff] [blame] | 647 | // Start scheduling g1 again for a cgo callback. |
| 648 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 649 | runtime·startcgocallback(G* g1) |
Ian Lance Taylor | 2e20386 | 2010-04-09 13:30:35 -0700 | [diff] [blame] | 650 | { |
Ian Lance Taylor | 2d39303 | 2011-01-08 10:22:37 -0800 | [diff] [blame] | 651 | Defer *d; |
Ian Lance Taylor | 2d39303 | 2011-01-08 10:22:37 -0800 | [diff] [blame] | 652 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 653 | runtime·lock(&runtime·sched); |
Ian Lance Taylor | 2e20386 | 2010-04-09 13:30:35 -0700 | [diff] [blame] | 654 | g1->status = Grunning; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 655 | runtime·sched.msyscall--; |
| 656 | runtime·sched.mcpu++; |
| 657 | runtime·unlock(&runtime·sched); |
Ian Lance Taylor | 2d39303 | 2011-01-08 10:22:37 -0800 | [diff] [blame] | 658 | |
| 659 | // Add an entry to the defer stack which restores the old |
| 660 | // position of m's scheduler stack. This is so that if the |
| 661 | // code we are calling panics, we won't lose the space on the |
| 662 | // scheduler stack. Note that we are locked to this m here. |
| 663 | d = runtime·malloc(sizeof(*d) + 2*sizeof(void*) - sizeof(d->args)); |
| 664 | d->fn = (byte*)runtime·unwindcgocallback; |
| 665 | d->siz = 2 * sizeof(uintptr); |
| 666 | ((void**)d->args)[0] = &m->sched.sp; |
| 667 | ((void**)d->args)[1] = m->sched.sp; |
| 668 | d->link = g1->defer; |
| 669 | g1->defer = d; |
Ian Lance Taylor | 2e20386 | 2010-04-09 13:30:35 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | // Stop scheduling g1 after a cgo callback. |
| 673 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 674 | runtime·endcgocallback(G* g1) |
Ian Lance Taylor | 2e20386 | 2010-04-09 13:30:35 -0700 | [diff] [blame] | 675 | { |
Ian Lance Taylor | 2d39303 | 2011-01-08 10:22:37 -0800 | [diff] [blame] | 676 | Defer *d; |
| 677 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 678 | runtime·lock(&runtime·sched); |
Ian Lance Taylor | 2e20386 | 2010-04-09 13:30:35 -0700 | [diff] [blame] | 679 | g1->status = Gsyscall; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 680 | runtime·sched.mcpu--; |
| 681 | runtime·sched.msyscall++; |
| 682 | runtime·unlock(&runtime·sched); |
Ian Lance Taylor | 2d39303 | 2011-01-08 10:22:37 -0800 | [diff] [blame] | 683 | |
| 684 | // Remove the entry on the defer stack added by |
| 685 | // startcgocallback. |
| 686 | d = g1->defer; |
| 687 | if (d == nil || d->fn != (byte*)runtime·unwindcgocallback) |
| 688 | runtime·throw("bad defer entry in endcgocallback"); |
| 689 | g1->defer = d->link; |
| 690 | runtime·free(d); |
Ian Lance Taylor | 2e20386 | 2010-04-09 13:30:35 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 693 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 694 | runtime·oldstack(void) |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 695 | { |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 696 | Stktop *top, old; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 697 | uint32 argsize; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 698 | byte *sp; |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 699 | G *g1; |
Russ Cox | 36c5c5b | 2010-03-04 15:34:25 -0800 | [diff] [blame] | 700 | static int32 goid; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 701 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 702 | //printf("oldstack m->cret=%p\n", m->cret); |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 703 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 704 | g1 = m->curg; |
| 705 | top = (Stktop*)g1->stackbase; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 706 | sp = (byte*)top; |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 707 | old = *top; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 708 | argsize = old.argsize; |
| 709 | if(argsize > 0) { |
| 710 | sp -= argsize; |
| 711 | runtime·mcpy(top->argp, sp, argsize); |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 712 | } |
Russ Cox | 36c5c5b | 2010-03-04 15:34:25 -0800 | [diff] [blame] | 713 | goid = old.gobuf.g->goid; // fault if g is bad, before gogo |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 714 | |
Russ Cox | afc6928 | 2011-01-25 16:35:36 -0500 | [diff] [blame] | 715 | if(old.free != 0) |
Russ Cox | d9fd114 | 2011-02-22 17:40:40 -0500 | [diff] [blame] | 716 | runtime·stackfree(g1->stackguard - StackGuard - StackSystem, old.free); |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 717 | g1->stackbase = old.stackbase; |
| 718 | g1->stackguard = old.stackguard; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 719 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 720 | runtime·gogo(&old.gobuf, m->cret); |
Russ Cox | 79e1db2 | 2008-12-04 08:30:54 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 724 | runtime·newstack(void) |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 725 | { |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 726 | int32 framesize, argsize; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 727 | Stktop *top; |
| 728 | byte *stk, *sp; |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 729 | G *g1; |
| 730 | Gobuf label; |
Russ Cox | afc6928 | 2011-01-25 16:35:36 -0500 | [diff] [blame] | 731 | bool reflectcall; |
| 732 | uintptr free; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 733 | |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 734 | framesize = m->moreframesize; |
| 735 | argsize = m->moreargsize; |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 736 | g1 = m->curg; |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 737 | |
Russ Cox | 1fa4173 | 2011-02-01 18:34:41 -0500 | [diff] [blame] | 738 | if(m->morebuf.sp < g1->stackguard - StackGuard) { |
| 739 | runtime·printf("runtime: split stack overflow: %p < %p\n", m->morebuf.sp, g1->stackguard - StackGuard); |
| 740 | runtime·throw("runtime: split stack overflow"); |
| 741 | } |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 742 | |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 743 | reflectcall = framesize==1; |
| 744 | if(reflectcall) |
| 745 | framesize = 0; |
| 746 | |
| 747 | if(reflectcall && m->morebuf.sp - sizeof(Stktop) - argsize - 32 > g1->stackguard) { |
| 748 | // special case: called from reflect.call (framesize==1) |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 749 | // to call code with an arbitrary argument size, |
| 750 | // and we have enough space on the current stack. |
| 751 | // the new Stktop* is necessary to unwind, but |
| 752 | // we don't need to create a new segment. |
| 753 | top = (Stktop*)(m->morebuf.sp - sizeof(*top)); |
Russ Cox | d9fd114 | 2011-02-22 17:40:40 -0500 | [diff] [blame] | 754 | stk = g1->stackguard - StackGuard - StackSystem; |
Russ Cox | afc6928 | 2011-01-25 16:35:36 -0500 | [diff] [blame] | 755 | free = 0; |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 756 | } else { |
| 757 | // allocate new segment. |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 758 | framesize += argsize; |
Hector Chu | aae5f91 | 2011-01-19 15:10:15 -0500 | [diff] [blame] | 759 | framesize += StackExtra; // room for more functions, Stktop. |
Russ Cox | d9fd114 | 2011-02-22 17:40:40 -0500 | [diff] [blame] | 760 | if(framesize < StackMin) |
| 761 | framesize = StackMin; |
| 762 | framesize += StackSystem; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 763 | stk = runtime·stackalloc(framesize); |
| 764 | top = (Stktop*)(stk+framesize-sizeof(*top)); |
Russ Cox | afc6928 | 2011-01-25 16:35:36 -0500 | [diff] [blame] | 765 | free = framesize; |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 766 | } |
Russ Cox | f25586a | 2010-02-10 00:00:12 -0800 | [diff] [blame] | 767 | |
Russ Cox | b287d7c | 2011-02-02 16:44:20 -0500 | [diff] [blame] | 768 | //runtime·printf("newstack framesize=%d argsize=%d morepc=%p moreargp=%p gobuf=%p, %p top=%p old=%p\n", |
| 769 | //framesize, argsize, m->morepc, m->moreargp, m->morebuf.pc, m->morebuf.sp, top, g1->stackbase); |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 770 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 771 | top->stackbase = g1->stackbase; |
| 772 | top->stackguard = g1->stackguard; |
| 773 | top->gobuf = m->morebuf; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 774 | top->argp = m->moreargp; |
| 775 | top->argsize = argsize; |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 776 | top->free = free; |
Russ Cox | b287d7c | 2011-02-02 16:44:20 -0500 | [diff] [blame] | 777 | |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 778 | // copy flag from panic |
| 779 | top->panic = g1->ispanic; |
| 780 | g1->ispanic = false; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 781 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 782 | g1->stackbase = (byte*)top; |
Russ Cox | d9fd114 | 2011-02-22 17:40:40 -0500 | [diff] [blame] | 783 | g1->stackguard = stk + StackGuard + StackSystem; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 784 | |
| 785 | sp = (byte*)top; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 786 | if(argsize > 0) { |
| 787 | sp -= argsize; |
| 788 | runtime·mcpy(sp, m->moreargp, argsize); |
| 789 | } |
| 790 | if(thechar == '5') { |
| 791 | // caller would have saved its LR below args. |
| 792 | sp -= sizeof(void*); |
| 793 | *(void**)sp = nil; |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 796 | // Continue as if lessstack had just called m->morepc |
| 797 | // (the PC that decided to grow the stack). |
| 798 | label.sp = sp; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 799 | label.pc = (byte*)runtime·lessstack; |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 800 | label.g = m->curg; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 801 | runtime·gogocall(&label, m->morepc); |
Ken Thompson | af58f17 | 2008-07-14 14:34:27 -0700 | [diff] [blame] | 802 | |
| 803 | *(int32*)345 = 123; // never return |
| 804 | } |
| 805 | |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 806 | G* |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 807 | runtime·malg(int32 stacksize) |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 808 | { |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 809 | G *newg; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 810 | byte *stk; |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 811 | int32 oldstatus; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 812 | |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 813 | newg = runtime·malloc(sizeof(G)); |
Russ Cox | f25586a | 2010-02-10 00:00:12 -0800 | [diff] [blame] | 814 | if(stacksize >= 0) { |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 815 | if(g == m->g0) { |
| 816 | // running on scheduler stack already. |
| 817 | stk = runtime·stackalloc(StackSystem + stacksize); |
| 818 | } else { |
| 819 | // have to call stackalloc on scheduler stack. |
| 820 | oldstatus = g->status; |
| 821 | g->param = (void*)(StackSystem + stacksize); |
| 822 | g->status = Gstackalloc; |
| 823 | // next two lines are runtime·gosched without the check |
| 824 | // of m->locks. we're almost certainly holding a lock, |
| 825 | // but this is not a real rescheduling so it's okay. |
| 826 | if(runtime·gosave(&g->sched) == 0) |
| 827 | runtime·gogo(&m->sched, 1); |
| 828 | stk = g->param; |
| 829 | g->param = nil; |
| 830 | g->status = oldstatus; |
| 831 | } |
| 832 | newg->stack0 = stk; |
| 833 | newg->stackguard = stk + StackSystem + StackGuard; |
| 834 | newg->stackbase = stk + StackSystem + stacksize - sizeof(Stktop); |
| 835 | runtime·memclr(newg->stackbase, sizeof(Stktop)); |
Russ Cox | f25586a | 2010-02-10 00:00:12 -0800 | [diff] [blame] | 836 | } |
Russ Cox | b5dfac4 | 2011-02-23 15:51:20 -0500 | [diff] [blame^] | 837 | return newg; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Newproc and deferproc need to be textflag 7 |
| 842 | * (no possible stack split when nearing overflow) |
| 843 | * because they assume that the arguments to fn |
| 844 | * are available sequentially beginning at &arg0. |
| 845 | * If a stack split happened, only the one word |
| 846 | * arg0 would be copied. It's okay if any functions |
| 847 | * they call split the stack below the newproc frame. |
| 848 | */ |
| 849 | #pragma textflag 7 |
| 850 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 851 | runtime·newproc(int32 siz, byte* fn, ...) |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 852 | { |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 853 | byte *argp; |
| 854 | |
| 855 | if(thechar == '5') |
| 856 | argp = (byte*)(&fn+2); // skip caller's saved LR |
| 857 | else |
| 858 | argp = (byte*)(&fn+1); |
| 859 | runtime·newproc1(fn, argp, siz, 0); |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Russ Cox | 4e28cfe | 2010-03-26 14:15:30 -0700 | [diff] [blame] | 862 | G* |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 863 | runtime·newproc1(byte *fn, byte *argp, int32 narg, int32 nret) |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 864 | { |
Russ Cox | f25586a | 2010-02-10 00:00:12 -0800 | [diff] [blame] | 865 | byte *sp; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 866 | G *newg; |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 867 | int32 siz; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 868 | |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 869 | //printf("newproc1 %p %p narg=%d nret=%d\n", fn, argp, narg, nret); |
| 870 | siz = narg + nret; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 871 | siz = (siz+7) & ~7; |
| 872 | if(siz > 1024) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 873 | runtime·throw("runtime.newproc: too many args"); |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 874 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 875 | runtime·lock(&runtime·sched); |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 876 | |
| 877 | if((newg = gfget()) != nil){ |
| 878 | newg->status = Gwaiting; |
Russ Cox | d9fd114 | 2011-02-22 17:40:40 -0500 | [diff] [blame] | 879 | if(newg->stackguard - StackGuard - StackSystem != newg->stack0) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 880 | runtime·throw("invalid stack in newg"); |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 881 | } else { |
Russ Cox | d9fd114 | 2011-02-22 17:40:40 -0500 | [diff] [blame] | 882 | newg = runtime·malg(StackMin); |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 883 | newg->status = Gwaiting; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 884 | newg->alllink = runtime·allg; |
| 885 | runtime·allg = newg; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 886 | } |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 887 | |
Russ Cox | f25586a | 2010-02-10 00:00:12 -0800 | [diff] [blame] | 888 | sp = newg->stackbase; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 889 | sp -= siz; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 890 | runtime·mcpy(sp, argp, narg); |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 891 | if(thechar == '5') { |
| 892 | // caller's LR |
| 893 | sp -= sizeof(void*); |
| 894 | *(void**)sp = nil; |
| 895 | } |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 896 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 897 | newg->sched.sp = sp; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 898 | newg->sched.pc = (byte*)runtime·goexit; |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 899 | newg->sched.g = newg; |
| 900 | newg->entry = fn; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 901 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 902 | runtime·sched.gcount++; |
| 903 | runtime·goidgen++; |
| 904 | newg->goid = runtime·goidgen; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 905 | |
Russ Cox | fe8ff95 | 2009-08-31 18:10:11 -0700 | [diff] [blame] | 906 | newprocreadylocked(newg); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 907 | runtime·unlock(&runtime·sched); |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 908 | |
Russ Cox | 4e28cfe | 2010-03-26 14:15:30 -0700 | [diff] [blame] | 909 | return newg; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 910 | //printf(" goid=%d\n", newg->goid); |
| 911 | } |
| 912 | |
| 913 | #pragma textflag 7 |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 914 | uintptr |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 915 | runtime·deferproc(int32 siz, byte* fn, ...) |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 916 | { |
| 917 | Defer *d; |
| 918 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 919 | d = runtime·malloc(sizeof(*d) + siz - sizeof(d->args)); |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 920 | d->fn = fn; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 921 | d->siz = siz; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 922 | d->pc = runtime·getcallerpc(&siz); |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 923 | if(thechar == '5') |
| 924 | d->argp = (byte*)(&fn+2); // skip caller's saved link register |
| 925 | else |
| 926 | d->argp = (byte*)(&fn+1); |
| 927 | runtime·mcpy(d->args, d->argp, d->siz); |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 928 | |
| 929 | d->link = g->defer; |
| 930 | g->defer = d; |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 931 | |
| 932 | // deferproc returns 0 normally. |
| 933 | // a deferred func that stops a panic |
| 934 | // makes the deferproc return 1. |
| 935 | // the code the compiler generates always |
| 936 | // checks the return value and jumps to the |
| 937 | // end of the function if deferproc returns != 0. |
| 938 | return 0; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | #pragma textflag 7 |
| 942 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 943 | runtime·deferreturn(uintptr arg0) |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 944 | { |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 945 | Defer *d; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 946 | byte *argp, *fn; |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 947 | |
| 948 | d = g->defer; |
| 949 | if(d == nil) |
| 950 | return; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 951 | argp = (byte*)&arg0; |
| 952 | if(d->argp != argp) |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 953 | return; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 954 | runtime·mcpy(argp, d->args, d->siz); |
Russ Cox | 9510034 | 2009-04-01 00:26:00 -0700 | [diff] [blame] | 955 | g->defer = d->link; |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 956 | fn = d->fn; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 957 | runtime·free(d); |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 958 | runtime·jmpdefer(fn, argp); |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static void |
| 962 | rundefer(void) |
| 963 | { |
| 964 | Defer *d; |
| 965 | |
| 966 | while((d = g->defer) != nil) { |
| 967 | g->defer = d->link; |
| 968 | reflect·call(d->fn, d->args, d->siz); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 969 | runtime·free(d); |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | // Free stack frames until we hit the last one |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 974 | // or until we find the one that contains the argp. |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 975 | static void |
| 976 | unwindstack(G *gp, byte *sp) |
| 977 | { |
| 978 | Stktop *top; |
| 979 | byte *stk; |
| 980 | |
| 981 | // Must be called from a different goroutine, usually m->g0. |
| 982 | if(g == gp) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 983 | runtime·throw("unwindstack on self"); |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 984 | |
| 985 | while((top = (Stktop*)gp->stackbase) != nil && top->stackbase != nil) { |
| 986 | stk = gp->stackguard - StackGuard; |
| 987 | if(stk <= sp && sp < gp->stackbase) |
| 988 | break; |
| 989 | gp->stackbase = top->stackbase; |
| 990 | gp->stackguard = top->stackguard; |
Russ Cox | afc6928 | 2011-01-25 16:35:36 -0500 | [diff] [blame] | 991 | if(top->free != 0) |
| 992 | runtime·stackfree(stk, top->free); |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 993 | } |
Russ Cox | 72157c3 | 2010-04-08 13:24:53 -0700 | [diff] [blame] | 994 | |
| 995 | if(sp != nil && (sp < gp->stackguard - StackGuard || gp->stackbase < sp)) { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 996 | runtime·printf("recover: %p not in [%p, %p]\n", sp, gp->stackguard - StackGuard, gp->stackbase); |
| 997 | runtime·throw("bad unwindstack"); |
Russ Cox | 72157c3 | 2010-04-08 13:24:53 -0700 | [diff] [blame] | 998 | } |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1001 | static void |
| 1002 | printpanics(Panic *p) |
| 1003 | { |
| 1004 | if(p->link) { |
| 1005 | printpanics(p->link); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1006 | runtime·printf("\t"); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1007 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1008 | runtime·printf("panic: "); |
| 1009 | runtime·printany(p->arg); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1010 | if(p->recovered) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1011 | runtime·printf(" [recovered]"); |
| 1012 | runtime·printf("\n"); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1016 | runtime·panic(Eface e) |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1017 | { |
| 1018 | Defer *d; |
| 1019 | Panic *p; |
| 1020 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1021 | p = runtime·mal(sizeof *p); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1022 | p->arg = e; |
| 1023 | p->link = g->panic; |
| 1024 | p->stackbase = g->stackbase; |
| 1025 | g->panic = p; |
| 1026 | |
| 1027 | for(;;) { |
| 1028 | d = g->defer; |
| 1029 | if(d == nil) |
| 1030 | break; |
| 1031 | // take defer off list in case of recursive panic |
| 1032 | g->defer = d->link; |
| 1033 | g->ispanic = true; // rock for newstack, where reflect.call ends up |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 1034 | reflect·call(d->fn, d->args, d->siz); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1035 | if(p->recovered) { |
| 1036 | g->panic = p->link; |
Russ Cox | 1230700 | 2011-01-18 14:15:11 -0500 | [diff] [blame] | 1037 | if(g->panic == nil) // must be done with signal |
| 1038 | g->sig = 0; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1039 | runtime·free(p); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1040 | // put recovering defer back on list |
| 1041 | // for scheduler to find. |
| 1042 | d->link = g->defer; |
| 1043 | g->defer = d; |
| 1044 | g->status = Grecovery; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1045 | runtime·gosched(); |
| 1046 | runtime·throw("recovery failed"); // gosched should not return |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1047 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1048 | runtime·free(d); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | // ran out of deferred calls - old-school panic now |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 1052 | runtime·startpanic(); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1053 | printpanics(g->panic); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1054 | runtime·dopanic(0); |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | #pragma textflag 7 /* no split, or else g->stackguard is not the stack for fp */ |
| 1058 | void |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 1059 | runtime·recover(byte *argp, Eface ret) |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1060 | { |
| 1061 | Stktop *top, *oldtop; |
| 1062 | Panic *p; |
| 1063 | |
| 1064 | // Must be a panic going on. |
| 1065 | if((p = g->panic) == nil || p->recovered) |
| 1066 | goto nomatch; |
| 1067 | |
| 1068 | // Frame must be at the top of the stack segment, |
| 1069 | // because each deferred call starts a new stack |
| 1070 | // segment as a side effect of using reflect.call. |
| 1071 | // (There has to be some way to remember the |
| 1072 | // variable argument frame size, and the segment |
| 1073 | // code already takes care of that for us, so we |
| 1074 | // reuse it.) |
| 1075 | // |
| 1076 | // As usual closures complicate things: the fp that |
| 1077 | // the closure implementation function claims to have |
| 1078 | // is where the explicit arguments start, after the |
| 1079 | // implicit pointer arguments and PC slot. |
| 1080 | // If we're on the first new segment for a closure, |
| 1081 | // then fp == top - top->args is correct, but if |
| 1082 | // the closure has its own big argument frame and |
| 1083 | // allocated a second segment (see below), |
| 1084 | // the fp is slightly above top - top->args. |
| 1085 | // That condition can't happen normally though |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 1086 | // (stack pointers go down, not up), so we can accept |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1087 | // any fp between top and top - top->args as |
| 1088 | // indicating the top of the segment. |
| 1089 | top = (Stktop*)g->stackbase; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 1090 | if(argp < (byte*)top - top->argsize || (byte*)top < argp) |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1091 | goto nomatch; |
| 1092 | |
| 1093 | // The deferred call makes a new segment big enough |
| 1094 | // for the argument frame but not necessarily big |
| 1095 | // enough for the function's local frame (size unknown |
| 1096 | // at the time of the call), so the function might have |
| 1097 | // made its own segment immediately. If that's the |
| 1098 | // case, back top up to the older one, the one that |
| 1099 | // reflect.call would have made for the panic. |
| 1100 | // |
| 1101 | // The fp comparison here checks that the argument |
| 1102 | // frame that was copied during the split (the top->args |
| 1103 | // bytes above top->fp) abuts the old top of stack. |
| 1104 | // This is a correct test for both closure and non-closure code. |
| 1105 | oldtop = (Stktop*)top->stackbase; |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 1106 | if(oldtop != nil && top->argp == (byte*)oldtop - top->argsize) |
Russ Cox | 9b1507b | 2010-03-31 11:46:01 -0700 | [diff] [blame] | 1107 | top = oldtop; |
| 1108 | |
| 1109 | // Now we have the segment that was created to |
| 1110 | // run this call. It must have been marked as a panic segment. |
| 1111 | if(!top->panic) |
| 1112 | goto nomatch; |
| 1113 | |
| 1114 | // Okay, this is the top frame of a deferred call |
| 1115 | // in response to a panic. It can see the panic argument. |
| 1116 | p->recovered = 1; |
| 1117 | ret = p->arg; |
| 1118 | FLUSH(&ret); |
| 1119 | return; |
| 1120 | |
| 1121 | nomatch: |
| 1122 | ret.type = nil; |
| 1123 | ret.data = nil; |
| 1124 | FLUSH(&ret); |
| 1125 | } |
| 1126 | |
| 1127 | |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 1128 | // Put on gfree list. Sched must be locked. |
| 1129 | static void |
| 1130 | gfput(G *g) |
| 1131 | { |
Russ Cox | d9fd114 | 2011-02-22 17:40:40 -0500 | [diff] [blame] | 1132 | if(g->stackguard - StackGuard - StackSystem != g->stack0) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1133 | runtime·throw("invalid stack in gfput"); |
| 1134 | g->schedlink = runtime·sched.gfree; |
| 1135 | runtime·sched.gfree = g; |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | // Get from gfree list. Sched must be locked. |
| 1139 | static G* |
| 1140 | gfget(void) |
| 1141 | { |
| 1142 | G *g; |
| 1143 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1144 | g = runtime·sched.gfree; |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 1145 | if(g) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1146 | runtime·sched.gfree = g->schedlink; |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 1147 | return g; |
| 1148 | } |
Russ Cox | 79e1db2 | 2008-12-04 08:30:54 -0800 | [diff] [blame] | 1149 | |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 1150 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1151 | runtime·Breakpoint(void) |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 1152 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1153 | runtime·breakpoint(); |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1157 | runtime·Goexit(void) |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 1158 | { |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 1159 | rundefer(); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1160 | runtime·goexit(); |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1164 | runtime·Gosched(void) |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 1165 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1166 | runtime·gosched(); |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 1169 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1170 | runtime·LockOSThread(void) |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 1171 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1172 | if(runtime·sched.predawn) |
| 1173 | runtime·throw("cannot wire during init"); |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 1174 | m->lockedg = g; |
| 1175 | g->lockedm = m; |
| 1176 | } |
| 1177 | |
Rob Pike | 7955490 | 2009-08-06 13:07:05 -0700 | [diff] [blame] | 1178 | // delete when scheduler is stronger |
Rob Pike | eb48bfb | 2010-05-06 11:50:47 -0700 | [diff] [blame] | 1179 | int32 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1180 | runtime·gomaxprocsfunc(int32 n) |
Rob Pike | 7955490 | 2009-08-06 13:07:05 -0700 | [diff] [blame] | 1181 | { |
Rob Pike | eb48bfb | 2010-05-06 11:50:47 -0700 | [diff] [blame] | 1182 | int32 ret; |
Rob Pike | 7955490 | 2009-08-06 13:07:05 -0700 | [diff] [blame] | 1183 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1184 | runtime·lock(&runtime·sched); |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 1185 | ret = runtime·gomaxprocs; |
Rob Pike | eb48bfb | 2010-05-06 11:50:47 -0700 | [diff] [blame] | 1186 | if (n <= 0) |
| 1187 | n = ret; |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 1188 | runtime·gomaxprocs = n; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1189 | runtime·sched.mcpumax = n; |
Russ Cox | 88ce9ce | 2010-04-06 13:48:31 -0700 | [diff] [blame] | 1190 | // handle fewer procs? |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1191 | if(runtime·sched.mcpu > runtime·sched.mcpumax) { |
| 1192 | runtime·unlock(&runtime·sched); |
Russ Cox | 88ce9ce | 2010-04-06 13:48:31 -0700 | [diff] [blame] | 1193 | // just give up the cpu. |
| 1194 | // we'll only get rescheduled once the |
| 1195 | // number has come down. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1196 | runtime·gosched(); |
Rob Pike | eb48bfb | 2010-05-06 11:50:47 -0700 | [diff] [blame] | 1197 | return ret; |
Rob Pike | 7955490 | 2009-08-06 13:07:05 -0700 | [diff] [blame] | 1198 | } |
| 1199 | // handle more procs |
| 1200 | matchmg(); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1201 | runtime·unlock(&runtime·sched); |
Rob Pike | eb48bfb | 2010-05-06 11:50:47 -0700 | [diff] [blame] | 1202 | return ret; |
Rob Pike | 7955490 | 2009-08-06 13:07:05 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 1205 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1206 | runtime·UnlockOSThread(void) |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 1207 | { |
| 1208 | m->lockedg = nil; |
| 1209 | g->lockedm = nil; |
| 1210 | } |
| 1211 | |
| 1212 | // for testing of wire, unwire |
| 1213 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 1214 | runtime·mid(uint32 ret) |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 1215 | { |
| 1216 | ret = m->id; |
| 1217 | FLUSH(&ret); |
| 1218 | } |
Keith Rarick | 51a2183 | 2010-12-07 18:06:31 -0500 | [diff] [blame] | 1219 | |
| 1220 | void |
| 1221 | runtime·Goroutines(int32 ret) |
| 1222 | { |
| 1223 | ret = runtime·sched.gcount; |
| 1224 | FLUSH(&ret); |
| 1225 | } |
Russ Cox | 4608feb | 2011-01-28 15:03:26 -0500 | [diff] [blame] | 1226 | |
| 1227 | int32 |
| 1228 | runtime·mcount(void) |
| 1229 | { |
| 1230 | return runtime·sched.mcount; |
| 1231 | } |