This is the mail archive of the
mailing list for the glibc project.
[PATCH][BZ #13613] Allow a single-threaded process to cancel itself
- From: Siddhesh Poyarekar <siddhesh at redhat dot com>
- To: libc-alpha at sourceware dot org
- Cc: jakub at redhat dot com
- Date: Wed, 9 May 2012 20:24:37 +0530
- Subject: [PATCH][BZ #13613] Allow a single-threaded process to cancel itself
A single-threaded process cannot cancel itself using pthread_cancel()
as reported in the bz. This is because header.multiple_threads is not
set till a thread is cloned and cancellation point entries are guarded
This is a little odd in terms of a request since this can be easily
done for the main process using exit and atexit. However, there is
nothing in the specification of pthread_cancel that explicitly
disallows this, so for the sake of compliance, pthread_cancel should
work for a single-threaded process too.
Attached patch unconditionally enables multiple_threads in the caller
of pthread_cancel. This should have an impact only for a
single-threaded process since any threads in a multi-threaded process
should have that flag enabled. This was suggested in the bz by Jakub
Jelinek. I have also added a test case to verify that this is fixed.
I have run this on x86_64 with the test case with and without the
patch to make sure that this is fixed. I did not find any regressions
introduced as a result of this patch.
2012-05-09 Siddhesh Poyarekar <firstname.lastname@example.org>
Jakub Jelinek <email@example.com>
* nptl/pthread_cancel.c (pthread_cancel): Enable
multiple_threads before marking the thread as cancelled.
* nptl/tst-cancel-self.c: New test case.
* nptl/Makefile (tests): Add tst-cancel-self.
diff --git a/nptl/Makefile b/nptl/Makefile
index 07a1022..eb1b6ca 100644
@@ -236,6 +236,7 @@ tests = tst-typesizes \
tst-cancel11 tst-cancel12 tst-cancel13 tst-cancel14 tst-cancel15 \
tst-cancel16 tst-cancel17 tst-cancel18 tst-cancel19 tst-cancel20 \
tst-cancel21 tst-cancel22 tst-cancel23 tst-cancel24 tst-cancel25 \
+ tst-cancel-self \
tst-cleanup0 tst-cleanup1 tst-cleanup2 tst-cleanup3 tst-cleanup4 \
tst-flock1 tst-flock2 \
tst-signal1 tst-signal2 tst-signal3 tst-signal4 tst-signal5 \
diff --git a/nptl/pthread_cancel.c b/nptl/pthread_cancel.c
index 249aa11..1bfca63 100644
@@ -95,6 +95,14 @@ pthread_cancel (th)
+ /* A single-threaded process should be able to kill itself, since there is
+ nothing in the POSIX specification that says that it cannot. So we set
+ multiple_threads to true so that cancellation points get executed. */
+ THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
+ __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1;
/* Mark the thread as canceled. This has to be done
atomically since other bits could be modified as well. */
diff --git a/nptl/tst-cancel-self.c b/nptl/tst-cancel-self.c
new file mode 100644
@@ -0,0 +1,54 @@
+/* Copyright (C) 2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Siddhesh Poyarekar <firstname.lastname@example.org>, 2012.
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+cleanup (void *arg)
+ printf ("Main thread got cancelled and is being cleaned up now\n");
+ exit (0);
+ int ret = 0;
+ pthread_cleanup_push (cleanup, NULL);
+ if ((ret = pthread_cancel (pthread_self ())) != 0)
+ printf ("cancel failed: %s\n", strerror (ret));
+ exit (1);
+ sleep (1);
+ printf ("Could not cancel self.\n");
+ pthread_cleanup_pop (0);
+ return 1;
+#define TEST_FUNCTION do_test ()