tst-sem10.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (C) 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
  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. #include <time.h>
  20. #include <unistd.h>
  21. #include <sys/time.h>
  22. static int
  23. do_test (void)
  24. {
  25. sem_t s;
  26. if (sem_init (&s, 0, 0) == -1)
  27. {
  28. puts ("sem_init failed");
  29. return 1;
  30. }
  31. struct timeval tv;
  32. if (gettimeofday (&tv, NULL) != 0)
  33. {
  34. puts ("gettimeofday failed");
  35. return 1;
  36. }
  37. struct timespec ts;
  38. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  39. /* Set ts to yesterday. */
  40. ts.tv_sec -= 86400;
  41. int type_before;
  42. if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &type_before) != 0)
  43. {
  44. puts ("first pthread_setcanceltype failed");
  45. return 1;
  46. }
  47. errno = 0;
  48. if (TEMP_FAILURE_RETRY (sem_timedwait (&s, &ts)) != -1)
  49. {
  50. puts ("sem_timedwait succeeded");
  51. return 1;
  52. }
  53. if (errno != ETIMEDOUT)
  54. {
  55. printf ("sem_timedwait return errno = %d instead of ETIMEDOUT\n",
  56. errno);
  57. return 1;
  58. }
  59. int type_after;
  60. if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &type_after) != 0)
  61. {
  62. puts ("second pthread_setcanceltype failed");
  63. return 1;
  64. }
  65. if (type_after != PTHREAD_CANCEL_DEFERRED)
  66. {
  67. puts ("sem_timedwait changed cancellation type");
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. #define TEST_FUNCTION do_test ()
  73. #include "../test-skeleton.c"