2
0

common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #ifndef __ST_COMMON_H__
  41. #define __ST_COMMON_H__
  42. #include <stddef.h>
  43. #include <unistd.h>
  44. #include <sys/types.h>
  45. #include <sys/time.h>
  46. #include <setjmp.h>
  47. /* Enable assertions only if DEBUG is defined */
  48. #ifndef DEBUG
  49. #define NDEBUG
  50. #endif
  51. #include <assert.h>
  52. #define ST_ASSERT(expr) assert(expr)
  53. #define ST_BEGIN_MACRO {
  54. #define ST_END_MACRO }
  55. #ifdef DEBUG
  56. #define ST_HIDDEN /*nothing*/
  57. #else
  58. #define ST_HIDDEN static
  59. #endif
  60. #include "public.h"
  61. #include "md.h"
  62. /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */
  63. #ifndef MD_VALGRIND
  64. #ifndef NVALGRIND
  65. #define NVALGRIND
  66. #endif
  67. #else
  68. #undef NVALGRIND
  69. #endif
  70. /*****************************************
  71. * Circular linked list definitions
  72. */
  73. typedef struct _st_clist {
  74. struct _st_clist *next;
  75. struct _st_clist *prev;
  76. } _st_clist_t;
  77. /* Insert element "_e" into the list, before "_l" */
  78. #define ST_INSERT_BEFORE(_e,_l) \
  79. ST_BEGIN_MACRO \
  80. (_e)->next = (_l); \
  81. (_e)->prev = (_l)->prev; \
  82. (_l)->prev->next = (_e); \
  83. (_l)->prev = (_e); \
  84. ST_END_MACRO
  85. /* Insert element "_e" into the list, after "_l" */
  86. #define ST_INSERT_AFTER(_e,_l) \
  87. ST_BEGIN_MACRO \
  88. (_e)->next = (_l)->next; \
  89. (_e)->prev = (_l); \
  90. (_l)->next->prev = (_e); \
  91. (_l)->next = (_e); \
  92. ST_END_MACRO
  93. /* Return the element following element "_e" */
  94. #define ST_NEXT_LINK(_e) ((_e)->next)
  95. /* Append an element "_e" to the end of the list "_l" */
  96. #define ST_APPEND_LINK(_e,_l) ST_INSERT_BEFORE(_e,_l)
  97. /* Insert an element "_e" at the head of the list "_l" */
  98. #define ST_INSERT_LINK(_e,_l) ST_INSERT_AFTER(_e,_l)
  99. /* Return the head/tail of the list */
  100. #define ST_LIST_HEAD(_l) (_l)->next
  101. #define ST_LIST_TAIL(_l) (_l)->prev
  102. /* Remove the element "_e" from it's circular list */
  103. #define ST_REMOVE_LINK(_e) \
  104. ST_BEGIN_MACRO \
  105. (_e)->prev->next = (_e)->next; \
  106. (_e)->next->prev = (_e)->prev; \
  107. ST_END_MACRO
  108. /* Return non-zero if the given circular list "_l" is empty, */
  109. /* zero if the circular list is not empty */
  110. #define ST_CLIST_IS_EMPTY(_l) \
  111. ((_l)->next == (_l))
  112. /* Initialize a circular list */
  113. #define ST_INIT_CLIST(_l) \
  114. ST_BEGIN_MACRO \
  115. (_l)->next = (_l); \
  116. (_l)->prev = (_l); \
  117. ST_END_MACRO
  118. #define ST_INIT_STATIC_CLIST(_l) \
  119. {(_l), (_l)}
  120. /*****************************************
  121. * Basic types definitions
  122. */
  123. typedef void (*_st_destructor_t)(void *);
  124. typedef struct _st_stack {
  125. _st_clist_t links;
  126. char *vaddr; /* Base of stack's allocated memory */
  127. int vaddr_size; /* Size of stack's allocated memory */
  128. int stk_size; /* Size of usable portion of the stack */
  129. char *stk_bottom; /* Lowest address of stack's usable portion */
  130. char *stk_top; /* Highest address of stack's usable portion */
  131. void *sp; /* Stack pointer from C's point of view */
  132. #ifdef __ia64__
  133. void *bsp; /* Register stack backing store pointer */
  134. #endif
  135. /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */
  136. #ifndef NVALGRIND
  137. /* id returned by VALGRIND_STACK_REGISTER */
  138. /* http://valgrind.org/docs/manual/manual-core-adv.html */
  139. unsigned long valgrind_stack_id;
  140. #endif
  141. } _st_stack_t;
  142. typedef struct _st_cond {
  143. _st_clist_t wait_q; /* Condition variable wait queue */
  144. } _st_cond_t;
  145. typedef struct _st_thread _st_thread_t;
  146. struct _st_thread {
  147. int state; /* Thread's state */
  148. int flags; /* Thread's flags */
  149. void *(*start)(void *arg); /* The start function of the thread */
  150. void *arg; /* Argument of the start function */
  151. void *retval; /* Return value of the start function */
  152. _st_stack_t *stack; /* Info about thread's stack */
  153. _st_clist_t links; /* For putting on run/sleep/zombie queue */
  154. _st_clist_t wait_links; /* For putting on mutex/condvar wait queue */
  155. #ifdef DEBUG
  156. _st_clist_t tlink; /* For putting on thread queue */
  157. #endif
  158. st_utime_t due; /* Wakeup time when thread is sleeping */
  159. _st_thread_t *left; /* For putting in timeout heap */
  160. _st_thread_t *right; /* -- see docs/timeout_heap.txt for details */
  161. int heap_index;
  162. void **private_data; /* Per thread private data */
  163. _st_cond_t *term; /* Termination condition variable for join */
  164. jmp_buf context; /* Thread's context */
  165. };
  166. typedef struct _st_mutex {
  167. _st_thread_t *owner; /* Current mutex owner */
  168. _st_clist_t wait_q; /* Mutex wait queue */
  169. } _st_mutex_t;
  170. typedef struct _st_pollq {
  171. _st_clist_t links; /* For putting on io queue */
  172. _st_thread_t *thread; /* Polling thread */
  173. struct pollfd *pds; /* Array of poll descriptors */
  174. int npds; /* Length of the array */
  175. int on_ioq; /* Is it on ioq? */
  176. } _st_pollq_t;
  177. typedef struct _st_eventsys_ops {
  178. const char *name; /* Name of this event system */
  179. int val; /* Type of this event system */
  180. int (*init)(void); /* Initialization */
  181. void (*dispatch)(void); /* Dispatch function */
  182. int (*pollset_add)(struct pollfd *, int); /* Add descriptor set */
  183. void (*pollset_del)(struct pollfd *, int); /* Delete descriptor set */
  184. int (*fd_new)(int); /* New descriptor allocated */
  185. int (*fd_close)(int); /* Descriptor closed */
  186. int (*fd_getlimit)(void); /* Descriptor hard limit */
  187. } _st_eventsys_t;
  188. typedef struct _st_vp {
  189. _st_thread_t *idle_thread; /* Idle thread for this vp */
  190. st_utime_t last_clock; /* The last time we went into vp_check_clock() */
  191. _st_clist_t run_q; /* run queue for this vp */
  192. _st_clist_t io_q; /* io queue for this vp */
  193. _st_clist_t zombie_q; /* zombie queue for this vp */
  194. #ifdef DEBUG
  195. _st_clist_t thread_q; /* all threads of this vp */
  196. #endif
  197. int pagesize;
  198. _st_thread_t *sleep_q; /* sleep queue for this vp */
  199. int sleepq_size; /* number of threads on sleep queue */
  200. #ifdef ST_SWITCH_CB
  201. st_switch_cb_t switch_out_cb; /* called when a thread is switched out */
  202. st_switch_cb_t switch_in_cb; /* called when a thread is switched in */
  203. #endif
  204. } _st_vp_t;
  205. typedef struct _st_netfd {
  206. int osfd; /* Underlying OS file descriptor */
  207. int inuse; /* In-use flag */
  208. void *private_data; /* Per descriptor private data */
  209. _st_destructor_t destructor; /* Private data destructor function */
  210. void *aux_data; /* Auxiliary data for internal use */
  211. struct _st_netfd *next; /* For putting on the free list */
  212. } _st_netfd_t;
  213. /*****************************************
  214. * Current vp, thread, and event system
  215. */
  216. extern _st_vp_t _st_this_vp;
  217. extern _st_thread_t *_st_this_thread;
  218. extern _st_eventsys_t *_st_eventsys;
  219. #define _ST_CURRENT_THREAD() (_st_this_thread)
  220. #define _ST_SET_CURRENT_THREAD(_thread) (_st_this_thread = (_thread))
  221. #define _ST_LAST_CLOCK (_st_this_vp.last_clock)
  222. #define _ST_RUNQ (_st_this_vp.run_q)
  223. #define _ST_IOQ (_st_this_vp.io_q)
  224. #define _ST_ZOMBIEQ (_st_this_vp.zombie_q)
  225. #ifdef DEBUG
  226. #define _ST_THREADQ (_st_this_vp.thread_q)
  227. #endif
  228. #define _ST_PAGE_SIZE (_st_this_vp.pagesize)
  229. #define _ST_SLEEPQ (_st_this_vp.sleep_q)
  230. #define _ST_SLEEPQ_SIZE (_st_this_vp.sleepq_size)
  231. #define _ST_VP_IDLE() (*_st_eventsys->dispatch)()
  232. /*****************************************
  233. * vp queues operations
  234. */
  235. #define _ST_ADD_IOQ(_pq) ST_APPEND_LINK(&_pq.links, &_ST_IOQ)
  236. #define _ST_DEL_IOQ(_pq) ST_REMOVE_LINK(&_pq.links)
  237. #define _ST_ADD_RUNQ(_thr) ST_APPEND_LINK(&(_thr)->links, &_ST_RUNQ)
  238. #define _ST_DEL_RUNQ(_thr) ST_REMOVE_LINK(&(_thr)->links)
  239. #define _ST_ADD_SLEEPQ(_thr, _timeout) _st_add_sleep_q(_thr, _timeout)
  240. #define _ST_DEL_SLEEPQ(_thr) _st_del_sleep_q(_thr)
  241. #define _ST_ADD_ZOMBIEQ(_thr) ST_APPEND_LINK(&(_thr)->links, &_ST_ZOMBIEQ)
  242. #define _ST_DEL_ZOMBIEQ(_thr) ST_REMOVE_LINK(&(_thr)->links)
  243. #ifdef DEBUG
  244. #define _ST_ADD_THREADQ(_thr) ST_APPEND_LINK(&(_thr)->tlink, &_ST_THREADQ)
  245. #define _ST_DEL_THREADQ(_thr) ST_REMOVE_LINK(&(_thr)->tlink)
  246. #endif
  247. /*****************************************
  248. * Thread states and flags
  249. */
  250. #define _ST_ST_RUNNING 0
  251. #define _ST_ST_RUNNABLE 1
  252. #define _ST_ST_IO_WAIT 2
  253. #define _ST_ST_LOCK_WAIT 3
  254. #define _ST_ST_COND_WAIT 4
  255. #define _ST_ST_SLEEPING 5
  256. #define _ST_ST_ZOMBIE 6
  257. #define _ST_ST_SUSPENDED 7
  258. #define _ST_FL_PRIMORDIAL 0x01
  259. #define _ST_FL_IDLE_THREAD 0x02
  260. #define _ST_FL_ON_SLEEPQ 0x04
  261. #define _ST_FL_INTERRUPT 0x08
  262. #define _ST_FL_TIMEDOUT 0x10
  263. /*****************************************
  264. * Pointer conversion
  265. */
  266. #ifndef offsetof
  267. #define offsetof(type, identifier) ((size_t)&(((type *)0)->identifier))
  268. #endif
  269. #define _ST_THREAD_PTR(_qp) \
  270. ((_st_thread_t *)((char *)(_qp) - offsetof(_st_thread_t, links)))
  271. #define _ST_THREAD_WAITQ_PTR(_qp) \
  272. ((_st_thread_t *)((char *)(_qp) - offsetof(_st_thread_t, wait_links)))
  273. #define _ST_THREAD_STACK_PTR(_qp) \
  274. ((_st_stack_t *)((char*)(_qp) - offsetof(_st_stack_t, links)))
  275. #define _ST_POLLQUEUE_PTR(_qp) \
  276. ((_st_pollq_t *)((char *)(_qp) - offsetof(_st_pollq_t, links)))
  277. #ifdef DEBUG
  278. #define _ST_THREAD_THREADQ_PTR(_qp) \
  279. ((_st_thread_t *)((char *)(_qp) - offsetof(_st_thread_t, tlink)))
  280. #endif
  281. /*****************************************
  282. * Constants
  283. */
  284. #ifndef ST_UTIME_NO_TIMEOUT
  285. #define ST_UTIME_NO_TIMEOUT ((st_utime_t) -1LL)
  286. #endif
  287. #ifndef __ia64__
  288. #define ST_DEFAULT_STACK_SIZE (64*1024)
  289. #else
  290. #define ST_DEFAULT_STACK_SIZE (128*1024) /* Includes register stack size */
  291. #endif
  292. #ifndef ST_KEYS_MAX
  293. #define ST_KEYS_MAX 16
  294. #endif
  295. #ifndef ST_MIN_POLLFDS_SIZE
  296. #define ST_MIN_POLLFDS_SIZE 64
  297. #endif
  298. /*****************************************
  299. * Threads context switching
  300. */
  301. #ifdef DEBUG
  302. void _st_iterate_threads(void);
  303. #define ST_DEBUG_ITERATE_THREADS() _st_iterate_threads()
  304. #else
  305. #define ST_DEBUG_ITERATE_THREADS()
  306. #endif
  307. #ifdef ST_SWITCH_CB
  308. #define ST_SWITCH_OUT_CB(_thread) \
  309. if (_st_this_vp.switch_out_cb != NULL && \
  310. _thread != _st_this_vp.idle_thread && \
  311. _thread->state != _ST_ST_ZOMBIE) { \
  312. _st_this_vp.switch_out_cb(); \
  313. }
  314. #define ST_SWITCH_IN_CB(_thread) \
  315. if (_st_this_vp.switch_in_cb != NULL && \
  316. _thread != _st_this_vp.idle_thread && \
  317. _thread->state != _ST_ST_ZOMBIE) { \
  318. _st_this_vp.switch_in_cb(); \
  319. }
  320. #else
  321. #define ST_SWITCH_OUT_CB(_thread)
  322. #define ST_SWITCH_IN_CB(_thread)
  323. #endif
  324. /*
  325. * Switch away from the current thread context by saving its state and
  326. * calling the thread scheduler
  327. */
  328. #define _ST_SWITCH_CONTEXT(_thread) \
  329. ST_BEGIN_MACRO \
  330. ST_SWITCH_OUT_CB(_thread); \
  331. if (!MD_SETJMP((_thread)->context)) { \
  332. _st_vp_schedule(); \
  333. } \
  334. ST_DEBUG_ITERATE_THREADS(); \
  335. ST_SWITCH_IN_CB(_thread); \
  336. ST_END_MACRO
  337. /*
  338. * Restore a thread context that was saved by _ST_SWITCH_CONTEXT or
  339. * initialized by _ST_INIT_CONTEXT
  340. */
  341. #define _ST_RESTORE_CONTEXT(_thread) \
  342. ST_BEGIN_MACRO \
  343. _ST_SET_CURRENT_THREAD(_thread); \
  344. MD_LONGJMP((_thread)->context, 1); \
  345. ST_END_MACRO
  346. /*
  347. * Initialize the thread context preparing it to execute _main
  348. */
  349. #ifdef MD_INIT_CONTEXT
  350. #define _ST_INIT_CONTEXT MD_INIT_CONTEXT
  351. #else
  352. #error Unknown OS
  353. #endif
  354. /*
  355. * Number of bytes reserved under the stack "bottom"
  356. */
  357. #define _ST_STACK_PAD_SIZE MD_STACK_PAD_SIZE
  358. /*****************************************
  359. * Forward declarations
  360. */
  361. void _st_vp_schedule(void);
  362. void _st_vp_check_clock(void);
  363. void *_st_idle_thread_start(void *arg);
  364. void _st_thread_main(void);
  365. void _st_thread_cleanup(_st_thread_t *thread);
  366. void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout);
  367. void _st_del_sleep_q(_st_thread_t *thread);
  368. _st_stack_t *_st_stack_new(int stack_size);
  369. void _st_stack_free(_st_stack_t *ts);
  370. int _st_io_init(void);
  371. st_utime_t st_utime(void);
  372. _st_cond_t *st_cond_new(void);
  373. int st_cond_destroy(_st_cond_t *cvar);
  374. int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout);
  375. int st_cond_signal(_st_cond_t *cvar);
  376. ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout);
  377. ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout);
  378. int st_poll(struct pollfd *pds, int npds, st_utime_t timeout);
  379. _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size);
  380. #endif /* !__ST_COMMON_H__ */