This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Purpose of pthread_cleanup_push_defer_np()
- From: Sebastian Huber <sebastian dot huber at embedded-brains dot de>
- To: libc-alpha at sourceware dot org
- Date: Wed, 10 Jul 2013 13:17:26 +0200
- Subject: Purpose of pthread_cleanup_push_defer_np()
Hello,
what is the purpose of pthread_cleanup_push_defer_np()? From the man page I have:
http://man7.org/linux/man-pages/man3/pthread_cleanup_pop_restore_np.3.html
This sequence of calls:
pthread_cleanup_push_defer_np(routine, arg);
pthread_cleanup_pop_restore_np(execute);
is equivalent to (but shorter and more efficient than):
int oldtype;
pthread_cleanup_push(routine, arg);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
...
pthread_setcanceltype(oldtype, NULL);
pthread_cleanup_pop(execute);
Why is the cancel type set to PTHREAD_CANCEL_DEFERRED after the
pthread_cleanup_push()? In case the thread gets canceled asynchronously
between the pthread_cleanup_push() and pthread_setcanceltype() the routine is
invoked. This is problematic in case the routine requires some conditions set
by the ... body. Here is an example:
http://www.sourceware.org/pthreads-win32/manual/pthread_cleanup_push.html
I thought that the pthread_cleanup_push_defer_np() is a short cut to deal with
this.
In "nptl/sysdeps/pthread/pthread.h" we have:
/* Remove a cleanup handler as pthread_cleanup_pop does, but also
restores the cancellation type that was in effect when the matching
pthread_cleanup_push_defer was called. */
# define pthread_cleanup_pop_restore_np(execute) \
do { } while (0);/* Empty to allow label before pthread_cleanup_pop. */\
} while (0); \
__pthread_unregister_cancel_restore (&__cancel_buf); \
if (execute) \
__cancel_routine (__cancel_arg); \
} while (0)
The __cancel_routine is invoked after the restore sequence, thus in case the
thread is asynchronously canceled after the
__pthread_unregister_cancel_restore() call, the __cancel_routine is not
invoked. Is this really intentional?
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.huber@embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.