summaryrefslogtreecommitdiffstats
path: root/upstream/opensuse-tumbleweed/man3/pthread_cleanup_push.3
blob: 729e08631b0d19ed86a55b5738dd00c5f22f66f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'\" t
.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
.\"     <mtk.manpages@gmail.com>
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH pthread_cleanup_push 3 2024-05-02 "Linux man-pages (unreleased)"
.SH NAME
pthread_cleanup_push, pthread_cleanup_pop \- push and pop
thread cancelation clean-up handlers
.SH LIBRARY
POSIX threads library
.RI ( libpthread ", " \-lpthread )
.SH SYNOPSIS
.nf
.B #include <pthread.h>
.P
.BI "void pthread_cleanup_push(void (*" routine ")(void *), void *" arg );
.BI "void pthread_cleanup_pop(int " execute );
.fi
.SH DESCRIPTION
These functions manipulate the calling thread's stack of
thread-cancelation clean-up handlers.
A clean-up handler is a function that is automatically executed
when a thread is canceled (or in various other circumstances
described below);
it might, for example, unlock a mutex so that
it becomes available to other threads in the process.
.P
The
.BR pthread_cleanup_push ()
function pushes
.I routine
onto the top of the stack of clean-up handlers.
When
.I routine
is later invoked, it will be given
.I arg
as its argument.
.P
The
.BR pthread_cleanup_pop ()
function removes the routine at the top of the stack of clean-up handlers,
and optionally executes it if
.I execute
is nonzero.
.P
A cancelation clean-up handler is popped from the stack
and executed in the following circumstances:
.IP \[bu] 3
When a thread is canceled,
all of the stacked clean-up handlers are popped and executed in
the reverse of the order in which they were pushed onto the stack.
.IP \[bu]
When a thread terminates by calling
.BR pthread_exit (3),
all clean-up handlers are executed as described in the preceding point.
(Clean-up handlers are
.I not
called if the thread terminates by
performing a
.I return
from the thread start function.)
.IP \[bu]
When a thread calls
.BR pthread_cleanup_pop ()
with a nonzero
.I execute
argument, the top-most clean-up handler is popped and executed.
.P
POSIX.1 permits
.BR pthread_cleanup_push ()
and
.BR pthread_cleanup_pop ()
to be implemented as macros that expand to text
containing \[aq]\fB{\fP\[aq] and \[aq]\fB}\fP\[aq], respectively.
For this reason, the caller must ensure that calls to these
functions are paired within the same function,
and at the same lexical nesting level.
(In other words, a clean-up handler is established only
during the execution of a specified section of code.)
.P
Calling
.BR longjmp (3)
.RB ( siglongjmp (3))
produces undefined results if any call has been made to
.BR pthread_cleanup_push ()
or
.BR pthread_cleanup_pop ()
without the matching call of the pair since the jump buffer
was filled by
.BR setjmp (3)
.RB ( sigsetjmp (3)).
Likewise, calling
.BR longjmp (3)
.RB ( siglongjmp (3))
from inside a clean-up handler produces undefined results
unless the jump buffer was also filled by
.BR setjmp (3)
.RB ( sigsetjmp (3))
inside the handler.
.SH RETURN VALUE
These functions do not return a value.
.SH ERRORS
There are no errors.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.na
.nh
.BR pthread_cleanup_push (),
.BR pthread_cleanup_pop ()
T}	Thread safety	MT-Safe
.TE
.SH VERSIONS
On glibc, the
.BR pthread_cleanup_push ()
and
.BR pthread_cleanup_pop ()
functions
.I are
implemented as macros that expand to text
containing \[aq]\fB{\fP\[aq] and \[aq]\fB}\fP\[aq], respectively.
This means that variables declared within the scope of
paired calls to these functions will be visible within only that scope.
.P
POSIX.1
.\" The text was actually added in the 2004 TC2
says that the effect of using
.IR return ,
.IR break ,
.IR continue ,
or
.I goto
to prematurely leave a block bracketed
.BR pthread_cleanup_push ()
and
.BR pthread_cleanup_pop ()
is undefined.
Portable applications should avoid doing this.
.SH STANDARDS
POSIX.1-2008.
.SH HISTORY
POSIX.1-2001.
glibc 2.0.
.SH EXAMPLES
The program below provides a simple example of the use of the functions
described in this page.
The program creates a thread that executes a loop bracketed by
.BR pthread_cleanup_push ()
and
.BR pthread_cleanup_pop ().
This loop increments a global variable,
.IR cnt ,
once each second.
Depending on what command-line arguments are supplied,
the main thread sends the other thread a cancelation request,
or sets a global variable that causes the other thread
to exit its loop and terminate normally (by doing a
.IR return ).
.P
In the following shell session,
the main thread sends a cancelation request to the other thread:
.P
.in +4n
.EX
$ \fB./a.out\fP
New thread started
cnt = 0
cnt = 1
Canceling thread
Called clean\-up handler
Thread was canceled; cnt = 0
.EE
.in
.P
From the above, we see that the thread was canceled,
and that the cancelation clean-up handler was called
and it reset the value of the global variable
.I cnt
to 0.
.P
In the next run, the main program sets a
global variable that causes other thread to terminate normally:
.P
.in +4n
.EX
$ \fB./a.out x\fP
New thread started
cnt = 0
cnt = 1
Thread terminated normally; cnt = 2
.EE
.in
.P
From the above, we see that the clean-up handler was not executed (because
.I cleanup_pop_arg
was 0), and therefore the value of
.I cnt
was not reset.
.P
In the next run, the main program sets a global variable that
causes the other thread to terminate normally,
and supplies a nonzero value for
.IR cleanup_pop_arg :
.P
.in +4n
.EX
$ \fB./a.out x 1\fP
New thread started
cnt = 0
cnt = 1
Called clean\-up handler
Thread terminated normally; cnt = 0
.EE
.in
.P
In the above, we see that although the thread was not canceled,
the clean-up handler was executed, because the argument given to
.BR pthread_cleanup_pop ()
was nonzero.
.SS Program source
\&
.\" SRC BEGIN (pthread_cleanup_push.c)
.EX
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
\&
#define handle_error_en(en, msg) \e
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
\&
static int done = 0;
static int cleanup_pop_arg = 0;
static int cnt = 0;
\&
static void
cleanup_handler(void *arg)
{
    printf("Called clean\-up handler\en");
    cnt = 0;
}
\&
static void *
thread_start(void *arg)
{
    time_t curr;
\&
    printf("New thread started\en");
\&
    pthread_cleanup_push(cleanup_handler, NULL);
\&
    curr = time(NULL);
\&
    while (!done) {
        pthread_testcancel();           /* A cancelation point */
        if (curr < time(NULL)) {
            curr = time(NULL);
            printf("cnt = %d\en", cnt);  /* A cancelation point */
            cnt++;
        }
    }
\&
    pthread_cleanup_pop(cleanup_pop_arg);
    return NULL;
}
\&
int
main(int argc, char *argv[])
{
    pthread_t thr;
    int s;
    void *res;
\&
    s = pthread_create(&thr, NULL, thread_start, NULL);
    if (s != 0)
        handle_error_en(s, "pthread_create");
\&
    sleep(2);           /* Allow new thread to run a while */
\&
    if (argc > 1) {
        if (argc > 2)
            cleanup_pop_arg = atoi(argv[2]);
        done = 1;
\&
    } else {
        printf("Canceling thread\en");
        s = pthread_cancel(thr);
        if (s != 0)
            handle_error_en(s, "pthread_cancel");
    }
\&
    s = pthread_join(thr, &res);
    if (s != 0)
        handle_error_en(s, "pthread_join");
\&
    if (res == PTHREAD_CANCELED)
        printf("Thread was canceled; cnt = %d\en", cnt);
    else
        printf("Thread terminated normally; cnt = %d\en", cnt);
    exit(EXIT_SUCCESS);
}
.EE
.\" SRC END
.SH SEE ALSO
.BR pthread_cancel (3),
.BR pthread_cleanup_push_defer_np (3),
.BR pthread_setcancelstate (3),
.BR pthread_testcancel (3),
.BR pthreads (7)