pthread_rwlock.c 7.0 KB

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