Commit 7fe9e2e089f4990b7d18d0798f591ab276b15f2b fixes [BZ# 17322]
[glibc.git] / nptl / tst-spin4.c
blob5b23a172cad46cab551bb713086ccec72a8c398b
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <unistd.h>
5 static int count = 0;
7 static void *
8 thread_add_one (void *arg)
10 int tmp;
11 pthread_spinlock_t *lock = (pthread_spinlock_t *) arg;
13 /* When do_test holds the lock for 1 sec, the two thread will be
14 in contention for the lock. */
15 if (pthread_spin_lock (lock) != 0)
17 puts ("thread_add_one(): spin_lock failed");
18 pthread_exit ((void *) 1l);
21 /* sleep 1s before modifying count */
22 tmp = count;
23 sleep (1);
24 count = tmp + 1;
26 if (pthread_spin_unlock (lock) != 0)
28 puts ("thread_add_one(): spin_unlock failed");
29 pthread_exit ((void *) 1l);
32 return NULL;
35 static int
36 do_test (void)
38 pthread_t thr1, thr2;
39 pthread_spinlock_t lock;
40 int tmp;
42 if (pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE) != 0)
44 puts ("spin_init failed");
45 return 1;
48 if (pthread_spin_lock (&lock) != 0)
50 puts ("1st spin_lock failed");
51 return 1;
54 if (pthread_create (&thr1, NULL, thread_add_one, (void *) &lock) != 0)
56 puts ("1st pthread_create failed");
57 return 1;
60 if (pthread_create (&thr2, NULL, thread_add_one, (void *) &lock) != 0)
62 puts ("2nd pthread_create failed");
63 return 1;
66 /* sleep 1s before modifying count */
67 tmp = count;
68 sleep (1);
69 count = tmp + 1;
71 if (pthread_spin_unlock (&lock) != 0)
73 puts ("1st spin_unlock failed");
74 return 1;
77 void *status;
78 if (pthread_join (thr1, &status) != 0)
80 puts ("1st pthread_join failed");
81 return 1;
83 if (status != NULL)
85 puts ("failure in the 1st thread");
86 return 1;
88 if (pthread_join (thr2, &status) != 0)
90 puts ("2nd pthread_join failed");
91 return 1;
93 if (status != NULL)
95 puts ("failure in the 2nd thread");
96 return 1;
99 if (count != 3)
101 printf ("count is %d, should be 3\n", count);
102 return 1;
104 return 0;
107 #define TIMEOUT 5
108 #define TEST_FUNCTION do_test ()
109 #include "../test-skeleton.c"