common.h 14 KB

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