pthread_rwlock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Copyright (c) 2006-2024 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. /**
  43. * @brief Initializes a read-write lock.
  44. *
  45. * This function initializes the read-write lock object pointed to by `rwlock` with the
  46. * attributes specified by `attr`. If `attr` is `NULL`, the default attributes are used.
  47. * A read-write lock allows multiple threads to read or a single thread to write, but not both simultaneously.
  48. *
  49. * @param rwlock A pointer to the read-write lock object to be initialized.
  50. * Must point to valid memory.
  51. * @param attr A pointer to the attributes for the read-write lock.
  52. * If `NULL`, default attributes are applied.
  53. *
  54. * @return
  55. * - `0` on success.
  56. * - A non-zero error code on failure, including:
  57. * - `EINVAL`: Invalid attributes.
  58. *
  59. * @note
  60. * - The read-write lock must be destroyed using `pthread_rwlock_destroy()` when it is no longer needed.
  61. * - 'rw_mutex' is used for protecting rwlock data.
  62. * 'rw_condreaders' is a condition variable for controlling readers.
  63. * 'rw_condwriters' is a condition variable for controlling writers.
  64. *
  65. * @see pthread_rwlock_destroy, pthread_rwlock_rdlock, pthread_rwlock_wrlock, pthread_rwlock_unlock
  66. */
  67. int pthread_rwlock_init(pthread_rwlock_t *rwlock,
  68. const pthread_rwlockattr_t *attr)
  69. {
  70. if (!rwlock)
  71. return EINVAL;
  72. rwlock->attr = PTHREAD_PROCESS_PRIVATE;
  73. pthread_mutex_init(&(rwlock->rw_mutex), NULL);
  74. pthread_cond_init(&(rwlock->rw_condreaders), NULL);
  75. pthread_cond_init(&(rwlock->rw_condwriters), NULL);
  76. rwlock->rw_nwaitwriters = 0;
  77. rwlock->rw_nwaitreaders = 0;
  78. rwlock->rw_refcount = 0;
  79. return 0;
  80. }
  81. RTM_EXPORT(pthread_rwlock_init);
  82. /**
  83. * @brief Destroys a read-write lock.
  84. *
  85. * This function destroys the read-write lock object pointed to by `rwlock`. After
  86. * the lock is destroyed, it cannot be used until it is reinitialized with
  87. * `pthread_rwlock_init`. Any threads currently blocked on the lock are affected by the destruction.
  88. *
  89. * @param rwlock A pointer to the read-write lock object to be destroyed.
  90. * Must point to a valid, initialized read-write lock.
  91. *
  92. * @return
  93. * - `0` on success.
  94. * - A non-zero error code on failure, including:
  95. * - `EINVAL`: The `rwlock` is invalid or uninitialized.
  96. * - `EBUSY`: The lock is currently in use by a thread, and cannot be destroyed.
  97. *
  98. * @note
  99. * - The read-write lock must not be in use (i.e., no threads should be blocked on it)
  100. * when `pthread_rwlock_destroy` is called.
  101. * - Calling this function on an uninitialized or destroyed lock will result in undefined behavior.
  102. * - Ensure that all threads have unlocked the lock before attempting to destroy it.
  103. *
  104. * @see pthread_rwlock_init, pthread_rwlock_rdlock, pthread_rwlock_wrlock, pthread_rwlock_unlock
  105. */
  106. int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
  107. {
  108. int result;
  109. if (!rwlock)
  110. return EINVAL;
  111. if (rwlock->attr == -1)
  112. return 0; /* rwlock is not initialized */
  113. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  114. return(result);
  115. if (rwlock->rw_refcount != 0 ||
  116. rwlock->rw_nwaitreaders != 0 ||
  117. rwlock->rw_nwaitwriters != 0)
  118. {
  119. result = EBUSY;
  120. return result;
  121. }
  122. else
  123. {
  124. /* check whether busy */
  125. result = rt_sem_trytake(&(rwlock->rw_condreaders.sem));
  126. if (result == RT_EOK)
  127. {
  128. result = rt_sem_trytake(&(rwlock->rw_condwriters.sem));
  129. if (result == RT_EOK)
  130. {
  131. rt_sem_release(&(rwlock->rw_condreaders.sem));
  132. rt_sem_release(&(rwlock->rw_condwriters.sem));
  133. pthread_cond_destroy(&rwlock->rw_condreaders);
  134. pthread_cond_destroy(&rwlock->rw_condwriters);
  135. }
  136. else
  137. {
  138. rt_sem_release(&(rwlock->rw_condreaders.sem));
  139. result = EBUSY;
  140. }
  141. }
  142. else
  143. result = EBUSY;
  144. }
  145. pthread_mutex_unlock(&rwlock->rw_mutex);
  146. if (result == 0)
  147. pthread_mutex_destroy(&rwlock->rw_mutex);
  148. return result;
  149. }
  150. RTM_EXPORT(pthread_rwlock_destroy);
  151. /**
  152. * @brief Acquire a read lock on a read-write lock.
  153. *
  154. * This function locks the specified read-write lock for reading. If the lock
  155. * is already held by one or more threads for reading, the calling thread
  156. * can acquire the lock as well (shared access). However, if the lock is
  157. * held by another writer thread, or other writer thread has been waiting
  158. * for the lock, the calling thread will block until the write lock is released.
  159. *
  160. * @param rwlock A pointer to the read-write lock to be locked.
  161. *
  162. * @return - 0 on success.
  163. * - EINVAL if the rwlock is invalid.
  164. * - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
  165. *
  166. * @note A thread that has acquired a read lock must eventually release it
  167. * using `pthread_rwlock_unlock`. Multiple read locks can be held
  168. * simultaneously, but a write lock excludes all other locks.
  169. *
  170. * @see pthread_rwlock_unlock
  171. * @see pthread_rwlock_wrlock
  172. * @see pthread_rwlock_tryrdlock
  173. */
  174. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
  175. {
  176. int result;
  177. if (!rwlock)
  178. return EINVAL;
  179. if (rwlock->attr == -1)
  180. pthread_rwlock_init(rwlock, NULL);
  181. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  182. return(result);
  183. /* give preference to waiting writers */
  184. while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  185. {
  186. rwlock->rw_nwaitreaders++;
  187. /* rw_mutex will be released when waiting for rw_condreaders */
  188. result = pthread_cond_wait(&rwlock->rw_condreaders, &rwlock->rw_mutex);
  189. /* rw_mutex should have been taken again when returned from waiting */
  190. rwlock->rw_nwaitreaders--;
  191. if (result != 0) /* wait error */
  192. break;
  193. }
  194. /* another reader has a read lock */
  195. if (result == 0)
  196. rwlock->rw_refcount++;
  197. pthread_mutex_unlock(&rwlock->rw_mutex);
  198. return (result);
  199. }
  200. RTM_EXPORT(pthread_rwlock_rdlock);
  201. /**
  202. * @brief Try to acquire a read lock on a read-write lock without blocking.
  203. *
  204. * This function attempts to acquire a read lock on the specified read-write lock.
  205. * If the lock is already held for writing, the function will return immediately
  206. * without blocking the calling thread. If the lock is available for reading,
  207. * it will be acquired successfully.
  208. *
  209. * @param rwlock A pointer to the read-write lock to attempt to lock.
  210. *
  211. * @return - 0 on success, indicating the read lock was acquired.
  212. * - EBUSY if the lock is currently held for writing by another thread.
  213. * - EINVAL if the rwlock is invalid.
  214. *
  215. * @note This function is non-blocking and returns immediately if the lock
  216. * cannot be acquired. After successfully acquiring the read lock,
  217. * the thread must release it using `pthread_rwlock_unlock`.
  218. *
  219. * @see pthread_rwlock_unlock
  220. * @see pthread_rwlock_rdlock
  221. * @see pthread_rwlock_trywrlock
  222. */
  223. int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
  224. {
  225. int result;
  226. if (!rwlock)
  227. return EINVAL;
  228. if (rwlock->attr == -1)
  229. pthread_rwlock_init(rwlock, NULL);
  230. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  231. return(result);
  232. if (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  233. result = EBUSY; /* held by a writer or waiting writers */
  234. else
  235. rwlock->rw_refcount++; /* increment count of reader locks */
  236. pthread_mutex_unlock(&rwlock->rw_mutex);
  237. return(result);
  238. }
  239. RTM_EXPORT(pthread_rwlock_tryrdlock);
  240. /**
  241. * @brief Acquire a read lock on a read-write lock with a timeout.
  242. *
  243. * This function attempts to lock the specified read-write lock for reading,
  244. * blocking until the lock becomes available or the specified timeout expires.
  245. * If the lock is held for writing by another thread, the calling thread will
  246. * block, but only up to the time specified by `abstime`.
  247. *
  248. * @param rwlock A pointer to the read-write lock to be locked.
  249. * @param abstime A pointer to a `timespec` structure specifying the
  250. * absolute timeout (in seconds and nanoseconds since the
  251. * Epoch, 1970-01-01 00:00:00 UTC).
  252. *
  253. * @return - 0 on success, indicating the read lock was acquired.
  254. * - ETIMEDOUT if the timeout expired before the lock could be acquired.
  255. * - EINVAL if the `rwlock` is invalid or `abstime` contains invalid values.
  256. * - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
  257. *
  258. * @note The timeout is specified as an absolute time (not relative). After
  259. * acquiring the read lock, the thread must release it using
  260. * `pthread_rwlock_unlock`.
  261. *
  262. * @warning If the system clock is changed (e.g., via manual adjustment or
  263. * NTP synchronization), the timeout behavior may be affected.
  264. *
  265. * @see pthread_rwlock_unlock
  266. * @see pthread_rwlock_rdlock
  267. * @see pthread_rwlock_tryrdlock
  268. */
  269. int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
  270. const struct timespec *abstime)
  271. {
  272. int result;
  273. if (!rwlock)
  274. return EINVAL;
  275. if (rwlock->attr == -1)
  276. pthread_rwlock_init(rwlock, NULL);
  277. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  278. return(result);
  279. /* give preference to waiting writers */
  280. while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
  281. {
  282. rwlock->rw_nwaitreaders++;
  283. /* rw_mutex will be released when waiting for rw_condreaders */
  284. result = pthread_cond_timedwait(&rwlock->rw_condreaders, &rwlock->rw_mutex, abstime);
  285. /* rw_mutex should have been taken again when returned from waiting */
  286. rwlock->rw_nwaitreaders--;
  287. if (result != 0)
  288. break;
  289. }
  290. /* another reader has a read lock */
  291. if (result == 0)
  292. rwlock->rw_refcount++;
  293. pthread_mutex_unlock(&rwlock->rw_mutex);
  294. return (result);
  295. }
  296. RTM_EXPORT(pthread_rwlock_timedrdlock);
  297. /**
  298. * @brief Acquire a write lock on a read-write lock with a timeout.
  299. *
  300. * This function attempts to acquire a write lock on the specified read-write lock,
  301. * blocking until the lock becomes available or the specified timeout expires.
  302. * If the lock is already held for reading or writing by another thread, the
  303. * calling thread will block, but only up to the time specified by `abstime`.
  304. *
  305. * @param rwlock A pointer to the read-write lock to be locked.
  306. * @param abstime A pointer to a `timespec` structure specifying the
  307. * absolute timeout (in seconds and nanoseconds since the
  308. * Epoch, 1970-01-01 00:00:00 UTC).
  309. *
  310. * @return
  311. * - 0 on success, indicating the write lock was acquired.
  312. * - EINVAL if the `rwlock` is invalid or `abstime` contains invalid values.
  313. * - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
  314. *
  315. * @note The timeout is specified as an absolute time (not relative). After
  316. * acquiring the write lock, the thread must release it using
  317. * `pthread_rwlock_unlock`.
  318. *
  319. * @warning If the system clock is adjusted (e.g., manually or via NTP synchronization),
  320. * the timeout behavior may be affected.
  321. *
  322. * @see pthread_rwlock_unlock
  323. * @see pthread_rwlock_wrlock
  324. * @see pthread_rwlock_trywrlock
  325. */
  326. int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
  327. const struct timespec *abstime)
  328. {
  329. int result;
  330. if (!rwlock)
  331. return EINVAL;
  332. if (rwlock->attr == -1)
  333. pthread_rwlock_init(rwlock, NULL);
  334. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  335. return(result);
  336. while (rwlock->rw_refcount != 0)
  337. {
  338. rwlock->rw_nwaitwriters++;
  339. /* rw_mutex will be released when waiting for rw_condwriters */
  340. result = pthread_cond_timedwait(&rwlock->rw_condwriters, &rwlock->rw_mutex, abstime);
  341. /* rw_mutex should have been taken again when returned from waiting */
  342. rwlock->rw_nwaitwriters--;
  343. if (result != 0)
  344. break;
  345. }
  346. if (result == 0)
  347. rwlock->rw_refcount = -1;
  348. pthread_mutex_unlock(&rwlock->rw_mutex);
  349. return(result);
  350. }
  351. RTM_EXPORT(pthread_rwlock_timedwrlock);
  352. /**
  353. * @brief Try to acquire a write lock on a read-write lock without blocking.
  354. *
  355. * This function attempts to acquire a write lock on the specified read-write lock.
  356. * If the lock is already held for reading or writing by another thread, the function
  357. * will return immediately without blocking the calling thread. If the lock is
  358. * available, it will be acquired successfully.
  359. *
  360. * @param rwlock A pointer to the read-write lock to attempt to lock.
  361. *
  362. * @return
  363. * - 0 on success, indicating the write lock was acquired.
  364. * - EBUSY if the lock is currently held by another thread (read or write lock).
  365. * - EINVAL if the `rwlock` is invalid.
  366. *
  367. * @note This function is non-blocking and returns immediately if the lock cannot
  368. * be acquired. After successfully acquiring the write lock, the thread must
  369. * release it using `pthread_rwlock_unlock`.
  370. *
  371. * @see pthread_rwlock_unlock
  372. * @see pthread_rwlock_wrlock
  373. * @see pthread_rwlock_timedwrlock
  374. */
  375. int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
  376. {
  377. int result;
  378. if (!rwlock)
  379. return EINVAL;
  380. if (rwlock->attr == -1)
  381. pthread_rwlock_init(rwlock, NULL);
  382. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  383. return(result);
  384. if (rwlock->rw_refcount != 0)
  385. result = EBUSY; /* held by either writer or reader(s) */
  386. else
  387. rwlock->rw_refcount = -1; /* available, indicate a writer has it */
  388. pthread_mutex_unlock(&rwlock->rw_mutex);
  389. return(result);
  390. }
  391. RTM_EXPORT(pthread_rwlock_trywrlock);
  392. /**
  393. * @brief Release a read or write lock on a read-write lock.
  394. *
  395. * This function unlocks the specified read-write lock, releasing either a read
  396. * lock or a write lock held by the calling thread. If the calling thread does
  397. * not hold the lock, the behavior is undefined.
  398. *
  399. * @param rwlock A pointer to the read-write lock to be unlocked.
  400. *
  401. * @return
  402. * - 0 on success.
  403. * - EINVAL if the `rwlock` is invalid.
  404. *
  405. * @note
  406. * - This function must only be called by the thread that successfully acquired
  407. * the lock.
  408. *
  409. * @see pthread_rwlock_rdlock
  410. * @see pthread_rwlock_wrlock
  411. * @see pthread_rwlock_tryrdlock
  412. * @see pthread_rwlock_trywrlock
  413. */
  414. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
  415. {
  416. int result;
  417. if (!rwlock)
  418. return EINVAL;
  419. if (rwlock->attr == -1)
  420. pthread_rwlock_init(rwlock, NULL);
  421. if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  422. return(result);
  423. if (rwlock->rw_refcount > 0)
  424. rwlock->rw_refcount--; /* releasing a reader */
  425. else if (rwlock->rw_refcount == -1)
  426. rwlock->rw_refcount = 0; /* releasing a writer */
  427. /* give preference to waiting writers over waiting readers */
  428. if (rwlock->rw_nwaitwriters > 0)
  429. {
  430. if (rwlock->rw_refcount == 0)
  431. result = pthread_cond_signal(&rwlock->rw_condwriters);
  432. }
  433. else if (rwlock->rw_nwaitreaders > 0)
  434. {
  435. result = pthread_cond_broadcast(&rwlock->rw_condreaders);
  436. }
  437. pthread_mutex_unlock(&rwlock->rw_mutex);
  438. return(result);
  439. }
  440. RTM_EXPORT(pthread_rwlock_unlock);
  441. /**
  442. * @brief Acquire a write lock on a read-write lock.
  443. *
  444. * This function locks the specified read-write lock for writing. If the lock
  445. * is already held by another thread for reading or writing, the calling thread
  446. * blocks until the lock becomes available.
  447. *
  448. * @param rwlock A pointer to the read-write lock to be locked.
  449. *
  450. * @return
  451. * - 0 on success, indicating the write lock was acquired.
  452. * - EINVAL if the `rwlock` is invalid.
  453. * - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
  454. *
  455. * @note
  456. * - A write lock is exclusive, meaning no other thread can acquire a read or
  457. * write lock while a write lock is held.
  458. * - The thread that successfully acquires the write lock must release it using
  459. * `pthread_rwlock_unlock`.
  460. *
  461. * @see pthread_rwlock_unlock
  462. * @see pthread_rwlock_trywrlock
  463. * @see pthread_rwlock_timedwrlock
  464. * @see pthread_rwlock_rdlock
  465. */
  466. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
  467. {
  468. int result;
  469. if (!rwlock)
  470. return EINVAL;
  471. if (rwlock->attr == -1)
  472. pthread_rwlock_init(rwlock, NULL);
  473. if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
  474. return(result);
  475. while (rwlock->rw_refcount != 0)
  476. {
  477. rwlock->rw_nwaitwriters++;
  478. /* rw_mutex will be released when waiting for rw_condwriters */
  479. result = pthread_cond_wait(&rwlock->rw_condwriters, &rwlock->rw_mutex);
  480. /* rw_mutex should have been taken again when returned from waiting */
  481. rwlock->rw_nwaitwriters--;
  482. if (result != 0)
  483. break;
  484. }
  485. if (result == 0)
  486. rwlock->rw_refcount = -1;
  487. pthread_mutex_unlock(&rwlock->rw_mutex);
  488. return(result);
  489. }
  490. RTM_EXPORT(pthread_rwlock_wrlock);