tst-mutex5.c 4.0 KB

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