pthread_rwlock_wrlock.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (C) 2003 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <sysdep.h>
  18. #include <lowlevellock.h>
  19. #include <pthread.h>
  20. #include <pthreadP.h>
  21. /* Acquire write lock for RWLOCK. */
  22. int
  23. __pthread_rwlock_wrlock (rwlock)
  24. pthread_rwlock_t *rwlock;
  25. {
  26. int result = 0;
  27. /* Make sure we are along. */
  28. lll_mutex_lock (rwlock->__data.__lock);
  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_mutex_unlock (rwlock->__data.__lock);
  57. /* Wait for the writer or reader(s) to finish. */
  58. lll_futex_wait (&rwlock->__data.__writer_wakeup, waitval);
  59. /* Get the lock. */
  60. lll_mutex_lock (rwlock->__data.__lock);
  61. /* To start over again, remove the thread from the writer list. */
  62. --rwlock->__data.__nr_writers_queued;
  63. }
  64. /* We are done, free the lock. */
  65. lll_mutex_unlock (rwlock->__data.__lock);
  66. return result;
  67. }
  68. weak_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock)
  69. strong_alias (__pthread_rwlock_wrlock, __pthread_rwlock_wrlock_internal)