sched_prim.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1993-1987 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. /*
  27. * File: sched_prim.c
  28. * Author: Avadis Tevanian, Jr.
  29. * Date: 1986
  30. *
  31. * Scheduling primitives
  32. *
  33. */
  34. #include <kern/printf.h>
  35. #include <mach/machine.h>
  36. #include <machine/locore.h>
  37. #include <machine/machspl.h> /* For def'n of splsched() */
  38. #include <machine/model_dep.h>
  39. #include <kern/ast.h>
  40. #include <kern/counters.h>
  41. #include <kern/cpu_number.h>
  42. #include <kern/debug.h>
  43. #include <kern/lock.h>
  44. #include <kern/mach_clock.h>
  45. #include <kern/mach_factor.h>
  46. #include <kern/macros.h>
  47. #include <kern/processor.h>
  48. #include <kern/queue.h>
  49. #include <kern/sched.h>
  50. #include <kern/sched_prim.h>
  51. #include <kern/syscall_subr.h>
  52. #include <kern/thread.h>
  53. #include <kern/thread_swap.h>
  54. #include <vm/pmap.h>
  55. #include <vm/vm_kern.h>
  56. #include <vm/vm_map.h>
  57. #if MACH_FIXPRI
  58. #include <mach/policy.h>
  59. #endif /* MACH_FIXPRI */
  60. int min_quantum; /* defines max context switch rate */
  61. unsigned sched_tick;
  62. #if SIMPLE_CLOCK
  63. int sched_usec;
  64. #endif /* SIMPLE_CLOCK */
  65. thread_t sched_thread_id;
  66. timer_elt_data_t recompute_priorities_timer;
  67. /*
  68. * State machine
  69. *
  70. * states are combinations of:
  71. * R running
  72. * W waiting (or on wait queue)
  73. * S suspended (or will suspend)
  74. * N non-interruptible
  75. *
  76. * init action
  77. * assert_wait thread_block clear_wait suspend resume
  78. *
  79. * R RW, RWN R; setrun - RS -
  80. * RS RWS, RWNS S; wake_active - - R
  81. * RN RWN RN; setrun - RNS -
  82. * RNS RWNS RNS; setrun - - RN
  83. *
  84. * RW W R RWS -
  85. * RWN WN RN RWNS -
  86. * RWS WS; wake_active RS - RW
  87. * RWNS WNS RNS - RWN
  88. *
  89. * W R; setrun WS -
  90. * WN RN; setrun WNS -
  91. * WNS RNS; setrun - WN
  92. *
  93. * S - - R
  94. * WS S - W
  95. *
  96. */
  97. /*
  98. * Waiting protocols and implementation:
  99. *
  100. * Each thread may be waiting for exactly one event; this event
  101. * is set using assert_wait(). That thread may be awakened either
  102. * by performing a thread_wakeup_prim() on its event,
  103. * or by directly waking that thread up with clear_wait().
  104. *
  105. * The implementation of wait events uses a hash table. Each
  106. * bucket is queue of threads having the same hash function
  107. * value; the chain for the queue (linked list) is the run queue
  108. * field. [It is not possible to be waiting and runnable at the
  109. * same time.]
  110. *
  111. * Locks on both the thread and on the hash buckets govern the
  112. * wait event field and the queue chain field. Because wakeup
  113. * operations only have the event as an argument, the event hash
  114. * bucket must be locked before any thread.
  115. *
  116. * Scheduling operations may also occur at interrupt level; therefore,
  117. * interrupts below splsched() must be prevented when holding
  118. * thread or hash bucket locks.
  119. *
  120. * The wait event hash table declarations are as follows:
  121. */
  122. #define NUMQUEUES 1031
  123. queue_head_t wait_queue[NUMQUEUES];
  124. decl_simple_lock_data(, wait_lock[NUMQUEUES])
  125. /* NOTE: we want a small positive integer out of this */
  126. #define wait_hash(event) \
  127. ((((long)(event) < 0) ? ~(long)(event) : (long)(event)) % NUMQUEUES)
  128. void wait_queue_init(void)
  129. {
  130. int i;
  131. for (i = 0; i < NUMQUEUES; i++) {
  132. queue_init(&wait_queue[i]);
  133. simple_lock_init(&wait_lock[i]);
  134. }
  135. }
  136. void sched_init(void)
  137. {
  138. recompute_priorities_timer.fcn = recompute_priorities;
  139. recompute_priorities_timer.param = NULL;
  140. min_quantum = hz / 10; /* context switch 10 times/second */
  141. wait_queue_init();
  142. pset_sys_bootstrap(); /* initialize processor mgmt. */
  143. queue_init(&action_queue);
  144. simple_lock_init(&action_lock);
  145. sched_tick = 0;
  146. #if SIMPLE_CLOCK
  147. sched_usec = 0;
  148. #endif /* SIMPLE_CLOCK */
  149. ast_init();
  150. }
  151. /*
  152. * Thread timeout routine, called when timer expires.
  153. * Called at splsoftclock.
  154. */
  155. void thread_timeout(
  156. void *_thread)
  157. {
  158. thread_t thread = _thread;
  159. assert(thread->timer.set == TELT_UNSET);
  160. clear_wait(thread, THREAD_TIMED_OUT, FALSE);
  161. }
  162. /*
  163. * thread_set_timeout:
  164. *
  165. * Set a timer for the current thread, if the thread
  166. * is ready to wait. Must be called between assert_wait()
  167. * and thread_block().
  168. */
  169. void thread_set_timeout(
  170. int t) /* timeout interval in ticks */
  171. {
  172. thread_t thread = current_thread();
  173. spl_t s;
  174. s = splsched();
  175. thread_lock(thread);
  176. if ((thread->state & TH_WAIT) != 0) {
  177. set_timeout(&thread->timer, t);
  178. }
  179. thread_unlock(thread);
  180. splx(s);
  181. }
  182. /*
  183. * Set up thread timeout element when thread is created.
  184. */
  185. void thread_timeout_setup(
  186. thread_t thread)
  187. {
  188. thread->timer.fcn = thread_timeout;
  189. thread->timer.param = thread;
  190. thread->depress_timer.fcn = (void (*)(void*))thread_depress_timeout;
  191. thread->depress_timer.param = thread;
  192. }
  193. /*
  194. * assert_wait:
  195. *
  196. * Assert that the current thread is about to go to
  197. * sleep until the specified event occurs.
  198. */
  199. void assert_wait(
  200. event_t event,
  201. boolean_t interruptible)
  202. {
  203. queue_t q;
  204. int index;
  205. thread_t thread;
  206. decl_simple_lock_data( , *lock);
  207. spl_t s;
  208. thread = current_thread();
  209. if (thread->wait_event != 0) {
  210. panic("assert_wait: already asserted event %p\n",
  211. thread->wait_event);
  212. }
  213. s = splsched();
  214. if (event != 0) {
  215. index = wait_hash(event);
  216. q = &wait_queue[index];
  217. lock = &wait_lock[index];
  218. simple_lock(lock);
  219. thread_lock(thread);
  220. enqueue_tail(q, &(thread->links));
  221. thread->wait_event = event;
  222. if (interruptible)
  223. thread->state |= TH_WAIT;
  224. else
  225. thread->state |= TH_WAIT | TH_UNINT;
  226. thread_unlock(thread);
  227. simple_unlock(lock);
  228. }
  229. else {
  230. thread_lock(thread);
  231. if (interruptible)
  232. thread->state |= TH_WAIT;
  233. else
  234. thread->state |= TH_WAIT | TH_UNINT;
  235. thread_unlock(thread);
  236. }
  237. splx(s);
  238. }
  239. /*
  240. * clear_wait:
  241. *
  242. * Clear the wait condition for the specified thread. Start the thread
  243. * executing if that is appropriate.
  244. *
  245. * parameters:
  246. * thread thread to awaken
  247. * result Wakeup result the thread should see
  248. * interrupt_only Don't wake up the thread if it isn't
  249. * interruptible.
  250. */
  251. void clear_wait(
  252. thread_t thread,
  253. int result,
  254. boolean_t interrupt_only)
  255. {
  256. int index;
  257. queue_t q;
  258. decl_simple_lock_data( , *lock);
  259. event_t event;
  260. spl_t s;
  261. s = splsched();
  262. thread_lock(thread);
  263. if (interrupt_only && (thread->state & TH_UNINT)) {
  264. /*
  265. * can`t interrupt thread
  266. */
  267. thread_unlock(thread);
  268. splx(s);
  269. return;
  270. }
  271. event = thread->wait_event;
  272. if (event != 0) {
  273. thread_unlock(thread);
  274. index = wait_hash(event);
  275. q = &wait_queue[index];
  276. lock = &wait_lock[index];
  277. simple_lock(lock);
  278. /*
  279. * If the thread is still waiting on that event,
  280. * then remove it from the list. If it is waiting
  281. * on a different event, or no event at all, then
  282. * someone else did our job for us.
  283. */
  284. thread_lock(thread);
  285. if (thread->wait_event == event) {
  286. remqueue(q, (queue_entry_t)thread);
  287. thread->wait_event = 0;
  288. event = 0; /* cause to run below */
  289. }
  290. simple_unlock(lock);
  291. }
  292. if (event == 0) {
  293. int state = thread->state;
  294. reset_timeout_check(&thread->timer);
  295. switch (state & TH_SCHED_STATE) {
  296. case TH_WAIT | TH_SUSP | TH_UNINT:
  297. case TH_WAIT | TH_UNINT:
  298. case TH_WAIT:
  299. /*
  300. * Sleeping and not suspendable - put
  301. * on run queue.
  302. */
  303. thread->state = (state &~ TH_WAIT) | TH_RUN;
  304. thread->wait_result = result;
  305. thread_setrun(thread, TRUE);
  306. break;
  307. case TH_WAIT | TH_SUSP:
  308. case TH_RUN | TH_WAIT:
  309. case TH_RUN | TH_WAIT | TH_SUSP:
  310. case TH_RUN | TH_WAIT | TH_UNINT:
  311. case TH_RUN | TH_WAIT | TH_SUSP | TH_UNINT:
  312. /*
  313. * Either already running, or suspended.
  314. */
  315. thread->state = state &~ TH_WAIT;
  316. thread->wait_result = result;
  317. break;
  318. default:
  319. /*
  320. * Not waiting.
  321. */
  322. break;
  323. }
  324. }
  325. thread_unlock(thread);
  326. splx(s);
  327. }
  328. #define state_panic(thread) \
  329. panic ("thread %p has unexpected state %x (%s%s%s%s%s%s%s%s)", \
  330. thread, thread->state, \
  331. thread->state & TH_WAIT ? "TH_WAIT|" : "", \
  332. thread->state & TH_SUSP ? "TH_SUSP|" : "", \
  333. thread->state & TH_RUN ? "TH_RUN|" : "", \
  334. thread->state & TH_UNINT ? "TH_UNINT|" : "", \
  335. thread->state & TH_HALTED ? "TH_HALTED|" : "", \
  336. thread->state & TH_IDLE ? "TH_IDLE|" : "", \
  337. thread->state & TH_SWAPPED ? "TH_SWAPPED|" : "", \
  338. thread->state & TH_SW_COMING_IN ? "TH_SW_COMING_IN|" : "")
  339. /*
  340. * thread_wakeup_prim:
  341. *
  342. * Common routine for thread_wakeup, thread_wakeup_with_result,
  343. * and thread_wakeup_one.
  344. *
  345. */
  346. boolean_t thread_wakeup_prim(
  347. event_t event,
  348. boolean_t one_thread,
  349. int result)
  350. {
  351. queue_t q;
  352. int index;
  353. boolean_t woke = FALSE;
  354. thread_t thread, next_th;
  355. decl_simple_lock_data( , *lock);
  356. spl_t s;
  357. int state;
  358. index = wait_hash(event);
  359. q = &wait_queue[index];
  360. s = splsched();
  361. lock = &wait_lock[index];
  362. simple_lock(lock);
  363. thread = (thread_t) queue_first(q);
  364. while (!queue_end(q, (queue_entry_t)thread)) {
  365. next_th = (thread_t) queue_next((queue_t) thread);
  366. if (thread->wait_event == event) {
  367. thread_lock(thread);
  368. remqueue(q, (queue_entry_t) thread);
  369. thread->wait_event = 0;
  370. reset_timeout_check(&thread->timer);
  371. state = thread->state;
  372. switch (state & TH_SCHED_STATE) {
  373. case TH_WAIT | TH_SUSP | TH_UNINT:
  374. case TH_WAIT | TH_UNINT:
  375. case TH_WAIT:
  376. /*
  377. * Sleeping and not suspendable - put
  378. * on run queue.
  379. */
  380. thread->state = (state &~ TH_WAIT) | TH_RUN;
  381. thread->wait_result = result;
  382. thread_setrun(thread, TRUE);
  383. break;
  384. case TH_WAIT | TH_SUSP:
  385. case TH_RUN | TH_WAIT:
  386. case TH_RUN | TH_WAIT | TH_SUSP:
  387. case TH_RUN | TH_WAIT | TH_UNINT:
  388. case TH_RUN | TH_WAIT | TH_SUSP | TH_UNINT:
  389. /*
  390. * Either already running, or suspended.
  391. */
  392. thread->state = state &~ TH_WAIT;
  393. thread->wait_result = result;
  394. break;
  395. default:
  396. state_panic(thread);
  397. break;
  398. }
  399. thread_unlock(thread);
  400. woke = TRUE;
  401. if (one_thread)
  402. break;
  403. }
  404. thread = next_th;
  405. }
  406. simple_unlock(lock);
  407. splx(s);
  408. return (woke);
  409. }
  410. /*
  411. * thread_sleep:
  412. *
  413. * Cause the current thread to wait until the specified event
  414. * occurs. The specified lock is unlocked before releasing
  415. * the cpu. (This is a convenient way to sleep without manually
  416. * calling assert_wait).
  417. *
  418. * Note: if the event may be woken from an interrupt handler, this must be
  419. * called at an spl level that prevents such interrupts.
  420. */
  421. void thread_sleep(
  422. event_t event,
  423. simple_lock_t lock,
  424. boolean_t interruptible)
  425. {
  426. assert_wait(event, interruptible); /* assert event */
  427. simple_unlock(lock); /* release the lock */
  428. thread_block(thread_no_continuation); /* block ourselves */
  429. }
  430. /*
  431. * thread_bind:
  432. *
  433. * Force a thread to execute on the specified processor.
  434. * If the thread is currently executing, it may wait until its
  435. * time slice is up before switching onto the specified processor.
  436. *
  437. * A processor of PROCESSOR_NULL causes the thread to be unbound.
  438. * xxx - DO NOT export this to users.
  439. */
  440. void thread_bind(
  441. thread_t thread,
  442. processor_t processor)
  443. {
  444. spl_t s;
  445. s = splsched();
  446. thread_lock(thread);
  447. thread->bound_processor = processor;
  448. thread_unlock(thread);
  449. (void) splx(s);
  450. }
  451. /*
  452. * Select a thread for this processor (the current processor) to run.
  453. * May select the current thread.
  454. * Assumes splsched.
  455. */
  456. thread_t thread_select(
  457. processor_t myprocessor)
  458. {
  459. thread_t thread;
  460. myprocessor->first_quantum = TRUE;
  461. /*
  462. * Check for obvious simple case; local runq is
  463. * empty and global runq has entry at hint.
  464. */
  465. if (myprocessor->runq.count > 0) {
  466. thread = choose_thread(myprocessor);
  467. myprocessor->quantum = min_quantum;
  468. }
  469. else {
  470. processor_set_t pset;
  471. #if MACH_HOST
  472. pset = myprocessor->processor_set;
  473. #else /* MACH_HOST */
  474. pset = &default_pset;
  475. #endif /* MACH_HOST */
  476. simple_lock(&pset->runq.lock);
  477. #if DEBUG
  478. checkrq(&pset->runq, "thread_select");
  479. #endif /* DEBUG */
  480. if (pset->runq.count == 0) {
  481. /*
  482. * Nothing else runnable. Return if this
  483. * thread is still runnable on this processor.
  484. * Check for priority update if required.
  485. */
  486. thread = current_thread();
  487. if ((thread->state == TH_RUN) &&
  488. #if MACH_HOST
  489. (thread->processor_set == pset) &&
  490. #endif /* MACH_HOST */
  491. ((thread->bound_processor == PROCESSOR_NULL) ||
  492. (thread->bound_processor == myprocessor))) {
  493. simple_unlock(&pset->runq.lock);
  494. thread_lock(thread);
  495. if (thread->sched_stamp != sched_tick)
  496. update_priority(thread);
  497. thread_unlock(thread);
  498. }
  499. else {
  500. thread = choose_pset_thread(myprocessor, pset);
  501. }
  502. }
  503. else {
  504. queue_t q;
  505. /*
  506. * If there is a thread at hint, grab it,
  507. * else call choose_pset_thread.
  508. */
  509. q = pset->runq.runq + pset->runq.low;
  510. if (queue_empty(q)) {
  511. pset->runq.low++;
  512. thread = choose_pset_thread(myprocessor, pset);
  513. }
  514. else {
  515. thread = (thread_t) dequeue_head(q);
  516. thread->runq = RUN_QUEUE_NULL;
  517. pset->runq.count--;
  518. #if MACH_FIXPRI
  519. /*
  520. * Cannot lazy evaluate pset->runq.low for
  521. * fixed priority policy
  522. */
  523. if ((pset->runq.count > 0) &&
  524. (pset->policies & POLICY_FIXEDPRI)) {
  525. while (queue_empty(q)) {
  526. pset->runq.low++;
  527. q++;
  528. }
  529. }
  530. #endif /* MACH_FIXPRI */
  531. #if DEBUG
  532. checkrq(&pset->runq, "thread_select: after");
  533. #endif /* DEBUG */
  534. simple_unlock(&pset->runq.lock);
  535. }
  536. }
  537. #if MACH_FIXPRI
  538. if (thread->policy == POLICY_TIMESHARE) {
  539. #endif /* MACH_FIXPRI */
  540. myprocessor->quantum = pset->set_quantum;
  541. #if MACH_FIXPRI
  542. }
  543. else {
  544. /*
  545. * POLICY_FIXEDPRI
  546. */
  547. myprocessor->quantum = thread->sched_data;
  548. }
  549. #endif /* MACH_FIXPRI */
  550. }
  551. return thread;
  552. }
  553. /*
  554. * Stop running the current thread and start running the new thread.
  555. * If continuation is non-zero, and the current thread is blocked,
  556. * then it will resume by executing continuation on a new stack.
  557. * Returns TRUE if the hand-off succeeds.
  558. * Assumes splsched.
  559. */
  560. boolean_t thread_invoke(
  561. thread_t old_thread,
  562. continuation_t continuation,
  563. thread_t new_thread)
  564. {
  565. /*
  566. * Check for invoking the same thread.
  567. */
  568. if (old_thread == new_thread) {
  569. /*
  570. * Mark thread interruptible.
  571. * Run continuation if there is one.
  572. */
  573. thread_lock(new_thread);
  574. new_thread->state &= ~TH_UNINT;
  575. thread_unlock(new_thread);
  576. thread_wakeup(TH_EV_STATE(new_thread));
  577. if (continuation != thread_no_continuation) {
  578. (void) spl0();
  579. call_continuation(continuation);
  580. /*NOTREACHED*/
  581. }
  582. return TRUE;
  583. }
  584. /*
  585. * Check for stack-handoff.
  586. */
  587. thread_lock(new_thread);
  588. if ((old_thread->stack_privilege != current_stack()) &&
  589. (continuation != thread_no_continuation))
  590. {
  591. switch (new_thread->state & TH_SWAP_STATE) {
  592. case TH_SWAPPED:
  593. new_thread->state &= ~(TH_SWAPPED | TH_UNINT);
  594. thread_unlock(new_thread);
  595. thread_wakeup(TH_EV_STATE(new_thread));
  596. #if NCPUS > 1
  597. new_thread->last_processor = current_processor();
  598. #endif /* NCPUS > 1 */
  599. /*
  600. * Set up ast context of new thread and
  601. * switch to its timer.
  602. */
  603. ast_context(new_thread, cpu_number());
  604. timer_switch(&new_thread->system_timer);
  605. stack_handoff(old_thread, new_thread);
  606. /*
  607. * We can dispatch the old thread now.
  608. * This is like thread_dispatch, except
  609. * that the old thread is left swapped
  610. * *without* freeing its stack.
  611. * This path is also much more frequent
  612. * than actual calls to thread_dispatch.
  613. */
  614. thread_lock(old_thread);
  615. old_thread->swap_func = continuation;
  616. switch (old_thread->state) {
  617. case TH_RUN | TH_SUSP:
  618. case TH_RUN | TH_SUSP | TH_HALTED:
  619. case TH_RUN | TH_WAIT | TH_SUSP:
  620. /*
  621. * Suspend the thread
  622. */
  623. old_thread->state = (old_thread->state & ~TH_RUN)
  624. | TH_SWAPPED;
  625. if (old_thread->wake_active) {
  626. old_thread->wake_active = FALSE;
  627. thread_unlock(old_thread);
  628. thread_wakeup(TH_EV_WAKE_ACTIVE(old_thread));
  629. goto after_old_thread;
  630. }
  631. break;
  632. case TH_RUN | TH_SUSP | TH_UNINT:
  633. case TH_RUN | TH_UNINT:
  634. case TH_RUN:
  635. /*
  636. * We can`t suspend the thread yet,
  637. * or it`s still running.
  638. * Put back on a run queue.
  639. */
  640. old_thread->state |= TH_SWAPPED;
  641. thread_setrun(old_thread, FALSE);
  642. break;
  643. case TH_RUN | TH_WAIT | TH_SUSP | TH_UNINT:
  644. case TH_RUN | TH_WAIT | TH_UNINT:
  645. case TH_RUN | TH_WAIT:
  646. /*
  647. * Waiting, and not suspendable.
  648. */
  649. old_thread->state = (old_thread->state & ~TH_RUN)
  650. | TH_SWAPPED;
  651. break;
  652. case TH_RUN | TH_IDLE:
  653. /*
  654. * Drop idle thread -- it is already in
  655. * idle_thread_array.
  656. */
  657. old_thread->state = TH_RUN | TH_IDLE | TH_SWAPPED;
  658. break;
  659. default:
  660. state_panic(old_thread);
  661. }
  662. thread_unlock(old_thread);
  663. after_old_thread:
  664. /*
  665. * call_continuation calls the continuation
  666. * after resetting the current stack pointer
  667. * to recover stack space. If we called
  668. * the continuation directly, we would risk
  669. * running out of stack.
  670. */
  671. counter(c_thread_invoke_hits++);
  672. (void) spl0();
  673. call_continuation(new_thread->swap_func);
  674. /*NOTREACHED*/
  675. return TRUE; /* help for the compiler */
  676. case TH_SW_COMING_IN:
  677. /*
  678. * Waiting for a stack
  679. */
  680. thread_swapin(new_thread);
  681. thread_unlock(new_thread);
  682. counter(c_thread_invoke_misses++);
  683. return FALSE;
  684. case 0:
  685. /*
  686. * Already has a stack - can`t handoff.
  687. */
  688. break;
  689. }
  690. }
  691. else {
  692. /*
  693. * Check that the thread is swapped-in.
  694. */
  695. if (new_thread->state & TH_SWAPPED) {
  696. if ((new_thread->state & TH_SW_COMING_IN) ||
  697. !stack_alloc_try(new_thread, thread_continue))
  698. {
  699. thread_swapin(new_thread);
  700. thread_unlock(new_thread);
  701. counter(c_thread_invoke_misses++);
  702. return FALSE;
  703. }
  704. }
  705. }
  706. new_thread->state &= ~(TH_SWAPPED | TH_UNINT);
  707. thread_unlock(new_thread);
  708. thread_wakeup(TH_EV_STATE(new_thread));
  709. /*
  710. * Thread is now interruptible.
  711. */
  712. #if NCPUS > 1
  713. new_thread->last_processor = current_processor();
  714. #endif /* NCPUS > 1 */
  715. /*
  716. * Set up ast context of new thread and switch to its timer.
  717. */
  718. ast_context(new_thread, cpu_number());
  719. timer_switch(&new_thread->system_timer);
  720. /*
  721. * switch_context is machine-dependent. It does the
  722. * machine-dependent components of a context-switch, like
  723. * changing address spaces. It updates active_threads.
  724. * It returns only if a continuation is not supplied.
  725. */
  726. counter(c_thread_invoke_csw++);
  727. old_thread = switch_context(old_thread, continuation, new_thread);
  728. /*
  729. * We're back. Now old_thread is the thread that resumed
  730. * us, and we have to dispatch it.
  731. */
  732. thread_dispatch(old_thread);
  733. return TRUE;
  734. }
  735. /*
  736. * thread_continue:
  737. *
  738. * Called when the current thread is given a new stack.
  739. * Called at splsched.
  740. */
  741. void thread_continue(
  742. thread_t old_thread)
  743. {
  744. continuation_t continuation = current_thread()->swap_func;
  745. /*
  746. * We must dispatch the old thread and then
  747. * call the current thread's continuation.
  748. * There might not be an old thread, if we are
  749. * the first thread to run on this processor.
  750. */
  751. if (old_thread != THREAD_NULL)
  752. thread_dispatch(old_thread);
  753. (void) spl0();
  754. (*continuation)();
  755. /*NOTREACHED*/
  756. }
  757. /*
  758. * thread_block:
  759. *
  760. * Block the current thread. If the thread is runnable
  761. * then someone must have woken it up between its request
  762. * to sleep and now. In this case, it goes back on a
  763. * run queue.
  764. *
  765. * If a continuation is specified, then thread_block will
  766. * attempt to discard the thread's kernel stack. When the
  767. * thread resumes, it will execute the continuation function
  768. * on a new kernel stack.
  769. */
  770. void thread_block(
  771. continuation_t continuation)
  772. {
  773. thread_t thread = current_thread();
  774. processor_t myprocessor = cpu_to_processor(cpu_number());
  775. thread_t new_thread;
  776. spl_t s;
  777. check_simple_locks();
  778. s = splsched();
  779. #if FAST_TAS
  780. {
  781. extern void recover_ras();
  782. if (csw_needed(thread, myprocessor))
  783. recover_ras(thread);
  784. }
  785. #endif /* FAST_TAS */
  786. ast_off(cpu_number(), AST_BLOCK);
  787. do
  788. new_thread = thread_select(myprocessor);
  789. while (!thread_invoke(thread, continuation, new_thread));
  790. splx(s);
  791. }
  792. /*
  793. * thread_run:
  794. *
  795. * Switch directly from the current thread to a specified
  796. * thread. Both the current and new threads must be
  797. * runnable.
  798. *
  799. * If a continuation is specified, then thread_block will
  800. * attempt to discard the current thread's kernel stack. When the
  801. * thread resumes, it will execute the continuation function
  802. * on a new kernel stack.
  803. */
  804. void thread_run(
  805. continuation_t continuation,
  806. thread_t new_thread)
  807. {
  808. thread_t thread = current_thread();
  809. processor_t myprocessor = cpu_to_processor(cpu_number());
  810. spl_t s;
  811. check_simple_locks();
  812. s = splsched();
  813. while (!thread_invoke(thread, continuation, new_thread))
  814. new_thread = thread_select(myprocessor);
  815. splx(s);
  816. }
  817. /*
  818. * Dispatches a running thread that is not on a runq.
  819. * Called at splsched.
  820. */
  821. void thread_dispatch(
  822. thread_t thread)
  823. {
  824. /*
  825. * If we are discarding the thread's stack, we must do it
  826. * before the thread has a chance to run.
  827. */
  828. thread_lock(thread);
  829. if (thread->swap_func != thread_no_continuation) {
  830. assert((thread->state & TH_SWAP_STATE) == 0);
  831. thread->state |= TH_SWAPPED;
  832. stack_free(thread);
  833. }
  834. switch (thread->state &~ TH_SWAP_STATE) {
  835. case TH_RUN | TH_SUSP:
  836. case TH_RUN | TH_SUSP | TH_HALTED:
  837. case TH_RUN | TH_WAIT | TH_SUSP:
  838. /*
  839. * Suspend the thread
  840. */
  841. thread->state &= ~TH_RUN;
  842. if (thread->wake_active) {
  843. thread->wake_active = FALSE;
  844. thread_unlock(thread);
  845. thread_wakeup(TH_EV_WAKE_ACTIVE(thread));
  846. return;
  847. }
  848. break;
  849. case TH_RUN | TH_SUSP | TH_UNINT:
  850. case TH_RUN | TH_UNINT:
  851. case TH_RUN:
  852. /*
  853. * No reason to stop. Put back on a run queue.
  854. */
  855. thread_setrun(thread, FALSE);
  856. break;
  857. case TH_RUN | TH_WAIT | TH_SUSP | TH_UNINT:
  858. case TH_RUN | TH_WAIT | TH_UNINT:
  859. case TH_RUN | TH_WAIT:
  860. /*
  861. * Waiting, and not suspended.
  862. */
  863. thread->state &= ~TH_RUN;
  864. break;
  865. case TH_RUN | TH_IDLE:
  866. /*
  867. * Drop idle thread -- it is already in
  868. * idle_thread_array.
  869. */
  870. break;
  871. default:
  872. state_panic(thread);
  873. }
  874. thread_unlock(thread);
  875. }
  876. /*
  877. * Define shifts for simulating (5/8)**n
  878. */
  879. shift_data_t wait_shift[32] = {
  880. {1,1},{1,3},{1,-3},{2,-7},{3,5},{3,-5},{4,-8},{5,7},
  881. {5,-7},{6,-10},{7,10},{7,-9},{8,-11},{9,12},{9,-11},{10,-13},
  882. {11,14},{11,-13},{12,-15},{13,17},{13,-15},{14,-17},{15,19},{16,18},
  883. {16,-19},{17,22},{18,20},{18,-20},{19,26},{20,22},{20,-22},{21,-27}};
  884. /*
  885. * do_priority_computation:
  886. *
  887. * Calculate new priority for thread based on its base priority plus
  888. * accumulated usage. PRI_SHIFT and PRI_SHIFT_2 convert from
  889. * usage to priorities. SCHED_SHIFT converts for the scaling
  890. * of the sched_usage field by SCHED_SCALE. This scaling comes
  891. * from the multiplication by sched_load (thread_timer_delta)
  892. * in sched.h. sched_load is calculated as a scaled overload
  893. * factor in compute_mach_factor (mach_factor.c).
  894. */
  895. #ifdef PRI_SHIFT_2
  896. #if PRI_SHIFT_2 > 0
  897. #define do_priority_computation(th, pri) \
  898. MACRO_BEGIN \
  899. (pri) = (th)->priority /* start with base priority */ \
  900. + ((th)->sched_usage >> (PRI_SHIFT + SCHED_SHIFT)) \
  901. + ((th)->sched_usage >> (PRI_SHIFT_2 + SCHED_SHIFT)); \
  902. if ((pri) > NRQS - 1) (pri) = NRQS - 1; \
  903. MACRO_END
  904. #else /* PRI_SHIFT_2 */
  905. #define do_priority_computation(th, pri) \
  906. MACRO_BEGIN \
  907. (pri) = (th)->priority /* start with base priority */ \
  908. + ((th)->sched_usage >> (PRI_SHIFT + SCHED_SHIFT)) \
  909. - ((th)->sched_usage >> (SCHED_SHIFT - PRI_SHIFT_2)); \
  910. if ((pri) > NRQS - 1) (pri) = NRQS - 1; \
  911. MACRO_END
  912. #endif /* PRI_SHIFT_2 */
  913. #else /* defined(PRI_SHIFT_2) */
  914. #define do_priority_computation(th, pri) \
  915. MACRO_BEGIN \
  916. (pri) = (th)->priority /* start with base priority */ \
  917. + ((th)->sched_usage >> (PRI_SHIFT + SCHED_SHIFT)); \
  918. if ((pri) > NRQS - 1) (pri) = NRQS - 1; \
  919. MACRO_END
  920. #endif /* defined(PRI_SHIFT_2) */
  921. /*
  922. * compute_priority:
  923. *
  924. * Compute the effective priority of the specified thread.
  925. * The effective priority computation is as follows:
  926. *
  927. * Take the base priority for this thread and add
  928. * to it an increment derived from its cpu_usage.
  929. *
  930. * The thread *must* be locked by the caller.
  931. */
  932. void compute_priority(
  933. thread_t thread,
  934. boolean_t resched)
  935. {
  936. int pri;
  937. #if MACH_FIXPRI
  938. if (thread->policy == POLICY_TIMESHARE) {
  939. #endif /* MACH_FIXPRI */
  940. do_priority_computation(thread, pri);
  941. if (thread->depress_priority < 0)
  942. set_pri(thread, pri, resched);
  943. else
  944. thread->depress_priority = pri;
  945. #if MACH_FIXPRI
  946. }
  947. else {
  948. set_pri(thread, thread->priority, resched);
  949. }
  950. #endif /* MACH_FIXPRI */
  951. }
  952. /*
  953. * compute_my_priority:
  954. *
  955. * Version of compute priority for current thread or thread
  956. * being manipulated by scheduler (going on or off a runq).
  957. * Only used for priority updates. Policy or priority changes
  958. * must call compute_priority above. Caller must have thread
  959. * locked and know it is timesharing and not depressed.
  960. */
  961. void compute_my_priority(
  962. thread_t thread)
  963. {
  964. int temp_pri;
  965. do_priority_computation(thread,temp_pri);
  966. thread->sched_pri = temp_pri;
  967. }
  968. /*
  969. * recompute_priorities:
  970. *
  971. * Update the priorities of all threads periodically.
  972. */
  973. void recompute_priorities(void *param)
  974. {
  975. #if SIMPLE_CLOCK
  976. int new_usec;
  977. #endif /* SIMPLE_CLOCK */
  978. sched_tick++; /* age usage one more time */
  979. set_timeout(&recompute_priorities_timer, hz);
  980. #if SIMPLE_CLOCK
  981. /*
  982. * Compensate for clock drift. sched_usec is an
  983. * exponential average of the number of microseconds in
  984. * a second. It decays in the same fashion as cpu_usage.
  985. */
  986. new_usec = sched_usec_elapsed();
  987. sched_usec = (5*sched_usec + 3*new_usec)/8;
  988. #endif /* SIMPLE_CLOCK */
  989. /*
  990. * Wakeup scheduler thread.
  991. */
  992. if (sched_thread_id != THREAD_NULL) {
  993. clear_wait(sched_thread_id, THREAD_AWAKENED, FALSE);
  994. }
  995. }
  996. /*
  997. * update_priority
  998. *
  999. * Cause the priority computation of a thread that has been
  1000. * sleeping or suspended to "catch up" with the system. Thread
  1001. * *MUST* be locked by caller. If thread is running, then this
  1002. * can only be called by the thread on itself.
  1003. */
  1004. void update_priority(
  1005. thread_t thread)
  1006. {
  1007. unsigned int ticks;
  1008. shift_t shiftp;
  1009. int temp_pri;
  1010. ticks = sched_tick - thread->sched_stamp;
  1011. assert(ticks != 0);
  1012. /*
  1013. * If asleep for more than 30 seconds forget all
  1014. * cpu_usage, else catch up on missed aging.
  1015. * 5/8 ** n is approximated by the two shifts
  1016. * in the wait_shift array.
  1017. */
  1018. thread->sched_stamp += ticks;
  1019. thread_timer_delta(thread);
  1020. if (ticks > 30) {
  1021. thread->cpu_usage = 0;
  1022. thread->sched_usage = 0;
  1023. }
  1024. else {
  1025. thread->cpu_usage += thread->cpu_delta;
  1026. thread->sched_usage += thread->sched_delta;
  1027. shiftp = &wait_shift[ticks];
  1028. if (shiftp->shift2 > 0) {
  1029. thread->cpu_usage =
  1030. (thread->cpu_usage >> shiftp->shift1) +
  1031. (thread->cpu_usage >> shiftp->shift2);
  1032. thread->sched_usage =
  1033. (thread->sched_usage >> shiftp->shift1) +
  1034. (thread->sched_usage >> shiftp->shift2);
  1035. }
  1036. else {
  1037. thread->cpu_usage =
  1038. (thread->cpu_usage >> shiftp->shift1) -
  1039. (thread->cpu_usage >> -(shiftp->shift2));
  1040. thread->sched_usage =
  1041. (thread->sched_usage >> shiftp->shift1) -
  1042. (thread->sched_usage >> -(shiftp->shift2));
  1043. }
  1044. }
  1045. thread->cpu_delta = 0;
  1046. thread->sched_delta = 0;
  1047. /*
  1048. * Recompute priority if appropriate.
  1049. */
  1050. if (
  1051. #if MACH_FIXPRI
  1052. (thread->policy == POLICY_TIMESHARE) &&
  1053. #endif /* MACH_FIXPRI */
  1054. (thread->depress_priority < 0)) {
  1055. do_priority_computation(thread, temp_pri);
  1056. thread->sched_pri = temp_pri;
  1057. }
  1058. }
  1059. /*
  1060. * run_queue_enqueue macro for thread_setrun().
  1061. */
  1062. #if DEBUG
  1063. #define run_queue_enqueue(rq, th) \
  1064. MACRO_BEGIN \
  1065. unsigned int whichq; \
  1066. \
  1067. whichq = (th)->sched_pri; \
  1068. if (whichq >= NRQS) { \
  1069. printf("thread_setrun: pri too high (%d)\n", (th)->sched_pri); \
  1070. whichq = NRQS - 1; \
  1071. } \
  1072. \
  1073. simple_lock(&(rq)->lock); /* lock the run queue */ \
  1074. checkrq((rq), "thread_setrun: before adding thread"); \
  1075. enqueue_tail(&(rq)->runq[whichq], &((th)->links)); \
  1076. \
  1077. if (whichq < (rq)->low || (rq)->count == 0) \
  1078. (rq)->low = whichq; /* minimize */ \
  1079. \
  1080. (rq)->count++; \
  1081. (th)->runq = (rq); \
  1082. thread_check((th), (rq)); \
  1083. checkrq((rq), "thread_setrun: after adding thread"); \
  1084. simple_unlock(&(rq)->lock); \
  1085. MACRO_END
  1086. #else /* DEBUG */
  1087. #define run_queue_enqueue(rq, th) \
  1088. MACRO_BEGIN \
  1089. unsigned int whichq; \
  1090. \
  1091. whichq = (th)->sched_pri; \
  1092. if (whichq >= NRQS) { \
  1093. printf("thread_setrun: pri too high (%d)\n", (th)->sched_pri); \
  1094. whichq = NRQS - 1; \
  1095. } \
  1096. \
  1097. simple_lock(&(rq)->lock); /* lock the run queue */ \
  1098. enqueue_tail(&(rq)->runq[whichq], &((th)->links)); \
  1099. \
  1100. if (whichq < (rq)->low || (rq)->count == 0) \
  1101. (rq)->low = whichq; /* minimize */ \
  1102. \
  1103. (rq)->count++; \
  1104. (th)->runq = (rq); \
  1105. simple_unlock(&(rq)->lock); \
  1106. MACRO_END
  1107. #endif /* DEBUG */
  1108. /*
  1109. * thread_setrun:
  1110. *
  1111. * Make thread runnable; dispatch directly onto an idle processor
  1112. * if possible. Else put on appropriate run queue (processor
  1113. * if bound, else processor set. Caller must have lock on thread.
  1114. * This is always called at splsched.
  1115. */
  1116. void thread_setrun(
  1117. thread_t th,
  1118. boolean_t may_preempt)
  1119. {
  1120. processor_t processor;
  1121. run_queue_t rq;
  1122. #if NCPUS > 1
  1123. processor_set_t pset;
  1124. #endif /* NCPUS > 1 */
  1125. /*
  1126. * Update priority if needed.
  1127. */
  1128. if (th->sched_stamp != sched_tick) {
  1129. update_priority(th);
  1130. }
  1131. assert(th->runq == RUN_QUEUE_NULL);
  1132. #if NCPUS > 1
  1133. /*
  1134. * Try to dispatch the thread directly onto an idle processor.
  1135. */
  1136. if ((processor = th->bound_processor) == PROCESSOR_NULL) {
  1137. /*
  1138. * Not bound, any processor in the processor set is ok.
  1139. */
  1140. pset = th->processor_set;
  1141. #if HW_FOOTPRINT
  1142. /*
  1143. * But first check the last processor it ran on.
  1144. */
  1145. processor = th->last_processor;
  1146. if (processor->state == PROCESSOR_IDLE) {
  1147. simple_lock(&processor->lock);
  1148. simple_lock(&pset->idle_lock);
  1149. if ((processor->state == PROCESSOR_IDLE)
  1150. #if MACH_HOST
  1151. && (processor->processor_set == pset)
  1152. #endif /* MACH_HOST */
  1153. ) {
  1154. queue_remove(&pset->idle_queue, processor,
  1155. processor_t, processor_queue);
  1156. pset->idle_count--;
  1157. processor->next_thread = th;
  1158. processor->state = PROCESSOR_DISPATCHING;
  1159. simple_unlock(&pset->idle_lock);
  1160. simple_unlock(&processor->lock);
  1161. return;
  1162. }
  1163. simple_unlock(&pset->idle_lock);
  1164. simple_unlock(&processor->lock);
  1165. }
  1166. #endif /* HW_FOOTPRINT */
  1167. if (pset->idle_count > 0) {
  1168. simple_lock(&pset->idle_lock);
  1169. if (pset->idle_count > 0) {
  1170. processor = (processor_t) queue_first(&pset->idle_queue);
  1171. queue_remove(&(pset->idle_queue), processor, processor_t,
  1172. processor_queue);
  1173. pset->idle_count--;
  1174. processor->next_thread = th;
  1175. processor->state = PROCESSOR_DISPATCHING;
  1176. simple_unlock(&pset->idle_lock);
  1177. return;
  1178. }
  1179. simple_unlock(&pset->idle_lock);
  1180. }
  1181. rq = &(pset->runq);
  1182. run_queue_enqueue(rq,th);
  1183. /*
  1184. * Preempt check
  1185. */
  1186. if (may_preempt &&
  1187. #if MACH_HOST
  1188. (pset == current_processor()->processor_set) &&
  1189. #endif /* MACH_HOST */
  1190. (current_thread()->sched_pri > th->sched_pri)) {
  1191. /*
  1192. * Turn off first_quantum to allow csw.
  1193. */
  1194. current_processor()->first_quantum = FALSE;
  1195. ast_on(cpu_number(), AST_BLOCK);
  1196. }
  1197. }
  1198. else {
  1199. /*
  1200. * Bound, can only run on bound processor. Have to lock
  1201. * processor here because it may not be the current one.
  1202. */
  1203. if (processor->state == PROCESSOR_IDLE) {
  1204. simple_lock(&processor->lock);
  1205. pset = processor->processor_set;
  1206. simple_lock(&pset->idle_lock);
  1207. if (processor->state == PROCESSOR_IDLE) {
  1208. queue_remove(&pset->idle_queue, processor,
  1209. processor_t, processor_queue);
  1210. pset->idle_count--;
  1211. processor->next_thread = th;
  1212. processor->state = PROCESSOR_DISPATCHING;
  1213. simple_unlock(&pset->idle_lock);
  1214. simple_unlock(&processor->lock);
  1215. return;
  1216. }
  1217. simple_unlock(&pset->idle_lock);
  1218. simple_unlock(&processor->lock);
  1219. }
  1220. rq = &(processor->runq);
  1221. run_queue_enqueue(rq,th);
  1222. /*
  1223. * Cause ast on processor if processor is on line.
  1224. *
  1225. * XXX Don't do this remotely to master because this will
  1226. * XXX send an interprocessor interrupt, and that's too
  1227. * XXX expensive for all the unparallelized U*x code.
  1228. */
  1229. if (processor == current_processor()) {
  1230. ast_on(cpu_number(), AST_BLOCK);
  1231. }
  1232. else if ((processor != master_processor) &&
  1233. (processor->state != PROCESSOR_OFF_LINE)) {
  1234. cause_ast_check(processor);
  1235. }
  1236. }
  1237. #else /* NCPUS > 1 */
  1238. /*
  1239. * XXX should replace queue with a boolean in this case.
  1240. */
  1241. if (default_pset.idle_count > 0) {
  1242. processor = (processor_t) queue_first(&default_pset.idle_queue);
  1243. queue_remove(&default_pset.idle_queue, processor,
  1244. processor_t, processor_queue);
  1245. default_pset.idle_count--;
  1246. processor->next_thread = th;
  1247. processor->state = PROCESSOR_DISPATCHING;
  1248. return;
  1249. }
  1250. if (th->bound_processor == PROCESSOR_NULL) {
  1251. rq = &(default_pset.runq);
  1252. }
  1253. else {
  1254. rq = &(master_processor->runq);
  1255. ast_on(cpu_number(), AST_BLOCK);
  1256. }
  1257. run_queue_enqueue(rq,th);
  1258. /*
  1259. * Preempt check
  1260. */
  1261. if (may_preempt && (current_thread()->sched_pri > th->sched_pri)) {
  1262. /*
  1263. * Turn off first_quantum to allow context switch.
  1264. */
  1265. current_processor()->first_quantum = FALSE;
  1266. ast_on(cpu_number(), AST_BLOCK);
  1267. }
  1268. #endif /* NCPUS > 1 */
  1269. }
  1270. /*
  1271. * set_pri:
  1272. *
  1273. * Set the priority of the specified thread to the specified
  1274. * priority. This may cause the thread to change queues.
  1275. *
  1276. * The thread *must* be locked by the caller.
  1277. */
  1278. void set_pri(
  1279. thread_t th,
  1280. int pri,
  1281. boolean_t resched)
  1282. {
  1283. struct run_queue *rq;
  1284. rq = rem_runq(th);
  1285. th->sched_pri = pri;
  1286. if (rq != RUN_QUEUE_NULL) {
  1287. if (resched)
  1288. thread_setrun(th, TRUE);
  1289. else
  1290. run_queue_enqueue(rq, th);
  1291. }
  1292. }
  1293. /*
  1294. * rem_runq:
  1295. *
  1296. * Remove a thread from its run queue.
  1297. * The run queue that the process was on is returned
  1298. * (or RUN_QUEUE_NULL if not on a run queue). Thread *must* be locked
  1299. * before calling this routine. Unusual locking protocol on runq
  1300. * field in thread structure makes this code interesting; see thread.h.
  1301. */
  1302. struct run_queue *rem_runq(
  1303. thread_t th)
  1304. {
  1305. struct run_queue *rq;
  1306. rq = th->runq;
  1307. /*
  1308. * If rq is RUN_QUEUE_NULL, the thread will stay out of the
  1309. * run_queues because the caller locked the thread. Otherwise
  1310. * the thread is on a runq, but could leave.
  1311. */
  1312. if (rq != RUN_QUEUE_NULL) {
  1313. simple_lock(&rq->lock);
  1314. #if DEBUG
  1315. checkrq(rq, "rem_runq: at entry");
  1316. #endif /* DEBUG */
  1317. if (rq == th->runq) {
  1318. /*
  1319. * Thread is in a runq and we have a lock on
  1320. * that runq.
  1321. */
  1322. #if DEBUG
  1323. checkrq(rq, "rem_runq: before removing thread");
  1324. thread_check(th, rq);
  1325. #endif /* DEBUG */
  1326. remqueue(&rq->runq[0], (queue_entry_t) th);
  1327. rq->count--;
  1328. #if DEBUG
  1329. checkrq(rq, "rem_runq: after removing thread");
  1330. #endif /* DEBUG */
  1331. th->runq = RUN_QUEUE_NULL;
  1332. simple_unlock(&rq->lock);
  1333. }
  1334. else {
  1335. /*
  1336. * The thread left the runq before we could
  1337. * lock the runq. It is not on a runq now, and
  1338. * can't move again because this routine's
  1339. * caller locked the thread.
  1340. */
  1341. simple_unlock(&rq->lock);
  1342. rq = RUN_QUEUE_NULL;
  1343. }
  1344. }
  1345. return rq;
  1346. }
  1347. /*
  1348. * choose_thread:
  1349. *
  1350. * Choose a thread to execute. The thread chosen is removed
  1351. * from its run queue. Note that this requires only that the runq
  1352. * lock be held.
  1353. *
  1354. * Strategy:
  1355. * Check processor runq first; if anything found, run it.
  1356. * Else check pset runq; if nothing found, return idle thread.
  1357. *
  1358. * Second line of strategy is implemented by choose_pset_thread.
  1359. * This is only called on processor startup and when thread_block
  1360. * thinks there's something in the processor runq.
  1361. */
  1362. thread_t choose_thread(
  1363. processor_t myprocessor)
  1364. {
  1365. thread_t th;
  1366. queue_t q;
  1367. run_queue_t runq;
  1368. int i;
  1369. processor_set_t pset;
  1370. runq = &myprocessor->runq;
  1371. simple_lock(&runq->lock);
  1372. if (runq->count > 0) {
  1373. q = runq->runq + runq->low;
  1374. for (i = runq->low; i < NRQS ; i++, q++) {
  1375. if (!queue_empty(q)) {
  1376. th = (thread_t) dequeue_head(q);
  1377. th->runq = RUN_QUEUE_NULL;
  1378. runq->count--;
  1379. runq->low = i;
  1380. simple_unlock(&runq->lock);
  1381. return th;
  1382. }
  1383. }
  1384. panic("choose_thread");
  1385. /*NOTREACHED*/
  1386. }
  1387. simple_unlock(&runq->lock);
  1388. pset = myprocessor->processor_set;
  1389. simple_lock(&pset->runq.lock);
  1390. return choose_pset_thread(myprocessor,pset);
  1391. }
  1392. /*
  1393. * choose_pset_thread: choose a thread from processor_set runq or
  1394. * set processor idle and choose its idle thread.
  1395. *
  1396. * Caller must be at splsched and have a lock on the runq. This
  1397. * lock is released by this routine. myprocessor is always the current
  1398. * processor, and pset must be its processor set.
  1399. * This routine chooses and removes a thread from the runq if there
  1400. * is one (and returns it), else it sets the processor idle and
  1401. * returns its idle thread.
  1402. */
  1403. thread_t choose_pset_thread(
  1404. processor_t myprocessor,
  1405. processor_set_t pset)
  1406. {
  1407. run_queue_t runq;
  1408. thread_t th;
  1409. queue_t q;
  1410. int i;
  1411. runq = &pset->runq;
  1412. if (runq->count > 0) {
  1413. q = runq->runq + runq->low;
  1414. for (i = runq->low; i < NRQS ; i++, q++) {
  1415. if (!queue_empty(q)) {
  1416. th = (thread_t) dequeue_head(q);
  1417. th->runq = RUN_QUEUE_NULL;
  1418. runq->count--;
  1419. /*
  1420. * For POLICY_FIXEDPRI, runq->low must be
  1421. * accurate!
  1422. */
  1423. #if MACH_FIXPRI
  1424. if ((runq->count > 0) &&
  1425. (pset->policies & POLICY_FIXEDPRI)) {
  1426. while (queue_empty(q)) {
  1427. q++;
  1428. i++;
  1429. }
  1430. }
  1431. #endif /* MACH_FIXPRI */
  1432. runq->low = i;
  1433. #if DEBUG
  1434. checkrq(runq, "choose_pset_thread");
  1435. #endif /* DEBUG */
  1436. simple_unlock(&runq->lock);
  1437. return th;
  1438. }
  1439. }
  1440. panic("choose_pset_thread");
  1441. /*NOTREACHED*/
  1442. }
  1443. simple_unlock(&runq->lock);
  1444. /*
  1445. * Nothing is runnable, so set this processor idle if it
  1446. * was running. If it was in an assignment or shutdown,
  1447. * leave it alone. Return its idle thread.
  1448. */
  1449. simple_lock(&pset->idle_lock);
  1450. if (myprocessor->state == PROCESSOR_RUNNING) {
  1451. myprocessor->state = PROCESSOR_IDLE;
  1452. /*
  1453. * XXX Until it goes away, put master on end of queue, others
  1454. * XXX on front so master gets used last.
  1455. */
  1456. if (myprocessor == master_processor) {
  1457. queue_enter(&(pset->idle_queue), myprocessor,
  1458. processor_t, processor_queue);
  1459. }
  1460. else {
  1461. queue_enter_first(&(pset->idle_queue), myprocessor,
  1462. processor_t, processor_queue);
  1463. }
  1464. pset->idle_count++;
  1465. }
  1466. simple_unlock(&pset->idle_lock);
  1467. return myprocessor->idle_thread;
  1468. }
  1469. /*
  1470. * no_dispatch_count counts number of times processors go non-idle
  1471. * without being dispatched. This should be very rare.
  1472. */
  1473. int no_dispatch_count = 0;
  1474. /*
  1475. * This is the idle thread, which just looks for other threads
  1476. * to execute.
  1477. */
  1478. void __attribute__((noreturn)) idle_thread_continue(void)
  1479. {
  1480. processor_t myprocessor;
  1481. volatile thread_t *threadp;
  1482. volatile int *gcount;
  1483. volatile int *lcount;
  1484. thread_t new_thread;
  1485. int state;
  1486. int mycpu;
  1487. spl_t s;
  1488. mycpu = cpu_number();
  1489. myprocessor = current_processor();
  1490. threadp = (volatile thread_t *) &myprocessor->next_thread;
  1491. lcount = (volatile int *) &myprocessor->runq.count;
  1492. while (TRUE) {
  1493. #ifdef MARK_CPU_IDLE
  1494. MARK_CPU_IDLE(mycpu);
  1495. #endif /* MARK_CPU_IDLE */
  1496. #if MACH_HOST
  1497. gcount = (volatile int *)
  1498. &myprocessor->processor_set->runq.count;
  1499. #else /* MACH_HOST */
  1500. gcount = (volatile int *) &default_pset.runq.count;
  1501. #endif /* MACH_HOST */
  1502. /*
  1503. * This cpu will be dispatched (by thread_setrun) by setting next_thread
  1504. * to the value of the thread to run next. Also check runq counts.
  1505. */
  1506. while ((*threadp == (volatile thread_t)THREAD_NULL) &&
  1507. (*gcount == 0) && (*lcount == 0)) {
  1508. /* check for ASTs while we wait */
  1509. if (need_ast[mycpu] &~ AST_SCHEDULING) {
  1510. (void) splsched();
  1511. /* don't allow scheduling ASTs */
  1512. need_ast[mycpu] &= ~AST_SCHEDULING;
  1513. ast_taken();
  1514. /* back at spl0 */
  1515. }
  1516. /*
  1517. * machine_idle is a machine dependent function,
  1518. * to conserve power.
  1519. */
  1520. #if POWER_SAVE
  1521. machine_idle(mycpu);
  1522. #endif /* POWER_SAVE */
  1523. }
  1524. #ifdef MARK_CPU_ACTIVE
  1525. MARK_CPU_ACTIVE(mycpu);
  1526. #endif /* MARK_CPU_ACTIVE */
  1527. s = splsched();
  1528. /*
  1529. * This is not a switch statement to avoid the
  1530. * bounds checking code in the common case.
  1531. */
  1532. retry:
  1533. state = myprocessor->state;
  1534. if (state == PROCESSOR_DISPATCHING) {
  1535. /*
  1536. * Commmon case -- cpu dispatched.
  1537. */
  1538. new_thread = (thread_t) *threadp;
  1539. *threadp = (volatile thread_t) THREAD_NULL;
  1540. myprocessor->state = PROCESSOR_RUNNING;
  1541. /*
  1542. * set up quantum for new thread.
  1543. */
  1544. #if MACH_FIXPRI
  1545. if (new_thread->policy == POLICY_TIMESHARE) {
  1546. #endif /* MACH_FIXPRI */
  1547. /*
  1548. * Just use set quantum. No point in
  1549. * checking for shorter local runq quantum;
  1550. * csw_needed will handle correctly.
  1551. */
  1552. #if MACH_HOST
  1553. myprocessor->quantum = new_thread->
  1554. processor_set->set_quantum;
  1555. #else /* MACH_HOST */
  1556. myprocessor->quantum =
  1557. default_pset.set_quantum;
  1558. #endif /* MACH_HOST */
  1559. #if MACH_FIXPRI
  1560. }
  1561. else {
  1562. /*
  1563. * POLICY_FIXEDPRI
  1564. */
  1565. myprocessor->quantum = new_thread->sched_data;
  1566. }
  1567. #endif /* MACH_FIXPRI */
  1568. myprocessor->first_quantum = TRUE;
  1569. counter(c_idle_thread_handoff++);
  1570. thread_run(idle_thread_continue, new_thread);
  1571. }
  1572. else if (state == PROCESSOR_IDLE) {
  1573. processor_set_t pset;
  1574. pset = myprocessor->processor_set;
  1575. simple_lock(&pset->idle_lock);
  1576. if (myprocessor->state != PROCESSOR_IDLE) {
  1577. /*
  1578. * Something happened, try again.
  1579. */
  1580. simple_unlock(&pset->idle_lock);
  1581. goto retry;
  1582. }
  1583. /*
  1584. * Processor was not dispatched (Rare).
  1585. * Set it running again.
  1586. */
  1587. no_dispatch_count++;
  1588. pset->idle_count--;
  1589. queue_remove(&pset->idle_queue, myprocessor,
  1590. processor_t, processor_queue);
  1591. myprocessor->state = PROCESSOR_RUNNING;
  1592. simple_unlock(&pset->idle_lock);
  1593. counter(c_idle_thread_block++);
  1594. thread_block(idle_thread_continue);
  1595. }
  1596. else if ((state == PROCESSOR_ASSIGN) ||
  1597. (state == PROCESSOR_SHUTDOWN)) {
  1598. /*
  1599. * Changing processor sets, or going off-line.
  1600. * Release next_thread if there is one. Actual
  1601. * thread to run is on a runq.
  1602. */
  1603. if ((new_thread = (thread_t)*threadp)!= THREAD_NULL) {
  1604. *threadp = (volatile thread_t) THREAD_NULL;
  1605. thread_setrun(new_thread, FALSE);
  1606. }
  1607. counter(c_idle_thread_block++);
  1608. thread_block(idle_thread_continue);
  1609. }
  1610. else {
  1611. printf(" Bad processor state %d (Cpu %d)\n",
  1612. cpu_state(mycpu), mycpu);
  1613. panic("idle_thread");
  1614. }
  1615. (void) splx(s);
  1616. }
  1617. }
  1618. void idle_thread(void)
  1619. {
  1620. thread_t self = current_thread();
  1621. spl_t s;
  1622. stack_privilege(self);
  1623. s = splsched();
  1624. self->priority = NRQS-1;
  1625. self->sched_pri = NRQS-1;
  1626. /*
  1627. * Set the idle flag to indicate that this is an idle thread,
  1628. * enter ourselves in the idle array, and thread_block() to get
  1629. * out of the run queues (and set the processor idle when we
  1630. * run next time).
  1631. */
  1632. thread_lock(self);
  1633. self->state |= TH_IDLE;
  1634. thread_unlock(self);
  1635. current_processor()->idle_thread = self;
  1636. (void) splx(s);
  1637. counter(c_idle_thread_block++);
  1638. thread_block(idle_thread_continue);
  1639. idle_thread_continue();
  1640. /*NOTREACHED*/
  1641. }
  1642. /*
  1643. * sched_thread: scheduler thread.
  1644. *
  1645. * This thread handles periodic calculations in the scheduler that
  1646. * we don't want to do at interrupt level. This allows us to
  1647. * avoid blocking.
  1648. */
  1649. void sched_thread_continue(void)
  1650. {
  1651. while (TRUE) {
  1652. (void) compute_mach_factor();
  1653. /*
  1654. * Check for stuck threads. This can't be done off of
  1655. * the callout queue because it requires operations that
  1656. * can't be used from interrupt level.
  1657. */
  1658. if (sched_tick & 1)
  1659. do_thread_scan();
  1660. assert_wait((event_t) 0, FALSE);
  1661. counter(c_sched_thread_block++);
  1662. thread_block(sched_thread_continue);
  1663. }
  1664. }
  1665. void sched_thread(void)
  1666. {
  1667. sched_thread_id = current_thread();
  1668. /*
  1669. * Sleep on event 0, recompute_priorities() will awaken
  1670. * us by calling clear_wait().
  1671. */
  1672. assert_wait((event_t) 0, FALSE);
  1673. counter(c_sched_thread_block++);
  1674. thread_block(sched_thread_continue);
  1675. sched_thread_continue();
  1676. /*NOTREACHED*/
  1677. }
  1678. #define MAX_STUCK_THREADS 16
  1679. /*
  1680. * do_thread_scan: scan for stuck threads. A thread is stuck if
  1681. * it is runnable but its priority is so low that it has not
  1682. * run for several seconds. Its priority should be higher, but
  1683. * won't be until it runs and calls update_priority. The scanner
  1684. * finds these threads and does the updates.
  1685. *
  1686. * Scanner runs in two passes. Pass one squirrels likely
  1687. * thread ids away in an array, and removes them from the run queue.
  1688. * Pass two does the priority updates. This is necessary because
  1689. * the run queue lock is required for the candidate scan, but
  1690. * cannot be held during updates [set_pri will deadlock].
  1691. *
  1692. * Array length should be enough so that restart isn't necessary,
  1693. * but restart logic is included. Does not scan processor runqs.
  1694. *
  1695. */
  1696. boolean_t do_thread_scan_debug = FALSE;
  1697. thread_t stuck_threads[MAX_STUCK_THREADS];
  1698. int stuck_count = 0;
  1699. /*
  1700. * do_runq_scan is the guts of pass 1. It scans a runq for
  1701. * stuck threads. A boolean is returned indicating whether
  1702. * it ran out of space.
  1703. */
  1704. boolean_t
  1705. do_runq_scan(
  1706. run_queue_t runq)
  1707. {
  1708. spl_t s;
  1709. queue_t q;
  1710. thread_t thread;
  1711. int count;
  1712. s = splsched();
  1713. simple_lock(&runq->lock);
  1714. if((count = runq->count) > 0) {
  1715. q = runq->runq + runq->low;
  1716. while (count > 0) {
  1717. thread = (thread_t) queue_first(q);
  1718. while (!queue_end(q, (queue_entry_t) thread)) {
  1719. /*
  1720. * Get the next thread now, since we may
  1721. * remove this thread from the run queue.
  1722. */
  1723. thread_t next = (thread_t) queue_next(&thread->links);
  1724. if ((thread->state & TH_SCHED_STATE) == TH_RUN &&
  1725. sched_tick - thread->sched_stamp > 1) {
  1726. /*
  1727. * Stuck, save its id for later.
  1728. */
  1729. if (stuck_count == MAX_STUCK_THREADS) {
  1730. /*
  1731. * !@#$% No more room.
  1732. */
  1733. simple_unlock(&runq->lock);
  1734. splx(s);
  1735. return TRUE;
  1736. }
  1737. /*
  1738. * We can`t take the thread_lock here,
  1739. * since we already have the runq lock.
  1740. * So we can`t grab a reference to the
  1741. * thread. However, a thread that is
  1742. * in RUN state cannot be deallocated
  1743. * until it stops running. If it isn`t
  1744. * on the runq, then thread_halt cannot
  1745. * see it. So we remove the thread
  1746. * from the runq to make it safe.
  1747. */
  1748. remqueue(q, (queue_entry_t) thread);
  1749. runq->count--;
  1750. thread->runq = RUN_QUEUE_NULL;
  1751. stuck_threads[stuck_count++] = thread;
  1752. if (do_thread_scan_debug)
  1753. printf("do_runq_scan: adding thread %p\n", thread);
  1754. }
  1755. count--;
  1756. thread = next;
  1757. }
  1758. q++;
  1759. }
  1760. }
  1761. simple_unlock(&runq->lock);
  1762. splx(s);
  1763. return FALSE;
  1764. }
  1765. void do_thread_scan(void)
  1766. {
  1767. spl_t s;
  1768. boolean_t restart_needed = 0;
  1769. thread_t thread;
  1770. #if MACH_HOST
  1771. processor_set_t pset;
  1772. #endif /* MACH_HOST */
  1773. do {
  1774. #if MACH_HOST
  1775. simple_lock(&all_psets_lock);
  1776. queue_iterate(&all_psets, pset, processor_set_t, all_psets) {
  1777. if (restart_needed = do_runq_scan(&pset->runq))
  1778. break;
  1779. }
  1780. simple_unlock(&all_psets_lock);
  1781. #else /* MACH_HOST */
  1782. restart_needed = do_runq_scan(&default_pset.runq);
  1783. #endif /* MACH_HOST */
  1784. if (!restart_needed)
  1785. restart_needed = do_runq_scan(&master_processor->runq);
  1786. /*
  1787. * Ok, we now have a collection of candidates -- fix them.
  1788. */
  1789. while (stuck_count > 0) {
  1790. thread = stuck_threads[--stuck_count];
  1791. stuck_threads[stuck_count] = THREAD_NULL;
  1792. s = splsched();
  1793. thread_lock(thread);
  1794. if ((thread->state & TH_SCHED_STATE) == TH_RUN) {
  1795. /*
  1796. * Do the priority update. Call
  1797. * thread_setrun because thread is
  1798. * off the run queues.
  1799. */
  1800. update_priority(thread);
  1801. thread_setrun(thread, TRUE);
  1802. }
  1803. thread_unlock(thread);
  1804. splx(s);
  1805. }
  1806. } while (restart_needed);
  1807. }
  1808. #if DEBUG
  1809. void checkrq(
  1810. run_queue_t rq,
  1811. const char *msg)
  1812. {
  1813. queue_t q1;
  1814. int i, j;
  1815. queue_entry_t e;
  1816. int low;
  1817. low = -1;
  1818. j = 0;
  1819. q1 = rq->runq;
  1820. for (i = 0; i < NRQS; i++) {
  1821. if (q1->next == q1) {
  1822. if (q1->prev != q1)
  1823. panic("checkrq: empty at %s", msg);
  1824. }
  1825. else {
  1826. if (low == -1)
  1827. low = i;
  1828. for (e = q1->next; e != q1; e = e->next) {
  1829. j++;
  1830. if (e->next->prev != e)
  1831. panic("checkrq-2 at %s", msg);
  1832. if (e->prev->next != e)
  1833. panic("checkrq-3 at %s", msg);
  1834. }
  1835. }
  1836. q1++;
  1837. }
  1838. if (j != rq->count)
  1839. panic("checkrq: count wrong at %s", msg);
  1840. if (rq->count != 0 && low < rq->low)
  1841. panic("checkrq: low wrong at %s", msg);
  1842. }
  1843. void thread_check(
  1844. thread_t th,
  1845. run_queue_t rq)
  1846. {
  1847. unsigned int whichq;
  1848. whichq = th->sched_pri;
  1849. if (whichq >= NRQS) {
  1850. printf("thread_check: priority too high\n");
  1851. whichq = NRQS-1;
  1852. }
  1853. if ((th->links.next == &rq->runq[whichq]) &&
  1854. (rq->runq[whichq].prev != (queue_entry_t)th))
  1855. panic("thread_check");
  1856. }
  1857. #endif /* DEBUG */