/nptl/tst-mutex5.c

https://github.com/nitinkamble/glibc · C · 202 lines · 150 code · 31 blank · 21 comment · 39 complexity · 85bc2fa6d3e16e1b5638a9b15551c54d MD5 · raw file

  1. /* Copyright (C) 2002, 2003, 2004, 2006 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 <pthread.h>
  18. #include <stdio.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include <sys/time.h>
  22. #ifndef TYPE
  23. # define TYPE PTHREAD_MUTEX_NORMAL
  24. #endif
  25. static int
  26. do_test (void)
  27. {
  28. pthread_mutex_t m;
  29. struct timespec ts;
  30. struct timeval tv;
  31. struct timeval tv2;
  32. int err;
  33. pthread_mutexattr_t a;
  34. if (pthread_mutexattr_init (&a) != 0)
  35. {
  36. puts ("mutexattr_init failed");
  37. return 1;
  38. }
  39. if (pthread_mutexattr_settype (&a, TYPE) != 0)
  40. {
  41. puts ("mutexattr_settype failed");
  42. return 1;
  43. }
  44. #ifdef ENABLE_PI
  45. if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
  46. {
  47. puts ("pthread_mutexattr_setprotocol failed");
  48. return 1;
  49. }
  50. #endif
  51. err = pthread_mutex_init (&m, &a);
  52. if (err != 0)
  53. {
  54. #ifdef ENABLE_PI
  55. if (err == ENOTSUP)
  56. {
  57. puts ("PI mutexes unsupported");
  58. return 0;
  59. }
  60. #endif
  61. puts ("mutex_init failed");
  62. return 1;
  63. }
  64. if (pthread_mutexattr_destroy (&a) != 0)
  65. {
  66. puts ("mutexattr_destroy failed");
  67. return 1;
  68. }
  69. if (pthread_mutex_lock (&m) != 0)
  70. {
  71. puts ("mutex_lock failed");
  72. return 1;
  73. }
  74. if (pthread_mutex_trylock (&m) == 0)
  75. {
  76. puts ("mutex_trylock succeeded");
  77. return 1;
  78. }
  79. gettimeofday (&tv, NULL);
  80. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  81. ts.tv_sec += 2; /* Wait 2 seconds. */
  82. err = pthread_mutex_timedlock (&m, &ts);
  83. if (err == 0)
  84. {
  85. puts ("timedlock succeeded");
  86. return 1;
  87. }
  88. else if (err != ETIMEDOUT)
  89. {
  90. printf ("timedlock error != ETIMEDOUT: %d\n", err);
  91. return 1;
  92. }
  93. else
  94. {
  95. int clk_tck = sysconf (_SC_CLK_TCK);
  96. gettimeofday (&tv2, NULL);
  97. tv2.tv_sec -= tv.tv_sec;
  98. tv2.tv_usec -= tv.tv_usec;
  99. if (tv2.tv_usec < 0)
  100. {
  101. tv2.tv_usec += 1000000;
  102. tv2.tv_sec -= 1;
  103. }
  104. /* Be a bit tolerant, add one CLK_TCK. */
  105. tv2.tv_usec += 1000000 / clk_tck;
  106. if (tv2.tv_usec >= 1000000)
  107. {
  108. tv2.tv_usec -= 1000000;
  109. ++tv2.tv_sec;
  110. }
  111. if (tv2.tv_sec < 2)
  112. {
  113. printf ("premature timeout: %ld.%06ld difference\n",
  114. tv2.tv_sec, tv2.tv_usec);
  115. return 1;
  116. }
  117. }
  118. (void) gettimeofday (&tv, NULL);
  119. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  120. ts.tv_sec += 2; /* Wait 2 seconds. */
  121. /* The following makes the ts value invalid. */
  122. ts.tv_nsec += 1000000000;
  123. err = pthread_mutex_timedlock (&m, &ts);
  124. if (err == 0)
  125. {
  126. puts ("2nd timedlock succeeded");
  127. return 1;
  128. }
  129. else if (err != EINVAL)
  130. {
  131. printf ("2nd timedlock error != EINVAL: %d\n", err);
  132. return 1;
  133. }
  134. if (pthread_mutex_unlock (&m) != 0)
  135. {
  136. puts ("mutex_unlock failed");
  137. return 1;
  138. }
  139. (void) gettimeofday (&tv, NULL);
  140. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  141. ts.tv_sec += 2; /* Wait 2 seconds. */
  142. if (pthread_mutex_timedlock (&m, &ts) != 0)
  143. {
  144. puts ("3rd timedlock failed");
  145. }
  146. (void) gettimeofday (&tv2, NULL);
  147. /* Check that timedlock didn't delay. We use a limit of 0.1 secs. */
  148. timersub (&tv2, &tv, &tv2);
  149. if (tv2.tv_sec > 0 || tv2.tv_usec > 100000)
  150. {
  151. puts ("3rd timedlock didn't return right away");
  152. return 1;
  153. }
  154. if (pthread_mutex_unlock (&m) != 0)
  155. {
  156. puts ("final mutex_unlock failed");
  157. return 1;
  158. }
  159. if (pthread_mutex_destroy (&m) != 0)
  160. {
  161. puts ("mutex_destroy failed");
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. #define TIMEOUT 4
  167. #define TEST_FUNCTION do_test ()
  168. #include "../test-skeleton.c"