pthread_rwlock.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * File : pthread_rwlock.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-10-26 Bernard the first version
  23. */
  24. #include <pthread.h>
  25. int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
  26. {
  27. if (!attr)
  28. return EINVAL;
  29. *attr = PTHREAD_PROCESS_PRIVATE;
  30. return 0;
  31. }
  32. RTM_EXPORT(pthread_rwlockattr_init);
  33. int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
  34. {
  35. if (!attr)
  36. return EINVAL;
  37. return 0;
  38. }
  39. RTM_EXPORT(pthread_rwlockattr_destroy);
  40. int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr,
  41. int *pshared)
  42. {
  43. if (!attr || !pshared)
  44. return EINVAL;
  45. *pshared = PTHREAD_PROCESS_PRIVATE;
  46. return 0;
  47. }
  48. RTM_EXPORT(pthread_rwlockattr_getpshared);
  49. int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
  50. {
  51. if (!attr || pshared != PTHREAD_PROCESS_PRIVATE)
  52. return EINVAL;
  53. return 0;
  54. }
  55. RTM_EXPORT(pthread_rwlockattr_setpshared);
  56. int pthread_rwlock_init(pthread_rwlock_t *rwlock,
  57. const pthread_rwlockattr_t *attr)
  58. {
  59. if (!rwlock)
  60. return EINVAL;
  61. rwlock->attr = PTHREAD_PROCESS_PRIVATE;
  62. pthread_mutex_init(&(rwlock->rw_mutex), NULL);
  63. pthread_cond_init(&(rwlock->rw_condreaders), NULL);
  64. pthread_cond_init(&(rwlock->rw_condwriters), NULL);
  65. rwlock->rw_nwaitwriters = 0;
  66. rwlock->rw_nwaitreaders = 0;
  67. rwlock->rw_refcount = 0;
  68. return 0;
  69. }
  70. RTM_EXPORT(pthread_rwlock_init);
  71. int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
  72. {
  73. int result;
  74. if (!rwlock)
  75. return EINVAL;
  76. if (rwlock->attr == -1)
  77. return 0; /* rwlock is not initialized */
  78. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  79. return(result);
  80. if (rwlock->rw_refcount != 0 ||
  81. rwlock->rw_nwaitreaders != 0 ||
  82. rwlock->rw_nwaitwriters != 0)
  83. {
  84. result = EBUSY;
  85. return result;
  86. }
  87. else
  88. {
  89. /* check whether busy */
  90. result = rt_sem_trytake(&(rwlock->rw_condreaders.sem));
  91. if (result == RT_EOK)
  92. {
  93. result = rt_sem_trytake(&(rwlock->rw_condwriters.sem));
  94. if (result == RT_EOK)
  95. {
  96. rt_sem_release(&(rwlock->rw_condreaders.sem));
  97. rt_sem_release(&(rwlock->rw_condwriters.sem));
  98. pthread_cond_destroy(&rwlock->rw_condreaders);
  99. pthread_cond_destroy(&rwlock->rw_condwriters);
  100. }
  101. else
  102. {
  103. rt_sem_release(&(rwlock->rw_condreaders.sem));
  104. result = EBUSY;
  105. }
  106. }
  107. else
  108. result = EBUSY;
  109. }
  110. pthread_mutex_unlock(&rwlock->rw_mutex);
  111. if (result == 0)
  112. pthread_mutex_destroy(&rwlock->rw_mutex);
  113. return result;
  114. }
  115. RTM_EXPORT(pthread_rwlock_destroy);
  116. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
  117. {
  118. int result;
  119. if (!rwlock)
  120. return EINVAL;
  121. if (rwlock->attr == -1)
  122. 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. /* rw_mutex will be released when waiting for rw_condreaders */
  130. result = pthread_cond_wait(&rwlock->rw_condreaders, &rwlock->rw_mutex);
  131. /* rw_mutex should have been taken again when returned from waiting */
  132. rwlock->rw_nwaitreaders--;
  133. if (result != 0) /* wait error */
  134. break;
  135. }
  136. /* another reader has a read lock */
  137. if (result == 0)
  138. rwlock->rw_refcount++;
  139. pthread_mutex_unlock(&rwlock->rw_mutex);
  140. return (result);
  141. }
  142. RTM_EXPORT(pthread_rwlock_rdlock);
  143. int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
  144. {
  145. int result;
  146. if (!rwlock)
  147. return EINVAL;
  148. if (rwlock->attr == -1)
  149. pthread_rwlock_init(rwlock, NULL);
  150. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  151. return(result);
  152. if (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  153. result = EBUSY; /* held by a writer or waiting writers */
  154. else
  155. rwlock->rw_refcount++; /* increment count of reader locks */
  156. pthread_mutex_unlock(&rwlock->rw_mutex);
  157. return(result);
  158. }
  159. RTM_EXPORT(pthread_rwlock_tryrdlock);
  160. int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
  161. const struct timespec *abstime)
  162. {
  163. int result;
  164. if (!rwlock)
  165. return EINVAL;
  166. if (rwlock->attr == -1)
  167. pthread_rwlock_init(rwlock, NULL);
  168. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  169. return(result);
  170. /* give preference to waiting writers */
  171. while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  172. {
  173. rwlock->rw_nwaitreaders++;
  174. /* rw_mutex will be released when waiting for rw_condreaders */
  175. result = pthread_cond_timedwait(&rwlock->rw_condreaders, &rwlock->rw_mutex, abstime);
  176. /* rw_mutex should have been taken again when returned from waiting */
  177. rwlock->rw_nwaitreaders--;
  178. if (result != 0)
  179. break;
  180. }
  181. /* another reader has a read lock */
  182. if (result == 0)
  183. rwlock->rw_refcount++;
  184. pthread_mutex_unlock(&rwlock->rw_mutex);
  185. return (result);
  186. }
  187. RTM_EXPORT(pthread_rwlock_timedrdlock);
  188. int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
  189. const struct timespec *abstime)
  190. {
  191. int result;
  192. if (!rwlock)
  193. return EINVAL;
  194. if (rwlock->attr == -1)
  195. pthread_rwlock_init(rwlock, NULL);
  196. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  197. return(result);
  198. while (rwlock->rw_refcount != 0)
  199. {
  200. rwlock->rw_nwaitwriters++;
  201. /* rw_mutex will be released when waiting for rw_condwriters */
  202. result = pthread_cond_timedwait(&rwlock->rw_condwriters, &rwlock->rw_mutex, abstime);
  203. /* rw_mutex should have been taken again when returned from waiting */
  204. rwlock->rw_nwaitwriters--;
  205. if (result != 0)
  206. break;
  207. }
  208. if (result == 0)
  209. rwlock->rw_refcount = -1;
  210. pthread_mutex_unlock(&rwlock->rw_mutex);
  211. return(result);
  212. }
  213. RTM_EXPORT(pthread_rwlock_timedwrlock);
  214. int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
  215. {
  216. int result;
  217. if (!rwlock)
  218. return EINVAL;
  219. if (rwlock->attr == -1)
  220. pthread_rwlock_init(rwlock, NULL);
  221. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  222. return(result);
  223. if (rwlock->rw_refcount != 0)
  224. result = EBUSY; /* held by either writer or reader(s) */
  225. else
  226. rwlock->rw_refcount = -1; /* available, indicate a writer has it */
  227. pthread_mutex_unlock(&rwlock->rw_mutex);
  228. return(result);
  229. }
  230. RTM_EXPORT(pthread_rwlock_trywrlock);
  231. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
  232. {
  233. int result;
  234. if (!rwlock)
  235. return EINVAL;
  236. if (rwlock->attr == -1)
  237. pthread_rwlock_init(rwlock, NULL);
  238. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  239. return(result);
  240. if (rwlock->rw_refcount > 0)
  241. rwlock->rw_refcount--; /* releasing a reader */
  242. else if (rwlock->rw_refcount == -1)
  243. rwlock->rw_refcount = 0; /* releasing a writer */
  244. /* give preference to waiting writers over waiting readers */
  245. if (rwlock->rw_nwaitwriters > 0)
  246. {
  247. if (rwlock->rw_refcount == 0)
  248. result = pthread_cond_signal(&rwlock->rw_condwriters);
  249. }
  250. else if (rwlock->rw_nwaitreaders > 0)
  251. {
  252. result = pthread_cond_broadcast(&rwlock->rw_condreaders);
  253. }
  254. pthread_mutex_unlock(&rwlock->rw_mutex);
  255. return(result);
  256. }
  257. RTM_EXPORT(pthread_rwlock_unlock);
  258. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
  259. {
  260. int result;
  261. if (!rwlock)
  262. return EINVAL;
  263. if (rwlock->attr == -1)
  264. pthread_rwlock_init(rwlock, NULL);
  265. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  266. return(result);
  267. while (rwlock->rw_refcount != 0)
  268. {
  269. rwlock->rw_nwaitwriters++;
  270. /* rw_mutex will be released when waiting for rw_condwriters */
  271. result = pthread_cond_wait(&rwlock->rw_condwriters, &rwlock->rw_mutex);
  272. /* rw_mutex should have been taken again when returned from waiting */
  273. rwlock->rw_nwaitwriters--;
  274. if (result != 0)
  275. break;
  276. }
  277. if (result == 0)
  278. rwlock->rw_refcount = -1;
  279. pthread_mutex_unlock(&rwlock->rw_mutex);
  280. return(result);
  281. }
  282. RTM_EXPORT(pthread_rwlock_wrlock);