sched.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */
  2. /*
  3. * The contents of this file are subject to the Mozilla Public
  4. * License Version 1.1 (the "License"); you may not use this file
  5. * except in compliance with the License. You may obtain a copy of
  6. * the License at http://www.mozilla.org/MPL/
  7. *
  8. * Software distributed under the License is distributed on an "AS
  9. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. * implied. See the License for the specific language governing
  11. * rights and limitations under the License.
  12. *
  13. * The Original Code is the Netscape Portable Runtime library.
  14. *
  15. * The Initial Developer of the Original Code is Netscape
  16. * Communications Corporation. Portions created by Netscape are
  17. * Copyright (C) 1994-2000 Netscape Communications Corporation. All
  18. * Rights Reserved.
  19. *
  20. * Contributor(s): Silicon Graphics, Inc.
  21. *
  22. * Portions created by SGI are Copyright (C) 2000-2001 Silicon
  23. * Graphics, Inc. All Rights Reserved.
  24. *
  25. * Alternatively, the contents of this file may be used under the
  26. * terms of the GNU General Public License Version 2 or later (the
  27. * "GPL"), in which case the provisions of the GPL are applicable
  28. * instead of those above. If you wish to allow use of your
  29. * version of this file only under the terms of the GPL and not to
  30. * allow others to use your version of this file under the MPL,
  31. * indicate your decision by deleting the provisions above and
  32. * replace them with the notice and other provisions required by
  33. * the GPL. If you do not delete the provisions above, a recipient
  34. * may use your version of this file under either the MPL or the
  35. * GPL.
  36. */
  37. /*
  38. * This file is derived directly from Netscape Communications Corporation,
  39. * and consists of extensive modifications made during the year(s) 1999-2000.
  40. */
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <string.h>
  45. #include <time.h>
  46. #include <errno.h>
  47. #include "common.h"
  48. /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */
  49. #ifndef NVALGRIND
  50. #include <valgrind/valgrind.h>
  51. #endif
  52. // Global stat.
  53. #if defined(DEBUG) && defined(DEBUG_STATS)
  54. __thread unsigned long long _st_stat_sched_15ms = 0;
  55. __thread unsigned long long _st_stat_sched_20ms = 0;
  56. __thread unsigned long long _st_stat_sched_25ms = 0;
  57. __thread unsigned long long _st_stat_sched_30ms = 0;
  58. __thread unsigned long long _st_stat_sched_35ms = 0;
  59. __thread unsigned long long _st_stat_sched_40ms = 0;
  60. __thread unsigned long long _st_stat_sched_80ms = 0;
  61. __thread unsigned long long _st_stat_sched_160ms = 0;
  62. __thread unsigned long long _st_stat_sched_s = 0;
  63. __thread unsigned long long _st_stat_thread_run = 0;
  64. __thread unsigned long long _st_stat_thread_idle = 0;
  65. __thread unsigned long long _st_stat_thread_yield = 0;
  66. __thread unsigned long long _st_stat_thread_yield2 = 0;
  67. #endif
  68. /* Global data */
  69. __thread _st_vp_t _st_this_vp; /* This VP */
  70. __thread _st_thread_t *_st_this_thread; /* Current thread */
  71. __thread int _st_active_count = 0; /* Active thread count */
  72. __thread time_t _st_curr_time = 0; /* Current time as returned by time(2) */
  73. __thread st_utime_t _st_last_tset; /* Last time it was fetched */
  74. // We should initialize the thread-local variable in st_init().
  75. extern __thread _st_clist_t _st_free_stacks;
  76. int st_poll(struct pollfd *pds, int npds, st_utime_t timeout)
  77. {
  78. struct pollfd *pd;
  79. struct pollfd *epd = pds + npds;
  80. _st_pollq_t pq;
  81. _st_thread_t *me = _ST_CURRENT_THREAD();
  82. int n;
  83. if (me->flags & _ST_FL_INTERRUPT) {
  84. me->flags &= ~_ST_FL_INTERRUPT;
  85. errno = EINTR;
  86. return -1;
  87. }
  88. if ((*_st_eventsys->pollset_add)(pds, npds) < 0)
  89. return -1;
  90. pq.pds = pds;
  91. pq.npds = npds;
  92. pq.thread = me;
  93. pq.on_ioq = 1;
  94. _ST_ADD_IOQ(pq);
  95. if (timeout != ST_UTIME_NO_TIMEOUT)
  96. _ST_ADD_SLEEPQ(me, timeout);
  97. me->state = _ST_ST_IO_WAIT;
  98. _ST_SWITCH_CONTEXT(me);
  99. n = 0;
  100. if (pq.on_ioq) {
  101. /* If we timed out, the pollq might still be on the ioq. Remove it */
  102. _ST_DEL_IOQ(pq);
  103. (*_st_eventsys->pollset_del)(pds, npds);
  104. } else {
  105. /* Count the number of ready descriptors */
  106. for (pd = pds; pd < epd; pd++) {
  107. if (pd->revents)
  108. n++;
  109. }
  110. }
  111. if (me->flags & _ST_FL_INTERRUPT) {
  112. me->flags &= ~_ST_FL_INTERRUPT;
  113. errno = EINTR;
  114. return -1;
  115. }
  116. return n;
  117. }
  118. void _st_vp_schedule(void)
  119. {
  120. _st_thread_t *thread;
  121. if (_ST_RUNQ.next != &_ST_RUNQ) {
  122. #if defined(DEBUG) && defined(DEBUG_STATS)
  123. ++_st_stat_thread_run;
  124. #endif
  125. /* Pull thread off of the run queue */
  126. thread = _ST_THREAD_PTR(_ST_RUNQ.next);
  127. _ST_DEL_RUNQ(thread);
  128. } else {
  129. #if defined(DEBUG) && defined(DEBUG_STATS)
  130. ++_st_stat_thread_idle;
  131. #endif
  132. /* If there are no threads to run, switch to the idle thread */
  133. thread = _st_this_vp.idle_thread;
  134. }
  135. ST_ASSERT(thread->state == _ST_ST_RUNNABLE);
  136. /* Resume the thread */
  137. thread->state = _ST_ST_RUNNING;
  138. _ST_RESTORE_CONTEXT(thread);
  139. }
  140. /*
  141. * Initialize this Virtual Processor
  142. */
  143. int st_init(void)
  144. {
  145. _st_thread_t *thread;
  146. if (_st_active_count) {
  147. /* Already initialized */
  148. return 0;
  149. }
  150. /* We can ignore return value here */
  151. st_set_eventsys(ST_EVENTSYS_DEFAULT);
  152. if (_st_io_init() < 0)
  153. return -1;
  154. // Initialize the thread-local variables.
  155. ST_INIT_CLIST(&_st_free_stacks);
  156. // Initialize ST.
  157. memset(&_st_this_vp, 0, sizeof(_st_vp_t));
  158. ST_INIT_CLIST(&_ST_RUNQ);
  159. ST_INIT_CLIST(&_ST_IOQ);
  160. ST_INIT_CLIST(&_ST_ZOMBIEQ);
  161. #ifdef DEBUG
  162. ST_INIT_CLIST(&_ST_THREADQ);
  163. #endif
  164. if ((*_st_eventsys->init)() < 0)
  165. return -1;
  166. _st_this_vp.pagesize = getpagesize();
  167. _st_this_vp.last_clock = st_utime();
  168. /*
  169. * Create idle thread
  170. */
  171. _st_this_vp.idle_thread = st_thread_create(_st_idle_thread_start, NULL, 0, 0);
  172. if (!_st_this_vp.idle_thread)
  173. return -1;
  174. _st_this_vp.idle_thread->flags = _ST_FL_IDLE_THREAD;
  175. _st_active_count--;
  176. _ST_DEL_RUNQ(_st_this_vp.idle_thread);
  177. /*
  178. * Initialize primordial thread
  179. */
  180. thread = (_st_thread_t *) calloc(1, sizeof(_st_thread_t) + (ST_KEYS_MAX * sizeof(void *)));
  181. if (!thread)
  182. return -1;
  183. thread->private_data = (void **) (thread + 1);
  184. thread->state = _ST_ST_RUNNING;
  185. thread->flags = _ST_FL_PRIMORDIAL;
  186. _ST_SET_CURRENT_THREAD(thread);
  187. _st_active_count++;
  188. #ifdef DEBUG
  189. _ST_ADD_THREADQ(thread);
  190. #endif
  191. return 0;
  192. }
  193. /*
  194. * Destroy this Virtual Processor
  195. */
  196. void st_destroy(void)
  197. {
  198. (*_st_eventsys->destroy)();
  199. }
  200. #ifdef ST_SWITCH_CB
  201. st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb)
  202. {
  203. st_switch_cb_t ocb = _st_this_vp.switch_in_cb;
  204. _st_this_vp.switch_in_cb = cb;
  205. return ocb;
  206. }
  207. st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb)
  208. {
  209. st_switch_cb_t ocb = _st_this_vp.switch_out_cb;
  210. _st_this_vp.switch_out_cb = cb;
  211. return ocb;
  212. }
  213. #endif
  214. /*
  215. * Start function for the idle thread
  216. */
  217. /* ARGSUSED */
  218. void *_st_idle_thread_start(void *arg)
  219. {
  220. _st_thread_t *me = _ST_CURRENT_THREAD();
  221. while (_st_active_count > 0) {
  222. /* Idle vp till I/O is ready or the smallest timeout expired */
  223. _ST_VP_IDLE();
  224. /* Check sleep queue for expired threads */
  225. _st_vp_check_clock();
  226. me->state = _ST_ST_RUNNABLE;
  227. _ST_SWITCH_CONTEXT(me);
  228. }
  229. /* No more threads */
  230. exit(0);
  231. /* NOTREACHED */
  232. return NULL;
  233. }
  234. void st_thread_exit(void *retval)
  235. {
  236. _st_thread_t *thread = _ST_CURRENT_THREAD();
  237. thread->retval = retval;
  238. _st_thread_cleanup(thread);
  239. _st_active_count--;
  240. if (thread->term) {
  241. /* Put thread on the zombie queue */
  242. thread->state = _ST_ST_ZOMBIE;
  243. _ST_ADD_ZOMBIEQ(thread);
  244. /* Notify on our termination condition variable */
  245. st_cond_signal(thread->term);
  246. /* Switch context and come back later */
  247. _ST_SWITCH_CONTEXT(thread);
  248. /* Continue the cleanup */
  249. st_cond_destroy(thread->term);
  250. thread->term = NULL;
  251. }
  252. #ifdef DEBUG
  253. _ST_DEL_THREADQ(thread);
  254. #endif
  255. /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */
  256. #ifndef NVALGRIND
  257. if (!(thread->flags & _ST_FL_PRIMORDIAL)) {
  258. VALGRIND_STACK_DEREGISTER(thread->stack->valgrind_stack_id);
  259. }
  260. #endif
  261. if (!(thread->flags & _ST_FL_PRIMORDIAL))
  262. _st_stack_free(thread->stack);
  263. /* Find another thread to run */
  264. _ST_SWITCH_CONTEXT(thread);
  265. /* Not going to land here */
  266. }
  267. int st_thread_join(_st_thread_t *thread, void **retvalp)
  268. {
  269. _st_cond_t *term = thread->term;
  270. /* Can't join a non-joinable thread */
  271. if (term == NULL) {
  272. errno = EINVAL;
  273. return -1;
  274. }
  275. if (_ST_CURRENT_THREAD() == thread) {
  276. errno = EDEADLK;
  277. return -1;
  278. }
  279. /* Multiple threads can't wait on the same joinable thread */
  280. if (term->wait_q.next != &term->wait_q) {
  281. errno = EINVAL;
  282. return -1;
  283. }
  284. while (thread->state != _ST_ST_ZOMBIE) {
  285. if (st_cond_timedwait(term, ST_UTIME_NO_TIMEOUT) != 0)
  286. return -1;
  287. }
  288. if (retvalp)
  289. *retvalp = thread->retval;
  290. /*
  291. * Remove target thread from the zombie queue and make it runnable.
  292. * When it gets scheduled later, it will do the clean up.
  293. */
  294. thread->state = _ST_ST_RUNNABLE;
  295. _ST_DEL_ZOMBIEQ(thread);
  296. _ST_ADD_RUNQ(thread);
  297. return 0;
  298. }
  299. void _st_thread_main(void)
  300. {
  301. _st_thread_t *thread = _ST_CURRENT_THREAD();
  302. /*
  303. * Cap the stack by zeroing out the saved return address register
  304. * value. This allows some debugging/profiling tools to know when
  305. * to stop unwinding the stack. It's a no-op on most platforms.
  306. */
  307. MD_CAP_STACK(&thread);
  308. /* Run thread main */
  309. thread->retval = (*thread->start)(thread->arg);
  310. /* All done, time to go away */
  311. st_thread_exit(thread->retval);
  312. }
  313. /*
  314. * Insert "thread" into the timeout heap, in the position
  315. * specified by thread->heap_index. See docs/timeout_heap.txt
  316. * for details about the timeout heap.
  317. */
  318. static _st_thread_t **heap_insert(_st_thread_t *thread) {
  319. int target = thread->heap_index;
  320. int s = target;
  321. _st_thread_t **p = &_ST_SLEEPQ;
  322. int bits = 0;
  323. int bit;
  324. int index = 1;
  325. while (s) {
  326. s >>= 1;
  327. bits++;
  328. }
  329. for (bit = bits - 2; bit >= 0; bit--) {
  330. if (thread->due < (*p)->due) {
  331. _st_thread_t *t = *p;
  332. thread->left = t->left;
  333. thread->right = t->right;
  334. *p = thread;
  335. thread->heap_index = index;
  336. thread = t;
  337. }
  338. index <<= 1;
  339. if (target & (1 << bit)) {
  340. p = &((*p)->right);
  341. index |= 1;
  342. } else {
  343. p = &((*p)->left);
  344. }
  345. }
  346. thread->heap_index = index;
  347. *p = thread;
  348. thread->left = thread->right = NULL;
  349. return p;
  350. }
  351. /*
  352. * Delete "thread" from the timeout heap.
  353. */
  354. static void heap_delete(_st_thread_t *thread) {
  355. _st_thread_t *t, **p;
  356. int bits = 0;
  357. int s, bit;
  358. /* First find and unlink the last heap element */
  359. p = &_ST_SLEEPQ;
  360. s = _ST_SLEEPQ_SIZE;
  361. while (s) {
  362. s >>= 1;
  363. bits++;
  364. }
  365. for (bit = bits - 2; bit >= 0; bit--) {
  366. if (_ST_SLEEPQ_SIZE & (1 << bit)) {
  367. p = &((*p)->right);
  368. } else {
  369. p = &((*p)->left);
  370. }
  371. }
  372. t = *p;
  373. *p = NULL;
  374. --_ST_SLEEPQ_SIZE;
  375. if (t != thread) {
  376. /*
  377. * Insert the unlinked last element in place of the element we are deleting
  378. */
  379. t->heap_index = thread->heap_index;
  380. p = heap_insert(t);
  381. t = *p;
  382. t->left = thread->left;
  383. t->right = thread->right;
  384. /*
  385. * Reestablish the heap invariant.
  386. */
  387. for (;;) {
  388. _st_thread_t *y; /* The younger child */
  389. int index_tmp;
  390. if (t->left == NULL)
  391. break;
  392. else if (t->right == NULL)
  393. y = t->left;
  394. else if (t->left->due < t->right->due)
  395. y = t->left;
  396. else
  397. y = t->right;
  398. if (t->due > y->due) {
  399. _st_thread_t *tl = y->left;
  400. _st_thread_t *tr = y->right;
  401. *p = y;
  402. if (y == t->left) {
  403. y->left = t;
  404. y->right = t->right;
  405. p = &y->left;
  406. } else {
  407. y->left = t->left;
  408. y->right = t;
  409. p = &y->right;
  410. }
  411. t->left = tl;
  412. t->right = tr;
  413. index_tmp = t->heap_index;
  414. t->heap_index = y->heap_index;
  415. y->heap_index = index_tmp;
  416. } else {
  417. break;
  418. }
  419. }
  420. }
  421. thread->left = thread->right = NULL;
  422. }
  423. void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout)
  424. {
  425. thread->due = _ST_LAST_CLOCK + timeout;
  426. thread->flags |= _ST_FL_ON_SLEEPQ;
  427. thread->heap_index = ++_ST_SLEEPQ_SIZE;
  428. heap_insert(thread);
  429. }
  430. void _st_del_sleep_q(_st_thread_t *thread)
  431. {
  432. heap_delete(thread);
  433. thread->flags &= ~_ST_FL_ON_SLEEPQ;
  434. }
  435. void _st_vp_check_clock(void)
  436. {
  437. _st_thread_t *thread;
  438. st_utime_t now;
  439. #if defined(DEBUG) && defined(DEBUG_STATS)
  440. st_utime_t elapsed;
  441. #endif
  442. now = st_utime();
  443. #if defined(DEBUG) && defined(DEBUG_STATS)
  444. elapsed = now < _ST_LAST_CLOCK? 0 : now - _ST_LAST_CLOCK; // Might step back.
  445. #endif
  446. _ST_LAST_CLOCK = now;
  447. #if defined(DEBUG) && defined(DEBUG_STATS)
  448. if (elapsed <= 10000) {
  449. ++_st_stat_sched_15ms;
  450. } else if (elapsed <= 21000) {
  451. ++_st_stat_sched_20ms;
  452. } else if (elapsed <= 25000) {
  453. ++_st_stat_sched_25ms;
  454. } else if (elapsed <= 30000) {
  455. ++_st_stat_sched_30ms;
  456. } else if (elapsed <= 35000) {
  457. ++_st_stat_sched_35ms;
  458. } else if (elapsed <= 40000) {
  459. ++_st_stat_sched_40ms;
  460. } else if (elapsed <= 80000) {
  461. ++_st_stat_sched_80ms;
  462. } else if (elapsed <= 160000) {
  463. ++_st_stat_sched_160ms;
  464. } else {
  465. ++_st_stat_sched_s;
  466. }
  467. #endif
  468. if (_st_curr_time && now - _st_last_tset > 999000) {
  469. _st_curr_time = time(NULL);
  470. _st_last_tset = now;
  471. }
  472. while (_ST_SLEEPQ != NULL) {
  473. thread = _ST_SLEEPQ;
  474. ST_ASSERT(thread->flags & _ST_FL_ON_SLEEPQ);
  475. if (thread->due > now)
  476. break;
  477. _ST_DEL_SLEEPQ(thread);
  478. /* If thread is waiting on condition variable, set the time out flag */
  479. if (thread->state == _ST_ST_COND_WAIT)
  480. thread->flags |= _ST_FL_TIMEDOUT;
  481. /* Make thread runnable */
  482. ST_ASSERT(!(thread->flags & _ST_FL_IDLE_THREAD));
  483. thread->state = _ST_ST_RUNNABLE;
  484. // Insert at the head of RunQ, to execute timer first.
  485. _ST_INSERT_RUNQ(thread);
  486. }
  487. }
  488. void st_thread_yield()
  489. {
  490. _st_thread_t *me = _ST_CURRENT_THREAD();
  491. #if defined(DEBUG) && defined(DEBUG_STATS)
  492. ++_st_stat_thread_yield;
  493. #endif
  494. /* Check sleep queue for expired threads */
  495. _st_vp_check_clock();
  496. // If not thread in RunQ to yield to, ignore and continue to run.
  497. if (_ST_RUNQ.next == &_ST_RUNQ) {
  498. return;
  499. }
  500. #if defined(DEBUG) && defined(DEBUG_STATS)
  501. ++_st_stat_thread_yield2;
  502. #endif
  503. // Append thread to the tail of RunQ, we will back after all threads executed.
  504. me->state = _ST_ST_RUNNABLE;
  505. _ST_ADD_RUNQ(me);
  506. // Yield to other threads in the RunQ.
  507. _ST_SWITCH_CONTEXT(me);
  508. }
  509. void st_thread_interrupt(_st_thread_t *thread)
  510. {
  511. /* If thread is already dead */
  512. if (thread->state == _ST_ST_ZOMBIE)
  513. return;
  514. thread->flags |= _ST_FL_INTERRUPT;
  515. if (thread->state == _ST_ST_RUNNING || thread->state == _ST_ST_RUNNABLE)
  516. return;
  517. if (thread->flags & _ST_FL_ON_SLEEPQ)
  518. _ST_DEL_SLEEPQ(thread);
  519. /* Make thread runnable */
  520. thread->state = _ST_ST_RUNNABLE;
  521. _ST_ADD_RUNQ(thread);
  522. }
  523. _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size)
  524. {
  525. _st_thread_t *thread;
  526. _st_stack_t *stack;
  527. void **ptds;
  528. char *sp;
  529. /* Adjust stack size */
  530. if (stk_size == 0)
  531. stk_size = ST_DEFAULT_STACK_SIZE;
  532. stk_size = ((stk_size + _ST_PAGE_SIZE - 1) / _ST_PAGE_SIZE) * _ST_PAGE_SIZE;
  533. stack = _st_stack_new(stk_size);
  534. if (!stack)
  535. return NULL;
  536. /* Allocate thread object and per-thread data off the stack */
  537. sp = stack->stk_top;
  538. sp = sp - (ST_KEYS_MAX * sizeof(void *));
  539. ptds = (void **) sp;
  540. sp = sp - sizeof(_st_thread_t);
  541. thread = (_st_thread_t *) sp;
  542. /* Make stack 64-byte aligned */
  543. if ((unsigned long)sp & 0x3f)
  544. sp = sp - ((unsigned long)sp & 0x3f);
  545. stack->sp = sp - _ST_STACK_PAD_SIZE;
  546. memset(thread, 0, sizeof(_st_thread_t));
  547. memset(ptds, 0, ST_KEYS_MAX * sizeof(void *));
  548. /* Initialize thread */
  549. thread->private_data = ptds;
  550. thread->stack = stack;
  551. thread->start = start;
  552. thread->arg = arg;
  553. _ST_INIT_CONTEXT(thread, stack->sp, _st_thread_main);
  554. /* If thread is joinable, allocate a termination condition variable */
  555. if (joinable) {
  556. thread->term = st_cond_new();
  557. if (thread->term == NULL) {
  558. _st_stack_free(thread->stack);
  559. return NULL;
  560. }
  561. }
  562. /* Make thread runnable */
  563. thread->state = _ST_ST_RUNNABLE;
  564. _st_active_count++;
  565. _ST_ADD_RUNQ(thread);
  566. #ifdef DEBUG
  567. _ST_ADD_THREADQ(thread);
  568. #endif
  569. /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */
  570. #ifndef NVALGRIND
  571. if (!(thread->flags & _ST_FL_PRIMORDIAL)) {
  572. thread->stack->valgrind_stack_id = VALGRIND_STACK_REGISTER(thread->stack->stk_top, thread->stack->stk_bottom);
  573. }
  574. #endif
  575. return thread;
  576. }
  577. _st_thread_t *st_thread_self(void)
  578. {
  579. return _ST_CURRENT_THREAD();
  580. }
  581. #ifdef DEBUG
  582. /* ARGSUSED */
  583. void _st_show_thread_stack(_st_thread_t *thread, const char *messg)
  584. {
  585. }
  586. /* To be set from debugger */
  587. int _st_iterate_threads_flag = 0;
  588. void _st_iterate_threads(void)
  589. {
  590. static __thread _st_thread_t *thread = NULL;
  591. static __thread _st_jmp_buf_t orig_jb, save_jb;
  592. _st_clist_t *q;
  593. if (!_st_iterate_threads_flag) {
  594. if (thread) {
  595. memcpy(thread->context, save_jb, sizeof(_st_jmp_buf_t));
  596. MD_LONGJMP(orig_jb, 1);
  597. }
  598. return;
  599. }
  600. if (thread) {
  601. memcpy(thread->context, save_jb, sizeof(_st_jmp_buf_t));
  602. _st_show_thread_stack(thread, NULL);
  603. } else {
  604. if (MD_SETJMP(orig_jb)) {
  605. _st_iterate_threads_flag = 0;
  606. thread = NULL;
  607. _st_show_thread_stack(thread, "Iteration completed");
  608. return;
  609. }
  610. thread = _ST_CURRENT_THREAD();
  611. _st_show_thread_stack(thread, "Iteration started");
  612. }
  613. q = thread->tlink.next;
  614. if (q == &_ST_THREADQ)
  615. q = q->next;
  616. ST_ASSERT(q != &_ST_THREADQ);
  617. thread = _ST_THREAD_THREADQ_PTR(q);
  618. if (thread == _ST_CURRENT_THREAD())
  619. MD_LONGJMP(orig_jb, 1);
  620. memcpy(save_jb, thread->context, sizeof(_st_jmp_buf_t));
  621. MD_LONGJMP(thread->context, 1);
  622. }
  623. #endif /* DEBUG */