tst-cleanup4.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@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 <pthread.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #if defined(__GLIBC__) || defined(__UCLIBC__)
  20. /* LinuxThreads pthread_cleanup_{push,pop} helpers. */
  21. extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer,
  22. void (*__routine) (void *),
  23. void *__arg);
  24. extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer,
  25. int __execute);
  26. static int fds[2];
  27. static pthread_barrier_t b2;
  28. static int global;
  29. /* Defined in tst-cleanup4aux.c, never compiled with -fexceptions. */
  30. extern void fn5 (void);
  31. extern void fn7 (void);
  32. extern void fn9 (void);
  33. void
  34. clh (void *arg)
  35. {
  36. int val = (long int) arg;
  37. printf ("clh (%d)\n", val);
  38. global *= val;
  39. global += val;
  40. }
  41. static __attribute__((noinline)) void
  42. fn_read (void)
  43. {
  44. int r = pthread_barrier_wait (&b2);
  45. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  46. {
  47. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  48. exit (1);
  49. }
  50. char c;
  51. read (fds[0], &c, 1);
  52. }
  53. __attribute__((noinline)) void
  54. fn0 (void)
  55. {
  56. pthread_cleanup_push (clh, (void *) 1l);
  57. fn_read ();
  58. pthread_cleanup_pop (1);
  59. }
  60. __attribute__((noinline)) void
  61. fn1 (void)
  62. {
  63. /* This is the old LinuxThreads pthread_cleanup_{push,pop}. */
  64. struct _pthread_cleanup_buffer b;
  65. _pthread_cleanup_push (&b, clh, (void *) 2l);
  66. fn0 ();
  67. _pthread_cleanup_pop (&b, 1);
  68. }
  69. static __attribute__((noinline)) void
  70. fn2 (void)
  71. {
  72. pthread_cleanup_push (clh, (void *) 3l);
  73. fn1 ();
  74. pthread_cleanup_pop (1);
  75. }
  76. static void *
  77. tf (void *a)
  78. {
  79. switch ((long) a)
  80. {
  81. case 0:
  82. fn2 ();
  83. break;
  84. case 1:
  85. fn5 ();
  86. break;
  87. case 2:
  88. fn7 ();
  89. break;
  90. case 3:
  91. fn9 ();
  92. break;
  93. }
  94. return NULL;
  95. }
  96. #endif
  97. int
  98. do_test (void)
  99. {
  100. #if defined(__GLIBC__) || defined(__UCLIBC__)
  101. int result = 0;
  102. if (pipe (fds) != 0)
  103. {
  104. puts ("pipe failed");
  105. exit (1);
  106. }
  107. if (pthread_barrier_init (&b2, NULL, 2) != 0)
  108. {
  109. puts ("b2 init failed");
  110. exit (1);
  111. }
  112. const int expect[] =
  113. {
  114. 15, /* 1 2 3 */
  115. 276, /* 1 4 5 6 */
  116. 120, /* 1 7 8 */
  117. 460 /* 1 2 9 10 */
  118. };
  119. long i;
  120. for (i = 0; i < 4; ++i)
  121. {
  122. global = 0;
  123. printf ("test %ld\n", i);
  124. pthread_t th;
  125. if (pthread_create (&th, NULL, tf, (void *) i) != 0)
  126. {
  127. puts ("create failed");
  128. exit (1);
  129. }
  130. int e = pthread_barrier_wait (&b2);
  131. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  132. {
  133. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  134. exit (1);
  135. }
  136. pthread_cancel (th);
  137. void *r;
  138. if ((e = pthread_join (th, &r)) != 0)
  139. {
  140. printf ("join failed: %d\n", e);
  141. _exit (1);
  142. }
  143. if (r != PTHREAD_CANCELED)
  144. {
  145. puts ("thread not canceled");
  146. exit (1);
  147. }
  148. if (global != expect[i])
  149. {
  150. printf ("global = %d, expected %d\n", global, expect[i]);
  151. result = 1;
  152. }
  153. }
  154. return result;
  155. #else
  156. return 23;
  157. #endif
  158. }
  159. #define TEST_FUNCTION do_test ()
  160. #include "../test-skeleton.c"