sync.c 7.7 KB

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