tst-cond9.c 3.1 KB

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