mutex4.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * mutex4.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Thread A locks mutex - thread B tries to unlock.
  37. *
  38. * Depends on API functions:
  39. * pthread_mutex_lock()
  40. * pthread_mutex_trylock()
  41. * pthread_mutex_unlock()
  42. */
  43. #include "test.h"
  44. static int wasHere = 0;
  45. static pthread_mutex_t mutex1;
  46. void * unlocker(void * arg)
  47. {
  48. int expectedResult = (int)(size_t)arg;
  49. wasHere++;
  50. assert(pthread_mutex_unlock(&mutex1) == expectedResult);
  51. wasHere++;
  52. return NULL;
  53. }
  54. int
  55. main()
  56. {
  57. pthread_t t;
  58. pthread_mutexattr_t ma;
  59. assert(pthread_mutexattr_init(&ma) == 0);
  60. BEGIN_MUTEX_STALLED_ROBUST(ma)
  61. wasHere = 0;
  62. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_DEFAULT) == 0);
  63. assert(pthread_mutex_init(&mutex1, &ma) == 0);
  64. assert(pthread_mutex_lock(&mutex1) == 0);
  65. assert(pthread_create(&t, NULL, unlocker, (void *)(size_t)(IS_ROBUST?EPERM:0)) == 0);
  66. assert(pthread_join(t, NULL) == 0);
  67. assert(pthread_mutex_unlock(&mutex1) == 0);
  68. assert(wasHere == 2);
  69. wasHere = 0;
  70. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_NORMAL) == 0);
  71. assert(pthread_mutex_init(&mutex1, &ma) == 0);
  72. assert(pthread_mutex_lock(&mutex1) == 0);
  73. assert(pthread_create(&t, NULL, unlocker, (void *)(size_t)(IS_ROBUST?EPERM:0)) == 0);
  74. assert(pthread_join(t, NULL) == 0);
  75. assert(pthread_mutex_unlock(&mutex1) == 0);
  76. assert(wasHere == 2);
  77. wasHere = 0;
  78. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK) == 0);
  79. assert(pthread_mutex_init(&mutex1, &ma) == 0);
  80. assert(pthread_mutex_lock(&mutex1) == 0);
  81. assert(pthread_create(&t, NULL, unlocker, (void *)(size_t) EPERM) == 0);
  82. assert(pthread_join(t, NULL) == 0);
  83. assert(pthread_mutex_unlock(&mutex1) == 0);
  84. assert(wasHere == 2);
  85. wasHere = 0;
  86. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE) == 0);
  87. assert(pthread_mutex_init(&mutex1, &ma) == 0);
  88. assert(pthread_mutex_lock(&mutex1) == 0);
  89. assert(pthread_create(&t, NULL, unlocker, (void *)(size_t) EPERM) == 0);
  90. assert(pthread_join(t, NULL) == 0);
  91. assert(pthread_mutex_unlock(&mutex1) == 0);
  92. assert(wasHere == 2);
  93. END_MUTEX_STALLED_ROBUST(ma)
  94. return 0;
  95. }