tst-cond9.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  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 <stdlib.h>
  19. #include <time.h>
  20. #include <sys/time.h>
  21. #if defined(__GLIBC__) || defined(__UCLIBC__)
  22. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  23. static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
  24. static void *
  25. tf (void *arg)
  26. {
  27. int err = pthread_cond_wait (&cond, &mut);
  28. if (err == 0)
  29. {
  30. puts ("cond_wait did not fail");
  31. exit (1);
  32. }
  33. if (err != EPERM)
  34. {
  35. printf ("cond_wait didn't return EPERM but %d\n", err);
  36. exit (1);
  37. }
  38. /* Current time. */
  39. struct timeval tv;
  40. (void) gettimeofday (&tv, NULL);
  41. /* +1000 seconds in correct format. */
  42. struct timespec ts;
  43. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  44. ts.tv_sec += 1000;
  45. err = pthread_cond_timedwait (&cond, &mut, &ts);
  46. if (err == 0)
  47. {
  48. puts ("cond_timedwait did not fail");
  49. exit (1);
  50. }
  51. if (err != EPERM)
  52. {
  53. printf ("cond_timedwait didn't return EPERM but %d\n", err);
  54. exit (1);
  55. }
  56. return (void *) 1l;
  57. }
  58. #endif
  59. static int
  60. do_test (void)
  61. {
  62. #if defined(__GLIBC__) || defined(__UCLIBC__)
  63. pthread_t th;
  64. int err;
  65. printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
  66. err = pthread_cond_wait (&cond, &mut);
  67. if (err == 0)
  68. {
  69. puts ("cond_wait did not fail");
  70. exit (1);
  71. }
  72. if (err != EPERM)
  73. {
  74. printf ("cond_wait didn't return EPERM but %d\n", err);
  75. exit (1);
  76. }
  77. /* Current time. */
  78. struct timeval tv;
  79. (void) gettimeofday (&tv, NULL);
  80. /* +1000 seconds in correct format. */
  81. struct timespec ts;
  82. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  83. ts.tv_sec += 1000;
  84. err = pthread_cond_timedwait (&cond, &mut, &ts);
  85. if (err == 0)
  86. {
  87. puts ("cond_timedwait did not fail");
  88. exit (1);
  89. }
  90. if (err != EPERM)
  91. {
  92. printf ("cond_timedwait didn't return EPERM but %d\n", err);
  93. exit (1);
  94. }
  95. if (pthread_mutex_lock (&mut) != 0)
  96. {
  97. puts ("parent: mutex_lock failed");
  98. exit (1);
  99. }
  100. puts ("creating thread");
  101. if (pthread_create (&th, NULL, tf, NULL) != 0)
  102. {
  103. puts ("create failed");
  104. exit (1);
  105. }
  106. void *r;
  107. if (pthread_join (th, &r) != 0)
  108. {
  109. puts ("join failed");
  110. exit (1);
  111. }
  112. if (r != (void *) 1l)
  113. {
  114. puts ("thread has wrong return value");
  115. exit (1);
  116. }
  117. puts ("done");
  118. return 0;
  119. #else
  120. return 23;
  121. #endif
  122. }
  123. #define TEST_FUNCTION do_test ()
  124. #include "../test-skeleton.c"