1
0

pthread_rwlock.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-10-26 Bernard the first version
  9. */
  10. #include <pthread.h>
  11. int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
  12. {
  13. if (!attr)
  14. return EINVAL;
  15. *attr = PTHREAD_PROCESS_PRIVATE;
  16. return 0;
  17. }
  18. RTM_EXPORT(pthread_rwlockattr_init);
  19. int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
  20. {
  21. if (!attr)
  22. return EINVAL;
  23. return 0;
  24. }
  25. RTM_EXPORT(pthread_rwlockattr_destroy);
  26. int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr,
  27. int *pshared)
  28. {
  29. if (!attr || !pshared)
  30. return EINVAL;
  31. *pshared = PTHREAD_PROCESS_PRIVATE;
  32. return 0;
  33. }
  34. RTM_EXPORT(pthread_rwlockattr_getpshared);
  35. int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
  36. {
  37. if (!attr || pshared != PTHREAD_PROCESS_PRIVATE)
  38. return EINVAL;
  39. return 0;
  40. }
  41. RTM_EXPORT(pthread_rwlockattr_setpshared);
  42. int pthread_rwlock_init(pthread_rwlock_t *rwlock,
  43. const pthread_rwlockattr_t *attr)
  44. {
  45. if (!rwlock)
  46. return EINVAL;
  47. rwlock->attr = PTHREAD_PROCESS_PRIVATE;
  48. pthread_mutex_init(&(rwlock->rw_mutex), NULL);
  49. pthread_cond_init(&(rwlock->rw_condreaders), NULL);
  50. pthread_cond_init(&(rwlock->rw_condwriters), NULL);
  51. rwlock->rw_nwaitwriters = 0;
  52. rwlock->rw_nwaitreaders = 0;
  53. rwlock->rw_refcount = 0;
  54. return 0;
  55. }
  56. RTM_EXPORT(pthread_rwlock_init);
  57. int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
  58. {
  59. int result;
  60. if (!rwlock)
  61. return EINVAL;
  62. if (rwlock->attr == -1)
  63. return 0; /* rwlock is not initialized */
  64. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  65. return(result);
  66. if (rwlock->rw_refcount != 0 ||
  67. rwlock->rw_nwaitreaders != 0 ||
  68. rwlock->rw_nwaitwriters != 0)
  69. {
  70. result = EBUSY;
  71. return result;
  72. }
  73. else
  74. {
  75. /* check whether busy */
  76. result = rt_sem_trytake(&(rwlock->rw_condreaders.sem));
  77. if (result == RT_EOK)
  78. {
  79. result = rt_sem_trytake(&(rwlock->rw_condwriters.sem));
  80. if (result == RT_EOK)
  81. {
  82. rt_sem_release(&(rwlock->rw_condreaders.sem));
  83. rt_sem_release(&(rwlock->rw_condwriters.sem));
  84. pthread_cond_destroy(&rwlock->rw_condreaders);
  85. pthread_cond_destroy(&rwlock->rw_condwriters);
  86. }
  87. else
  88. {
  89. rt_sem_release(&(rwlock->rw_condreaders.sem));
  90. result = EBUSY;
  91. }
  92. }
  93. else
  94. result = EBUSY;
  95. }
  96. pthread_mutex_unlock(&rwlock->rw_mutex);
  97. if (result == 0)
  98. pthread_mutex_destroy(&rwlock->rw_mutex);
  99. return result;
  100. }
  101. RTM_EXPORT(pthread_rwlock_destroy);
  102. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
  103. {
  104. int result;
  105. if (!rwlock)
  106. return EINVAL;
  107. if (rwlock->attr == -1)
  108. pthread_rwlock_init(rwlock, NULL);
  109. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  110. return(result);
  111. /* give preference to waiting writers */
  112. while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  113. {
  114. rwlock->rw_nwaitreaders++;
  115. /* rw_mutex will be released when waiting for rw_condreaders */
  116. result = pthread_cond_wait(&rwlock->rw_condreaders, &rwlock->rw_mutex);
  117. /* rw_mutex should have been taken again when returned from waiting */
  118. rwlock->rw_nwaitreaders--;
  119. if (result != 0) /* wait error */
  120. break;
  121. }
  122. /* another reader has a read lock */
  123. if (result == 0)
  124. rwlock->rw_refcount++;
  125. pthread_mutex_unlock(&rwlock->rw_mutex);
  126. return (result);
  127. }
  128. RTM_EXPORT(pthread_rwlock_rdlock);
  129. int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
  130. {
  131. int result;
  132. if (!rwlock)
  133. return EINVAL;
  134. if (rwlock->attr == -1)
  135. pthread_rwlock_init(rwlock, NULL);
  136. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  137. return(result);
  138. if (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  139. result = EBUSY; /* held by a writer or waiting writers */
  140. else
  141. rwlock->rw_refcount++; /* increment count of reader locks */
  142. pthread_mutex_unlock(&rwlock->rw_mutex);
  143. return(result);
  144. }
  145. RTM_EXPORT(pthread_rwlock_tryrdlock);
  146. int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
  147. const struct timespec *abstime)
  148. {
  149. int result;
  150. if (!rwlock)
  151. return EINVAL;
  152. if (rwlock->attr == -1)
  153. pthread_rwlock_init(rwlock, NULL);
  154. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  155. return(result);
  156. /* give preference to waiting writers */
  157. while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  158. {
  159. rwlock->rw_nwaitreaders++;
  160. /* rw_mutex will be released when waiting for rw_condreaders */
  161. result = pthread_cond_timedwait(&rwlock->rw_condreaders, &rwlock->rw_mutex, abstime);
  162. /* rw_mutex should have been taken again when returned from waiting */
  163. rwlock->rw_nwaitreaders--;
  164. if (result != 0)
  165. break;
  166. }
  167. /* another reader has a read lock */
  168. if (result == 0)
  169. rwlock->rw_refcount++;
  170. pthread_mutex_unlock(&rwlock->rw_mutex);
  171. return (result);
  172. }
  173. RTM_EXPORT(pthread_rwlock_timedrdlock);
  174. int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
  175. const struct timespec *abstime)
  176. {
  177. int result;
  178. if (!rwlock)
  179. return EINVAL;
  180. if (rwlock->attr == -1)
  181. pthread_rwlock_init(rwlock, NULL);
  182. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  183. return(result);
  184. while (rwlock->rw_refcount != 0)
  185. {
  186. rwlock->rw_nwaitwriters++;
  187. /* rw_mutex will be released when waiting for rw_condwriters */
  188. result = pthread_cond_timedwait(&rwlock->rw_condwriters, &rwlock->rw_mutex, abstime);
  189. /* rw_mutex should have been taken again when returned from waiting */
  190. rwlock->rw_nwaitwriters--;
  191. if (result != 0)
  192. break;
  193. }
  194. if (result == 0)
  195. rwlock->rw_refcount = -1;
  196. pthread_mutex_unlock(&rwlock->rw_mutex);
  197. return(result);
  198. }
  199. RTM_EXPORT(pthread_rwlock_timedwrlock);
  200. int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
  201. {
  202. int result;
  203. if (!rwlock)
  204. return EINVAL;
  205. if (rwlock->attr == -1)
  206. pthread_rwlock_init(rwlock, NULL);
  207. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  208. return(result);
  209. if (rwlock->rw_refcount != 0)
  210. result = EBUSY; /* held by either writer or reader(s) */
  211. else
  212. rwlock->rw_refcount = -1; /* available, indicate a writer has it */
  213. pthread_mutex_unlock(&rwlock->rw_mutex);
  214. return(result);
  215. }
  216. RTM_EXPORT(pthread_rwlock_trywrlock);
  217. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
  218. {
  219. int result;
  220. if (!rwlock)
  221. return EINVAL;
  222. if (rwlock->attr == -1)
  223. pthread_rwlock_init(rwlock, NULL);
  224. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  225. return(result);
  226. if (rwlock->rw_refcount > 0)
  227. rwlock->rw_refcount--; /* releasing a reader */
  228. else if (rwlock->rw_refcount == -1)
  229. rwlock->rw_refcount = 0; /* releasing a writer */
  230. /* give preference to waiting writers over waiting readers */
  231. if (rwlock->rw_nwaitwriters > 0)
  232. {
  233. if (rwlock->rw_refcount == 0)
  234. result = pthread_cond_signal(&rwlock->rw_condwriters);
  235. }
  236. else if (rwlock->rw_nwaitreaders > 0)
  237. {
  238. result = pthread_cond_broadcast(&rwlock->rw_condreaders);
  239. }
  240. pthread_mutex_unlock(&rwlock->rw_mutex);
  241. return(result);
  242. }
  243. RTM_EXPORT(pthread_rwlock_unlock);
  244. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
  245. {
  246. int result;
  247. if (!rwlock)
  248. return EINVAL;
  249. if (rwlock->attr == -1)
  250. pthread_rwlock_init(rwlock, NULL);
  251. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  252. return(result);
  253. while (rwlock->rw_refcount != 0)
  254. {
  255. rwlock->rw_nwaitwriters++;
  256. /* rw_mutex will be released when waiting for rw_condwriters */
  257. result = pthread_cond_wait(&rwlock->rw_condwriters, &rwlock->rw_mutex);
  258. /* rw_mutex should have been taken again when returned from waiting */
  259. rwlock->rw_nwaitwriters--;
  260. if (result != 0)
  261. break;
  262. }
  263. if (result == 0)
  264. rwlock->rw_refcount = -1;
  265. pthread_mutex_unlock(&rwlock->rw_mutex);
  266. return(result);
  267. }
  268. RTM_EXPORT(pthread_rwlock_wrlock);