tst-cond9.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, 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 <stdlib.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  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. static int
  59. do_test (void)
  60. {
  61. pthread_t th;
  62. int err;
  63. printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
  64. err = pthread_cond_wait (&cond, &mut);
  65. if (err == 0)
  66. {
  67. puts ("cond_wait did not fail");
  68. exit (1);
  69. }
  70. if (err != EPERM)
  71. {
  72. printf ("cond_wait didn't return EPERM but %d\n", err);
  73. exit (1);
  74. }
  75. /* Current time. */
  76. struct timeval tv;
  77. (void) gettimeofday (&tv, NULL);
  78. /* +1000 seconds in correct format. */
  79. struct timespec ts;
  80. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  81. ts.tv_sec += 1000;
  82. err = pthread_cond_timedwait (&cond, &mut, &ts);
  83. if (err == 0)
  84. {
  85. puts ("cond_timedwait did not fail");
  86. exit (1);
  87. }
  88. if (err != EPERM)
  89. {
  90. printf ("cond_timedwait didn't return EPERM but %d\n", err);
  91. exit (1);
  92. }
  93. if (pthread_mutex_lock (&mut) != 0)
  94. {
  95. puts ("parent: mutex_lock failed");
  96. exit (1);
  97. }
  98. puts ("creating thread");
  99. if (pthread_create (&th, NULL, tf, NULL) != 0)
  100. {
  101. puts ("create failed");
  102. exit (1);
  103. }
  104. void *r;
  105. if (pthread_join (th, &r) != 0)
  106. {
  107. puts ("join failed");
  108. exit (1);
  109. }
  110. if (r != (void *) 1l)
  111. {
  112. puts ("thread has wrong return value");
  113. exit (1);
  114. }
  115. puts ("done");
  116. return 0;
  117. }
  118. #define TEST_FUNCTION do_test ()
  119. #include "../test-skeleton.c"