tst-join3.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <sys/time.h>
  21. static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  22. static void *
  23. tf (void *arg)
  24. {
  25. if (pthread_mutex_lock (&lock) != 0)
  26. {
  27. puts ("child: mutex_lock failed");
  28. return NULL;
  29. }
  30. return (void *) 42l;
  31. }
  32. static int
  33. do_test (void)
  34. {
  35. pthread_t th;
  36. if (pthread_mutex_lock (&lock) != 0)
  37. {
  38. puts ("mutex_lock failed");
  39. exit (1);
  40. }
  41. if (pthread_create (&th, NULL, tf, NULL) != 0)
  42. {
  43. puts ("mutex_create failed");
  44. exit (1);
  45. }
  46. void *status;
  47. struct timespec ts;
  48. struct timeval tv;
  49. (void) gettimeofday (&tv, NULL);
  50. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  51. ts.tv_nsec += 200000000;
  52. if (ts.tv_nsec >= 1000000000)
  53. {
  54. ts.tv_nsec -= 1000000000;
  55. ++ts.tv_sec;
  56. }
  57. int val = pthread_timedjoin_np (th, &status, &ts);
  58. if (val == 0)
  59. {
  60. puts ("1st timedjoin succeeded");
  61. exit (1);
  62. }
  63. else if (val != ETIMEDOUT)
  64. {
  65. puts ("1st timedjoin didn't return ETIMEDOUT");
  66. exit (1);
  67. }
  68. if (pthread_mutex_unlock (&lock) != 0)
  69. {
  70. puts ("mutex_unlock failed");
  71. exit (1);
  72. }
  73. while (1)
  74. {
  75. (void) gettimeofday (&tv, NULL);
  76. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  77. ts.tv_nsec += 200000000;
  78. if (ts.tv_nsec >= 1000000000)
  79. {
  80. ts.tv_nsec -= 1000000000;
  81. ++ts.tv_sec;
  82. }
  83. val = pthread_timedjoin_np (th, &status, &ts);
  84. if (val == 0)
  85. break;
  86. if (val != ETIMEDOUT)
  87. {
  88. printf ("timedjoin returned %s (%d), expected only 0 or ETIMEDOUT\n",
  89. strerror (val), val);
  90. exit (1);
  91. }
  92. }
  93. if (status != (void *) 42l)
  94. {
  95. printf ("return value %p, expected %p\n", status, (void *) 42l);
  96. exit (1);
  97. }
  98. return 0;
  99. }
  100. #define TEST_FUNCTION do_test ()
  101. #include "../test-skeleton.c"