sync.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <time.h>
  42. #include <errno.h>
  43. #include "common.h"
  44. extern time_t _st_curr_time;
  45. extern st_utime_t _st_last_tset;
  46. extern int _st_active_count;
  47. static st_utime_t (*_st_utime)(void) = NULL;
  48. /*****************************************
  49. * Time functions
  50. */
  51. st_utime_t st_utime(void)
  52. {
  53. if (_st_utime == NULL) {
  54. #ifdef MD_GET_UTIME
  55. MD_GET_UTIME();
  56. #else
  57. #error Unknown OS
  58. #endif
  59. }
  60. return (*_st_utime)();
  61. }
  62. int st_set_utime_function(st_utime_t (*func)(void))
  63. {
  64. if (_st_active_count) {
  65. errno = EINVAL;
  66. return -1;
  67. }
  68. _st_utime = func;
  69. return 0;
  70. }
  71. st_utime_t st_utime_last_clock(void)
  72. {
  73. return _ST_LAST_CLOCK;
  74. }
  75. int st_timecache_set(int on)
  76. {
  77. int wason = (_st_curr_time) ? 1 : 0;
  78. if (on) {
  79. _st_curr_time = time(NULL);
  80. _st_last_tset = st_utime();
  81. } else
  82. _st_curr_time = 0;
  83. return wason;
  84. }
  85. time_t st_time(void)
  86. {
  87. if (_st_curr_time)
  88. return _st_curr_time;
  89. return time(NULL);
  90. }
  91. int st_usleep(st_utime_t usecs)
  92. {
  93. _st_thread_t *me = _ST_CURRENT_THREAD();
  94. if (me->flags & _ST_FL_INTERRUPT) {
  95. me->flags &= ~_ST_FL_INTERRUPT;
  96. errno = EINTR;
  97. return -1;
  98. }
  99. if (usecs != ST_UTIME_NO_TIMEOUT) {
  100. me->state = _ST_ST_SLEEPING;
  101. _ST_ADD_SLEEPQ(me, usecs);
  102. } else
  103. me->state = _ST_ST_SUSPENDED;
  104. _ST_SWITCH_CONTEXT(me);
  105. if (me->flags & _ST_FL_INTERRUPT) {
  106. me->flags &= ~_ST_FL_INTERRUPT;
  107. errno = EINTR;
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. int st_sleep(int secs)
  113. {
  114. return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL : ST_UTIME_NO_TIMEOUT);
  115. }
  116. /*****************************************
  117. * Condition variable functions
  118. */
  119. _st_cond_t *st_cond_new(void)
  120. {
  121. _st_cond_t *cvar;
  122. cvar = (_st_cond_t *) calloc(1, sizeof(_st_cond_t));
  123. if (cvar) {
  124. ST_INIT_CLIST(&cvar->wait_q);
  125. }
  126. return cvar;
  127. }
  128. int st_cond_destroy(_st_cond_t *cvar)
  129. {
  130. if (cvar->wait_q.next != &cvar->wait_q) {
  131. errno = EBUSY;
  132. return -1;
  133. }
  134. free(cvar);
  135. return 0;
  136. }
  137. int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
  138. {
  139. _st_thread_t *me = _ST_CURRENT_THREAD();
  140. int rv;
  141. if (me->flags & _ST_FL_INTERRUPT) {
  142. me->flags &= ~_ST_FL_INTERRUPT;
  143. errno = EINTR;
  144. return -1;
  145. }
  146. /* Put caller thread on the condition variable's wait queue */
  147. me->state = _ST_ST_COND_WAIT;
  148. ST_APPEND_LINK(&me->wait_links, &cvar->wait_q);
  149. if (timeout != ST_UTIME_NO_TIMEOUT)
  150. _ST_ADD_SLEEPQ(me, timeout);
  151. _ST_SWITCH_CONTEXT(me);
  152. ST_REMOVE_LINK(&me->wait_links);
  153. rv = 0;
  154. if (me->flags & _ST_FL_TIMEDOUT) {
  155. me->flags &= ~_ST_FL_TIMEDOUT;
  156. errno = ETIME;
  157. rv = -1;
  158. }
  159. if (me->flags & _ST_FL_INTERRUPT) {
  160. me->flags &= ~_ST_FL_INTERRUPT;
  161. errno = EINTR;
  162. rv = -1;
  163. }
  164. return rv;
  165. }
  166. int st_cond_wait(_st_cond_t *cvar)
  167. {
  168. return st_cond_timedwait(cvar, ST_UTIME_NO_TIMEOUT);
  169. }
  170. static int _st_cond_signal(_st_cond_t *cvar, int broadcast)
  171. {
  172. _st_thread_t *thread;
  173. _st_clist_t *q;
  174. for (q = cvar->wait_q.next; q != &cvar->wait_q; q = q->next) {
  175. thread = _ST_THREAD_WAITQ_PTR(q);
  176. if (thread->state == _ST_ST_COND_WAIT) {
  177. if (thread->flags & _ST_FL_ON_SLEEPQ)
  178. _ST_DEL_SLEEPQ(thread);
  179. /* Make thread runnable */
  180. thread->state = _ST_ST_RUNNABLE;
  181. _ST_ADD_RUNQ(thread);
  182. if (!broadcast)
  183. break;
  184. }
  185. }
  186. return 0;
  187. }
  188. int st_cond_signal(_st_cond_t *cvar)
  189. {
  190. return _st_cond_signal(cvar, 0);
  191. }
  192. int st_cond_broadcast(_st_cond_t *cvar)
  193. {
  194. return _st_cond_signal(cvar, 1);
  195. }
  196. /*****************************************
  197. * Mutex functions
  198. */
  199. _st_mutex_t *st_mutex_new(void)
  200. {
  201. _st_mutex_t *lock;
  202. lock = (_st_mutex_t *) calloc(1, sizeof(_st_mutex_t));
  203. if (lock) {
  204. ST_INIT_CLIST(&lock->wait_q);
  205. lock->owner = NULL;
  206. }
  207. return lock;
  208. }
  209. int st_mutex_destroy(_st_mutex_t *lock)
  210. {
  211. if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) {
  212. errno = EBUSY;
  213. return -1;
  214. }
  215. free(lock);
  216. return 0;
  217. }
  218. int st_mutex_lock(_st_mutex_t *lock)
  219. {
  220. _st_thread_t *me = _ST_CURRENT_THREAD();
  221. if (me->flags & _ST_FL_INTERRUPT) {
  222. me->flags &= ~_ST_FL_INTERRUPT;
  223. errno = EINTR;
  224. return -1;
  225. }
  226. if (lock->owner == NULL) {
  227. /* Got the mutex */
  228. lock->owner = me;
  229. return 0;
  230. }
  231. if (lock->owner == me) {
  232. errno = EDEADLK;
  233. return -1;
  234. }
  235. /* Put caller thread on the mutex's wait queue */
  236. me->state = _ST_ST_LOCK_WAIT;
  237. ST_APPEND_LINK(&me->wait_links, &lock->wait_q);
  238. _ST_SWITCH_CONTEXT(me);
  239. ST_REMOVE_LINK(&me->wait_links);
  240. if ((me->flags & _ST_FL_INTERRUPT) && lock->owner != me) {
  241. me->flags &= ~_ST_FL_INTERRUPT;
  242. errno = EINTR;
  243. return -1;
  244. }
  245. return 0;
  246. }
  247. int st_mutex_unlock(_st_mutex_t *lock)
  248. {
  249. _st_thread_t *thread;
  250. _st_clist_t *q;
  251. if (lock->owner != _ST_CURRENT_THREAD()) {
  252. errno = EPERM;
  253. return -1;
  254. }
  255. for (q = lock->wait_q.next; q != &lock->wait_q; q = q->next) {
  256. thread = _ST_THREAD_WAITQ_PTR(q);
  257. if (thread->state == _ST_ST_LOCK_WAIT) {
  258. lock->owner = thread;
  259. /* Make thread runnable */
  260. thread->state = _ST_ST_RUNNABLE;
  261. _ST_ADD_RUNQ(thread);
  262. return 0;
  263. }
  264. }
  265. /* No threads waiting on this mutex */
  266. lock->owner = NULL;
  267. return 0;
  268. }
  269. int st_mutex_trylock(_st_mutex_t *lock)
  270. {
  271. if (lock->owner != NULL) {
  272. errno = EBUSY;
  273. return -1;
  274. }
  275. /* Got the mutex */
  276. lock->owner = _ST_CURRENT_THREAD();
  277. return 0;
  278. }