tst-join2.c 2.4 KB

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