tst-mutex5.c 4.0 KB

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