]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
htl: Make pthread_setcanceltype / state a cancellation point
authorSamuel Thibault <samuel.thibault@ens-lyon.org>
Sat, 15 Mar 2025 15:23:42 +0000 (15:23 +0000)
committerSamuel Thibault <samuel.thibault@ens-lyon.org>
Sat, 15 Mar 2025 19:45:55 +0000 (19:45 +0000)
as expected by tst-cancel32.

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

index 6e09c260ea6deb1bbbfcd581b10d463f5dff1a4d..0d5692e66187d77769c0841c32b08e4dba02c4ea 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;
 }
 libc_hidden_def (__pthread_setcancelstate)
index 0b76fbfbd69aec6ce695f3b389d36f674fa97178..b33931c4688a1f425ed0300e680d32be701acc07 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;
 }
 libc_hidden_def (__pthread_setcanceltype)
index 9479b9ef247977a7f40f37d65d2aeb3010a61340..78ef4e7674f3918e820501037b824fc2809eaea1 100644 (file)
@@ -221,6 +221,14 @@ hidden_proto (__pthread_setspecific)
 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)