1
.TH PTHREAD_CLEANUP 3 LinuxThreads
3
.XREF pthread_cleanup_pop
4
.XREF pthread_cleanup_push_defer_np
5
.XREF pthread_cleanup_pop_restore_np
8
pthread_cleanup_push, pthread_cleanup_pop, pthread_cleanup_push_defer_np, pthread_cleanup_pop_restore_np \- install and remove cleanup handlers
13
void pthread_cleanup_push(void (*routine) (void *), void *arg);
15
void pthread_cleanup_pop(int execute);
17
void pthread_cleanup_push_defer_np(void (*routine) (void *), void *arg);
19
void pthread_cleanup_pop_restore_np(int execute);
23
Cleanup handlers are functions that get called when a thread
24
terminates, either by calling !pthread_exit!(3) or because of
25
cancellation. Cleanup handlers are installed and removed following a
26
stack-like discipline.
28
The purpose of cleanup handlers is to free the resources that a thread
29
may hold at the time it terminates. In particular, if a thread
30
exits or is cancelled while it owns a locked mutex, the mutex will
31
remain locked forever and prevent other threads from executing
32
normally. The best way to avoid this is, just before locking the
33
mutex, to install a cleanup handler whose effect is to unlock the
34
mutex. Cleanup handlers can be used similarly to free blocks allocated
35
with !malloc!(3) or close file descriptors on thread termination.
37
!pthread_cleanup_push! installs the |routine| function with argument
38
|arg| as a cleanup handler. From this point on to the matching
39
!pthread_cleanup_pop!, the function |routine| will be called with
40
arguments |arg| when the thread terminates, either through !pthread_exit!(3)
41
or by cancellation. If several cleanup handlers are active at that
42
point, they are called in LIFO order: the most recently installed
43
handler is called first.
45
!pthread_cleanup_pop! removes the most recently installed cleanup
46
handler. If the |execute| argument is not 0, it also executes the
47
handler, by calling the |routine| function with arguments |arg|. If
48
the |execute| argument is 0, the handler is only removed but not
51
Matching pairs of !pthread_cleanup_push! and !pthread_cleanup_pop!
52
must occur in the same function, at the same level of block nesting.
53
Actually, !pthread_cleanup_push! and !pthread_cleanup_pop! are macros,
54
and the expansion of !pthread_cleanup_push! introduces an open brace !{!
55
with the matching closing brace !}! being introduced by the expansion
56
of the matching !pthread_cleanup_pop!.
58
!pthread_cleanup_push_defer_np! is a non-portable extension that
59
combines !pthread_cleanup_push! and !pthread_setcanceltype!(3).
60
It pushes a cleanup handler just as !pthread_cleanup_push! does, but
61
also saves the current cancellation type and sets it to deferred
62
cancellation. This ensures that the cleanup mechanism is effective
63
even if the thread was initially in asynchronous cancellation mode.
65
!pthread_cleanup_pop_restore_np! pops a cleanup handler introduced by
66
!pthread_cleanup_push_defer_np!, and restores the cancellation type to
67
its value at the time !pthread_cleanup_push_defer_np! was called.
69
!pthread_cleanup_push_defer_np! and !pthread_cleanup_pop_restore_np!
70
must occur in matching pairs, at the same level of block nesting.
72
The following sequence
78
pthread_cleanup_push_defer_np(routine, arg);
80
pthread_cleanup_pop_defer_np(execute);
86
is functionally equivalent to (but more compact and more efficient than)
93
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
94
pthread_cleanup_push(routine, arg);
96
pthread_cleanup_pop(execute);
97
pthread_setcanceltype(oldtype, NULL);
113
Xavier Leroy <Xavier.Leroy@inria.fr>
118
!pthread_setcanceltype!(3).
122
Here is how to lock a mutex |mut| in such a way that it will be
123
unlocked if the thread is canceled while |mut| is locked:
129
pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut);
130
pthread_mutex_lock(&mut);
132
pthread_mutex_unlock(&mut);
133
pthread_cleanup_pop(0);
139
Equivalently, the last two lines can be replaced by
145
pthread_cleanup_pop(1);
151
Notice that the code above is safe only in deferred cancellation mode
152
(see !pthread_setcanceltype!(3)). In asynchronous cancellation mode,
153
a cancellation can occur between !pthread_cleanup_push! and
154
!pthread_mutex_lock!, or between !pthread_mutex_unlock! and
155
!pthread_cleanup_pop!, resulting in both cases in the thread trying to
156
unlock a mutex not locked by the current thread. This is the main
157
reason why asynchronous cancellation is difficult to use.
159
If the code above must also work in asynchronous cancellation mode,
160
then it must switch to deferred mode for locking and unlocking the
167
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
168
pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut);
169
pthread_mutex_lock(&mut);
171
pthread_cleanup_pop(1);
172
pthread_setcanceltype(oldtype, NULL);
178
The code above can be rewritten in a more compact and more
179
efficient way, using the non-portable functions
180
!pthread_cleanup_push_defer_np! and !pthread_cleanup_pop_restore_np!:
186
pthread_cleanup_push_restore_np(pthread_mutex_unlock, (void *) &mut);
187
pthread_mutex_lock(&mut);
189
pthread_cleanup_pop_restore_np(1);