tst-cond5.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright (C) 2002 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 <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  22. static pthread_mutex_t mut;
  23. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  24. static int
  25. do_test (void)
  26. {
  27. pthread_mutexattr_t ma;
  28. int err;
  29. struct timespec ts;
  30. struct timeval tv;
  31. if (pthread_mutexattr_init (&ma) != 0)
  32. {
  33. puts ("mutexattr_init failed");
  34. exit (1);
  35. }
  36. if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
  37. {
  38. puts ("mutexattr_settype failed");
  39. exit (1);
  40. }
  41. if (pthread_mutex_init (&mut, &ma) != 0)
  42. {
  43. puts ("mutex_init failed");
  44. exit (1);
  45. }
  46. /* Get the mutex. */
  47. if (pthread_mutex_lock (&mut) != 0)
  48. {
  49. puts ("mutex_lock failed");
  50. exit (1);
  51. }
  52. /* Waiting for the condition will fail. But we want the timeout here. */
  53. if (gettimeofday (&tv, NULL) != 0)
  54. {
  55. puts ("gettimeofday failed");
  56. exit (1);
  57. }
  58. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  59. ts.tv_nsec += 500000000;
  60. if (ts.tv_nsec >= 1000000000)
  61. {
  62. ts.tv_nsec -= 1000000000;
  63. ++ts.tv_sec;
  64. }
  65. err = pthread_cond_timedwait (&cond, &mut, &ts);
  66. if (err == 0)
  67. {
  68. /* This could in theory happen but here without any signal and
  69. additional waiter it should not. */
  70. puts ("cond_timedwait succeeded");
  71. exit (1);
  72. }
  73. else if (err != ETIMEDOUT)
  74. {
  75. printf ("cond_timedwait returned with %s\n", strerror (err));
  76. exit (1);
  77. }
  78. err = pthread_mutex_unlock (&mut);
  79. if (err != 0)
  80. {
  81. printf ("mutex_unlock failed: %s\n", strerror (err));
  82. exit (1);
  83. }
  84. return 0;
  85. }
  86. #define TEST_FUNCTION do_test ()
  87. #include "../test-skeleton.c"