blob: 95fa4a061625d8f4974963f52998481b9a1aa106 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include "pthreadP.h"
21
22
23void
24attribute_protected
25_pthread_cleanup_push_defer (
26 struct _pthread_cleanup_buffer *buffer,
27 void (*routine) (void *),
28 void *arg)
29{
30 struct pthread *self = THREAD_SELF;
31
32 buffer->__routine = routine;
33 buffer->__arg = arg;
34 buffer->__prev = THREAD_GETMEM (self, cleanup);
35
36 int cancelhandling = THREAD_GETMEM (self, cancelhandling);
37
38 /* Disable asynchronous cancellation for now. */
39 if (__builtin_expect (cancelhandling & CANCELTYPE_BITMASK, 0))
40 while (1)
41 {
42 int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
43 cancelhandling
44 & ~CANCELTYPE_BITMASK,
45 cancelhandling);
46 if (__builtin_expect (curval == cancelhandling, 1))
47 /* Successfully replaced the value. */
48 break;
49
50 /* Prepare for the next round. */
51 cancelhandling = curval;
52 }
53
54 buffer->__canceltype = (cancelhandling & CANCELTYPE_BITMASK
55 ? PTHREAD_CANCEL_ASYNCHRONOUS
56 : PTHREAD_CANCEL_DEFERRED);
57
58 THREAD_SETMEM (self, cleanup, buffer);
59}
60strong_alias (_pthread_cleanup_push_defer, __pthread_cleanup_push_defer)
61
62
63void
64attribute_protected
65_pthread_cleanup_pop_restore (
66 struct _pthread_cleanup_buffer *buffer,
67 int execute)
68{
69 struct pthread *self = THREAD_SELF;
70
71 THREAD_SETMEM (self, cleanup, buffer->__prev);
72
73 int cancelhandling;
74 if (__builtin_expect (buffer->__canceltype != PTHREAD_CANCEL_DEFERRED, 0)
75 && ((cancelhandling = THREAD_GETMEM (self, cancelhandling))
76 & CANCELTYPE_BITMASK) == 0)
77 {
78 while (1)
79 {
80 int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
81 cancelhandling
82 | CANCELTYPE_BITMASK,
83 cancelhandling);
84 if (__builtin_expect (curval == cancelhandling, 1))
85 /* Successfully replaced the value. */
86 break;
87
88 /* Prepare for the next round. */
89 cancelhandling = curval;
90 }
91
92 CANCELLATION_P (self);
93 }
94
95 /* If necessary call the cleanup routine after we removed the
96 current cleanup block from the list. */
97 if (execute)
98 buffer->__routine (buffer->__arg);
99}
100strong_alias (_pthread_cleanup_pop_restore, __pthread_cleanup_pop_restore)