NPTL/arc: notify kernel of the TP value
[uClibc.git] / test / pthread / tst-too-many-cleanups.c
blob7828c5036af639c20611094ba93980e7807dd2d2
1 /*
2 * This illustrates the bug where the cleanup function
3 * of a thread may be called too many times.
5 * main thread:
6 * - grab mutex
7 * - spawn thread1
8 * - go to sleep
9 * thread1:
10 * - register cleanup handler via pthread_cleanup_push()
11 * - try to grab mutex and sleep
12 * main:
13 * - kill thread1
14 * - go to sleep
15 * thread1 cleanup handler:
16 * - try to grab mutex and sleep
17 * main:
18 * - kill thread1
19 * - go to sleep
20 * thread1 cleanup handler:
21 * - wrongly called again
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <pthread.h>
31 #include <assert.h>
32 #include <unistd.h>
34 #define warn(fmt, args...) fprintf(stderr, "[%p] " fmt, (void*)pthread_self(), ## args)
35 #define warnf(fmt, args...) warn("%s:%i: " fmt, __FUNCTION__, __LINE__, ## args)
37 int ok_to_kill_thread;
39 static void thread_killed(void *arg);
41 static void *KillMeThread(void *thread_par)
43 pthread_t pthread_id;
45 warnf("Starting child thread\n");
47 pthread_id = pthread_self();
48 pthread_cleanup_push(thread_killed, (void *)pthread_id);
50 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
51 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
53 /* main code */
54 warnf("please kill me now\n");
55 while (1) {
56 ok_to_kill_thread = 1;
57 sleep(1);
60 pthread_cleanup_pop(0);
62 return 0;
65 static void thread_killed(void *arg)
67 static int num_times_called = 0;
69 warnf("killing %p [cnt=%i]\n", arg, ++num_times_called);
70 assert(num_times_called == 1);
72 /* pick any cancellation endpoint, sleep() will do just fine */
73 while (1) {
74 warnf("sleeping in cancellation endpoint ...\n");
75 sleep(1);
78 warnf("done cleaning up\n");
81 int main(int argc, char *argv[])
83 int count = 3;
84 pthread_t app_pthread_id;
86 /* need to tweak this test a bit to play nice with signals and LT */
87 return 0;
89 ok_to_kill_thread = 0;
91 pthread_create(&app_pthread_id, NULL, KillMeThread, NULL);
93 warnf("waiting for thread to prepare itself\n");
94 while (!ok_to_kill_thread)
95 sleep(1);
97 while (count--) {
98 warnf("killing thread\n");
99 pthread_cancel(app_pthread_id);
100 sleep(3);
103 return 0;