pthread_mutex_unlock.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 "pthreadP.h"
  18. #include <lowlevellock.h>
  19. int
  20. internal_function attribute_hidden
  21. __pthread_mutex_unlock_usercnt (mutex, decr)
  22. pthread_mutex_t *mutex;
  23. int decr;
  24. {
  25. switch (__builtin_expect (mutex->__data.__kind, PTHREAD_MUTEX_TIMED_NP))
  26. {
  27. case PTHREAD_MUTEX_RECURSIVE_NP:
  28. /* Recursive mutex. */
  29. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  30. return EPERM;
  31. if (--mutex->__data.__count != 0)
  32. /* We still hold the mutex. */
  33. return 0;
  34. break;
  35. case PTHREAD_MUTEX_ERRORCHECK_NP:
  36. /* Error checking mutex. */
  37. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid)
  38. || ! lll_mutex_islocked (mutex->__data.__lock))
  39. return EPERM;
  40. break;
  41. default:
  42. /* Correct code cannot set any other type. */
  43. case PTHREAD_MUTEX_TIMED_NP:
  44. case PTHREAD_MUTEX_ADAPTIVE_NP:
  45. /* Normal mutex. Nothing special to do. */
  46. break;
  47. }
  48. /* Always reset the owner field. */
  49. mutex->__data.__owner = 0;
  50. if (decr)
  51. /* One less user. */
  52. --mutex->__data.__nusers;
  53. /* Unlock. */
  54. lll_mutex_unlock (mutex->__data.__lock);
  55. return 0;
  56. }
  57. int
  58. __pthread_mutex_unlock (mutex)
  59. pthread_mutex_t *mutex;
  60. {
  61. return __pthread_mutex_unlock_usercnt (mutex, 1);
  62. }
  63. strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
  64. strong_alias (__pthread_mutex_unlock, __pthread_mutex_unlock_internal)