tst-join3.c 2.7 KB

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