tst-cleanup4.c 3.7 KB

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