tst-abstime.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 2010-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Andreas Schwab <schwab@redhat.com>, 2010.
  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 <semaphore.h>
  18. #include <stdio.h>
  19. static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
  20. static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
  21. static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
  22. static pthread_rwlock_t rw1 = PTHREAD_RWLOCK_INITIALIZER;
  23. static pthread_rwlock_t rw2 = PTHREAD_RWLOCK_INITIALIZER;
  24. static sem_t sem;
  25. static void *
  26. th (void *arg)
  27. {
  28. long int res = 0;
  29. int r;
  30. struct timespec t = { -2, 0 };
  31. r = pthread_mutex_timedlock (&m1, &t);
  32. if (r != ETIMEDOUT)
  33. {
  34. puts ("pthread_mutex_timedlock did not return ETIMEDOUT");
  35. res = 1;
  36. }
  37. r = pthread_rwlock_timedrdlock (&rw1, &t);
  38. if (r != ETIMEDOUT)
  39. {
  40. puts ("pthread_rwlock_timedrdlock did not return ETIMEDOUT");
  41. res = 1;
  42. }
  43. r = pthread_rwlock_timedwrlock (&rw2, &t);
  44. if (r != ETIMEDOUT)
  45. {
  46. puts ("pthread_rwlock_timedwrlock did not return ETIMEDOUT");
  47. res = 1;
  48. }
  49. return (void *) res;
  50. }
  51. static int
  52. do_test (void)
  53. {
  54. int res = 0;
  55. int r;
  56. struct timespec t = { -2, 0 };
  57. pthread_t pth;
  58. sem_init (&sem, 0, 0);
  59. r = sem_timedwait (&sem, &t);
  60. if (r != -1 || errno != ETIMEDOUT)
  61. {
  62. puts ("sem_timedwait did not fail with ETIMEDOUT");
  63. res = 1;
  64. }
  65. pthread_mutex_lock (&m1);
  66. pthread_rwlock_wrlock (&rw1);
  67. pthread_rwlock_rdlock (&rw2);
  68. pthread_mutex_lock (&m2);
  69. if (pthread_create (&pth, 0, th, 0) != 0)
  70. {
  71. puts ("cannot create thread");
  72. return 1;
  73. }
  74. r = pthread_cond_timedwait (&c, &m2, &t);
  75. if (r != ETIMEDOUT)
  76. {
  77. puts ("pthread_cond_timedwait did not return ETIMEDOUT");
  78. res = 1;
  79. }
  80. void *thres;
  81. pthread_join (pth, &thres);
  82. return res | (thres != NULL);
  83. }
  84. #define TEST_FUNCTION do_test ()
  85. #include "../test-skeleton.c"