tst-cond5.c 2.6 KB

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