tst-once3.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (C) 2002, 2003 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 <pthread.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #define N 100
  20. static pthread_once_t once = PTHREAD_ONCE_INIT;
  21. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  22. static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  23. static pthread_barrier_t bar;
  24. static int global;
  25. static int cl_called;
  26. static void
  27. once_handler1 (void)
  28. {
  29. if (pthread_mutex_lock (&mut) != 0)
  30. {
  31. puts ("once_handler1: mutex_lock failed");
  32. exit (1);
  33. }
  34. puts ("once_handler1: locked");
  35. int r = pthread_barrier_wait (&bar);
  36. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  37. {
  38. puts ("once_handler1: barrier_wait failed");
  39. exit (1);
  40. }
  41. pthread_cond_wait (&cond, &mut);
  42. /* We should never get here. */
  43. exit (42);
  44. }
  45. static void
  46. once_handler2 (void)
  47. {
  48. global = 1;
  49. }
  50. static void
  51. cl (void *arg)
  52. {
  53. cl_called = 1;
  54. }
  55. static void *
  56. tf (void *arg)
  57. {
  58. pthread_cleanup_push (cl, NULL)
  59. pthread_once (&once, once_handler1);
  60. pthread_cleanup_pop (0);
  61. /* We should never get here. */
  62. puts ("pthread_once in tf returned");
  63. exit (1);
  64. }
  65. static int
  66. do_test (void)
  67. {
  68. pthread_t th;
  69. if (pthread_barrier_init (&bar, NULL, 2) != 0)
  70. {
  71. puts ("barrier_init failed");
  72. return 1;
  73. }
  74. if (pthread_create (&th, NULL, tf, NULL) != 0)
  75. {
  76. puts ("first create failed");
  77. return 1;
  78. }
  79. int r = pthread_barrier_wait (&bar);
  80. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  81. {
  82. puts ("barrier_wait failed");
  83. return 1;
  84. }
  85. if (pthread_mutex_lock (&mut) != 0)
  86. {
  87. puts ("mutex_lock failed");
  88. return 1;
  89. }
  90. /* We unlock the mutex so that we catch the case where the pthread_cond_wait
  91. call incorrectly resumes and tries to get the mutex. */
  92. if (pthread_mutex_unlock (&mut) != 0)
  93. {
  94. puts ("mutex_unlock failed");
  95. return 1;
  96. }
  97. /* Cancel the thread. */
  98. puts ("going to cancel");
  99. if (pthread_cancel (th) != 0)
  100. {
  101. puts ("cancel failed");
  102. return 1;
  103. }
  104. void *result;
  105. pthread_join (th, &result);
  106. if (result != PTHREAD_CANCELED)
  107. {
  108. puts ("join didn't return PTHREAD_CANCELED");
  109. return 1;
  110. }
  111. if (cl_called != 1)
  112. {
  113. puts ("cleanup handler not called");
  114. return 1;
  115. }
  116. pthread_once (&once, once_handler2);
  117. if (global != 1)
  118. {
  119. puts ("global still 0");
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. #define TEST_FUNCTION do_test ()
  125. #define TIMEOUT 4
  126. #include "../test-skeleton.c"