git-pthread_setcancel
authorGNU Libc Maintainers <debian-glibc@lists.debian.org>
Thu, 17 Jul 2025 11:21:32 +0000 (13:21 +0200)
committerAurelien Jarno <aurel32@debian.org>
Thu, 17 Jul 2025 11:21:32 +0000 (13:21 +0200)
commit bfb2f2f481147da54237ade3266f2586a51d43c9
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sat Mar 15 15:23:42 2025 +0000

    htl: Make pthread_setcanceltype / state a cancellation point

    as expected by tst-cancel32.

Gbp-Pq: Topic hurd-i386
Gbp-Pq: Name git-pthread_setcancel.diff

htl/pt-setcancelstate.c
htl/pt-setcanceltype.c
sysdeps/htl/pthreadP.h

index 57894de7189a29a0335fabe9e8760244d2e5892b..7241122c0224965aec5c39fb828be83e0fe6d046 100644 (file)
@@ -24,6 +24,7 @@ int
 __pthread_setcancelstate (int state, int *oldstate)
 {
   struct __pthread *p = _pthread_self ();
+  int cancelled;
 
   switch (state)
     {
@@ -38,8 +39,15 @@ __pthread_setcancelstate (int state, int *oldstate)
   if (oldstate != NULL)
     *oldstate = p->cancel_state;
   p->cancel_state = state;
+  cancelled = (p->cancel_state == PTHREAD_CANCEL_ENABLE) && p->cancel_pending == 1 && (p->cancel_type == PTHREAD_CANCEL_ASYNCHRONOUS);
+  if (cancelled)
+    /* Do not achieve cancel when called again, notably from __pthread_exit itself.  */
+    p->cancel_pending = 2;
   __pthread_mutex_unlock (&p->cancel_lock);
 
+  if (cancelled && __pthread_exit)
+    __pthread_exit (PTHREAD_CANCELED);
+
   return 0;
 }
 
index f9474f9c39208060105d7f8c9071d7d842a29c6a..a4fdeb6919eda60c31ae34f6c2959b6e605cc25d 100644 (file)
@@ -24,6 +24,7 @@ int
 __pthread_setcanceltype (int type, int *oldtype)
 {
   struct __pthread *p = _pthread_self ();
+  int cancelled;
 
   switch (type)
     {
@@ -38,8 +39,12 @@ __pthread_setcanceltype (int type, int *oldtype)
   if (oldtype != NULL)
     *oldtype = p->cancel_type;
   p->cancel_type = type;
+  cancelled = (p->cancel_state == PTHREAD_CANCEL_ENABLE) && p->cancel_pending && (p->cancel_type == PTHREAD_CANCEL_ASYNCHRONOUS);
   __pthread_mutex_unlock (&p->cancel_lock);
 
+  if (cancelled && __pthread_exit)
+    __pthread_exit (PTHREAD_CANCELED);
+
   return 0;
 }
 
index 20dd5a1cc5552503ed133f2d83af52a5a697eb47..893198f451485f8efa20aa823928b51d3a51ff1f 100644 (file)
@@ -172,6 +172,14 @@ hidden_proto (__pthread_mutex_timedlock)
 hidden_proto (__pthread_get_cleanup_stack)
 #endif
 
+#if !defined(__NO_WEAK_PTHREAD_ALIASES) && !IS_IN (libpthread)
+# ifdef weak_extern
+weak_extern (__pthread_exit)
+# else
+#  pragma weak __pthread_exit
+# endif
+#endif
+
 #define ASSERT_TYPE_SIZE(type, size)                                   \
   _Static_assert (sizeof (type) == size,                               \
                  "sizeof (" #type ") != " #size)