proc.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434
  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. #include <limits.h>
  5. #include <signal.h>
  6. #include <stdlib.h>
  7. #include <pthread.h>
  8. #include <unistd.h>
  9. #include "config.h"
  10. #ifdef HAVE_DL_ITERATE_PHDR
  11. #include <link.h>
  12. #endif
  13. #include "runtime.h"
  14. #include "arch.h"
  15. #include "defs.h"
  16. #include "malloc.h"
  17. #include "go-type.h"
  18. #include "go-defer.h"
  19. #ifdef USING_SPLIT_STACK
  20. /* FIXME: These are not declared anywhere. */
  21. extern void __splitstack_getcontext(void *context[10]);
  22. extern void __splitstack_setcontext(void *context[10]);
  23. extern void *__splitstack_makecontext(size_t, void *context[10], size_t *);
  24. extern void * __splitstack_resetcontext(void *context[10], size_t *);
  25. extern void *__splitstack_find(void *, void *, size_t *, void **, void **,
  26. void **);
  27. extern void __splitstack_block_signals (int *, int *);
  28. extern void __splitstack_block_signals_context (void *context[10], int *,
  29. int *);
  30. #endif
  31. #ifndef PTHREAD_STACK_MIN
  32. # define PTHREAD_STACK_MIN 8192
  33. #endif
  34. #if defined(USING_SPLIT_STACK) && defined(LINKER_SUPPORTS_SPLIT_STACK)
  35. # define StackMin PTHREAD_STACK_MIN
  36. #else
  37. # define StackMin ((sizeof(char *) < 8) ? 2 * 1024 * 1024 : 4 * 1024 * 1024)
  38. #endif
  39. uintptr runtime_stacks_sys;
  40. static void gtraceback(G*);
  41. #ifdef __rtems__
  42. #define __thread
  43. #endif
  44. static __thread G *g;
  45. static __thread M *m;
  46. #ifndef SETCONTEXT_CLOBBERS_TLS
  47. static inline void
  48. initcontext(void)
  49. {
  50. }
  51. static inline void
  52. fixcontext(ucontext_t *c __attribute__ ((unused)))
  53. {
  54. }
  55. #else
  56. # if defined(__x86_64__) && defined(__sun__)
  57. // x86_64 Solaris 10 and 11 have a bug: setcontext switches the %fs
  58. // register to that of the thread which called getcontext. The effect
  59. // is that the address of all __thread variables changes. This bug
  60. // also affects pthread_self() and pthread_getspecific. We work
  61. // around it by clobbering the context field directly to keep %fs the
  62. // same.
  63. static __thread greg_t fs;
  64. static inline void
  65. initcontext(void)
  66. {
  67. ucontext_t c;
  68. getcontext(&c);
  69. fs = c.uc_mcontext.gregs[REG_FSBASE];
  70. }
  71. static inline void
  72. fixcontext(ucontext_t* c)
  73. {
  74. c->uc_mcontext.gregs[REG_FSBASE] = fs;
  75. }
  76. # elif defined(__NetBSD__)
  77. // NetBSD has a bug: setcontext clobbers tlsbase, we need to save
  78. // and restore it ourselves.
  79. static __thread __greg_t tlsbase;
  80. static inline void
  81. initcontext(void)
  82. {
  83. ucontext_t c;
  84. getcontext(&c);
  85. tlsbase = c.uc_mcontext._mc_tlsbase;
  86. }
  87. static inline void
  88. fixcontext(ucontext_t* c)
  89. {
  90. c->uc_mcontext._mc_tlsbase = tlsbase;
  91. }
  92. # elif defined(__sparc__)
  93. static inline void
  94. initcontext(void)
  95. {
  96. }
  97. static inline void
  98. fixcontext(ucontext_t *c)
  99. {
  100. /* ??? Using
  101. register unsigned long thread __asm__("%g7");
  102. c->uc_mcontext.gregs[REG_G7] = thread;
  103. results in
  104. error: variable ‘thread’ might be clobbered by \
  105. ‘longjmp’ or ‘vfork’ [-Werror=clobbered]
  106. which ought to be false, as %g7 is a fixed register. */
  107. if (sizeof (c->uc_mcontext.gregs[REG_G7]) == 8)
  108. asm ("stx %%g7, %0" : "=m"(c->uc_mcontext.gregs[REG_G7]));
  109. else
  110. asm ("st %%g7, %0" : "=m"(c->uc_mcontext.gregs[REG_G7]));
  111. }
  112. # else
  113. # error unknown case for SETCONTEXT_CLOBBERS_TLS
  114. # endif
  115. #endif
  116. // We can not always refer to the TLS variables directly. The
  117. // compiler will call tls_get_addr to get the address of the variable,
  118. // and it may hold it in a register across a call to schedule. When
  119. // we get back from the call we may be running in a different thread,
  120. // in which case the register now points to the TLS variable for a
  121. // different thread. We use non-inlinable functions to avoid this
  122. // when necessary.
  123. G* runtime_g(void) __attribute__ ((noinline, no_split_stack));
  124. G*
  125. runtime_g(void)
  126. {
  127. return g;
  128. }
  129. M* runtime_m(void) __attribute__ ((noinline, no_split_stack));
  130. M*
  131. runtime_m(void)
  132. {
  133. return m;
  134. }
  135. // Set m and g.
  136. void
  137. runtime_setmg(M* mp, G* gp)
  138. {
  139. m = mp;
  140. g = gp;
  141. }
  142. // Start a new thread.
  143. static void
  144. runtime_newosproc(M *mp)
  145. {
  146. pthread_attr_t attr;
  147. sigset_t clear, old;
  148. pthread_t tid;
  149. int ret;
  150. if(pthread_attr_init(&attr) != 0)
  151. runtime_throw("pthread_attr_init");
  152. if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
  153. runtime_throw("pthread_attr_setdetachstate");
  154. // Block signals during pthread_create so that the new thread
  155. // starts with signals disabled. It will enable them in minit.
  156. sigfillset(&clear);
  157. #ifdef SIGTRAP
  158. // Blocking SIGTRAP reportedly breaks gdb on Alpha GNU/Linux.
  159. sigdelset(&clear, SIGTRAP);
  160. #endif
  161. sigemptyset(&old);
  162. pthread_sigmask(SIG_BLOCK, &clear, &old);
  163. ret = pthread_create(&tid, &attr, runtime_mstart, mp);
  164. pthread_sigmask(SIG_SETMASK, &old, nil);
  165. if (ret != 0)
  166. runtime_throw("pthread_create");
  167. }
  168. // First function run by a new goroutine. This replaces gogocall.
  169. static void
  170. kickoff(void)
  171. {
  172. void (*fn)(void*);
  173. if(g->traceback != nil)
  174. gtraceback(g);
  175. fn = (void (*)(void*))(g->entry);
  176. fn(g->param);
  177. runtime_goexit();
  178. }
  179. // Switch context to a different goroutine. This is like longjmp.
  180. void runtime_gogo(G*) __attribute__ ((noinline));
  181. void
  182. runtime_gogo(G* newg)
  183. {
  184. #ifdef USING_SPLIT_STACK
  185. __splitstack_setcontext(&newg->stack_context[0]);
  186. #endif
  187. g = newg;
  188. newg->fromgogo = true;
  189. fixcontext(&newg->context);
  190. setcontext(&newg->context);
  191. runtime_throw("gogo setcontext returned");
  192. }
  193. // Save context and call fn passing g as a parameter. This is like
  194. // setjmp. Because getcontext always returns 0, unlike setjmp, we use
  195. // g->fromgogo as a code. It will be true if we got here via
  196. // setcontext. g == nil the first time this is called in a new m.
  197. void runtime_mcall(void (*)(G*)) __attribute__ ((noinline));
  198. void
  199. runtime_mcall(void (*pfn)(G*))
  200. {
  201. M *mp;
  202. G *gp;
  203. // Ensure that all registers are on the stack for the garbage
  204. // collector.
  205. __builtin_unwind_init();
  206. mp = m;
  207. gp = g;
  208. if(gp == mp->g0)
  209. runtime_throw("runtime: mcall called on m->g0 stack");
  210. if(gp != nil) {
  211. #ifdef USING_SPLIT_STACK
  212. __splitstack_getcontext(&g->stack_context[0]);
  213. #else
  214. gp->gcnext_sp = &pfn;
  215. #endif
  216. gp->fromgogo = false;
  217. getcontext(&gp->context);
  218. // When we return from getcontext, we may be running
  219. // in a new thread. That means that m and g may have
  220. // changed. They are global variables so we will
  221. // reload them, but the addresses of m and g may be
  222. // cached in our local stack frame, and those
  223. // addresses may be wrong. Call functions to reload
  224. // the values for this thread.
  225. mp = runtime_m();
  226. gp = runtime_g();
  227. if(gp->traceback != nil)
  228. gtraceback(gp);
  229. }
  230. if (gp == nil || !gp->fromgogo) {
  231. #ifdef USING_SPLIT_STACK
  232. __splitstack_setcontext(&mp->g0->stack_context[0]);
  233. #endif
  234. mp->g0->entry = (byte*)pfn;
  235. mp->g0->param = gp;
  236. // It's OK to set g directly here because this case
  237. // can not occur if we got here via a setcontext to
  238. // the getcontext call just above.
  239. g = mp->g0;
  240. fixcontext(&mp->g0->context);
  241. setcontext(&mp->g0->context);
  242. runtime_throw("runtime: mcall function returned");
  243. }
  244. }
  245. // Goroutine scheduler
  246. // The scheduler's job is to distribute ready-to-run goroutines over worker threads.
  247. //
  248. // The main concepts are:
  249. // G - goroutine.
  250. // M - worker thread, or machine.
  251. // P - processor, a resource that is required to execute Go code.
  252. // M must have an associated P to execute Go code, however it can be
  253. // blocked or in a syscall w/o an associated P.
  254. //
  255. // Design doc at http://golang.org/s/go11sched.
  256. typedef struct Sched Sched;
  257. struct Sched {
  258. Lock;
  259. uint64 goidgen;
  260. M* midle; // idle m's waiting for work
  261. int32 nmidle; // number of idle m's waiting for work
  262. int32 nmidlelocked; // number of locked m's waiting for work
  263. int32 mcount; // number of m's that have been created
  264. int32 maxmcount; // maximum number of m's allowed (or die)
  265. P* pidle; // idle P's
  266. uint32 npidle;
  267. uint32 nmspinning;
  268. // Global runnable queue.
  269. G* runqhead;
  270. G* runqtail;
  271. int32 runqsize;
  272. // Global cache of dead G's.
  273. Lock gflock;
  274. G* gfree;
  275. uint32 gcwaiting; // gc is waiting to run
  276. int32 stopwait;
  277. Note stopnote;
  278. uint32 sysmonwait;
  279. Note sysmonnote;
  280. uint64 lastpoll;
  281. int32 profilehz; // cpu profiling rate
  282. };
  283. enum
  284. {
  285. // The max value of GOMAXPROCS.
  286. // There are no fundamental restrictions on the value.
  287. MaxGomaxprocs = 1<<8,
  288. // Number of goroutine ids to grab from runtime_sched.goidgen to local per-P cache at once.
  289. // 16 seems to provide enough amortization, but other than that it's mostly arbitrary number.
  290. GoidCacheBatch = 16,
  291. };
  292. Sched runtime_sched;
  293. int32 runtime_gomaxprocs;
  294. uint32 runtime_needextram = 1;
  295. M runtime_m0;
  296. G runtime_g0; // idle goroutine for m0
  297. G* runtime_lastg;
  298. M* runtime_allm;
  299. P** runtime_allp;
  300. M* runtime_extram;
  301. int8* runtime_goos;
  302. int32 runtime_ncpu;
  303. bool runtime_precisestack;
  304. static int32 newprocs;
  305. static Lock allglock; // the following vars are protected by this lock or by stoptheworld
  306. G** runtime_allg;
  307. uintptr runtime_allglen;
  308. static uintptr allgcap;
  309. bool runtime_isarchive;
  310. void* runtime_mstart(void*);
  311. static void runqput(P*, G*);
  312. static G* runqget(P*);
  313. static bool runqputslow(P*, G*, uint32, uint32);
  314. static G* runqsteal(P*, P*);
  315. static void mput(M*);
  316. static M* mget(void);
  317. static void mcommoninit(M*);
  318. static void schedule(void);
  319. static void procresize(int32);
  320. static void acquirep(P*);
  321. static P* releasep(void);
  322. static void newm(void(*)(void), P*);
  323. static void stopm(void);
  324. static void startm(P*, bool);
  325. static void handoffp(P*);
  326. static void wakep(void);
  327. static void stoplockedm(void);
  328. static void startlockedm(G*);
  329. static void sysmon(void);
  330. static uint32 retake(int64);
  331. static void incidlelocked(int32);
  332. static void checkdead(void);
  333. static void exitsyscall0(G*);
  334. static void park0(G*);
  335. static void goexit0(G*);
  336. static void gfput(P*, G*);
  337. static G* gfget(P*);
  338. static void gfpurge(P*);
  339. static void globrunqput(G*);
  340. static void globrunqputbatch(G*, G*, int32);
  341. static G* globrunqget(P*, int32);
  342. static P* pidleget(void);
  343. static void pidleput(P*);
  344. static void injectglist(G*);
  345. static bool preemptall(void);
  346. static bool exitsyscallfast(void);
  347. static void allgadd(G*);
  348. bool runtime_isstarted;
  349. // The bootstrap sequence is:
  350. //
  351. // call osinit
  352. // call schedinit
  353. // make & queue new G
  354. // call runtime_mstart
  355. //
  356. // The new G calls runtime_main.
  357. void
  358. runtime_schedinit(void)
  359. {
  360. int32 n, procs;
  361. const byte *p;
  362. Eface i;
  363. m = &runtime_m0;
  364. g = &runtime_g0;
  365. m->g0 = g;
  366. m->curg = g;
  367. g->m = m;
  368. initcontext();
  369. runtime_sched.maxmcount = 10000;
  370. runtime_precisestack = 0;
  371. // runtime_symtabinit();
  372. runtime_mallocinit();
  373. mcommoninit(m);
  374. // Initialize the itable value for newErrorCString,
  375. // so that the next time it gets called, possibly
  376. // in a fault during a garbage collection, it will not
  377. // need to allocated memory.
  378. runtime_newErrorCString(0, &i);
  379. // Initialize the cached gotraceback value, since
  380. // gotraceback calls getenv, which mallocs on Plan 9.
  381. runtime_gotraceback(nil);
  382. runtime_goargs();
  383. runtime_goenvs();
  384. runtime_parsedebugvars();
  385. runtime_sched.lastpoll = runtime_nanotime();
  386. procs = 1;
  387. p = runtime_getenv("GOMAXPROCS");
  388. if(p != nil && (n = runtime_atoi(p)) > 0) {
  389. if(n > MaxGomaxprocs)
  390. n = MaxGomaxprocs;
  391. procs = n;
  392. }
  393. runtime_allp = runtime_malloc((MaxGomaxprocs+1)*sizeof(runtime_allp[0]));
  394. procresize(procs);
  395. // Can not enable GC until all roots are registered.
  396. // mstats.enablegc = 1;
  397. }
  398. extern void main_init(void) __asm__ (GOSYM_PREFIX "__go_init_main");
  399. extern void main_main(void) __asm__ (GOSYM_PREFIX "main.main");
  400. // Used to determine the field alignment.
  401. struct field_align
  402. {
  403. char c;
  404. Hchan *p;
  405. };
  406. // main_init_done is a signal used by cgocallbackg that initialization
  407. // has been completed. It is made before _cgo_notify_runtime_init_done,
  408. // so all cgo calls can rely on it existing. When main_init is
  409. // complete, it is closed, meaning cgocallbackg can reliably receive
  410. // from it.
  411. Hchan *runtime_main_init_done;
  412. // The chan bool type, for runtime_main_init_done.
  413. extern const struct __go_type_descriptor bool_type_descriptor
  414. __asm__ (GOSYM_PREFIX "__go_tdn_bool");
  415. static struct __go_channel_type chan_bool_type_descriptor =
  416. {
  417. /* __common */
  418. {
  419. /* __code */
  420. GO_CHAN,
  421. /* __align */
  422. __alignof (Hchan *),
  423. /* __field_align */
  424. offsetof (struct field_align, p) - 1,
  425. /* __size */
  426. sizeof (Hchan *),
  427. /* __hash */
  428. 0, /* This value doesn't matter. */
  429. /* __hashfn */
  430. __go_type_hash_error,
  431. /* __equalfn */
  432. __go_type_equal_error,
  433. /* __gc */
  434. NULL, /* This value doesn't matter */
  435. /* __reflection */
  436. NULL, /* This value doesn't matter */
  437. /* __uncommon */
  438. NULL,
  439. /* __pointer_to_this */
  440. NULL,
  441. /* __zero */
  442. NULL /* This value doesn't matter */
  443. },
  444. /* __element_type */
  445. &bool_type_descriptor,
  446. /* __dir */
  447. CHANNEL_BOTH_DIR
  448. };
  449. extern Hchan *__go_new_channel (ChanType *, uintptr);
  450. extern void closechan(Hchan *) __asm__ (GOSYM_PREFIX "runtime.closechan");
  451. static void
  452. initDone(void *arg __attribute__ ((unused))) {
  453. runtime_unlockOSThread();
  454. };
  455. // The main goroutine.
  456. // Note: C frames in general are not copyable during stack growth, for two reasons:
  457. // 1) We don't know where in a frame to find pointers to other stack locations.
  458. // 2) There's no guarantee that globals or heap values do not point into the frame.
  459. //
  460. // The C frame for runtime.main is copyable, because:
  461. // 1) There are no pointers to other stack locations in the frame
  462. // (d.fn points at a global, d.link is nil, d.argp is -1).
  463. // 2) The only pointer into this frame is from the defer chain,
  464. // which is explicitly handled during stack copying.
  465. void
  466. runtime_main(void* dummy __attribute__((unused)))
  467. {
  468. Defer d;
  469. _Bool frame;
  470. newm(sysmon, nil);
  471. // Lock the main goroutine onto this, the main OS thread,
  472. // during initialization. Most programs won't care, but a few
  473. // do require certain calls to be made by the main thread.
  474. // Those can arrange for main.main to run in the main thread
  475. // by calling runtime.LockOSThread during initialization
  476. // to preserve the lock.
  477. runtime_lockOSThread();
  478. // Defer unlock so that runtime.Goexit during init does the unlock too.
  479. d.__pfn = initDone;
  480. d.__next = g->defer;
  481. d.__arg = (void*)-1;
  482. d.__panic = g->panic;
  483. d.__retaddr = nil;
  484. d.__makefunc_can_recover = 0;
  485. d.__frame = &frame;
  486. d.__special = true;
  487. g->defer = &d;
  488. if(m != &runtime_m0)
  489. runtime_throw("runtime_main not on m0");
  490. __go_go(runtime_MHeap_Scavenger, nil);
  491. runtime_main_init_done = __go_new_channel(&chan_bool_type_descriptor, 0);
  492. _cgo_notify_runtime_init_done();
  493. main_init();
  494. closechan(runtime_main_init_done);
  495. if(g->defer != &d || d.__pfn != initDone)
  496. runtime_throw("runtime: bad defer entry after init");
  497. g->defer = d.__next;
  498. runtime_unlockOSThread();
  499. // For gccgo we have to wait until after main is initialized
  500. // to enable GC, because initializing main registers the GC
  501. // roots.
  502. mstats.enablegc = 1;
  503. if(runtime_isarchive) {
  504. // This is not a complete program, but is instead a
  505. // library built using -buildmode=c-archive or
  506. // c-shared. Now that we are initialized, there is
  507. // nothing further to do.
  508. return;
  509. }
  510. main_main();
  511. // Make racy client program work: if panicking on
  512. // another goroutine at the same time as main returns,
  513. // let the other goroutine finish printing the panic trace.
  514. // Once it does, it will exit. See issue 3934.
  515. if(runtime_panicking)
  516. runtime_park(nil, nil, "panicwait");
  517. runtime_exit(0);
  518. for(;;)
  519. *(int32*)0 = 0;
  520. }
  521. void
  522. runtime_goroutineheader(G *gp)
  523. {
  524. const char *status;
  525. int64 waitfor;
  526. switch(gp->status) {
  527. case Gidle:
  528. status = "idle";
  529. break;
  530. case Grunnable:
  531. status = "runnable";
  532. break;
  533. case Grunning:
  534. status = "running";
  535. break;
  536. case Gsyscall:
  537. status = "syscall";
  538. break;
  539. case Gwaiting:
  540. if(gp->waitreason)
  541. status = gp->waitreason;
  542. else
  543. status = "waiting";
  544. break;
  545. default:
  546. status = "???";
  547. break;
  548. }
  549. // approx time the G is blocked, in minutes
  550. waitfor = 0;
  551. if((gp->status == Gwaiting || gp->status == Gsyscall) && gp->waitsince != 0)
  552. waitfor = (runtime_nanotime() - gp->waitsince) / (60LL*1000*1000*1000);
  553. if(waitfor < 1)
  554. runtime_printf("goroutine %D [%s]:\n", gp->goid, status);
  555. else
  556. runtime_printf("goroutine %D [%s, %D minutes]:\n", gp->goid, status, waitfor);
  557. }
  558. void
  559. runtime_printcreatedby(G *g)
  560. {
  561. if(g != nil && g->gopc != 0 && g->goid != 1) {
  562. String fn;
  563. String file;
  564. intgo line;
  565. if(__go_file_line(g->gopc - 1, &fn, &file, &line)) {
  566. runtime_printf("created by %S\n", fn);
  567. runtime_printf("\t%S:%D\n", file, (int64) line);
  568. }
  569. }
  570. }
  571. struct Traceback
  572. {
  573. G* gp;
  574. Location locbuf[TracebackMaxFrames];
  575. int32 c;
  576. };
  577. void
  578. runtime_tracebackothers(G * volatile me)
  579. {
  580. G * volatile gp;
  581. Traceback tb;
  582. int32 traceback;
  583. volatile uintptr i;
  584. tb.gp = me;
  585. traceback = runtime_gotraceback(nil);
  586. // Show the current goroutine first, if we haven't already.
  587. if((gp = m->curg) != nil && gp != me) {
  588. runtime_printf("\n");
  589. runtime_goroutineheader(gp);
  590. gp->traceback = &tb;
  591. #ifdef USING_SPLIT_STACK
  592. __splitstack_getcontext(&me->stack_context[0]);
  593. #endif
  594. getcontext(&me->context);
  595. if(gp->traceback != nil) {
  596. runtime_gogo(gp);
  597. }
  598. runtime_printtrace(tb.locbuf, tb.c, false);
  599. runtime_printcreatedby(gp);
  600. }
  601. runtime_lock(&allglock);
  602. for(i = 0; i < runtime_allglen; i++) {
  603. gp = runtime_allg[i];
  604. if(gp == me || gp == m->curg || gp->status == Gdead)
  605. continue;
  606. if(gp->issystem && traceback < 2)
  607. continue;
  608. runtime_printf("\n");
  609. runtime_goroutineheader(gp);
  610. // Our only mechanism for doing a stack trace is
  611. // _Unwind_Backtrace. And that only works for the
  612. // current thread, not for other random goroutines.
  613. // So we need to switch context to the goroutine, get
  614. // the backtrace, and then switch back.
  615. // This means that if g is running or in a syscall, we
  616. // can't reliably print a stack trace. FIXME.
  617. if(gp->status == Grunning) {
  618. runtime_printf("\tgoroutine running on other thread; stack unavailable\n");
  619. runtime_printcreatedby(gp);
  620. } else if(gp->status == Gsyscall) {
  621. runtime_printf("\tgoroutine in C code; stack unavailable\n");
  622. runtime_printcreatedby(gp);
  623. } else {
  624. gp->traceback = &tb;
  625. #ifdef USING_SPLIT_STACK
  626. __splitstack_getcontext(&me->stack_context[0]);
  627. #endif
  628. getcontext(&me->context);
  629. if(gp->traceback != nil) {
  630. runtime_gogo(gp);
  631. }
  632. runtime_printtrace(tb.locbuf, tb.c, false);
  633. runtime_printcreatedby(gp);
  634. }
  635. }
  636. runtime_unlock(&allglock);
  637. }
  638. static void
  639. checkmcount(void)
  640. {
  641. // sched lock is held
  642. if(runtime_sched.mcount > runtime_sched.maxmcount) {
  643. runtime_printf("runtime: program exceeds %d-thread limit\n", runtime_sched.maxmcount);
  644. runtime_throw("thread exhaustion");
  645. }
  646. }
  647. // Do a stack trace of gp, and then restore the context to
  648. // gp->dotraceback.
  649. static void
  650. gtraceback(G* gp)
  651. {
  652. Traceback* traceback;
  653. traceback = gp->traceback;
  654. gp->traceback = nil;
  655. traceback->c = runtime_callers(1, traceback->locbuf,
  656. sizeof traceback->locbuf / sizeof traceback->locbuf[0], false);
  657. runtime_gogo(traceback->gp);
  658. }
  659. static void
  660. mcommoninit(M *mp)
  661. {
  662. // If there is no mcache runtime_callers() will crash,
  663. // and we are most likely in sysmon thread so the stack is senseless anyway.
  664. if(m->mcache)
  665. runtime_callers(1, mp->createstack, nelem(mp->createstack), false);
  666. mp->fastrand = 0x49f6428aUL + mp->id + runtime_cputicks();
  667. runtime_lock(&runtime_sched);
  668. mp->id = runtime_sched.mcount++;
  669. checkmcount();
  670. runtime_mpreinit(mp);
  671. // Add to runtime_allm so garbage collector doesn't free m
  672. // when it is just in a register or thread-local storage.
  673. mp->alllink = runtime_allm;
  674. // runtime_NumCgoCall() iterates over allm w/o schedlock,
  675. // so we need to publish it safely.
  676. runtime_atomicstorep(&runtime_allm, mp);
  677. runtime_unlock(&runtime_sched);
  678. }
  679. // Mark gp ready to run.
  680. void
  681. runtime_ready(G *gp)
  682. {
  683. // Mark runnable.
  684. m->locks++; // disable preemption because it can be holding p in a local var
  685. if(gp->status != Gwaiting) {
  686. runtime_printf("goroutine %D has status %d\n", gp->goid, gp->status);
  687. runtime_throw("bad g->status in ready");
  688. }
  689. gp->status = Grunnable;
  690. runqput(m->p, gp);
  691. if(runtime_atomicload(&runtime_sched.npidle) != 0 && runtime_atomicload(&runtime_sched.nmspinning) == 0) // TODO: fast atomic
  692. wakep();
  693. m->locks--;
  694. }
  695. int32
  696. runtime_gcprocs(void)
  697. {
  698. int32 n;
  699. // Figure out how many CPUs to use during GC.
  700. // Limited by gomaxprocs, number of actual CPUs, and MaxGcproc.
  701. runtime_lock(&runtime_sched);
  702. n = runtime_gomaxprocs;
  703. if(n > runtime_ncpu)
  704. n = runtime_ncpu > 0 ? runtime_ncpu : 1;
  705. if(n > MaxGcproc)
  706. n = MaxGcproc;
  707. if(n > runtime_sched.nmidle+1) // one M is currently running
  708. n = runtime_sched.nmidle+1;
  709. runtime_unlock(&runtime_sched);
  710. return n;
  711. }
  712. static bool
  713. needaddgcproc(void)
  714. {
  715. int32 n;
  716. runtime_lock(&runtime_sched);
  717. n = runtime_gomaxprocs;
  718. if(n > runtime_ncpu)
  719. n = runtime_ncpu;
  720. if(n > MaxGcproc)
  721. n = MaxGcproc;
  722. n -= runtime_sched.nmidle+1; // one M is currently running
  723. runtime_unlock(&runtime_sched);
  724. return n > 0;
  725. }
  726. void
  727. runtime_helpgc(int32 nproc)
  728. {
  729. M *mp;
  730. int32 n, pos;
  731. runtime_lock(&runtime_sched);
  732. pos = 0;
  733. for(n = 1; n < nproc; n++) { // one M is currently running
  734. if(runtime_allp[pos]->mcache == m->mcache)
  735. pos++;
  736. mp = mget();
  737. if(mp == nil)
  738. runtime_throw("runtime_gcprocs inconsistency");
  739. mp->helpgc = n;
  740. mp->mcache = runtime_allp[pos]->mcache;
  741. pos++;
  742. runtime_notewakeup(&mp->park);
  743. }
  744. runtime_unlock(&runtime_sched);
  745. }
  746. // Similar to stoptheworld but best-effort and can be called several times.
  747. // There is no reverse operation, used during crashing.
  748. // This function must not lock any mutexes.
  749. void
  750. runtime_freezetheworld(void)
  751. {
  752. int32 i;
  753. if(runtime_gomaxprocs == 1)
  754. return;
  755. // stopwait and preemption requests can be lost
  756. // due to races with concurrently executing threads,
  757. // so try several times
  758. for(i = 0; i < 5; i++) {
  759. // this should tell the scheduler to not start any new goroutines
  760. runtime_sched.stopwait = 0x7fffffff;
  761. runtime_atomicstore((uint32*)&runtime_sched.gcwaiting, 1);
  762. // this should stop running goroutines
  763. if(!preemptall())
  764. break; // no running goroutines
  765. runtime_usleep(1000);
  766. }
  767. // to be sure
  768. runtime_usleep(1000);
  769. preemptall();
  770. runtime_usleep(1000);
  771. }
  772. void
  773. runtime_stoptheworld(void)
  774. {
  775. int32 i;
  776. uint32 s;
  777. P *p;
  778. bool wait;
  779. runtime_lock(&runtime_sched);
  780. runtime_sched.stopwait = runtime_gomaxprocs;
  781. runtime_atomicstore((uint32*)&runtime_sched.gcwaiting, 1);
  782. preemptall();
  783. // stop current P
  784. m->p->status = Pgcstop;
  785. runtime_sched.stopwait--;
  786. // try to retake all P's in Psyscall status
  787. for(i = 0; i < runtime_gomaxprocs; i++) {
  788. p = runtime_allp[i];
  789. s = p->status;
  790. if(s == Psyscall && runtime_cas(&p->status, s, Pgcstop))
  791. runtime_sched.stopwait--;
  792. }
  793. // stop idle P's
  794. while((p = pidleget()) != nil) {
  795. p->status = Pgcstop;
  796. runtime_sched.stopwait--;
  797. }
  798. wait = runtime_sched.stopwait > 0;
  799. runtime_unlock(&runtime_sched);
  800. // wait for remaining P's to stop voluntarily
  801. if(wait) {
  802. runtime_notesleep(&runtime_sched.stopnote);
  803. runtime_noteclear(&runtime_sched.stopnote);
  804. }
  805. if(runtime_sched.stopwait)
  806. runtime_throw("stoptheworld: not stopped");
  807. for(i = 0; i < runtime_gomaxprocs; i++) {
  808. p = runtime_allp[i];
  809. if(p->status != Pgcstop)
  810. runtime_throw("stoptheworld: not stopped");
  811. }
  812. }
  813. static void
  814. mhelpgc(void)
  815. {
  816. m->helpgc = -1;
  817. }
  818. void
  819. runtime_starttheworld(void)
  820. {
  821. P *p, *p1;
  822. M *mp;
  823. G *gp;
  824. bool add;
  825. m->locks++; // disable preemption because it can be holding p in a local var
  826. gp = runtime_netpoll(false); // non-blocking
  827. injectglist(gp);
  828. add = needaddgcproc();
  829. runtime_lock(&runtime_sched);
  830. if(newprocs) {
  831. procresize(newprocs);
  832. newprocs = 0;
  833. } else
  834. procresize(runtime_gomaxprocs);
  835. runtime_sched.gcwaiting = 0;
  836. p1 = nil;
  837. while((p = pidleget()) != nil) {
  838. // procresize() puts p's with work at the beginning of the list.
  839. // Once we reach a p without a run queue, the rest don't have one either.
  840. if(p->runqhead == p->runqtail) {
  841. pidleput(p);
  842. break;
  843. }
  844. p->m = mget();
  845. p->link = p1;
  846. p1 = p;
  847. }
  848. if(runtime_sched.sysmonwait) {
  849. runtime_sched.sysmonwait = false;
  850. runtime_notewakeup(&runtime_sched.sysmonnote);
  851. }
  852. runtime_unlock(&runtime_sched);
  853. while(p1) {
  854. p = p1;
  855. p1 = p1->link;
  856. if(p->m) {
  857. mp = p->m;
  858. p->m = nil;
  859. if(mp->nextp)
  860. runtime_throw("starttheworld: inconsistent mp->nextp");
  861. mp->nextp = p;
  862. runtime_notewakeup(&mp->park);
  863. } else {
  864. // Start M to run P. Do not start another M below.
  865. newm(nil, p);
  866. add = false;
  867. }
  868. }
  869. if(add) {
  870. // If GC could have used another helper proc, start one now,
  871. // in the hope that it will be available next time.
  872. // It would have been even better to start it before the collection,
  873. // but doing so requires allocating memory, so it's tricky to
  874. // coordinate. This lazy approach works out in practice:
  875. // we don't mind if the first couple gc rounds don't have quite
  876. // the maximum number of procs.
  877. newm(mhelpgc, nil);
  878. }
  879. m->locks--;
  880. }
  881. // Called to start an M.
  882. void*
  883. runtime_mstart(void* mp)
  884. {
  885. m = (M*)mp;
  886. g = m->g0;
  887. initcontext();
  888. g->entry = nil;
  889. g->param = nil;
  890. // Record top of stack for use by mcall.
  891. // Once we call schedule we're never coming back,
  892. // so other calls can reuse this stack space.
  893. #ifdef USING_SPLIT_STACK
  894. __splitstack_getcontext(&g->stack_context[0]);
  895. #else
  896. g->gcinitial_sp = &mp;
  897. // Setting gcstack_size to 0 is a marker meaning that gcinitial_sp
  898. // is the top of the stack, not the bottom.
  899. g->gcstack_size = 0;
  900. g->gcnext_sp = &mp;
  901. #endif
  902. getcontext(&g->context);
  903. if(g->entry != nil) {
  904. // Got here from mcall.
  905. void (*pfn)(G*) = (void (*)(G*))g->entry;
  906. G* gp = (G*)g->param;
  907. pfn(gp);
  908. *(int*)0x21 = 0x21;
  909. }
  910. runtime_minit();
  911. #ifdef USING_SPLIT_STACK
  912. {
  913. int dont_block_signals = 0;
  914. __splitstack_block_signals(&dont_block_signals, nil);
  915. }
  916. #endif
  917. // Install signal handlers; after minit so that minit can
  918. // prepare the thread to be able to handle the signals.
  919. if(m == &runtime_m0) {
  920. if(runtime_iscgo && !runtime_cgoHasExtraM) {
  921. runtime_cgoHasExtraM = true;
  922. runtime_newextram();
  923. runtime_needextram = 0;
  924. }
  925. runtime_initsig();
  926. }
  927. if(m->mstartfn)
  928. m->mstartfn();
  929. if(m->helpgc) {
  930. m->helpgc = 0;
  931. stopm();
  932. } else if(m != &runtime_m0) {
  933. acquirep(m->nextp);
  934. m->nextp = nil;
  935. }
  936. schedule();
  937. // TODO(brainman): This point is never reached, because scheduler
  938. // does not release os threads at the moment. But once this path
  939. // is enabled, we must remove our seh here.
  940. return nil;
  941. }
  942. typedef struct CgoThreadStart CgoThreadStart;
  943. struct CgoThreadStart
  944. {
  945. M *m;
  946. G *g;
  947. uintptr *tls;
  948. void (*fn)(void);
  949. };
  950. // Allocate a new m unassociated with any thread.
  951. // Can use p for allocation context if needed.
  952. M*
  953. runtime_allocm(P *p, int32 stacksize, byte** ret_g0_stack, size_t* ret_g0_stacksize)
  954. {
  955. M *mp;
  956. m->locks++; // disable GC because it can be called from sysmon
  957. if(m->p == nil)
  958. acquirep(p); // temporarily borrow p for mallocs in this function
  959. #if 0
  960. if(mtype == nil) {
  961. Eface e;
  962. runtime_gc_m_ptr(&e);
  963. mtype = ((const PtrType*)e.__type_descriptor)->__element_type;
  964. }
  965. #endif
  966. mp = runtime_mal(sizeof *mp);
  967. mcommoninit(mp);
  968. mp->g0 = runtime_malg(stacksize, ret_g0_stack, ret_g0_stacksize);
  969. if(p == m->p)
  970. releasep();
  971. m->locks--;
  972. return mp;
  973. }
  974. static G*
  975. allocg(void)
  976. {
  977. G *gp;
  978. // static Type *gtype;
  979. // if(gtype == nil) {
  980. // Eface e;
  981. // runtime_gc_g_ptr(&e);
  982. // gtype = ((PtrType*)e.__type_descriptor)->__element_type;
  983. // }
  984. // gp = runtime_cnew(gtype);
  985. gp = runtime_malloc(sizeof(G));
  986. return gp;
  987. }
  988. static M* lockextra(bool nilokay);
  989. static void unlockextra(M*);
  990. // needm is called when a cgo callback happens on a
  991. // thread without an m (a thread not created by Go).
  992. // In this case, needm is expected to find an m to use
  993. // and return with m, g initialized correctly.
  994. // Since m and g are not set now (likely nil, but see below)
  995. // needm is limited in what routines it can call. In particular
  996. // it can only call nosplit functions (textflag 7) and cannot
  997. // do any scheduling that requires an m.
  998. //
  999. // In order to avoid needing heavy lifting here, we adopt
  1000. // the following strategy: there is a stack of available m's
  1001. // that can be stolen. Using compare-and-swap
  1002. // to pop from the stack has ABA races, so we simulate
  1003. // a lock by doing an exchange (via casp) to steal the stack
  1004. // head and replace the top pointer with MLOCKED (1).
  1005. // This serves as a simple spin lock that we can use even
  1006. // without an m. The thread that locks the stack in this way
  1007. // unlocks the stack by storing a valid stack head pointer.
  1008. //
  1009. // In order to make sure that there is always an m structure
  1010. // available to be stolen, we maintain the invariant that there
  1011. // is always one more than needed. At the beginning of the
  1012. // program (if cgo is in use) the list is seeded with a single m.
  1013. // If needm finds that it has taken the last m off the list, its job
  1014. // is - once it has installed its own m so that it can do things like
  1015. // allocate memory - to create a spare m and put it on the list.
  1016. //
  1017. // Each of these extra m's also has a g0 and a curg that are
  1018. // pressed into service as the scheduling stack and current
  1019. // goroutine for the duration of the cgo callback.
  1020. //
  1021. // When the callback is done with the m, it calls dropm to
  1022. // put the m back on the list.
  1023. //
  1024. // Unlike the gc toolchain, we start running on curg, since we are
  1025. // just going to return and let the caller continue.
  1026. void
  1027. runtime_needm(void)
  1028. {
  1029. M *mp;
  1030. if(runtime_needextram) {
  1031. // Can happen if C/C++ code calls Go from a global ctor.
  1032. // Can not throw, because scheduler is not initialized yet.
  1033. int rv __attribute__((unused));
  1034. rv = runtime_write(2, "fatal error: cgo callback before cgo call\n",
  1035. sizeof("fatal error: cgo callback before cgo call\n")-1);
  1036. runtime_exit(1);
  1037. }
  1038. // Lock extra list, take head, unlock popped list.
  1039. // nilokay=false is safe here because of the invariant above,
  1040. // that the extra list always contains or will soon contain
  1041. // at least one m.
  1042. mp = lockextra(false);
  1043. // Set needextram when we've just emptied the list,
  1044. // so that the eventual call into cgocallbackg will
  1045. // allocate a new m for the extra list. We delay the
  1046. // allocation until then so that it can be done
  1047. // after exitsyscall makes sure it is okay to be
  1048. // running at all (that is, there's no garbage collection
  1049. // running right now).
  1050. mp->needextram = mp->schedlink == nil;
  1051. unlockextra(mp->schedlink);
  1052. // Install m and g (= m->curg).
  1053. runtime_setmg(mp, mp->curg);
  1054. // Initialize g's context as in mstart.
  1055. initcontext();
  1056. g->status = Gsyscall;
  1057. g->entry = nil;
  1058. g->param = nil;
  1059. #ifdef USING_SPLIT_STACK
  1060. __splitstack_getcontext(&g->stack_context[0]);
  1061. #else
  1062. g->gcinitial_sp = &mp;
  1063. g->gcstack = nil;
  1064. g->gcstack_size = 0;
  1065. g->gcnext_sp = &mp;
  1066. #endif
  1067. getcontext(&g->context);
  1068. if(g->entry != nil) {
  1069. // Got here from mcall.
  1070. void (*pfn)(G*) = (void (*)(G*))g->entry;
  1071. G* gp = (G*)g->param;
  1072. pfn(gp);
  1073. *(int*)0x22 = 0x22;
  1074. }
  1075. // Initialize this thread to use the m.
  1076. runtime_minit();
  1077. #ifdef USING_SPLIT_STACK
  1078. {
  1079. int dont_block_signals = 0;
  1080. __splitstack_block_signals(&dont_block_signals, nil);
  1081. }
  1082. #endif
  1083. }
  1084. // newextram allocates an m and puts it on the extra list.
  1085. // It is called with a working local m, so that it can do things
  1086. // like call schedlock and allocate.
  1087. void
  1088. runtime_newextram(void)
  1089. {
  1090. M *mp, *mnext;
  1091. G *gp;
  1092. byte *g0_sp, *sp;
  1093. size_t g0_spsize, spsize;
  1094. // Create extra goroutine locked to extra m.
  1095. // The goroutine is the context in which the cgo callback will run.
  1096. // The sched.pc will never be returned to, but setting it to
  1097. // runtime.goexit makes clear to the traceback routines where
  1098. // the goroutine stack ends.
  1099. mp = runtime_allocm(nil, StackMin, &g0_sp, &g0_spsize);
  1100. gp = runtime_malg(StackMin, &sp, &spsize);
  1101. gp->status = Gdead;
  1102. mp->curg = gp;
  1103. mp->locked = LockInternal;
  1104. mp->lockedg = gp;
  1105. gp->lockedm = mp;
  1106. gp->goid = runtime_xadd64(&runtime_sched.goidgen, 1);
  1107. // put on allg for garbage collector
  1108. allgadd(gp);
  1109. // The context for gp will be set up in runtime_needm. But
  1110. // here we need to set up the context for g0.
  1111. getcontext(&mp->g0->context);
  1112. mp->g0->context.uc_stack.ss_sp = g0_sp;
  1113. mp->g0->context.uc_stack.ss_size = g0_spsize;
  1114. makecontext(&mp->g0->context, kickoff, 0);
  1115. // Add m to the extra list.
  1116. mnext = lockextra(true);
  1117. mp->schedlink = mnext;
  1118. unlockextra(mp);
  1119. }
  1120. // dropm is called when a cgo callback has called needm but is now
  1121. // done with the callback and returning back into the non-Go thread.
  1122. // It puts the current m back onto the extra list.
  1123. //
  1124. // The main expense here is the call to signalstack to release the
  1125. // m's signal stack, and then the call to needm on the next callback
  1126. // from this thread. It is tempting to try to save the m for next time,
  1127. // which would eliminate both these costs, but there might not be
  1128. // a next time: the current thread (which Go does not control) might exit.
  1129. // If we saved the m for that thread, there would be an m leak each time
  1130. // such a thread exited. Instead, we acquire and release an m on each
  1131. // call. These should typically not be scheduling operations, just a few
  1132. // atomics, so the cost should be small.
  1133. //
  1134. // TODO(rsc): An alternative would be to allocate a dummy pthread per-thread
  1135. // variable using pthread_key_create. Unlike the pthread keys we already use
  1136. // on OS X, this dummy key would never be read by Go code. It would exist
  1137. // only so that we could register at thread-exit-time destructor.
  1138. // That destructor would put the m back onto the extra list.
  1139. // This is purely a performance optimization. The current version,
  1140. // in which dropm happens on each cgo call, is still correct too.
  1141. // We may have to keep the current version on systems with cgo
  1142. // but without pthreads, like Windows.
  1143. void
  1144. runtime_dropm(void)
  1145. {
  1146. M *mp, *mnext;
  1147. // Undo whatever initialization minit did during needm.
  1148. runtime_unminit();
  1149. // Clear m and g, and return m to the extra list.
  1150. // After the call to setmg we can only call nosplit functions.
  1151. mp = m;
  1152. runtime_setmg(nil, nil);
  1153. mp->curg->status = Gdead;
  1154. mp->curg->gcstack = nil;
  1155. mp->curg->gcnext_sp = nil;
  1156. mnext = lockextra(true);
  1157. mp->schedlink = mnext;
  1158. unlockextra(mp);
  1159. }
  1160. #define MLOCKED ((M*)1)
  1161. // lockextra locks the extra list and returns the list head.
  1162. // The caller must unlock the list by storing a new list head
  1163. // to runtime.extram. If nilokay is true, then lockextra will
  1164. // return a nil list head if that's what it finds. If nilokay is false,
  1165. // lockextra will keep waiting until the list head is no longer nil.
  1166. static M*
  1167. lockextra(bool nilokay)
  1168. {
  1169. M *mp;
  1170. void (*yield)(void);
  1171. for(;;) {
  1172. mp = runtime_atomicloadp(&runtime_extram);
  1173. if(mp == MLOCKED) {
  1174. yield = runtime_osyield;
  1175. yield();
  1176. continue;
  1177. }
  1178. if(mp == nil && !nilokay) {
  1179. runtime_usleep(1);
  1180. continue;
  1181. }
  1182. if(!runtime_casp(&runtime_extram, mp, MLOCKED)) {
  1183. yield = runtime_osyield;
  1184. yield();
  1185. continue;
  1186. }
  1187. break;
  1188. }
  1189. return mp;
  1190. }
  1191. static void
  1192. unlockextra(M *mp)
  1193. {
  1194. runtime_atomicstorep(&runtime_extram, mp);
  1195. }
  1196. static int32
  1197. countextra()
  1198. {
  1199. M *mp, *mc;
  1200. int32 c;
  1201. for(;;) {
  1202. mp = runtime_atomicloadp(&runtime_extram);
  1203. if(mp == MLOCKED) {
  1204. runtime_osyield();
  1205. continue;
  1206. }
  1207. if(!runtime_casp(&runtime_extram, mp, MLOCKED)) {
  1208. runtime_osyield();
  1209. continue;
  1210. }
  1211. c = 0;
  1212. for(mc = mp; mc != nil; mc = mc->schedlink)
  1213. c++;
  1214. runtime_atomicstorep(&runtime_extram, mp);
  1215. return c;
  1216. }
  1217. }
  1218. // Create a new m. It will start off with a call to fn, or else the scheduler.
  1219. static void
  1220. newm(void(*fn)(void), P *p)
  1221. {
  1222. M *mp;
  1223. mp = runtime_allocm(p, -1, nil, nil);
  1224. mp->nextp = p;
  1225. mp->mstartfn = fn;
  1226. runtime_newosproc(mp);
  1227. }
  1228. // Stops execution of the current m until new work is available.
  1229. // Returns with acquired P.
  1230. static void
  1231. stopm(void)
  1232. {
  1233. if(m->locks)
  1234. runtime_throw("stopm holding locks");
  1235. if(m->p)
  1236. runtime_throw("stopm holding p");
  1237. if(m->spinning) {
  1238. m->spinning = false;
  1239. runtime_xadd(&runtime_sched.nmspinning, -1);
  1240. }
  1241. retry:
  1242. runtime_lock(&runtime_sched);
  1243. mput(m);
  1244. runtime_unlock(&runtime_sched);
  1245. runtime_notesleep(&m->park);
  1246. runtime_noteclear(&m->park);
  1247. if(m->helpgc) {
  1248. runtime_gchelper();
  1249. m->helpgc = 0;
  1250. m->mcache = nil;
  1251. goto retry;
  1252. }
  1253. acquirep(m->nextp);
  1254. m->nextp = nil;
  1255. }
  1256. static void
  1257. mspinning(void)
  1258. {
  1259. m->spinning = true;
  1260. }
  1261. // Schedules some M to run the p (creates an M if necessary).
  1262. // If p==nil, tries to get an idle P, if no idle P's does nothing.
  1263. static void
  1264. startm(P *p, bool spinning)
  1265. {
  1266. M *mp;
  1267. void (*fn)(void);
  1268. runtime_lock(&runtime_sched);
  1269. if(p == nil) {
  1270. p = pidleget();
  1271. if(p == nil) {
  1272. runtime_unlock(&runtime_sched);
  1273. if(spinning)
  1274. runtime_xadd(&runtime_sched.nmspinning, -1);
  1275. return;
  1276. }
  1277. }
  1278. mp = mget();
  1279. runtime_unlock(&runtime_sched);
  1280. if(mp == nil) {
  1281. fn = nil;
  1282. if(spinning)
  1283. fn = mspinning;
  1284. newm(fn, p);
  1285. return;
  1286. }
  1287. if(mp->spinning)
  1288. runtime_throw("startm: m is spinning");
  1289. if(mp->nextp)
  1290. runtime_throw("startm: m has p");
  1291. mp->spinning = spinning;
  1292. mp->nextp = p;
  1293. runtime_notewakeup(&mp->park);
  1294. }
  1295. // Hands off P from syscall or locked M.
  1296. static void
  1297. handoffp(P *p)
  1298. {
  1299. // if it has local work, start it straight away
  1300. if(p->runqhead != p->runqtail || runtime_sched.runqsize) {
  1301. startm(p, false);
  1302. return;
  1303. }
  1304. // no local work, check that there are no spinning/idle M's,
  1305. // otherwise our help is not required
  1306. if(runtime_atomicload(&runtime_sched.nmspinning) + runtime_atomicload(&runtime_sched.npidle) == 0 && // TODO: fast atomic
  1307. runtime_cas(&runtime_sched.nmspinning, 0, 1)) {
  1308. startm(p, true);
  1309. return;
  1310. }
  1311. runtime_lock(&runtime_sched);
  1312. if(runtime_sched.gcwaiting) {
  1313. p->status = Pgcstop;
  1314. if(--runtime_sched.stopwait == 0)
  1315. runtime_notewakeup(&runtime_sched.stopnote);
  1316. runtime_unlock(&runtime_sched);
  1317. return;
  1318. }
  1319. if(runtime_sched.runqsize) {
  1320. runtime_unlock(&runtime_sched);
  1321. startm(p, false);
  1322. return;
  1323. }
  1324. // If this is the last running P and nobody is polling network,
  1325. // need to wakeup another M to poll network.
  1326. if(runtime_sched.npidle == (uint32)runtime_gomaxprocs-1 && runtime_atomicload64(&runtime_sched.lastpoll) != 0) {
  1327. runtime_unlock(&runtime_sched);
  1328. startm(p, false);
  1329. return;
  1330. }
  1331. pidleput(p);
  1332. runtime_unlock(&runtime_sched);
  1333. }
  1334. // Tries to add one more P to execute G's.
  1335. // Called when a G is made runnable (newproc, ready).
  1336. static void
  1337. wakep(void)
  1338. {
  1339. // be conservative about spinning threads
  1340. if(!runtime_cas(&runtime_sched.nmspinning, 0, 1))
  1341. return;
  1342. startm(nil, true);
  1343. }
  1344. // Stops execution of the current m that is locked to a g until the g is runnable again.
  1345. // Returns with acquired P.
  1346. static void
  1347. stoplockedm(void)
  1348. {
  1349. P *p;
  1350. if(m->lockedg == nil || m->lockedg->lockedm != m)
  1351. runtime_throw("stoplockedm: inconsistent locking");
  1352. if(m->p) {
  1353. // Schedule another M to run this p.
  1354. p = releasep();
  1355. handoffp(p);
  1356. }
  1357. incidlelocked(1);
  1358. // Wait until another thread schedules lockedg again.
  1359. runtime_notesleep(&m->park);
  1360. runtime_noteclear(&m->park);
  1361. if(m->lockedg->status != Grunnable)
  1362. runtime_throw("stoplockedm: not runnable");
  1363. acquirep(m->nextp);
  1364. m->nextp = nil;
  1365. }
  1366. // Schedules the locked m to run the locked gp.
  1367. static void
  1368. startlockedm(G *gp)
  1369. {
  1370. M *mp;
  1371. P *p;
  1372. mp = gp->lockedm;
  1373. if(mp == m)
  1374. runtime_throw("startlockedm: locked to me");
  1375. if(mp->nextp)
  1376. runtime_throw("startlockedm: m has p");
  1377. // directly handoff current P to the locked m
  1378. incidlelocked(-1);
  1379. p = releasep();
  1380. mp->nextp = p;
  1381. runtime_notewakeup(&mp->park);
  1382. stopm();
  1383. }
  1384. // Stops the current m for stoptheworld.
  1385. // Returns when the world is restarted.
  1386. static void
  1387. gcstopm(void)
  1388. {
  1389. P *p;
  1390. if(!runtime_sched.gcwaiting)
  1391. runtime_throw("gcstopm: not waiting for gc");
  1392. if(m->spinning) {
  1393. m->spinning = false;
  1394. runtime_xadd(&runtime_sched.nmspinning, -1);
  1395. }
  1396. p = releasep();
  1397. runtime_lock(&runtime_sched);
  1398. p->status = Pgcstop;
  1399. if(--runtime_sched.stopwait == 0)
  1400. runtime_notewakeup(&runtime_sched.stopnote);
  1401. runtime_unlock(&runtime_sched);
  1402. stopm();
  1403. }
  1404. // Schedules gp to run on the current M.
  1405. // Never returns.
  1406. static void
  1407. execute(G *gp)
  1408. {
  1409. int32 hz;
  1410. if(gp->status != Grunnable) {
  1411. runtime_printf("execute: bad g status %d\n", gp->status);
  1412. runtime_throw("execute: bad g status");
  1413. }
  1414. gp->status = Grunning;
  1415. gp->waitsince = 0;
  1416. m->p->schedtick++;
  1417. m->curg = gp;
  1418. gp->m = m;
  1419. // Check whether the profiler needs to be turned on or off.
  1420. hz = runtime_sched.profilehz;
  1421. if(m->profilehz != hz)
  1422. runtime_resetcpuprofiler(hz);
  1423. runtime_gogo(gp);
  1424. }
  1425. // Finds a runnable goroutine to execute.
  1426. // Tries to steal from other P's, get g from global queue, poll network.
  1427. static G*
  1428. findrunnable(void)
  1429. {
  1430. G *gp;
  1431. P *p;
  1432. int32 i;
  1433. top:
  1434. if(runtime_sched.gcwaiting) {
  1435. gcstopm();
  1436. goto top;
  1437. }
  1438. if(runtime_fingwait && runtime_fingwake && (gp = runtime_wakefing()) != nil)
  1439. runtime_ready(gp);
  1440. // local runq
  1441. gp = runqget(m->p);
  1442. if(gp)
  1443. return gp;
  1444. // global runq
  1445. if(runtime_sched.runqsize) {
  1446. runtime_lock(&runtime_sched);
  1447. gp = globrunqget(m->p, 0);
  1448. runtime_unlock(&runtime_sched);
  1449. if(gp)
  1450. return gp;
  1451. }
  1452. // poll network
  1453. gp = runtime_netpoll(false); // non-blocking
  1454. if(gp) {
  1455. injectglist(gp->schedlink);
  1456. gp->status = Grunnable;
  1457. return gp;
  1458. }
  1459. // If number of spinning M's >= number of busy P's, block.
  1460. // This is necessary to prevent excessive CPU consumption
  1461. // when GOMAXPROCS>>1 but the program parallelism is low.
  1462. if(!m->spinning && 2 * runtime_atomicload(&runtime_sched.nmspinning) >= runtime_gomaxprocs - runtime_atomicload(&runtime_sched.npidle)) // TODO: fast atomic
  1463. goto stop;
  1464. if(!m->spinning) {
  1465. m->spinning = true;
  1466. runtime_xadd(&runtime_sched.nmspinning, 1);
  1467. }
  1468. // random steal from other P's
  1469. for(i = 0; i < 2*runtime_gomaxprocs; i++) {
  1470. if(runtime_sched.gcwaiting)
  1471. goto top;
  1472. p = runtime_allp[runtime_fastrand1()%runtime_gomaxprocs];
  1473. if(p == m->p)
  1474. gp = runqget(p);
  1475. else
  1476. gp = runqsteal(m->p, p);
  1477. if(gp)
  1478. return gp;
  1479. }
  1480. stop:
  1481. // return P and block
  1482. runtime_lock(&runtime_sched);
  1483. if(runtime_sched.gcwaiting) {
  1484. runtime_unlock(&runtime_sched);
  1485. goto top;
  1486. }
  1487. if(runtime_sched.runqsize) {
  1488. gp = globrunqget(m->p, 0);
  1489. runtime_unlock(&runtime_sched);
  1490. return gp;
  1491. }
  1492. p = releasep();
  1493. pidleput(p);
  1494. runtime_unlock(&runtime_sched);
  1495. if(m->spinning) {
  1496. m->spinning = false;
  1497. runtime_xadd(&runtime_sched.nmspinning, -1);
  1498. }
  1499. // check all runqueues once again
  1500. for(i = 0; i < runtime_gomaxprocs; i++) {
  1501. p = runtime_allp[i];
  1502. if(p && p->runqhead != p->runqtail) {
  1503. runtime_lock(&runtime_sched);
  1504. p = pidleget();
  1505. runtime_unlock(&runtime_sched);
  1506. if(p) {
  1507. acquirep(p);
  1508. goto top;
  1509. }
  1510. break;
  1511. }
  1512. }
  1513. // poll network
  1514. if(runtime_xchg64(&runtime_sched.lastpoll, 0) != 0) {
  1515. if(m->p)
  1516. runtime_throw("findrunnable: netpoll with p");
  1517. if(m->spinning)
  1518. runtime_throw("findrunnable: netpoll with spinning");
  1519. gp = runtime_netpoll(true); // block until new work is available
  1520. runtime_atomicstore64(&runtime_sched.lastpoll, runtime_nanotime());
  1521. if(gp) {
  1522. runtime_lock(&runtime_sched);
  1523. p = pidleget();
  1524. runtime_unlock(&runtime_sched);
  1525. if(p) {
  1526. acquirep(p);
  1527. injectglist(gp->schedlink);
  1528. gp->status = Grunnable;
  1529. return gp;
  1530. }
  1531. injectglist(gp);
  1532. }
  1533. }
  1534. stopm();
  1535. goto top;
  1536. }
  1537. static void
  1538. resetspinning(void)
  1539. {
  1540. int32 nmspinning;
  1541. if(m->spinning) {
  1542. m->spinning = false;
  1543. nmspinning = runtime_xadd(&runtime_sched.nmspinning, -1);
  1544. if(nmspinning < 0)
  1545. runtime_throw("findrunnable: negative nmspinning");
  1546. } else
  1547. nmspinning = runtime_atomicload(&runtime_sched.nmspinning);
  1548. // M wakeup policy is deliberately somewhat conservative (see nmspinning handling),
  1549. // so see if we need to wakeup another P here.
  1550. if (nmspinning == 0 && runtime_atomicload(&runtime_sched.npidle) > 0)
  1551. wakep();
  1552. }
  1553. // Injects the list of runnable G's into the scheduler.
  1554. // Can run concurrently with GC.
  1555. static void
  1556. injectglist(G *glist)
  1557. {
  1558. int32 n;
  1559. G *gp;
  1560. if(glist == nil)
  1561. return;
  1562. runtime_lock(&runtime_sched);
  1563. for(n = 0; glist; n++) {
  1564. gp = glist;
  1565. glist = gp->schedlink;
  1566. gp->status = Grunnable;
  1567. globrunqput(gp);
  1568. }
  1569. runtime_unlock(&runtime_sched);
  1570. for(; n && runtime_sched.npidle; n--)
  1571. startm(nil, false);
  1572. }
  1573. // One round of scheduler: find a runnable goroutine and execute it.
  1574. // Never returns.
  1575. static void
  1576. schedule(void)
  1577. {
  1578. G *gp;
  1579. uint32 tick;
  1580. if(m->locks)
  1581. runtime_throw("schedule: holding locks");
  1582. top:
  1583. if(runtime_sched.gcwaiting) {
  1584. gcstopm();
  1585. goto top;
  1586. }
  1587. gp = nil;
  1588. // Check the global runnable queue once in a while to ensure fairness.
  1589. // Otherwise two goroutines can completely occupy the local runqueue
  1590. // by constantly respawning each other.
  1591. tick = m->p->schedtick;
  1592. // This is a fancy way to say tick%61==0,
  1593. // it uses 2 MUL instructions instead of a single DIV and so is faster on modern processors.
  1594. if(tick - (((uint64)tick*0x4325c53fu)>>36)*61 == 0 && runtime_sched.runqsize > 0) {
  1595. runtime_lock(&runtime_sched);
  1596. gp = globrunqget(m->p, 1);
  1597. runtime_unlock(&runtime_sched);
  1598. if(gp)
  1599. resetspinning();
  1600. }
  1601. if(gp == nil) {
  1602. gp = runqget(m->p);
  1603. if(gp && m->spinning)
  1604. runtime_throw("schedule: spinning with local work");
  1605. }
  1606. if(gp == nil) {
  1607. gp = findrunnable(); // blocks until work is available
  1608. resetspinning();
  1609. }
  1610. if(gp->lockedm) {
  1611. // Hands off own p to the locked m,
  1612. // then blocks waiting for a new p.
  1613. startlockedm(gp);
  1614. goto top;
  1615. }
  1616. execute(gp);
  1617. }
  1618. // Puts the current goroutine into a waiting state and calls unlockf.
  1619. // If unlockf returns false, the goroutine is resumed.
  1620. void
  1621. runtime_park(bool(*unlockf)(G*, void*), void *lock, const char *reason)
  1622. {
  1623. if(g->status != Grunning)
  1624. runtime_throw("bad g status");
  1625. m->waitlock = lock;
  1626. m->waitunlockf = unlockf;
  1627. g->waitreason = reason;
  1628. runtime_mcall(park0);
  1629. }
  1630. static bool
  1631. parkunlock(G *gp, void *lock)
  1632. {
  1633. USED(gp);
  1634. runtime_unlock(lock);
  1635. return true;
  1636. }
  1637. // Puts the current goroutine into a waiting state and unlocks the lock.
  1638. // The goroutine can be made runnable again by calling runtime_ready(gp).
  1639. void
  1640. runtime_parkunlock(Lock *lock, const char *reason)
  1641. {
  1642. runtime_park(parkunlock, lock, reason);
  1643. }
  1644. // runtime_park continuation on g0.
  1645. static void
  1646. park0(G *gp)
  1647. {
  1648. bool ok;
  1649. gp->status = Gwaiting;
  1650. gp->m = nil;
  1651. m->curg = nil;
  1652. if(m->waitunlockf) {
  1653. ok = m->waitunlockf(gp, m->waitlock);
  1654. m->waitunlockf = nil;
  1655. m->waitlock = nil;
  1656. if(!ok) {
  1657. gp->status = Grunnable;
  1658. execute(gp); // Schedule it back, never returns.
  1659. }
  1660. }
  1661. if(m->lockedg) {
  1662. stoplockedm();
  1663. execute(gp); // Never returns.
  1664. }
  1665. schedule();
  1666. }
  1667. // Scheduler yield.
  1668. void
  1669. runtime_gosched(void)
  1670. {
  1671. if(g->status != Grunning)
  1672. runtime_throw("bad g status");
  1673. runtime_mcall(runtime_gosched0);
  1674. }
  1675. // runtime_gosched continuation on g0.
  1676. void
  1677. runtime_gosched0(G *gp)
  1678. {
  1679. gp->status = Grunnable;
  1680. gp->m = nil;
  1681. m->curg = nil;
  1682. runtime_lock(&runtime_sched);
  1683. globrunqput(gp);
  1684. runtime_unlock(&runtime_sched);
  1685. if(m->lockedg) {
  1686. stoplockedm();
  1687. execute(gp); // Never returns.
  1688. }
  1689. schedule();
  1690. }
  1691. // Finishes execution of the current goroutine.
  1692. // Need to mark it as nosplit, because it runs with sp > stackbase (as runtime_lessstack).
  1693. // Since it does not return it does not matter. But if it is preempted
  1694. // at the split stack check, GC will complain about inconsistent sp.
  1695. void runtime_goexit(void) __attribute__ ((noinline));
  1696. void
  1697. runtime_goexit(void)
  1698. {
  1699. if(g->status != Grunning)
  1700. runtime_throw("bad g status");
  1701. runtime_mcall(goexit0);
  1702. }
  1703. // runtime_goexit continuation on g0.
  1704. static void
  1705. goexit0(G *gp)
  1706. {
  1707. gp->status = Gdead;
  1708. gp->entry = nil;
  1709. gp->m = nil;
  1710. gp->lockedm = nil;
  1711. gp->paniconfault = 0;
  1712. gp->defer = nil; // should be true already but just in case.
  1713. gp->panic = nil; // non-nil for Goexit during panic. points at stack-allocated data.
  1714. gp->writenbuf = 0;
  1715. gp->writebuf = nil;
  1716. gp->waitreason = nil;
  1717. gp->param = nil;
  1718. m->curg = nil;
  1719. m->lockedg = nil;
  1720. if(m->locked & ~LockExternal) {
  1721. runtime_printf("invalid m->locked = %d\n", m->locked);
  1722. runtime_throw("internal lockOSThread error");
  1723. }
  1724. m->locked = 0;
  1725. gfput(m->p, gp);
  1726. schedule();
  1727. }
  1728. // The goroutine g is about to enter a system call.
  1729. // Record that it's not using the cpu anymore.
  1730. // This is called only from the go syscall library and cgocall,
  1731. // not from the low-level system calls used by the runtime.
  1732. //
  1733. // Entersyscall cannot split the stack: the runtime_gosave must
  1734. // make g->sched refer to the caller's stack segment, because
  1735. // entersyscall is going to return immediately after.
  1736. void runtime_entersyscall(void) __attribute__ ((no_split_stack));
  1737. static void doentersyscall(void) __attribute__ ((no_split_stack, noinline));
  1738. void
  1739. runtime_entersyscall()
  1740. {
  1741. // Save the registers in the g structure so that any pointers
  1742. // held in registers will be seen by the garbage collector.
  1743. getcontext(&g->gcregs);
  1744. // Do the work in a separate function, so that this function
  1745. // doesn't save any registers on its own stack. If this
  1746. // function does save any registers, we might store the wrong
  1747. // value in the call to getcontext.
  1748. //
  1749. // FIXME: This assumes that we do not need to save any
  1750. // callee-saved registers to access the TLS variable g. We
  1751. // don't want to put the ucontext_t on the stack because it is
  1752. // large and we can not split the stack here.
  1753. doentersyscall();
  1754. }
  1755. static void
  1756. doentersyscall()
  1757. {
  1758. // Disable preemption because during this function g is in Gsyscall status,
  1759. // but can have inconsistent g->sched, do not let GC observe it.
  1760. m->locks++;
  1761. // Leave SP around for GC and traceback.
  1762. #ifdef USING_SPLIT_STACK
  1763. g->gcstack = __splitstack_find(nil, nil, &g->gcstack_size,
  1764. &g->gcnext_segment, &g->gcnext_sp,
  1765. &g->gcinitial_sp);
  1766. #else
  1767. {
  1768. void *v;
  1769. g->gcnext_sp = (byte *) &v;
  1770. }
  1771. #endif
  1772. g->status = Gsyscall;
  1773. if(runtime_atomicload(&runtime_sched.sysmonwait)) { // TODO: fast atomic
  1774. runtime_lock(&runtime_sched);
  1775. if(runtime_atomicload(&runtime_sched.sysmonwait)) {
  1776. runtime_atomicstore(&runtime_sched.sysmonwait, 0);
  1777. runtime_notewakeup(&runtime_sched.sysmonnote);
  1778. }
  1779. runtime_unlock(&runtime_sched);
  1780. }
  1781. m->mcache = nil;
  1782. m->p->m = nil;
  1783. runtime_atomicstore(&m->p->status, Psyscall);
  1784. if(runtime_sched.gcwaiting) {
  1785. runtime_lock(&runtime_sched);
  1786. if (runtime_sched.stopwait > 0 && runtime_cas(&m->p->status, Psyscall, Pgcstop)) {
  1787. if(--runtime_sched.stopwait == 0)
  1788. runtime_notewakeup(&runtime_sched.stopnote);
  1789. }
  1790. runtime_unlock(&runtime_sched);
  1791. }
  1792. m->locks--;
  1793. }
  1794. // The same as runtime_entersyscall(), but with a hint that the syscall is blocking.
  1795. void
  1796. runtime_entersyscallblock(void)
  1797. {
  1798. P *p;
  1799. m->locks++; // see comment in entersyscall
  1800. // Leave SP around for GC and traceback.
  1801. #ifdef USING_SPLIT_STACK
  1802. g->gcstack = __splitstack_find(nil, nil, &g->gcstack_size,
  1803. &g->gcnext_segment, &g->gcnext_sp,
  1804. &g->gcinitial_sp);
  1805. #else
  1806. g->gcnext_sp = (byte *) &p;
  1807. #endif
  1808. // Save the registers in the g structure so that any pointers
  1809. // held in registers will be seen by the garbage collector.
  1810. getcontext(&g->gcregs);
  1811. g->status = Gsyscall;
  1812. p = releasep();
  1813. handoffp(p);
  1814. if(g->isbackground) // do not consider blocked scavenger for deadlock detection
  1815. incidlelocked(1);
  1816. m->locks--;
  1817. }
  1818. // The goroutine g exited its system call.
  1819. // Arrange for it to run on a cpu again.
  1820. // This is called only from the go syscall library, not
  1821. // from the low-level system calls used by the runtime.
  1822. void
  1823. runtime_exitsyscall(void)
  1824. {
  1825. G *gp;
  1826. m->locks++; // see comment in entersyscall
  1827. gp = g;
  1828. if(gp->isbackground) // do not consider blocked scavenger for deadlock detection
  1829. incidlelocked(-1);
  1830. g->waitsince = 0;
  1831. if(exitsyscallfast()) {
  1832. // There's a cpu for us, so we can run.
  1833. m->p->syscalltick++;
  1834. gp->status = Grunning;
  1835. // Garbage collector isn't running (since we are),
  1836. // so okay to clear gcstack and gcsp.
  1837. #ifdef USING_SPLIT_STACK
  1838. gp->gcstack = nil;
  1839. #endif
  1840. gp->gcnext_sp = nil;
  1841. runtime_memclr(&gp->gcregs, sizeof gp->gcregs);
  1842. m->locks--;
  1843. return;
  1844. }
  1845. m->locks--;
  1846. // Call the scheduler.
  1847. runtime_mcall(exitsyscall0);
  1848. // Scheduler returned, so we're allowed to run now.
  1849. // Delete the gcstack information that we left for
  1850. // the garbage collector during the system call.
  1851. // Must wait until now because until gosched returns
  1852. // we don't know for sure that the garbage collector
  1853. // is not running.
  1854. #ifdef USING_SPLIT_STACK
  1855. gp->gcstack = nil;
  1856. #endif
  1857. gp->gcnext_sp = nil;
  1858. runtime_memclr(&gp->gcregs, sizeof gp->gcregs);
  1859. // Don't refer to m again, we might be running on a different
  1860. // thread after returning from runtime_mcall.
  1861. runtime_m()->p->syscalltick++;
  1862. }
  1863. static bool
  1864. exitsyscallfast(void)
  1865. {
  1866. P *p;
  1867. // Freezetheworld sets stopwait but does not retake P's.
  1868. if(runtime_sched.stopwait) {
  1869. m->p = nil;
  1870. return false;
  1871. }
  1872. // Try to re-acquire the last P.
  1873. if(m->p && m->p->status == Psyscall && runtime_cas(&m->p->status, Psyscall, Prunning)) {
  1874. // There's a cpu for us, so we can run.
  1875. m->mcache = m->p->mcache;
  1876. m->p->m = m;
  1877. return true;
  1878. }
  1879. // Try to get any other idle P.
  1880. m->p = nil;
  1881. if(runtime_sched.pidle) {
  1882. runtime_lock(&runtime_sched);
  1883. p = pidleget();
  1884. if(p && runtime_atomicload(&runtime_sched.sysmonwait)) {
  1885. runtime_atomicstore(&runtime_sched.sysmonwait, 0);
  1886. runtime_notewakeup(&runtime_sched.sysmonnote);
  1887. }
  1888. runtime_unlock(&runtime_sched);
  1889. if(p) {
  1890. acquirep(p);
  1891. return true;
  1892. }
  1893. }
  1894. return false;
  1895. }
  1896. // runtime_exitsyscall slow path on g0.
  1897. // Failed to acquire P, enqueue gp as runnable.
  1898. static void
  1899. exitsyscall0(G *gp)
  1900. {
  1901. P *p;
  1902. gp->status = Grunnable;
  1903. gp->m = nil;
  1904. m->curg = nil;
  1905. runtime_lock(&runtime_sched);
  1906. p = pidleget();
  1907. if(p == nil)
  1908. globrunqput(gp);
  1909. else if(runtime_atomicload(&runtime_sched.sysmonwait)) {
  1910. runtime_atomicstore(&runtime_sched.sysmonwait, 0);
  1911. runtime_notewakeup(&runtime_sched.sysmonnote);
  1912. }
  1913. runtime_unlock(&runtime_sched);
  1914. if(p) {
  1915. acquirep(p);
  1916. execute(gp); // Never returns.
  1917. }
  1918. if(m->lockedg) {
  1919. // Wait until another thread schedules gp and so m again.
  1920. stoplockedm();
  1921. execute(gp); // Never returns.
  1922. }
  1923. stopm();
  1924. schedule(); // Never returns.
  1925. }
  1926. // Called from syscall package before fork.
  1927. void syscall_runtime_BeforeFork(void)
  1928. __asm__(GOSYM_PREFIX "syscall.runtime_BeforeFork");
  1929. void
  1930. syscall_runtime_BeforeFork(void)
  1931. {
  1932. // Fork can hang if preempted with signals frequently enough (see issue 5517).
  1933. // Ensure that we stay on the same M where we disable profiling.
  1934. runtime_m()->locks++;
  1935. if(runtime_m()->profilehz != 0)
  1936. runtime_resetcpuprofiler(0);
  1937. }
  1938. // Called from syscall package after fork in parent.
  1939. void syscall_runtime_AfterFork(void)
  1940. __asm__(GOSYM_PREFIX "syscall.runtime_AfterFork");
  1941. void
  1942. syscall_runtime_AfterFork(void)
  1943. {
  1944. int32 hz;
  1945. hz = runtime_sched.profilehz;
  1946. if(hz != 0)
  1947. runtime_resetcpuprofiler(hz);
  1948. runtime_m()->locks--;
  1949. }
  1950. // Allocate a new g, with a stack big enough for stacksize bytes.
  1951. G*
  1952. runtime_malg(int32 stacksize, byte** ret_stack, size_t* ret_stacksize)
  1953. {
  1954. G *newg;
  1955. newg = allocg();
  1956. if(stacksize >= 0) {
  1957. #if USING_SPLIT_STACK
  1958. int dont_block_signals = 0;
  1959. *ret_stack = __splitstack_makecontext(stacksize,
  1960. &newg->stack_context[0],
  1961. ret_stacksize);
  1962. __splitstack_block_signals_context(&newg->stack_context[0],
  1963. &dont_block_signals, nil);
  1964. #else
  1965. *ret_stack = runtime_mallocgc(stacksize, 0, FlagNoProfiling|FlagNoGC);
  1966. *ret_stacksize = stacksize;
  1967. newg->gcinitial_sp = *ret_stack;
  1968. newg->gcstack_size = stacksize;
  1969. runtime_xadd(&runtime_stacks_sys, stacksize);
  1970. #endif
  1971. }
  1972. return newg;
  1973. }
  1974. /* For runtime package testing. */
  1975. // Create a new g running fn with siz bytes of arguments.
  1976. // Put it on the queue of g's waiting to run.
  1977. // The compiler turns a go statement into a call to this.
  1978. // Cannot split the stack because it assumes that the arguments
  1979. // are available sequentially after &fn; they would not be
  1980. // copied if a stack split occurred. It's OK for this to call
  1981. // functions that split the stack.
  1982. void runtime_testing_entersyscall(void)
  1983. __asm__ (GOSYM_PREFIX "runtime.entersyscall");
  1984. void
  1985. runtime_testing_entersyscall()
  1986. {
  1987. runtime_entersyscall();
  1988. }
  1989. void runtime_testing_exitsyscall(void)
  1990. __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
  1991. void
  1992. runtime_testing_exitsyscall()
  1993. {
  1994. runtime_exitsyscall();
  1995. }
  1996. G*
  1997. __go_go(void (*fn)(void*), void* arg)
  1998. {
  1999. byte *sp;
  2000. size_t spsize;
  2001. G *newg;
  2002. P *p;
  2003. //runtime_printf("newproc1 %p %p narg=%d nret=%d\n", fn->fn, argp, narg, nret);
  2004. if(fn == nil) {
  2005. m->throwing = -1; // do not dump full stacks
  2006. runtime_throw("go of nil func value");
  2007. }
  2008. m->locks++; // disable preemption because it can be holding p in a local var
  2009. p = m->p;
  2010. if((newg = gfget(p)) != nil) {
  2011. #ifdef USING_SPLIT_STACK
  2012. int dont_block_signals = 0;
  2013. sp = __splitstack_resetcontext(&newg->stack_context[0],
  2014. &spsize);
  2015. __splitstack_block_signals_context(&newg->stack_context[0],
  2016. &dont_block_signals, nil);
  2017. #else
  2018. sp = newg->gcinitial_sp;
  2019. spsize = newg->gcstack_size;
  2020. if(spsize == 0)
  2021. runtime_throw("bad spsize in __go_go");
  2022. newg->gcnext_sp = sp;
  2023. #endif
  2024. } else {
  2025. newg = runtime_malg(StackMin, &sp, &spsize);
  2026. allgadd(newg);
  2027. }
  2028. newg->entry = (byte*)fn;
  2029. newg->param = arg;
  2030. newg->gopc = (uintptr)__builtin_return_address(0);
  2031. newg->status = Grunnable;
  2032. if(p->goidcache == p->goidcacheend) {
  2033. p->goidcache = runtime_xadd64(&runtime_sched.goidgen, GoidCacheBatch);
  2034. p->goidcacheend = p->goidcache + GoidCacheBatch;
  2035. }
  2036. newg->goid = p->goidcache++;
  2037. {
  2038. // Avoid warnings about variables clobbered by
  2039. // longjmp.
  2040. byte * volatile vsp = sp;
  2041. size_t volatile vspsize = spsize;
  2042. G * volatile vnewg = newg;
  2043. getcontext(&vnewg->context);
  2044. vnewg->context.uc_stack.ss_sp = vsp;
  2045. #ifdef MAKECONTEXT_STACK_TOP
  2046. vnewg->context.uc_stack.ss_sp += vspsize;
  2047. #endif
  2048. vnewg->context.uc_stack.ss_size = vspsize;
  2049. makecontext(&vnewg->context, kickoff, 0);
  2050. runqput(p, vnewg);
  2051. if(runtime_atomicload(&runtime_sched.npidle) != 0 && runtime_atomicload(&runtime_sched.nmspinning) == 0 && fn != runtime_main) // TODO: fast atomic
  2052. wakep();
  2053. m->locks--;
  2054. return vnewg;
  2055. }
  2056. }
  2057. static void
  2058. allgadd(G *gp)
  2059. {
  2060. G **new;
  2061. uintptr cap;
  2062. runtime_lock(&allglock);
  2063. if(runtime_allglen >= allgcap) {
  2064. cap = 4096/sizeof(new[0]);
  2065. if(cap < 2*allgcap)
  2066. cap = 2*allgcap;
  2067. new = runtime_malloc(cap*sizeof(new[0]));
  2068. if(new == nil)
  2069. runtime_throw("runtime: cannot allocate memory");
  2070. if(runtime_allg != nil) {
  2071. runtime_memmove(new, runtime_allg, runtime_allglen*sizeof(new[0]));
  2072. runtime_free(runtime_allg);
  2073. }
  2074. runtime_allg = new;
  2075. allgcap = cap;
  2076. }
  2077. runtime_allg[runtime_allglen++] = gp;
  2078. runtime_unlock(&allglock);
  2079. }
  2080. // Put on gfree list.
  2081. // If local list is too long, transfer a batch to the global list.
  2082. static void
  2083. gfput(P *p, G *gp)
  2084. {
  2085. gp->schedlink = p->gfree;
  2086. p->gfree = gp;
  2087. p->gfreecnt++;
  2088. if(p->gfreecnt >= 64) {
  2089. runtime_lock(&runtime_sched.gflock);
  2090. while(p->gfreecnt >= 32) {
  2091. p->gfreecnt--;
  2092. gp = p->gfree;
  2093. p->gfree = gp->schedlink;
  2094. gp->schedlink = runtime_sched.gfree;
  2095. runtime_sched.gfree = gp;
  2096. }
  2097. runtime_unlock(&runtime_sched.gflock);
  2098. }
  2099. }
  2100. // Get from gfree list.
  2101. // If local list is empty, grab a batch from global list.
  2102. static G*
  2103. gfget(P *p)
  2104. {
  2105. G *gp;
  2106. retry:
  2107. gp = p->gfree;
  2108. if(gp == nil && runtime_sched.gfree) {
  2109. runtime_lock(&runtime_sched.gflock);
  2110. while(p->gfreecnt < 32 && runtime_sched.gfree) {
  2111. p->gfreecnt++;
  2112. gp = runtime_sched.gfree;
  2113. runtime_sched.gfree = gp->schedlink;
  2114. gp->schedlink = p->gfree;
  2115. p->gfree = gp;
  2116. }
  2117. runtime_unlock(&runtime_sched.gflock);
  2118. goto retry;
  2119. }
  2120. if(gp) {
  2121. p->gfree = gp->schedlink;
  2122. p->gfreecnt--;
  2123. }
  2124. return gp;
  2125. }
  2126. // Purge all cached G's from gfree list to the global list.
  2127. static void
  2128. gfpurge(P *p)
  2129. {
  2130. G *gp;
  2131. runtime_lock(&runtime_sched.gflock);
  2132. while(p->gfreecnt) {
  2133. p->gfreecnt--;
  2134. gp = p->gfree;
  2135. p->gfree = gp->schedlink;
  2136. gp->schedlink = runtime_sched.gfree;
  2137. runtime_sched.gfree = gp;
  2138. }
  2139. runtime_unlock(&runtime_sched.gflock);
  2140. }
  2141. void
  2142. runtime_Breakpoint(void)
  2143. {
  2144. runtime_breakpoint();
  2145. }
  2146. void runtime_Gosched (void) __asm__ (GOSYM_PREFIX "runtime.Gosched");
  2147. void
  2148. runtime_Gosched(void)
  2149. {
  2150. runtime_gosched();
  2151. }
  2152. // Implementation of runtime.GOMAXPROCS.
  2153. // delete when scheduler is even stronger
  2154. int32
  2155. runtime_gomaxprocsfunc(int32 n)
  2156. {
  2157. int32 ret;
  2158. if(n > MaxGomaxprocs)
  2159. n = MaxGomaxprocs;
  2160. runtime_lock(&runtime_sched);
  2161. ret = runtime_gomaxprocs;
  2162. if(n <= 0 || n == ret) {
  2163. runtime_unlock(&runtime_sched);
  2164. return ret;
  2165. }
  2166. runtime_unlock(&runtime_sched);
  2167. runtime_semacquire(&runtime_worldsema, false);
  2168. m->gcing = 1;
  2169. runtime_stoptheworld();
  2170. newprocs = n;
  2171. m->gcing = 0;
  2172. runtime_semrelease(&runtime_worldsema);
  2173. runtime_starttheworld();
  2174. return ret;
  2175. }
  2176. // lockOSThread is called by runtime.LockOSThread and runtime.lockOSThread below
  2177. // after they modify m->locked. Do not allow preemption during this call,
  2178. // or else the m might be different in this function than in the caller.
  2179. static void
  2180. lockOSThread(void)
  2181. {
  2182. m->lockedg = g;
  2183. g->lockedm = m;
  2184. }
  2185. void runtime_LockOSThread(void) __asm__ (GOSYM_PREFIX "runtime.LockOSThread");
  2186. void
  2187. runtime_LockOSThread(void)
  2188. {
  2189. m->locked |= LockExternal;
  2190. lockOSThread();
  2191. }
  2192. void
  2193. runtime_lockOSThread(void)
  2194. {
  2195. m->locked += LockInternal;
  2196. lockOSThread();
  2197. }
  2198. // unlockOSThread is called by runtime.UnlockOSThread and runtime.unlockOSThread below
  2199. // after they update m->locked. Do not allow preemption during this call,
  2200. // or else the m might be in different in this function than in the caller.
  2201. static void
  2202. unlockOSThread(void)
  2203. {
  2204. if(m->locked != 0)
  2205. return;
  2206. m->lockedg = nil;
  2207. g->lockedm = nil;
  2208. }
  2209. void runtime_UnlockOSThread(void) __asm__ (GOSYM_PREFIX "runtime.UnlockOSThread");
  2210. void
  2211. runtime_UnlockOSThread(void)
  2212. {
  2213. m->locked &= ~LockExternal;
  2214. unlockOSThread();
  2215. }
  2216. void
  2217. runtime_unlockOSThread(void)
  2218. {
  2219. if(m->locked < LockInternal)
  2220. runtime_throw("runtime: internal error: misuse of lockOSThread/unlockOSThread");
  2221. m->locked -= LockInternal;
  2222. unlockOSThread();
  2223. }
  2224. bool
  2225. runtime_lockedOSThread(void)
  2226. {
  2227. return g->lockedm != nil && m->lockedg != nil;
  2228. }
  2229. int32
  2230. runtime_gcount(void)
  2231. {
  2232. G *gp;
  2233. int32 n, s;
  2234. uintptr i;
  2235. n = 0;
  2236. runtime_lock(&allglock);
  2237. // TODO(dvyukov): runtime.NumGoroutine() is O(N).
  2238. // We do not want to increment/decrement centralized counter in newproc/goexit,
  2239. // just to make runtime.NumGoroutine() faster.
  2240. // Compromise solution is to introduce per-P counters of active goroutines.
  2241. for(i = 0; i < runtime_allglen; i++) {
  2242. gp = runtime_allg[i];
  2243. s = gp->status;
  2244. if(s == Grunnable || s == Grunning || s == Gsyscall || s == Gwaiting)
  2245. n++;
  2246. }
  2247. runtime_unlock(&allglock);
  2248. return n;
  2249. }
  2250. int32
  2251. runtime_mcount(void)
  2252. {
  2253. return runtime_sched.mcount;
  2254. }
  2255. static struct {
  2256. Lock;
  2257. void (*fn)(uintptr*, int32);
  2258. int32 hz;
  2259. uintptr pcbuf[TracebackMaxFrames];
  2260. Location locbuf[TracebackMaxFrames];
  2261. } prof;
  2262. static void System(void) {}
  2263. static void GC(void) {}
  2264. // Called if we receive a SIGPROF signal.
  2265. void
  2266. runtime_sigprof()
  2267. {
  2268. M *mp = m;
  2269. int32 n, i;
  2270. bool traceback;
  2271. if(prof.fn == nil || prof.hz == 0)
  2272. return;
  2273. if(mp == nil)
  2274. return;
  2275. // Profiling runs concurrently with GC, so it must not allocate.
  2276. mp->mallocing++;
  2277. traceback = true;
  2278. if(mp->mcache == nil)
  2279. traceback = false;
  2280. runtime_lock(&prof);
  2281. if(prof.fn == nil) {
  2282. runtime_unlock(&prof);
  2283. mp->mallocing--;
  2284. return;
  2285. }
  2286. n = 0;
  2287. if(runtime_atomicload(&runtime_in_callers) > 0) {
  2288. // If SIGPROF arrived while already fetching runtime
  2289. // callers we can have trouble on older systems
  2290. // because the unwind library calls dl_iterate_phdr
  2291. // which was not recursive in the past.
  2292. traceback = false;
  2293. }
  2294. if(traceback) {
  2295. n = runtime_callers(0, prof.locbuf, nelem(prof.locbuf), false);
  2296. for(i = 0; i < n; i++)
  2297. prof.pcbuf[i] = prof.locbuf[i].pc;
  2298. }
  2299. if(!traceback || n <= 0) {
  2300. n = 2;
  2301. prof.pcbuf[0] = (uintptr)runtime_getcallerpc(&n);
  2302. if(mp->gcing || mp->helpgc)
  2303. prof.pcbuf[1] = (uintptr)GC;
  2304. else
  2305. prof.pcbuf[1] = (uintptr)System;
  2306. }
  2307. prof.fn(prof.pcbuf, n);
  2308. runtime_unlock(&prof);
  2309. mp->mallocing--;
  2310. }
  2311. // Arrange to call fn with a traceback hz times a second.
  2312. void
  2313. runtime_setcpuprofilerate(void (*fn)(uintptr*, int32), int32 hz)
  2314. {
  2315. // Force sane arguments.
  2316. if(hz < 0)
  2317. hz = 0;
  2318. if(hz == 0)
  2319. fn = nil;
  2320. if(fn == nil)
  2321. hz = 0;
  2322. // Disable preemption, otherwise we can be rescheduled to another thread
  2323. // that has profiling enabled.
  2324. m->locks++;
  2325. // Stop profiler on this thread so that it is safe to lock prof.
  2326. // if a profiling signal came in while we had prof locked,
  2327. // it would deadlock.
  2328. runtime_resetcpuprofiler(0);
  2329. runtime_lock(&prof);
  2330. prof.fn = fn;
  2331. prof.hz = hz;
  2332. runtime_unlock(&prof);
  2333. runtime_lock(&runtime_sched);
  2334. runtime_sched.profilehz = hz;
  2335. runtime_unlock(&runtime_sched);
  2336. if(hz != 0)
  2337. runtime_resetcpuprofiler(hz);
  2338. m->locks--;
  2339. }
  2340. // Change number of processors. The world is stopped, sched is locked.
  2341. static void
  2342. procresize(int32 new)
  2343. {
  2344. int32 i, old;
  2345. bool empty;
  2346. G *gp;
  2347. P *p;
  2348. old = runtime_gomaxprocs;
  2349. if(old < 0 || old > MaxGomaxprocs || new <= 0 || new >MaxGomaxprocs)
  2350. runtime_throw("procresize: invalid arg");
  2351. // initialize new P's
  2352. for(i = 0; i < new; i++) {
  2353. p = runtime_allp[i];
  2354. if(p == nil) {
  2355. p = (P*)runtime_mallocgc(sizeof(*p), 0, FlagNoInvokeGC);
  2356. p->id = i;
  2357. p->status = Pgcstop;
  2358. runtime_atomicstorep(&runtime_allp[i], p);
  2359. }
  2360. if(p->mcache == nil) {
  2361. if(old==0 && i==0)
  2362. p->mcache = m->mcache; // bootstrap
  2363. else
  2364. p->mcache = runtime_allocmcache();
  2365. }
  2366. }
  2367. // redistribute runnable G's evenly
  2368. // collect all runnable goroutines in global queue preserving FIFO order
  2369. // FIFO order is required to ensure fairness even during frequent GCs
  2370. // see http://golang.org/issue/7126
  2371. empty = false;
  2372. while(!empty) {
  2373. empty = true;
  2374. for(i = 0; i < old; i++) {
  2375. p = runtime_allp[i];
  2376. if(p->runqhead == p->runqtail)
  2377. continue;
  2378. empty = false;
  2379. // pop from tail of local queue
  2380. p->runqtail--;
  2381. gp = p->runq[p->runqtail%nelem(p->runq)];
  2382. // push onto head of global queue
  2383. gp->schedlink = runtime_sched.runqhead;
  2384. runtime_sched.runqhead = gp;
  2385. if(runtime_sched.runqtail == nil)
  2386. runtime_sched.runqtail = gp;
  2387. runtime_sched.runqsize++;
  2388. }
  2389. }
  2390. // fill local queues with at most nelem(p->runq)/2 goroutines
  2391. // start at 1 because current M already executes some G and will acquire allp[0] below,
  2392. // so if we have a spare G we want to put it into allp[1].
  2393. for(i = 1; (uint32)i < (uint32)new * nelem(p->runq)/2 && runtime_sched.runqsize > 0; i++) {
  2394. gp = runtime_sched.runqhead;
  2395. runtime_sched.runqhead = gp->schedlink;
  2396. if(runtime_sched.runqhead == nil)
  2397. runtime_sched.runqtail = nil;
  2398. runtime_sched.runqsize--;
  2399. runqput(runtime_allp[i%new], gp);
  2400. }
  2401. // free unused P's
  2402. for(i = new; i < old; i++) {
  2403. p = runtime_allp[i];
  2404. runtime_freemcache(p->mcache);
  2405. p->mcache = nil;
  2406. gfpurge(p);
  2407. p->status = Pdead;
  2408. // can't free P itself because it can be referenced by an M in syscall
  2409. }
  2410. if(m->p)
  2411. m->p->m = nil;
  2412. m->p = nil;
  2413. m->mcache = nil;
  2414. p = runtime_allp[0];
  2415. p->m = nil;
  2416. p->status = Pidle;
  2417. acquirep(p);
  2418. for(i = new-1; i > 0; i--) {
  2419. p = runtime_allp[i];
  2420. p->status = Pidle;
  2421. pidleput(p);
  2422. }
  2423. runtime_atomicstore((uint32*)&runtime_gomaxprocs, new);
  2424. }
  2425. // Associate p and the current m.
  2426. static void
  2427. acquirep(P *p)
  2428. {
  2429. if(m->p || m->mcache)
  2430. runtime_throw("acquirep: already in go");
  2431. if(p->m || p->status != Pidle) {
  2432. runtime_printf("acquirep: p->m=%p(%d) p->status=%d\n", p->m, p->m ? p->m->id : 0, p->status);
  2433. runtime_throw("acquirep: invalid p state");
  2434. }
  2435. m->mcache = p->mcache;
  2436. m->p = p;
  2437. p->m = m;
  2438. p->status = Prunning;
  2439. }
  2440. // Disassociate p and the current m.
  2441. static P*
  2442. releasep(void)
  2443. {
  2444. P *p;
  2445. if(m->p == nil || m->mcache == nil)
  2446. runtime_throw("releasep: invalid arg");
  2447. p = m->p;
  2448. if(p->m != m || p->mcache != m->mcache || p->status != Prunning) {
  2449. runtime_printf("releasep: m=%p m->p=%p p->m=%p m->mcache=%p p->mcache=%p p->status=%d\n",
  2450. m, m->p, p->m, m->mcache, p->mcache, p->status);
  2451. runtime_throw("releasep: invalid p state");
  2452. }
  2453. m->p = nil;
  2454. m->mcache = nil;
  2455. p->m = nil;
  2456. p->status = Pidle;
  2457. return p;
  2458. }
  2459. static void
  2460. incidlelocked(int32 v)
  2461. {
  2462. runtime_lock(&runtime_sched);
  2463. runtime_sched.nmidlelocked += v;
  2464. if(v > 0)
  2465. checkdead();
  2466. runtime_unlock(&runtime_sched);
  2467. }
  2468. // Check for deadlock situation.
  2469. // The check is based on number of running M's, if 0 -> deadlock.
  2470. static void
  2471. checkdead(void)
  2472. {
  2473. G *gp;
  2474. int32 run, grunning, s;
  2475. uintptr i;
  2476. // For -buildmode=c-shared or -buildmode=c-archive it's OK if
  2477. // there are no running goroutines. The calling program is
  2478. // assumed to be running.
  2479. if(runtime_isarchive) {
  2480. return;
  2481. }
  2482. // -1 for sysmon
  2483. run = runtime_sched.mcount - runtime_sched.nmidle - runtime_sched.nmidlelocked - 1 - countextra();
  2484. if(run > 0)
  2485. return;
  2486. // If we are dying because of a signal caught on an already idle thread,
  2487. // freezetheworld will cause all running threads to block.
  2488. // And runtime will essentially enter into deadlock state,
  2489. // except that there is a thread that will call runtime_exit soon.
  2490. if(runtime_panicking > 0)
  2491. return;
  2492. if(run < 0) {
  2493. runtime_printf("runtime: checkdead: nmidle=%d nmidlelocked=%d mcount=%d\n",
  2494. runtime_sched.nmidle, runtime_sched.nmidlelocked, runtime_sched.mcount);
  2495. runtime_throw("checkdead: inconsistent counts");
  2496. }
  2497. grunning = 0;
  2498. runtime_lock(&allglock);
  2499. for(i = 0; i < runtime_allglen; i++) {
  2500. gp = runtime_allg[i];
  2501. if(gp->isbackground)
  2502. continue;
  2503. s = gp->status;
  2504. if(s == Gwaiting)
  2505. grunning++;
  2506. else if(s == Grunnable || s == Grunning || s == Gsyscall) {
  2507. runtime_unlock(&allglock);
  2508. runtime_printf("runtime: checkdead: find g %D in status %d\n", gp->goid, s);
  2509. runtime_throw("checkdead: runnable g");
  2510. }
  2511. }
  2512. runtime_unlock(&allglock);
  2513. if(grunning == 0) // possible if main goroutine calls runtime_Goexit()
  2514. runtime_throw("no goroutines (main called runtime.Goexit) - deadlock!");
  2515. m->throwing = -1; // do not dump full stacks
  2516. runtime_throw("all goroutines are asleep - deadlock!");
  2517. }
  2518. static void
  2519. sysmon(void)
  2520. {
  2521. uint32 idle, delay;
  2522. int64 now, lastpoll, lasttrace;
  2523. G *gp;
  2524. lasttrace = 0;
  2525. idle = 0; // how many cycles in succession we had not wokeup somebody
  2526. delay = 0;
  2527. for(;;) {
  2528. if(idle == 0) // start with 20us sleep...
  2529. delay = 20;
  2530. else if(idle > 50) // start doubling the sleep after 1ms...
  2531. delay *= 2;
  2532. if(delay > 10*1000) // up to 10ms
  2533. delay = 10*1000;
  2534. runtime_usleep(delay);
  2535. if(runtime_debug.schedtrace <= 0 &&
  2536. (runtime_sched.gcwaiting || runtime_atomicload(&runtime_sched.npidle) == (uint32)runtime_gomaxprocs)) { // TODO: fast atomic
  2537. runtime_lock(&runtime_sched);
  2538. if(runtime_atomicload(&runtime_sched.gcwaiting) || runtime_atomicload(&runtime_sched.npidle) == (uint32)runtime_gomaxprocs) {
  2539. runtime_atomicstore(&runtime_sched.sysmonwait, 1);
  2540. runtime_unlock(&runtime_sched);
  2541. runtime_notesleep(&runtime_sched.sysmonnote);
  2542. runtime_noteclear(&runtime_sched.sysmonnote);
  2543. idle = 0;
  2544. delay = 20;
  2545. } else
  2546. runtime_unlock(&runtime_sched);
  2547. }
  2548. // poll network if not polled for more than 10ms
  2549. lastpoll = runtime_atomicload64(&runtime_sched.lastpoll);
  2550. now = runtime_nanotime();
  2551. if(lastpoll != 0 && lastpoll + 10*1000*1000 < now) {
  2552. runtime_cas64(&runtime_sched.lastpoll, lastpoll, now);
  2553. gp = runtime_netpoll(false); // non-blocking
  2554. if(gp) {
  2555. // Need to decrement number of idle locked M's
  2556. // (pretending that one more is running) before injectglist.
  2557. // Otherwise it can lead to the following situation:
  2558. // injectglist grabs all P's but before it starts M's to run the P's,
  2559. // another M returns from syscall, finishes running its G,
  2560. // observes that there is no work to do and no other running M's
  2561. // and reports deadlock.
  2562. incidlelocked(-1);
  2563. injectglist(gp);
  2564. incidlelocked(1);
  2565. }
  2566. }
  2567. // retake P's blocked in syscalls
  2568. // and preempt long running G's
  2569. if(retake(now))
  2570. idle = 0;
  2571. else
  2572. idle++;
  2573. if(runtime_debug.schedtrace > 0 && lasttrace + runtime_debug.schedtrace*1000000ll <= now) {
  2574. lasttrace = now;
  2575. runtime_schedtrace(runtime_debug.scheddetail);
  2576. }
  2577. }
  2578. }
  2579. typedef struct Pdesc Pdesc;
  2580. struct Pdesc
  2581. {
  2582. uint32 schedtick;
  2583. int64 schedwhen;
  2584. uint32 syscalltick;
  2585. int64 syscallwhen;
  2586. };
  2587. static Pdesc pdesc[MaxGomaxprocs];
  2588. static uint32
  2589. retake(int64 now)
  2590. {
  2591. uint32 i, s, n;
  2592. int64 t;
  2593. P *p;
  2594. Pdesc *pd;
  2595. n = 0;
  2596. for(i = 0; i < (uint32)runtime_gomaxprocs; i++) {
  2597. p = runtime_allp[i];
  2598. if(p==nil)
  2599. continue;
  2600. pd = &pdesc[i];
  2601. s = p->status;
  2602. if(s == Psyscall) {
  2603. // Retake P from syscall if it's there for more than 1 sysmon tick (at least 20us).
  2604. t = p->syscalltick;
  2605. if(pd->syscalltick != t) {
  2606. pd->syscalltick = t;
  2607. pd->syscallwhen = now;
  2608. continue;
  2609. }
  2610. // On the one hand we don't want to retake Ps if there is no other work to do,
  2611. // but on the other hand we want to retake them eventually
  2612. // because they can prevent the sysmon thread from deep sleep.
  2613. if(p->runqhead == p->runqtail &&
  2614. runtime_atomicload(&runtime_sched.nmspinning) + runtime_atomicload(&runtime_sched.npidle) > 0 &&
  2615. pd->syscallwhen + 10*1000*1000 > now)
  2616. continue;
  2617. // Need to decrement number of idle locked M's
  2618. // (pretending that one more is running) before the CAS.
  2619. // Otherwise the M from which we retake can exit the syscall,
  2620. // increment nmidle and report deadlock.
  2621. incidlelocked(-1);
  2622. if(runtime_cas(&p->status, s, Pidle)) {
  2623. n++;
  2624. handoffp(p);
  2625. }
  2626. incidlelocked(1);
  2627. } else if(s == Prunning) {
  2628. // Preempt G if it's running for more than 10ms.
  2629. t = p->schedtick;
  2630. if(pd->schedtick != t) {
  2631. pd->schedtick = t;
  2632. pd->schedwhen = now;
  2633. continue;
  2634. }
  2635. if(pd->schedwhen + 10*1000*1000 > now)
  2636. continue;
  2637. // preemptone(p);
  2638. }
  2639. }
  2640. return n;
  2641. }
  2642. // Tell all goroutines that they have been preempted and they should stop.
  2643. // This function is purely best-effort. It can fail to inform a goroutine if a
  2644. // processor just started running it.
  2645. // No locks need to be held.
  2646. // Returns true if preemption request was issued to at least one goroutine.
  2647. static bool
  2648. preemptall(void)
  2649. {
  2650. return false;
  2651. }
  2652. void
  2653. runtime_schedtrace(bool detailed)
  2654. {
  2655. static int64 starttime;
  2656. int64 now;
  2657. int64 id1, id2, id3;
  2658. int32 i, t, h;
  2659. uintptr gi;
  2660. const char *fmt;
  2661. M *mp, *lockedm;
  2662. G *gp, *lockedg;
  2663. P *p;
  2664. now = runtime_nanotime();
  2665. if(starttime == 0)
  2666. starttime = now;
  2667. runtime_lock(&runtime_sched);
  2668. runtime_printf("SCHED %Dms: gomaxprocs=%d idleprocs=%d threads=%d idlethreads=%d runqueue=%d",
  2669. (now-starttime)/1000000, runtime_gomaxprocs, runtime_sched.npidle, runtime_sched.mcount,
  2670. runtime_sched.nmidle, runtime_sched.runqsize);
  2671. if(detailed) {
  2672. runtime_printf(" gcwaiting=%d nmidlelocked=%d nmspinning=%d stopwait=%d sysmonwait=%d\n",
  2673. runtime_sched.gcwaiting, runtime_sched.nmidlelocked, runtime_sched.nmspinning,
  2674. runtime_sched.stopwait, runtime_sched.sysmonwait);
  2675. }
  2676. // We must be careful while reading data from P's, M's and G's.
  2677. // Even if we hold schedlock, most data can be changed concurrently.
  2678. // E.g. (p->m ? p->m->id : -1) can crash if p->m changes from non-nil to nil.
  2679. for(i = 0; i < runtime_gomaxprocs; i++) {
  2680. p = runtime_allp[i];
  2681. if(p == nil)
  2682. continue;
  2683. mp = p->m;
  2684. h = runtime_atomicload(&p->runqhead);
  2685. t = runtime_atomicload(&p->runqtail);
  2686. if(detailed)
  2687. runtime_printf(" P%d: status=%d schedtick=%d syscalltick=%d m=%d runqsize=%d gfreecnt=%d\n",
  2688. i, p->status, p->schedtick, p->syscalltick, mp ? mp->id : -1, t-h, p->gfreecnt);
  2689. else {
  2690. // In non-detailed mode format lengths of per-P run queues as:
  2691. // [len1 len2 len3 len4]
  2692. fmt = " %d";
  2693. if(runtime_gomaxprocs == 1)
  2694. fmt = " [%d]\n";
  2695. else if(i == 0)
  2696. fmt = " [%d";
  2697. else if(i == runtime_gomaxprocs-1)
  2698. fmt = " %d]\n";
  2699. runtime_printf(fmt, t-h);
  2700. }
  2701. }
  2702. if(!detailed) {
  2703. runtime_unlock(&runtime_sched);
  2704. return;
  2705. }
  2706. for(mp = runtime_allm; mp; mp = mp->alllink) {
  2707. p = mp->p;
  2708. gp = mp->curg;
  2709. lockedg = mp->lockedg;
  2710. id1 = -1;
  2711. if(p)
  2712. id1 = p->id;
  2713. id2 = -1;
  2714. if(gp)
  2715. id2 = gp->goid;
  2716. id3 = -1;
  2717. if(lockedg)
  2718. id3 = lockedg->goid;
  2719. runtime_printf(" M%d: p=%D curg=%D mallocing=%d throwing=%d gcing=%d"
  2720. " locks=%d dying=%d helpgc=%d spinning=%d blocked=%d lockedg=%D\n",
  2721. mp->id, id1, id2,
  2722. mp->mallocing, mp->throwing, mp->gcing, mp->locks, mp->dying, mp->helpgc,
  2723. mp->spinning, m->blocked, id3);
  2724. }
  2725. runtime_lock(&allglock);
  2726. for(gi = 0; gi < runtime_allglen; gi++) {
  2727. gp = runtime_allg[gi];
  2728. mp = gp->m;
  2729. lockedm = gp->lockedm;
  2730. runtime_printf(" G%D: status=%d(%s) m=%d lockedm=%d\n",
  2731. gp->goid, gp->status, gp->waitreason, mp ? mp->id : -1,
  2732. lockedm ? lockedm->id : -1);
  2733. }
  2734. runtime_unlock(&allglock);
  2735. runtime_unlock(&runtime_sched);
  2736. }
  2737. // Put mp on midle list.
  2738. // Sched must be locked.
  2739. static void
  2740. mput(M *mp)
  2741. {
  2742. mp->schedlink = runtime_sched.midle;
  2743. runtime_sched.midle = mp;
  2744. runtime_sched.nmidle++;
  2745. checkdead();
  2746. }
  2747. // Try to get an m from midle list.
  2748. // Sched must be locked.
  2749. static M*
  2750. mget(void)
  2751. {
  2752. M *mp;
  2753. if((mp = runtime_sched.midle) != nil){
  2754. runtime_sched.midle = mp->schedlink;
  2755. runtime_sched.nmidle--;
  2756. }
  2757. return mp;
  2758. }
  2759. // Put gp on the global runnable queue.
  2760. // Sched must be locked.
  2761. static void
  2762. globrunqput(G *gp)
  2763. {
  2764. gp->schedlink = nil;
  2765. if(runtime_sched.runqtail)
  2766. runtime_sched.runqtail->schedlink = gp;
  2767. else
  2768. runtime_sched.runqhead = gp;
  2769. runtime_sched.runqtail = gp;
  2770. runtime_sched.runqsize++;
  2771. }
  2772. // Put a batch of runnable goroutines on the global runnable queue.
  2773. // Sched must be locked.
  2774. static void
  2775. globrunqputbatch(G *ghead, G *gtail, int32 n)
  2776. {
  2777. gtail->schedlink = nil;
  2778. if(runtime_sched.runqtail)
  2779. runtime_sched.runqtail->schedlink = ghead;
  2780. else
  2781. runtime_sched.runqhead = ghead;
  2782. runtime_sched.runqtail = gtail;
  2783. runtime_sched.runqsize += n;
  2784. }
  2785. // Try get a batch of G's from the global runnable queue.
  2786. // Sched must be locked.
  2787. static G*
  2788. globrunqget(P *p, int32 max)
  2789. {
  2790. G *gp, *gp1;
  2791. int32 n;
  2792. if(runtime_sched.runqsize == 0)
  2793. return nil;
  2794. n = runtime_sched.runqsize/runtime_gomaxprocs+1;
  2795. if(n > runtime_sched.runqsize)
  2796. n = runtime_sched.runqsize;
  2797. if(max > 0 && n > max)
  2798. n = max;
  2799. if((uint32)n > nelem(p->runq)/2)
  2800. n = nelem(p->runq)/2;
  2801. runtime_sched.runqsize -= n;
  2802. if(runtime_sched.runqsize == 0)
  2803. runtime_sched.runqtail = nil;
  2804. gp = runtime_sched.runqhead;
  2805. runtime_sched.runqhead = gp->schedlink;
  2806. n--;
  2807. while(n--) {
  2808. gp1 = runtime_sched.runqhead;
  2809. runtime_sched.runqhead = gp1->schedlink;
  2810. runqput(p, gp1);
  2811. }
  2812. return gp;
  2813. }
  2814. // Put p to on pidle list.
  2815. // Sched must be locked.
  2816. static void
  2817. pidleput(P *p)
  2818. {
  2819. p->link = runtime_sched.pidle;
  2820. runtime_sched.pidle = p;
  2821. runtime_xadd(&runtime_sched.npidle, 1); // TODO: fast atomic
  2822. }
  2823. // Try get a p from pidle list.
  2824. // Sched must be locked.
  2825. static P*
  2826. pidleget(void)
  2827. {
  2828. P *p;
  2829. p = runtime_sched.pidle;
  2830. if(p) {
  2831. runtime_sched.pidle = p->link;
  2832. runtime_xadd(&runtime_sched.npidle, -1); // TODO: fast atomic
  2833. }
  2834. return p;
  2835. }
  2836. // Try to put g on local runnable queue.
  2837. // If it's full, put onto global queue.
  2838. // Executed only by the owner P.
  2839. static void
  2840. runqput(P *p, G *gp)
  2841. {
  2842. uint32 h, t;
  2843. retry:
  2844. h = runtime_atomicload(&p->runqhead); // load-acquire, synchronize with consumers
  2845. t = p->runqtail;
  2846. if(t - h < nelem(p->runq)) {
  2847. p->runq[t%nelem(p->runq)] = gp;
  2848. runtime_atomicstore(&p->runqtail, t+1); // store-release, makes the item available for consumption
  2849. return;
  2850. }
  2851. if(runqputslow(p, gp, h, t))
  2852. return;
  2853. // the queue is not full, now the put above must suceed
  2854. goto retry;
  2855. }
  2856. // Put g and a batch of work from local runnable queue on global queue.
  2857. // Executed only by the owner P.
  2858. static bool
  2859. runqputslow(P *p, G *gp, uint32 h, uint32 t)
  2860. {
  2861. G *batch[nelem(p->runq)/2+1];
  2862. uint32 n, i;
  2863. // First, grab a batch from local queue.
  2864. n = t-h;
  2865. n = n/2;
  2866. if(n != nelem(p->runq)/2)
  2867. runtime_throw("runqputslow: queue is not full");
  2868. for(i=0; i<n; i++)
  2869. batch[i] = p->runq[(h+i)%nelem(p->runq)];
  2870. if(!runtime_cas(&p->runqhead, h, h+n)) // cas-release, commits consume
  2871. return false;
  2872. batch[n] = gp;
  2873. // Link the goroutines.
  2874. for(i=0; i<n; i++)
  2875. batch[i]->schedlink = batch[i+1];
  2876. // Now put the batch on global queue.
  2877. runtime_lock(&runtime_sched);
  2878. globrunqputbatch(batch[0], batch[n], n+1);
  2879. runtime_unlock(&runtime_sched);
  2880. return true;
  2881. }
  2882. // Get g from local runnable queue.
  2883. // Executed only by the owner P.
  2884. static G*
  2885. runqget(P *p)
  2886. {
  2887. G *gp;
  2888. uint32 t, h;
  2889. for(;;) {
  2890. h = runtime_atomicload(&p->runqhead); // load-acquire, synchronize with other consumers
  2891. t = p->runqtail;
  2892. if(t == h)
  2893. return nil;
  2894. gp = p->runq[h%nelem(p->runq)];
  2895. if(runtime_cas(&p->runqhead, h, h+1)) // cas-release, commits consume
  2896. return gp;
  2897. }
  2898. }
  2899. // Grabs a batch of goroutines from local runnable queue.
  2900. // batch array must be of size nelem(p->runq)/2. Returns number of grabbed goroutines.
  2901. // Can be executed by any P.
  2902. static uint32
  2903. runqgrab(P *p, G **batch)
  2904. {
  2905. uint32 t, h, n, i;
  2906. for(;;) {
  2907. h = runtime_atomicload(&p->runqhead); // load-acquire, synchronize with other consumers
  2908. t = runtime_atomicload(&p->runqtail); // load-acquire, synchronize with the producer
  2909. n = t-h;
  2910. n = n - n/2;
  2911. if(n == 0)
  2912. break;
  2913. if(n > nelem(p->runq)/2) // read inconsistent h and t
  2914. continue;
  2915. for(i=0; i<n; i++)
  2916. batch[i] = p->runq[(h+i)%nelem(p->runq)];
  2917. if(runtime_cas(&p->runqhead, h, h+n)) // cas-release, commits consume
  2918. break;
  2919. }
  2920. return n;
  2921. }
  2922. // Steal half of elements from local runnable queue of p2
  2923. // and put onto local runnable queue of p.
  2924. // Returns one of the stolen elements (or nil if failed).
  2925. static G*
  2926. runqsteal(P *p, P *p2)
  2927. {
  2928. G *gp;
  2929. G *batch[nelem(p->runq)/2];
  2930. uint32 t, h, n, i;
  2931. n = runqgrab(p2, batch);
  2932. if(n == 0)
  2933. return nil;
  2934. n--;
  2935. gp = batch[n];
  2936. if(n == 0)
  2937. return gp;
  2938. h = runtime_atomicload(&p->runqhead); // load-acquire, synchronize with consumers
  2939. t = p->runqtail;
  2940. if(t - h + n >= nelem(p->runq))
  2941. runtime_throw("runqsteal: runq overflow");
  2942. for(i=0; i<n; i++, t++)
  2943. p->runq[t%nelem(p->runq)] = batch[i];
  2944. runtime_atomicstore(&p->runqtail, t); // store-release, makes the item available for consumption
  2945. return gp;
  2946. }
  2947. void runtime_testSchedLocalQueue(void)
  2948. __asm__("runtime.testSchedLocalQueue");
  2949. void
  2950. runtime_testSchedLocalQueue(void)
  2951. {
  2952. P p;
  2953. G gs[nelem(p.runq)];
  2954. int32 i, j;
  2955. runtime_memclr((byte*)&p, sizeof(p));
  2956. for(i = 0; i < (int32)nelem(gs); i++) {
  2957. if(runqget(&p) != nil)
  2958. runtime_throw("runq is not empty initially");
  2959. for(j = 0; j < i; j++)
  2960. runqput(&p, &gs[i]);
  2961. for(j = 0; j < i; j++) {
  2962. if(runqget(&p) != &gs[i]) {
  2963. runtime_printf("bad element at iter %d/%d\n", i, j);
  2964. runtime_throw("bad element");
  2965. }
  2966. }
  2967. if(runqget(&p) != nil)
  2968. runtime_throw("runq is not empty afterwards");
  2969. }
  2970. }
  2971. void runtime_testSchedLocalQueueSteal(void)
  2972. __asm__("runtime.testSchedLocalQueueSteal");
  2973. void
  2974. runtime_testSchedLocalQueueSteal(void)
  2975. {
  2976. P p1, p2;
  2977. G gs[nelem(p1.runq)], *gp;
  2978. int32 i, j, s;
  2979. runtime_memclr((byte*)&p1, sizeof(p1));
  2980. runtime_memclr((byte*)&p2, sizeof(p2));
  2981. for(i = 0; i < (int32)nelem(gs); i++) {
  2982. for(j = 0; j < i; j++) {
  2983. gs[j].sig = 0;
  2984. runqput(&p1, &gs[j]);
  2985. }
  2986. gp = runqsteal(&p2, &p1);
  2987. s = 0;
  2988. if(gp) {
  2989. s++;
  2990. gp->sig++;
  2991. }
  2992. while((gp = runqget(&p2)) != nil) {
  2993. s++;
  2994. gp->sig++;
  2995. }
  2996. while((gp = runqget(&p1)) != nil)
  2997. gp->sig++;
  2998. for(j = 0; j < i; j++) {
  2999. if(gs[j].sig != 1) {
  3000. runtime_printf("bad element %d(%d) at iter %d\n", j, gs[j].sig, i);
  3001. runtime_throw("bad element");
  3002. }
  3003. }
  3004. if(s != i/2 && s != i/2+1) {
  3005. runtime_printf("bad steal %d, want %d or %d, iter %d\n",
  3006. s, i/2, i/2+1, i);
  3007. runtime_throw("bad steal");
  3008. }
  3009. }
  3010. }
  3011. int32
  3012. runtime_setmaxthreads(int32 in)
  3013. {
  3014. int32 out;
  3015. runtime_lock(&runtime_sched);
  3016. out = runtime_sched.maxmcount;
  3017. runtime_sched.maxmcount = in;
  3018. checkmcount();
  3019. runtime_unlock(&runtime_sched);
  3020. return out;
  3021. }
  3022. void
  3023. runtime_proc_scan(struct Workbuf** wbufp, void (*enqueue1)(struct Workbuf**, Obj))
  3024. {
  3025. enqueue1(wbufp, (Obj){(byte*)&runtime_sched, sizeof runtime_sched, 0});
  3026. enqueue1(wbufp, (Obj){(byte*)&runtime_main_init_done, sizeof runtime_main_init_done, 0});
  3027. }
  3028. // Return whether we are waiting for a GC. This gc toolchain uses
  3029. // preemption instead.
  3030. bool
  3031. runtime_gcwaiting(void)
  3032. {
  3033. return runtime_sched.gcwaiting;
  3034. }