oldsemaphore.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. int __old_sem_init(old_sem_t *sem, int pshared, unsigned int value)
  48. {
  49. if (value > SEM_VALUE_MAX) {
  50. errno = EINVAL;
  51. return -1;
  52. }
  53. if (pshared) {
  54. errno = ENOSYS;
  55. return -1;
  56. }
  57. sem->sem_spinlock = 0;
  58. sem->sem_status = ((long)value << 1) + 1;
  59. return 0;
  60. }
  61. /* Function called by pthread_cancel to remove the thread from
  62. waiting inside __old_sem_wait. Here we simply unconditionally
  63. indicate that the thread is to be woken, by returning 1. */
  64. static int old_sem_extricate_func(void *obj attribute_unused, pthread_descr th attribute_unused)
  65. {
  66. return 1;
  67. }
  68. int __old_sem_wait(old_sem_t * sem);
  69. int __old_sem_wait(old_sem_t * sem)
  70. {
  71. long oldstatus, newstatus;
  72. volatile pthread_descr self = thread_self();
  73. pthread_descr * th;
  74. pthread_extricate_if extr;
  75. /* Set up extrication interface */
  76. extr.pu_object = 0;
  77. extr.pu_extricate_func = old_sem_extricate_func;
  78. while (1) {
  79. /* Register extrication interface */
  80. __pthread_set_own_extricate_if(self, &extr);
  81. do {
  82. oldstatus = sem->sem_status;
  83. if ((oldstatus & 1) && (oldstatus != 1))
  84. newstatus = oldstatus - 2;
  85. else {
  86. newstatus = (long) self;
  87. self->p_nextwaiting = (pthread_descr) oldstatus;
  88. }
  89. }
  90. while (! sem_compare_and_swap(sem, oldstatus, newstatus));
  91. if (newstatus & 1) {
  92. /* We got the semaphore. */
  93. __pthread_set_own_extricate_if(self, 0);
  94. return 0;
  95. }
  96. /* Wait for sem_post or cancellation */
  97. suspend(self);
  98. __pthread_set_own_extricate_if(self, 0);
  99. /* This is a cancellation point */
  100. if (self->p_canceled && self->p_cancelstate == PTHREAD_CANCEL_ENABLE) {
  101. /* Remove ourselves from the waiting list if we're still on it */
  102. /* First check if we're at the head of the list. */
  103. do {
  104. oldstatus = sem->sem_status;
  105. if (oldstatus != (long) self) break;
  106. newstatus = (long) self->p_nextwaiting;
  107. }
  108. while (! sem_compare_and_swap(sem, oldstatus, newstatus));
  109. /* Now, check if we're somewhere in the list.
  110. There's a race condition with sem_post here, but it does not matter:
  111. the net result is that at the time pthread_exit is called,
  112. self is no longer reachable from sem->sem_status. */
  113. if (oldstatus != (long) self && (oldstatus & 1) == 0) {
  114. for (th = &(((pthread_descr) oldstatus)->p_nextwaiting);
  115. *th != NULL && *th != (pthread_descr) 1;
  116. th = &((*th)->p_nextwaiting)) {
  117. if (*th == self) {
  118. *th = self->p_nextwaiting;
  119. break;
  120. }
  121. }
  122. }
  123. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  124. }
  125. }
  126. }
  127. int __old_sem_trywait(old_sem_t * sem);
  128. int __old_sem_trywait(old_sem_t * sem)
  129. {
  130. long oldstatus, newstatus;
  131. do {
  132. oldstatus = sem->sem_status;
  133. if ((oldstatus & 1) == 0 || (oldstatus == 1)) {
  134. errno = EAGAIN;
  135. return -1;
  136. }
  137. newstatus = oldstatus - 2;
  138. }
  139. while (! sem_compare_and_swap(sem, oldstatus, newstatus));
  140. return 0;
  141. }
  142. int __old_sem_post(old_sem_t * sem);
  143. int __old_sem_post(old_sem_t * sem)
  144. {
  145. long oldstatus, newstatus;
  146. do {
  147. oldstatus = sem->sem_status;
  148. if ((oldstatus & 1) == 0)
  149. newstatus = 3;
  150. else {
  151. if (oldstatus >= SEM_VALUE_MAX) {
  152. /* Overflow */
  153. errno = ERANGE;
  154. return -1;
  155. }
  156. newstatus = oldstatus + 2;
  157. }
  158. }
  159. while (! sem_compare_and_swap(sem, oldstatus, newstatus));
  160. if ((oldstatus & 1) == 0)
  161. sem_restart_list((pthread_descr) oldstatus);
  162. return 0;
  163. }
  164. int __old_sem_getvalue(old_sem_t * sem, int * sval);
  165. int __old_sem_getvalue(old_sem_t * sem, int * sval)
  166. {
  167. long status = sem->sem_status;
  168. if (status & 1)
  169. *sval = (int)((unsigned long) status >> 1);
  170. else
  171. *sval = 0;
  172. return 0;
  173. }
  174. int __old_sem_destroy(old_sem_t * sem);
  175. int __old_sem_destroy(old_sem_t * sem)
  176. {
  177. if ((sem->sem_status & 1) == 0) {
  178. errno = EBUSY;
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. /* Auxiliary function for restarting all threads on a waiting list,
  184. in priority order. */
  185. static void sem_restart_list(pthread_descr waiting)
  186. {
  187. pthread_descr th, towake, *p;
  188. /* Sort list of waiting threads by decreasing priority (insertion sort) */
  189. towake = NULL;
  190. while (waiting != (pthread_descr) 1) {
  191. th = waiting;
  192. waiting = waiting->p_nextwaiting;
  193. p = &towake;
  194. while (*p != NULL && th->p_priority < (*p)->p_priority)
  195. p = &((*p)->p_nextwaiting);
  196. th->p_nextwaiting = *p;
  197. *p = th;
  198. }
  199. /* Wake up threads in priority order */
  200. while (towake != NULL) {
  201. th = towake;
  202. towake = towake->p_nextwaiting;
  203. th->p_nextwaiting = NULL;
  204. restart(th);
  205. }
  206. }
  207. #if defined __PIC__ && defined DO_VERSIONING
  208. symbol_version (__old_sem_init, sem_init, GLIBC_2.0);
  209. symbol_version (__old_sem_wait, sem_wait, GLIBC_2.0);
  210. symbol_version (__old_sem_trywait, sem_trywait, GLIBC_2.0);
  211. symbol_version (__old_sem_post, sem_post, GLIBC_2.0);
  212. symbol_version (__old_sem_getvalue, sem_getvalue, GLIBC_2.0);
  213. symbol_version (__old_sem_destroy, sem_destroy, GLIBC_2.0);
  214. #endif