pthread_rwlock_wrlock.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright (C) 2003, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <sysdep.h>
  17. #include <lowlevellock.h>
  18. #include <pthread.h>
  19. #include <pthreadP.h>
  20. /* Acquire write lock for RWLOCK. */
  21. int
  22. attribute_protected
  23. __pthread_rwlock_wrlock (
  24. pthread_rwlock_t *rwlock)
  25. {
  26. int result = 0;
  27. /* Make sure we are along. */
  28. lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
  29. while (1)
  30. {
  31. /* Get the rwlock if there is no writer and no reader. */
  32. if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
  33. {
  34. /* Mark self as writer. */
  35. rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
  36. break;
  37. }
  38. /* Make sure we are not holding the rwlock as a writer. This is
  39. a deadlock situation we recognize and report. */
  40. if (__builtin_expect (rwlock->__data.__writer
  41. == THREAD_GETMEM (THREAD_SELF, tid), 0))
  42. {
  43. result = EDEADLK;
  44. break;
  45. }
  46. /* Remember that we are a writer. */
  47. if (++rwlock->__data.__nr_writers_queued == 0)
  48. {
  49. /* Overflow on number of queued writers. */
  50. --rwlock->__data.__nr_writers_queued;
  51. result = EAGAIN;
  52. break;
  53. }
  54. int waitval = rwlock->__data.__writer_wakeup;
  55. /* Free the lock. */
  56. lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
  57. /* Wait for the writer or reader(s) to finish. */
  58. lll_futex_wait (&rwlock->__data.__writer_wakeup, waitval,
  59. rwlock->__data.__shared);
  60. /* Get the lock. */
  61. lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
  62. /* To start over again, remove the thread from the writer list. */
  63. --rwlock->__data.__nr_writers_queued;
  64. }
  65. /* We are done, free the lock. */
  66. lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
  67. return result;
  68. }
  69. weak_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock)
  70. strong_alias (__pthread_rwlock_wrlock, __pthread_rwlock_wrlock_internal)