oldsemaphore.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * This file contains the old semaphore code that we need to
  3. * preserve for glibc-2.0 backwards compatibility. Port to glibc 2.1
  4. * done by Cristian Gafton.
  5. */
  6. /* Linuxthreads - a simple clone()-based implementation of Posix */
  7. /* threads for Linux. */
  8. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  9. /* */
  10. /* This program is free software; you can redistribute it and/or */
  11. /* modify it under the terms of the GNU Library General Public License */
  12. /* as published by the Free Software Foundation; either version 2 */
  13. /* of the License, or (at your option) any later version. */
  14. /* */
  15. /* This program is distributed in the hope that it will be useful, */
  16. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  17. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  18. /* GNU Library General Public License for more details. */
  19. /* Semaphores a la POSIX 1003.1b */
  20. #include <errno.h>
  21. #include "pthread.h"
  22. #include "internals.h"
  23. #include "spinlock.h"
  24. #include "restart.h"
  25. #include "queue.h"
  26. typedef struct {
  27. long int sem_status;
  28. int sem_spinlock;
  29. } old_sem_t;
  30. /* Maximum value the semaphore can have. */
  31. #define SEM_VALUE_MAX ((int) ((~0u) >> 1))
  32. static inline int sem_compare_and_swap(old_sem_t *sem, long oldval, long newval)
  33. {
  34. return compare_and_swap(&sem->sem_status, oldval, newval, &sem->sem_spinlock);
  35. }
  36. /* The state of a semaphore is represented by a long int encoding
  37. either the semaphore count if >= 0 and no thread is waiting on it,
  38. or the head of the list of threads waiting for the semaphore.
  39. To distinguish the two cases, we encode the semaphore count N
  40. as 2N+1, so that it has the lowest bit set.
  41. A sequence of sem_wait operations on a semaphore initialized to N
  42. result in the following successive states:
  43. 2N+1, 2N-1, ..., 3, 1, &first_waiting_thread, &second_waiting_thread, ...
  44. */
  45. static void sem_restart_list(pthread_descr waiting);
  46. int __old_sem_init(old_sem_t *sem, int pshared, unsigned int value)
  47. {
  48. if (value > SEM_VALUE_MAX) {
  49. errno = EINVAL;
  50. return -1;
  51. }
  52. if (pshared) {
  53. errno = ENOSYS;
  54. return -1;
  55. }
  56. sem->sem_spinlock = 0;
  57. sem->sem_status = ((long)value << 1) + 1;
  58. return 0;
  59. }
  60. /* Function called by pthread_cancel to remove the thread from
  61. waiting inside __old_sem_wait. Here we simply unconditionally
  62. indicate that the thread is to be woken, by returning 1. */
  63. static int old_sem_extricate_func(void *obj, pthread_descr th)
  64. {
  65. return 1;
  66. }
  67. int __old_sem_wait(old_sem_t * sem)
  68. {
  69. long oldstatus, newstatus;
  70. volatile pthread_descr self = thread_self();
  71. pthread_descr * th;
  72. pthread_extricate_if extr;
  73. /* Set up extrication interface */
  74. extr.pu_object = 0;
  75. extr.pu_extricate_func = old_sem_extricate_func;
  76. while (1) {
  77. /* Register extrication interface */
  78. __pthread_set_own_extricate_if(self, &extr);
  79. do {
  80. oldstatus = sem->sem_status;
  81. if ((oldstatus & 1) && (oldstatus != 1))
  82. newstatus = oldstatus - 2;
  83. else {
  84. newstatus = (long) self;
  85. self->p_nextwaiting = (pthread_descr) oldstatus;
  86. }
  87. }
  88. while (! sem_compare_and_swap(sem, oldstatus, newstatus));
  89. if (newstatus & 1) {
  90. /* We got the semaphore. */
  91. __pthread_set_own_extricate_if(self, 0);
  92. return 0;
  93. }
  94. /* Wait for sem_post or cancellation */
  95. suspend(self);
  96. __pthread_set_own_extricate_if(self, 0);
  97. /* This is a cancellation point */
  98. if (self->p_canceled && self->p_cancelstate == PTHREAD_CANCEL_ENABLE) {
  99. /* Remove ourselves from the waiting list if we're still on it */
  100. /* First check if we're at the head of the list. */
  101. do {
  102. oldstatus = sem->sem_status;
  103. if (oldstatus != (long) self) break;
  104. newstatus = (long) self->p_nextwaiting;
  105. }
  106. while (! sem_compare_and_swap(sem, oldstatus, newstatus));
  107. /* Now, check if we're somewhere in the list.
  108. There's a race condition with sem_post here, but it does not matter:
  109. the net result is that at the time pthread_exit is called,
  110. self is no longer reachable from sem->sem_status. */
  111. if (oldstatus != (long) self && (oldstatus & 1) == 0) {
  112. for (th = &(((pthread_descr) oldstatus)->p_nextwaiting);
  113. *th != NULL && *th != (pthread_descr) 1;
  114. th = &((*th)->p_nextwaiting)) {
  115. if (*th == self) {
  116. *th = self->p_nextwaiting;
  117. break;
  118. }
  119. }
  120. }
  121. pthread_exit(PTHREAD_CANCELED);
  122. }
  123. }
  124. }
  125. int __old_sem_trywait(old_sem_t * sem)
  126. {
  127. long oldstatus, newstatus;
  128. do {
  129. oldstatus = sem->sem_status;
  130. if ((oldstatus & 1) == 0 || (oldstatus == 1)) {
  131. errno = EAGAIN;
  132. return -1;
  133. }
  134. newstatus = oldstatus - 2;
  135. }
  136. while (! sem_compare_and_swap(sem, oldstatus, newstatus));
  137. return 0;
  138. }
  139. int __old_sem_post(old_sem_t * sem)
  140. {
  141. long oldstatus, newstatus;
  142. do {
  143. oldstatus = sem->sem_status;
  144. if ((oldstatus & 1) == 0)
  145. newstatus = 3;
  146. else {
  147. if (oldstatus >= SEM_VALUE_MAX) {
  148. /* Overflow */
  149. errno = ERANGE;
  150. return -1;
  151. }
  152. newstatus = oldstatus + 2;
  153. }
  154. }
  155. while (! sem_compare_and_swap(sem, oldstatus, newstatus));
  156. if ((oldstatus & 1) == 0)
  157. sem_restart_list((pthread_descr) oldstatus);
  158. return 0;
  159. }
  160. int __old_sem_getvalue(old_sem_t * sem, int * sval)
  161. {
  162. long status = sem->sem_status;
  163. if (status & 1)
  164. *sval = (int)((unsigned long) status >> 1);
  165. else
  166. *sval = 0;
  167. return 0;
  168. }
  169. int __old_sem_destroy(old_sem_t * sem)
  170. {
  171. if ((sem->sem_status & 1) == 0) {
  172. errno = EBUSY;
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. /* Auxiliary function for restarting all threads on a waiting list,
  178. in priority order. */
  179. static void sem_restart_list(pthread_descr waiting)
  180. {
  181. pthread_descr th, towake, *p;
  182. /* Sort list of waiting threads by decreasing priority (insertion sort) */
  183. towake = NULL;
  184. while (waiting != (pthread_descr) 1) {
  185. th = waiting;
  186. waiting = waiting->p_nextwaiting;
  187. p = &towake;
  188. while (*p != NULL && th->p_priority < (*p)->p_priority)
  189. p = &((*p)->p_nextwaiting);
  190. th->p_nextwaiting = *p;
  191. *p = th;
  192. }
  193. /* Wake up threads in priority order */
  194. while (towake != NULL) {
  195. th = towake;
  196. towake = towake->p_nextwaiting;
  197. th->p_nextwaiting = NULL;
  198. restart(th);
  199. }
  200. }
  201. #if defined __PIC__ && defined DO_VERSIONING
  202. symbol_version (__old_sem_init, sem_init, GLIBC_2.0);
  203. symbol_version (__old_sem_wait, sem_wait, GLIBC_2.0);
  204. symbol_version (__old_sem_trywait, sem_trywait, GLIBC_2.0);
  205. symbol_version (__old_sem_post, sem_post, GLIBC_2.0);
  206. symbol_version (__old_sem_getvalue, sem_getvalue, GLIBC_2.0);
  207. symbol_version (__old_sem_destroy, sem_destroy, GLIBC_2.0);
  208. #endif