OSDN Git Service

007bbaab741eba48da0f937d84299aaf12688ea9
[pf3gnuchains/gcc-fork.git] / libitm / testsuite / libitm.c / reentrant.c
1 /* { dg-do run { xfail *-*-* } }
2
3 /* Tests that new transactions can be started from both transaction_pure and
4    transaction_unsafe code. This also requires proper handling of reentrant
5    nesting in the serial_lock implementation. */
6
7 #include <stdlib.h>
8 #include <pthread.h>
9 #include <libitm.h>
10
11 int x = 0;
12
13 int __attribute__((transaction_pure)) pure(int i)
14 {
15   __transaction_atomic {
16     x++;
17   }
18   if (_ITM_inTransaction() == outsideTransaction)
19     abort();
20   return i+1;
21 }
22
23 int __attribute__((transaction_unsafe)) unsafe(int i)
24 {
25   if (_ITM_inTransaction() != inIrrevocableTransaction)
26     abort();
27   __transaction_atomic {
28     x++;
29   }
30   if (_ITM_inTransaction() != inIrrevocableTransaction)
31     abort();
32   return i+1;
33 }
34
35 static void *thread (void *dummy __attribute__((unused)))
36 {
37   __transaction_atomic {
38     pure(1);
39   }
40   __transaction_relaxed {
41     unsafe(1);
42   }
43   return 0;
44 }
45
46 int main()
47 {
48   pthread_t pt;
49   int r = 0;
50
51   __transaction_atomic {
52     r += pure(1) + x;
53   }
54   __transaction_relaxed {
55     r += unsafe(1) + x;
56   }
57   if (r != 7)
58     abort();
59
60   // Spawn a new thread to check that the serial lock is not held.
61   pthread_create(&pt, NULL, thread, NULL);
62   pthread_join(pt, NULL);
63   if (x != 4)
64     abort();
65   return 0;
66 }