tst-join5.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  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. static void *
  20. tf1 (void *arg)
  21. {
  22. pthread_join ((pthread_t) arg, NULL);
  23. puts ("1st join returned");
  24. return (void *) 1l;
  25. }
  26. static void *
  27. tf2 (void *arg)
  28. {
  29. int a;
  30. a = pthread_join ((pthread_t) arg, NULL);
  31. puts ("2nd join returned");
  32. printf("a = %i\n", a);
  33. return (void *) 1l;
  34. }
  35. static int
  36. do_test (void)
  37. {
  38. pthread_t th;
  39. int err = pthread_join (pthread_self (), NULL);
  40. if (err == 0)
  41. {
  42. puts ("1st circular join succeeded");
  43. exit (1);
  44. }
  45. if (err != EDEADLK)
  46. {
  47. printf ("1st circular join %d, not EDEADLK\n", err);
  48. exit (1);
  49. }
  50. if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0)
  51. {
  52. puts ("1st create failed");
  53. exit (1);
  54. }
  55. if (pthread_cancel (th) != 0)
  56. {
  57. puts ("cannot cancel 1st thread");
  58. exit (1);
  59. }
  60. void *r;
  61. err = pthread_join (th, &r);
  62. if (err != 0)
  63. {
  64. printf ("cannot join 1st thread: %d\n", err);
  65. exit (1);
  66. }
  67. if (r != PTHREAD_CANCELED)
  68. {
  69. puts ("1st thread not canceled");
  70. exit (1);
  71. }
  72. err = pthread_join (pthread_self (), NULL);
  73. if (err == 0)
  74. {
  75. puts ("2nd circular join succeeded");
  76. exit (1);
  77. }
  78. if (err != EDEADLK)
  79. {
  80. printf ("2nd circular join %d, not EDEADLK\n", err);
  81. exit (1);
  82. }
  83. if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0)
  84. {
  85. puts ("2nd create failed");
  86. exit (1);
  87. }
  88. if (pthread_cancel (th) != 0)
  89. {
  90. puts ("cannot cancel 2nd thread");
  91. exit (1);
  92. }
  93. if (pthread_join (th, &r) != 0)
  94. {
  95. puts ("cannot join 2nd thread");
  96. exit (1);
  97. }
  98. if (r != PTHREAD_CANCELED)
  99. {
  100. puts ("2nd thread not canceled");
  101. exit (1);
  102. }
  103. err = pthread_join (pthread_self (), NULL);
  104. if (err == 0)
  105. {
  106. puts ("2nd circular join succeeded");
  107. exit (1);
  108. }
  109. if (err != EDEADLK)
  110. {
  111. printf ("2nd circular join %d, not EDEADLK\n", err);
  112. exit (1);
  113. }
  114. exit (0);
  115. }
  116. #define TEST_FUNCTION do_test ()
  117. #include "../test-skeleton.c"