pthread_rwlock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include <pthread.h>
  2. int pthread_rwlockattr_init (pthread_rwlockattr_t * attr)
  3. {
  4. if (!attr) return EINVAL;
  5. *attr = PTHREAD_PROCESS_PRIVATE;
  6. return 0;
  7. }
  8. int pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr)
  9. {
  10. if (!attr) return EINVAL;
  11. return 0;
  12. }
  13. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr, int *pshared)
  14. {
  15. if (!attr || !pshared) return EINVAL;
  16. *pshared = PTHREAD_PROCESS_PRIVATE;
  17. return 0;
  18. }
  19. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t * attr, int pshared)
  20. {
  21. if (!attr || pshared != PTHREAD_PROCESS_PRIVATE) return EINVAL;
  22. return 0;
  23. }
  24. int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t * attr)
  25. {
  26. if (!rwlock) return EINVAL;
  27. rwlock->attr = PTHREAD_PROCESS_PRIVATE;
  28. pthread_mutex_init(&(rwlock->rw_mutex), NULL);
  29. pthread_cond_init(&(rwlock->rw_condreaders), NULL);
  30. pthread_cond_init(&(rwlock->rw_condwriters), NULL);
  31. rwlock->rw_nwaitwriters = 0;
  32. rwlock->rw_nwaitreaders = 0;
  33. rwlock->rw_refcount = 0;
  34. return 0;
  35. }
  36. int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
  37. {
  38. int result;
  39. if (!rwlock) return EINVAL;
  40. if (rwlock->attr == -1) return 0; /* rwlock is not initialized */
  41. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  42. return(result);
  43. if (rwlock->rw_refcount != 0 ||
  44. rwlock->rw_nwaitreaders != 0 || rwlock->rw_nwaitwriters != 0)
  45. {
  46. result = EBUSY;
  47. return(EBUSY);
  48. }
  49. else
  50. {
  51. /* check whether busy */
  52. result = rt_sem_trytake(&(rwlock->rw_condreaders.sem));
  53. if (result == RT_EOK)
  54. {
  55. result = rt_sem_trytake(&(rwlock->rw_condwriters.sem));
  56. if (result == RT_EOK)
  57. {
  58. rt_sem_release(&(rwlock->rw_condreaders.sem));
  59. rt_sem_release(&(rwlock->rw_condwriters.sem));
  60. pthread_cond_destroy(&rwlock->rw_condreaders);
  61. pthread_cond_destroy(&rwlock->rw_condwriters);
  62. }
  63. else
  64. {
  65. rt_sem_release(&(rwlock->rw_condreaders.sem));
  66. result = EBUSY;
  67. }
  68. }
  69. else result = EBUSY;
  70. }
  71. pthread_mutex_unlock(&rwlock->rw_mutex);
  72. if (result == 0) pthread_mutex_destroy(&rwlock->rw_mutex);
  73. return result;
  74. }
  75. int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
  76. {
  77. int result;
  78. if (!rwlock) return EINVAL;
  79. if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
  80. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  81. return(result);
  82. /* give preference to waiting writers */
  83. while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  84. {
  85. rwlock->rw_nwaitreaders++;
  86. result = pthread_cond_wait(&rwlock->rw_condreaders, &rwlock->rw_mutex);
  87. rwlock->rw_nwaitreaders--;
  88. if (result != 0)
  89. break;
  90. }
  91. /* another reader has a read lock */
  92. if (result == 0) rwlock->rw_refcount++;
  93. pthread_mutex_unlock(&rwlock->rw_mutex);
  94. return (result);
  95. }
  96. int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
  97. {
  98. int result;
  99. if (!rwlock) return EINVAL;
  100. if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
  101. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  102. return(result);
  103. if (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  104. result = EBUSY; /* held by a writer or waiting writers */
  105. else
  106. rwlock->rw_refcount++; /* increment count of reader locks */
  107. pthread_mutex_unlock(&rwlock->rw_mutex);
  108. return(result);
  109. }
  110. int pthread_rwlock_timedrdlock (pthread_rwlock_t * rwlock, const struct timespec *abstime)
  111. {
  112. int result;
  113. if (!rwlock) return EINVAL;
  114. if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
  115. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  116. return(result);
  117. /* give preference to waiting writers */
  118. while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  119. {
  120. rwlock->rw_nwaitreaders++;
  121. result = pthread_cond_timedwait(&rwlock->rw_condreaders, &rwlock->rw_mutex, abstime);
  122. rwlock->rw_nwaitreaders--;
  123. if (result != 0)
  124. break;
  125. }
  126. /* another reader has a read lock */
  127. if (result == 0) rwlock->rw_refcount++;
  128. pthread_mutex_unlock(&rwlock->rw_mutex);
  129. return (result);
  130. }
  131. int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime)
  132. {
  133. int result;
  134. if (!rwlock) return EINVAL;
  135. if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
  136. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  137. return(result);
  138. while (rwlock->rw_refcount != 0)
  139. {
  140. rwlock->rw_nwaitwriters++;
  141. result = pthread_cond_timedwait(&rwlock->rw_condwriters, &rwlock->rw_mutex, abstime);
  142. rwlock->rw_nwaitwriters--;
  143. if (result != 0) break;
  144. }
  145. if (result == 0) rwlock->rw_refcount = -1;
  146. pthread_mutex_unlock(&rwlock->rw_mutex);
  147. return(result);
  148. }
  149. int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
  150. {
  151. int result;
  152. if (!rwlock) return EINVAL;
  153. if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
  154. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  155. return(result);
  156. if (rwlock->rw_refcount != 0)
  157. result = EBUSY; /* held by either writer or reader(s) */
  158. else
  159. rwlock->rw_refcount = -1; /* available, indicate a writer has it */
  160. pthread_mutex_unlock(&rwlock->rw_mutex);
  161. return(result);
  162. }
  163. int pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
  164. {
  165. int result;
  166. if (!rwlock) return EINVAL;
  167. if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
  168. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  169. return(result);
  170. if (rwlock->rw_refcount > 0)
  171. rwlock->rw_refcount--; /* releasing a reader */
  172. else if (rwlock->rw_refcount == -1)
  173. rwlock->rw_refcount = 0; /* releasing a reader */
  174. /* give preference to waiting writers over waiting readers */
  175. if (rwlock->rw_nwaitwriters > 0)
  176. {
  177. if (rwlock->rw_refcount == 0)
  178. result = pthread_cond_signal(&rwlock->rw_condwriters);
  179. }
  180. else if (rwlock->rw_nwaitreaders > 0)
  181. {
  182. result = pthread_cond_broadcast(&rwlock->rw_condreaders);
  183. }
  184. pthread_mutex_unlock(&rwlock->rw_mutex);
  185. return(result);
  186. }
  187. int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
  188. {
  189. int result;
  190. if (!rwlock) return EINVAL;
  191. if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
  192. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  193. return(result);
  194. while (rwlock->rw_refcount != 0)
  195. {
  196. rwlock->rw_nwaitwriters++;
  197. result = pthread_cond_wait(&rwlock->rw_condwriters, &rwlock->rw_mutex);
  198. rwlock->rw_nwaitwriters--;
  199. if (result != 0) break;
  200. }
  201. if (result == 0) rwlock->rw_refcount = -1;
  202. pthread_mutex_unlock(&rwlock->rw_mutex);
  203. return(result);
  204. }