blob: 7828c5036af639c20611094ba93980e7807dd2d2 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * This illustrates the bug where the cleanup function
3 * of a thread may be called too many times.
4 *
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
22 */
23
24#ifndef _GNU_SOURCE
25#define _GNU_SOURCE
26#endif
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <pthread.h>
31#include <assert.h>
32#include <unistd.h>
33
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)
36
37int ok_to_kill_thread;
38
39static void thread_killed(void *arg);
40
41static void *KillMeThread(void *thread_par)
42{
43 pthread_t pthread_id;
44
45 warnf("Starting child thread\n");
46
47 pthread_id = pthread_self();
48 pthread_cleanup_push(thread_killed, (void *)pthread_id);
49
50 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
51 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
52
53 /* main code */
54 warnf("please kill me now\n");
55 while (1) {
56 ok_to_kill_thread = 1;
57 sleep(1);
58 }
59
60 pthread_cleanup_pop(0);
61
62 return 0;
63}
64
65static void thread_killed(void *arg)
66{
67 static int num_times_called = 0;
68
69 warnf("killing %p [cnt=%i]\n", arg, ++num_times_called);
70 assert(num_times_called == 1);
71
72 /* pick any cancellation endpoint, sleep() will do just fine */
73 while (1) {
74 warnf("sleeping in cancellation endpoint ...\n");
75 sleep(1);
76 }
77
78 warnf("done cleaning up\n");
79}
80
81int main(int argc, char *argv[])
82{
83 int count = 3;
84 pthread_t app_pthread_id;
85
86 /* need to tweak this test a bit to play nice with signals and LT */
87 return 0;
88
89 ok_to_kill_thread = 0;
90
91 pthread_create(&app_pthread_id, NULL, KillMeThread, NULL);
92
93 warnf("waiting for thread to prepare itself\n");
94 while (!ok_to_kill_thread)
95 sleep(1);
96
97 while (count--) {
98 warnf("killing thread\n");
99 pthread_cancel(app_pthread_id);
100 sleep(3);
101 }
102
103 return 0;
104}