tst-cleanup4.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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, 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 <unistd.h>
  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. int
  97. do_test (void)
  98. {
  99. int result = 0;
  100. if (pipe (fds) != 0)
  101. {
  102. puts ("pipe failed");
  103. exit (1);
  104. }
  105. if (pthread_barrier_init (&b2, NULL, 2) != 0)
  106. {
  107. puts ("b2 init failed");
  108. exit (1);
  109. }
  110. const int expect[] =
  111. {
  112. 15, /* 1 2 3 */
  113. 276, /* 1 4 5 6 */
  114. 120, /* 1 7 8 */
  115. 460 /* 1 2 9 10 */
  116. };
  117. long i;
  118. for (i = 0; i < 4; ++i)
  119. {
  120. global = 0;
  121. printf ("test %ld\n", i);
  122. pthread_t th;
  123. if (pthread_create (&th, NULL, tf, (void *) i) != 0)
  124. {
  125. puts ("create failed");
  126. exit (1);
  127. }
  128. int e = pthread_barrier_wait (&b2);
  129. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  130. {
  131. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  132. exit (1);
  133. }
  134. pthread_cancel (th);
  135. void *r;
  136. if ((e = pthread_join (th, &r)) != 0)
  137. {
  138. printf ("join failed: %d\n", e);
  139. _exit (1);
  140. }
  141. if (r != PTHREAD_CANCELED)
  142. {
  143. puts ("thread not canceled");
  144. exit (1);
  145. }
  146. if (global != expect[i])
  147. {
  148. printf ("global = %d, expected %d\n", global, expect[i]);
  149. result = 1;
  150. }
  151. }
  152. return result;
  153. }
  154. #define TEST_FUNCTION do_test ()
  155. #include "../test-skeleton.c"