tst-join2.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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; see the file COPYING.LIB. If
  14. not, see <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 <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. int val = pthread_tryjoin_np (th, &status);
  48. if (val == 0)
  49. {
  50. puts ("1st tryjoin succeeded");
  51. exit (1);
  52. }
  53. else if (val != EBUSY)
  54. {
  55. puts ("1st tryjoin didn't return EBUSY");
  56. exit (1);
  57. }
  58. if (pthread_mutex_unlock (&lock) != 0)
  59. {
  60. puts ("mutex_unlock failed");
  61. exit (1);
  62. }
  63. while ((val = pthread_tryjoin_np (th, &status)) != 0)
  64. {
  65. if (val != EBUSY)
  66. {
  67. printf ("tryjoin returned %s (%d), expected only 0 or EBUSY\n",
  68. strerror (val), val);
  69. exit (1);
  70. }
  71. /* Delay minimally. */
  72. struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
  73. nanosleep (&ts, NULL);
  74. }
  75. if (status != (void *) 42l)
  76. {
  77. printf ("return value %p, expected %p\n", status, (void *) 42l);
  78. exit (1);
  79. }
  80. return 0;
  81. }
  82. #define TEST_FUNCTION do_test ()
  83. #include "../test-skeleton.c"