tst-once3.c 3.3 KB

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