This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[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
by this.

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  <>
	    Jakub Jelinek  <>

	[BZ #13613]
	* 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
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -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
--- a/nptl/pthread_cancel.c
+++ b/nptl/pthread_cancel.c
@@ -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
index 0000000..2b6baf8
--- /dev/null
+++ b/nptl/tst-cancel-self.c
@@ -0,0 +1,54 @@
+/* Copyright (C) 2012 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Siddhesh Poyarekar <>, 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
+   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
+   <>.  */
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+static void
+cleanup (void *arg)
+  printf ("Main thread got cancelled and is being cleaned up now\n");
+  exit (0);
+static int
+do_test (void)
+  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 ()
+#include "../test-skeleton.c"

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]