PR c++/27115
[official-gcc.git] / gcc / testsuite / g++.dg / abi / forced-sticky.C
blob0d31ee435471d436c52fad23498408bacf604556
1 // Test for "sticky cancel": if a catch (...) block discards the
2 // cancellation exception, a new one is raised at the next cancellation
3 // point.
5 // This test only applies to glibc targets.
6 // { dg-do run { target *-*-linux* } }
7 // { dg-options "-pthread" }
9 #include <pthread.h>
10 #include <cxxabi.h>
11 extern "C" int printf (const char *, ...);
13 void* thread_main(void*)
15   try
16     {
17       // Spin until we get cancelled.
18       while (1)
19         pthread_testcancel();
20     }
21   catch (...)
22     {
23       // Catch and discard the forced unwind.
24       printf ("caught ...\n");
25     }
27   try
28     {
29       // Start unwinding again.
30       pthread_testcancel();
31     }
32   catch (...)
33     {
34       // Catch and discard again.  This time the thread exits before the
35       // next cancellation point, so we're done.
36       printf ("caught ... again\n");
37       return 0;
38     }
40   return (void*)4;
43 int main()
45   pthread_t thread;
46   int r;
47   void *p;
49   r = pthread_create (&thread, NULL, thread_main, NULL);
50   if (r)
51     return 1;
53   r = pthread_cancel (thread);
54   if (r)
55     return 2;
57   r = pthread_join (thread, &p);
58   if (r)
59     return 3;
61   return (int)p;