sync.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */
  2. /*
  3. * The contents of this file are subject to the Mozilla Public
  4. * License Version 1.1 (the "License"); you may not use this file
  5. * except in compliance with the License. You may obtain a copy of
  6. * the License at http://www.mozilla.org/MPL/
  7. *
  8. * Software distributed under the License is distributed on an "AS
  9. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. * implied. See the License for the specific language governing
  11. * rights and limitations under the License.
  12. *
  13. * The Original Code is the Netscape Portable Runtime library.
  14. *
  15. * The Initial Developer of the Original Code is Netscape
  16. * Communications Corporation. Portions created by Netscape are
  17. * Copyright (C) 1994-2000 Netscape Communications Corporation. All
  18. * Rights Reserved.
  19. *
  20. * Contributor(s): Silicon Graphics, Inc.
  21. *
  22. * Portions created by SGI are Copyright (C) 2000-2001 Silicon
  23. * Graphics, Inc. All Rights Reserved.
  24. *
  25. * Alternatively, the contents of this file may be used under the
  26. * terms of the GNU General Public License Version 2 or later (the
  27. * "GPL"), in which case the provisions of the GPL are applicable
  28. * instead of those above. If you wish to allow use of your
  29. * version of this file only under the terms of the GPL and not to
  30. * allow others to use your version of this file under the MPL,
  31. * indicate your decision by deleting the provisions above and
  32. * replace them with the notice and other provisions required by
  33. * the GPL. If you do not delete the provisions above, a recipient
  34. * may use your version of this file under either the MPL or the
  35. * GPL.
  36. */
  37. /*
  38. * This file is derived directly from Netscape Communications Corporation,
  39. * and consists of extensive modifications made during the year(s) 1999-2000.
  40. */
  41. #include <stdlib.h>
  42. #include <time.h>
  43. #include <errno.h>
  44. #include "common.h"
  45. extern __thread time_t _st_curr_time;
  46. extern __thread st_utime_t _st_last_tset;
  47. extern __thread int _st_active_count;
  48. static st_utime_t (*_st_utime)(void) = NULL;
  49. /*****************************************
  50. * Time functions
  51. */
  52. st_utime_t st_utime(void)
  53. {
  54. if (_st_utime == NULL) {
  55. #ifdef MD_GET_UTIME
  56. MD_GET_UTIME();
  57. #else
  58. #error Unknown OS
  59. #endif
  60. }
  61. return (*_st_utime)();
  62. }
  63. int st_set_utime_function(st_utime_t (*func)(void))
  64. {
  65. if (_st_active_count) {
  66. errno = EINVAL;
  67. return -1;
  68. }
  69. _st_utime = func;
  70. return 0;
  71. }
  72. st_utime_t st_utime_last_clock(void)
  73. {
  74. return _ST_LAST_CLOCK;
  75. }
  76. int st_timecache_set(int on)
  77. {
  78. int wason = (_st_curr_time) ? 1 : 0;
  79. if (on) {
  80. _st_curr_time = time(NULL);
  81. _st_last_tset = st_utime();
  82. } else
  83. _st_curr_time = 0;
  84. return wason;
  85. }
  86. time_t st_time(void)
  87. {
  88. if (_st_curr_time)
  89. return _st_curr_time;
  90. return time(NULL);
  91. }
  92. int st_usleep(st_utime_t usecs)
  93. {
  94. _st_thread_t *me = _ST_CURRENT_THREAD();
  95. if (me->flags & _ST_FL_INTERRUPT) {
  96. me->flags &= ~_ST_FL_INTERRUPT;
  97. errno = EINTR;
  98. return -1;
  99. }
  100. if (usecs != ST_UTIME_NO_TIMEOUT) {
  101. me->state = _ST_ST_SLEEPING;
  102. _ST_ADD_SLEEPQ(me, usecs);
  103. } else
  104. me->state = _ST_ST_SUSPENDED;
  105. _ST_SWITCH_CONTEXT(me);
  106. if (me->flags & _ST_FL_INTERRUPT) {
  107. me->flags &= ~_ST_FL_INTERRUPT;
  108. errno = EINTR;
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. int st_sleep(int secs)
  114. {
  115. return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL : ST_UTIME_NO_TIMEOUT);
  116. }
  117. /*****************************************
  118. * Condition variable functions
  119. */
  120. _st_cond_t *st_cond_new(void)
  121. {
  122. _st_cond_t *cvar;
  123. cvar = (_st_cond_t *) calloc(1, sizeof(_st_cond_t));
  124. if (cvar) {
  125. ST_INIT_CLIST(&cvar->wait_q);
  126. }
  127. return cvar;
  128. }
  129. int st_cond_destroy(_st_cond_t *cvar)
  130. {
  131. if (cvar->wait_q.next != &cvar->wait_q) {
  132. errno = EBUSY;
  133. return -1;
  134. }
  135. free(cvar);
  136. return 0;
  137. }
  138. int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
  139. {
  140. _st_thread_t *me = _ST_CURRENT_THREAD();
  141. int rv;
  142. if (me->flags & _ST_FL_INTERRUPT) {
  143. me->flags &= ~_ST_FL_INTERRUPT;
  144. errno = EINTR;
  145. return -1;
  146. }
  147. /* Put caller thread on the condition variable's wait queue */
  148. me->state = _ST_ST_COND_WAIT;
  149. ST_APPEND_LINK(&me->wait_links, &cvar->wait_q);
  150. if (timeout != ST_UTIME_NO_TIMEOUT)
  151. _ST_ADD_SLEEPQ(me, timeout);
  152. _ST_SWITCH_CONTEXT(me);
  153. ST_REMOVE_LINK(&me->wait_links);
  154. rv = 0;
  155. if (me->flags & _ST_FL_TIMEDOUT) {
  156. me->flags &= ~_ST_FL_TIMEDOUT;
  157. errno = ETIME;
  158. rv = -1;
  159. }
  160. if (me->flags & _ST_FL_INTERRUPT) {
  161. me->flags &= ~_ST_FL_INTERRUPT;
  162. errno = EINTR;
  163. rv = -1;
  164. }
  165. return rv;
  166. }
  167. int st_cond_wait(_st_cond_t *cvar)
  168. {
  169. return st_cond_timedwait(cvar, ST_UTIME_NO_TIMEOUT);
  170. }
  171. static int _st_cond_signal(_st_cond_t *cvar, int broadcast)
  172. {
  173. _st_thread_t *thread;
  174. _st_clist_t *q;
  175. for (q = cvar->wait_q.next; q != &cvar->wait_q; q = q->next) {
  176. thread = _ST_THREAD_WAITQ_PTR(q);
  177. if (thread->state == _ST_ST_COND_WAIT) {
  178. if (thread->flags & _ST_FL_ON_SLEEPQ)
  179. _ST_DEL_SLEEPQ(thread);
  180. /* Make thread runnable */
  181. thread->state = _ST_ST_RUNNABLE;
  182. _ST_ADD_RUNQ(thread);
  183. if (!broadcast)
  184. break;
  185. }
  186. }
  187. return 0;
  188. }
  189. int st_cond_signal(_st_cond_t *cvar)
  190. {
  191. return _st_cond_signal(cvar, 0);
  192. }
  193. int st_cond_broadcast(_st_cond_t *cvar)
  194. {
  195. return _st_cond_signal(cvar, 1);
  196. }
  197. /*****************************************
  198. * Mutex functions
  199. */
  200. _st_mutex_t *st_mutex_new(void)
  201. {
  202. _st_mutex_t *lock;
  203. lock = (_st_mutex_t *) calloc(1, sizeof(_st_mutex_t));
  204. if (lock) {
  205. ST_INIT_CLIST(&lock->wait_q);
  206. lock->owner = NULL;
  207. }
  208. return lock;
  209. }
  210. int st_mutex_destroy(_st_mutex_t *lock)
  211. {
  212. if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) {
  213. errno = EBUSY;
  214. return -1;
  215. }
  216. free(lock);
  217. return 0;
  218. }
  219. int st_mutex_lock(_st_mutex_t *lock)
  220. {
  221. _st_thread_t *me = _ST_CURRENT_THREAD();
  222. if (me->flags & _ST_FL_INTERRUPT) {
  223. me->flags &= ~_ST_FL_INTERRUPT;
  224. errno = EINTR;
  225. return -1;
  226. }
  227. if (lock->owner == NULL) {
  228. /* Got the mutex */
  229. lock->owner = me;
  230. return 0;
  231. }
  232. if (lock->owner == me) {
  233. errno = EDEADLK;
  234. return -1;
  235. }
  236. /* Put caller thread on the mutex's wait queue */
  237. me->state = _ST_ST_LOCK_WAIT;
  238. ST_APPEND_LINK(&me->wait_links, &lock->wait_q);
  239. _ST_SWITCH_CONTEXT(me);
  240. ST_REMOVE_LINK(&me->wait_links);
  241. if ((me->flags & _ST_FL_INTERRUPT) && lock->owner != me) {
  242. me->flags &= ~_ST_FL_INTERRUPT;
  243. errno = EINTR;
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. int st_mutex_unlock(_st_mutex_t *lock)
  249. {
  250. _st_thread_t *thread;
  251. _st_clist_t *q;
  252. if (lock->owner != _ST_CURRENT_THREAD()) {
  253. errno = EPERM;
  254. return -1;
  255. }
  256. for (q = lock->wait_q.next; q != &lock->wait_q; q = q->next) {
  257. thread = _ST_THREAD_WAITQ_PTR(q);
  258. if (thread->state == _ST_ST_LOCK_WAIT) {
  259. lock->owner = thread;
  260. /* Make thread runnable */
  261. thread->state = _ST_ST_RUNNABLE;
  262. _ST_ADD_RUNQ(thread);
  263. return 0;
  264. }
  265. }
  266. /* No threads waiting on this mutex */
  267. lock->owner = NULL;
  268. return 0;
  269. }
  270. int st_mutex_trylock(_st_mutex_t *lock)
  271. {
  272. if (lock->owner != NULL) {
  273. errno = EBUSY;
  274. return -1;
  275. }
  276. /* Got the mutex */
  277. lock->owner = _ST_CURRENT_THREAD();
  278. return 0;
  279. }