2
0

sched.c 18 KB

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