Bug Summary

File:home/fedora/workspace/kea-dev/clang-static-analyzer/build/meson-private/tmpfptr8n1f/../../../src/lib/dhcp/tests/pkt_filter_iface_test_utils.cc
Warning:line 65, column 48
Call to blocking function 'read' inside of critical section

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-redhat-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name pkt_filter_iface_test_utils.cc -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/fedora/workspace/kea-dev/clang-static-analyzer/build/meson-private/tmpfptr8n1f -fcoverage-compilation-dir=/home/fedora/workspace/kea-dev/clang-static-analyzer/build/meson-private/tmpfptr8n1f -resource-dir /usr/bin/../lib/clang/21 -I src/lib/dhcp/tests/kea-dhcp-tests.p -I src/lib/dhcp/tests -I ../../../src/lib/dhcp/tests -I . -I ../../.. -I src -I ../../../src -I src/bin -I ../../../src/bin -I src/lib -I ../../../src/lib -I /usr/src/googletest/googletest -I /usr/src/googletest/googletest/include -I /usr/include -D _GLIBCXX_ASSERTIONS=1 -D _FILE_OFFSET_BITS=64 -D BOOST_ALL_NO_LIB -D TEST_DATA_BUILDDIR="/home/fedora/workspace/kea-dev/clang-static-analyzer/build/meson-private/tmpfptr8n1f/src/lib/dhcp/tests" -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/backward -internal-isystem /usr/bin/../lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-missing-field-initializers -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/fedora/workspace/kea-dev/clang-static-analyzer/build/meson-logs/scanbuild/2026-04-21-112018-4899-1 -x c++ ../../../src/lib/dhcp/tests/pkt_filter_iface_test_utils.cc

../../../src/lib/dhcp/tests/pkt_filter_iface_test_utils.cc

1// Copyright (C) 2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8#include <asiolink/io_address.h>
9#include <dhcp/pkt4.h>
10#include <dhcp/tests/pkt_filter_iface_test_utils.h>
11
12using namespace isc::asiolink;
13
14namespace isc {
15namespace dhcp {
16namespace test {
17
18const uint8_t MARKER = 0;
19
20PktFilterIfaceSocketTest::PktFilterIfaceSocketTest(bool ready_on_send, bool clear_on_read)
21 : ready_on_send_(ready_on_send),
22 clear_on_read_(clear_on_read) {
23}
24
25PktFilterIfaceSocketTest::~PktFilterIfaceSocketTest() {
26 for (auto it = socket_fds_.begin(); it != socket_fds_.end(); ++it) {
27 close(it->first);
28 close(it->second);
29 }
30}
31
32SocketInfo
33PktFilterIfaceSocketTest::openSocketCommon(Iface& /* iface */, const isc::asiolink::IOAddress& addr,
34 const uint16_t) {
35 int pipe_fds[2];
36 if (pipe(pipe_fds) < 0) {
37 isc_throw(Unexpected, "failed to open test pipe")do { std::ostringstream oss__; oss__ << "failed to open test pipe"
; throw Unexpected("../../../src/lib/dhcp/tests/pkt_filter_iface_test_utils.cc"
, 37, oss__.str().c_str()); } while (1)
;
38 }
39 if (fcntl(pipe_fds[0], F_SETFL4, O_NONBLOCK04000) < 0) {
40 close(pipe_fds[0]);
41 close(pipe_fds[1]);
42 isc_throw(Unexpected, "fcntl " << strerror(errno))do { std::ostringstream oss__; oss__ << "fcntl " <<
strerror((*__errno_location ())); throw Unexpected("../../../src/lib/dhcp/tests/pkt_filter_iface_test_utils.cc"
, 42, oss__.str().c_str()); } while (1)
;
43 }
44 if (fcntl(pipe_fds[1], F_SETFL4, O_NONBLOCK04000) < 0) {
45 close(pipe_fds[0]);
46 close(pipe_fds[1]);
47 isc_throw(Unexpected, "fcntl " << strerror(errno))do { std::ostringstream oss__; oss__ << "fcntl " <<
strerror((*__errno_location ())); throw Unexpected("../../../src/lib/dhcp/tests/pkt_filter_iface_test_utils.cc"
, 47, oss__.str().c_str()); } while (1)
;
48 }
49 std::unique_lock<std::mutex> lk(mutex_);
50 socket_fds_[pipe_fds[0]] = pipe_fds[1];
51 return (SocketInfo(addr, 9999, pipe_fds[0]));
52}
53
54PktPtr
55PktFilterIfaceSocketTest::receiveCommon(const SocketInfo& s) {
56 std::unique_lock<std::mutex> lk(mutex_);
2
Calling constructor for 'unique_lock<std::mutex>'
17
Returning from constructor for 'unique_lock<std::mutex>'
18
Entering critical section for the 2nd time here
57 auto it = socket_fds_.find(s.sockfd_);
58 if (it == socket_fds_.end()) {
19
Taking false branch
59 std::cout << "receive no such socket: " << s.sockfd_ << std::endl;
60 return (PktPtr());
61 }
62 PktPtr result = pkts_[s.sockfd_];
63 if (clear_on_read_) {
20
Assuming field 'clear_on_read_' is true
21
Taking true branch
64 uint8_t data;
65 for (size_t count = -1; count; count = read(s.sockfd_, &data, sizeof(data)));
22
Loop condition is true. Entering loop body
23
Call to blocking function 'read' inside of critical section
66 }
67 return (result);
68}
69
70int
71PktFilterIfaceSocketTest::sendCommon(const Iface& /* iface */, uint16_t sockfd, const PktPtr& pkt) {
72 std::unique_lock<std::mutex> lk(mutex_);
73 auto it = socket_fds_.find(sockfd);
74 if (it == socket_fds_.end()) {
75 std::cout << "send no such socket: " << sockfd << std::endl;
76 return (-1);
77 }
78 pkts_[sockfd] = pkt;
79 if (ready_on_send_) {
80 uint8_t data = MARKER;
81 uint8_t count = 0;
82 do {
83 count = write(it->second, &data, sizeof(data));
84 } while (!count);
85 }
86 return (0);
87}
88
89PktFilter4IfaceSocketTest::PktFilter4IfaceSocketTest(bool ready_on_send, bool clear_on_read)
90 : PktFilterIfaceSocketTest(ready_on_send, clear_on_read) {
91}
92
93bool
94PktFilter4IfaceSocketTest::isDirectResponseSupported() const {
95 return (true);
96}
97
98bool
99PktFilter4IfaceSocketTest::isSocketReceivedTimeSupported() const {
100 return (true);
101}
102
103SocketInfo
104PktFilter4IfaceSocketTest::openSocket(Iface& iface,
105 const isc::asiolink::IOAddress& addr,
106 const uint16_t port, const bool, const bool) {
107 return (PktFilterIfaceSocketTest::openSocketCommon(iface, addr, port));
108}
109
110Pkt4Ptr
111PktFilter4IfaceSocketTest::receive(Iface& /* iface */, const SocketInfo& s) {
112 return (boost::dynamic_pointer_cast<Pkt4>(PktFilterIfaceSocketTest::receiveCommon(s)));
113}
114
115int
116PktFilter4IfaceSocketTest::send(const Iface& iface, uint16_t sockfd, const Pkt4Ptr& pkt) {
117 return (PktFilterIfaceSocketTest::sendCommon(iface, sockfd, pkt));
118}
119
120PktFilter6IfaceSocketTest::PktFilter6IfaceSocketTest(bool ready_on_send, bool clear_on_read)
121 : PktFilterIfaceSocketTest(ready_on_send, clear_on_read) {
122}
123
124SocketInfo
125PktFilter6IfaceSocketTest::openSocket(Iface& iface,
126 const isc::asiolink::IOAddress& addr,
127 const uint16_t port, const bool) {
128 return (PktFilterIfaceSocketTest::openSocketCommon(iface, addr, port));
129}
130
131Pkt6Ptr
132PktFilter6IfaceSocketTest::receive(const SocketInfo& s) {
133 return (boost::dynamic_pointer_cast<Pkt6>(PktFilterIfaceSocketTest::receiveCommon(s)));
1
Calling 'PktFilterIfaceSocketTest::receiveCommon'
134}
135
136int
137PktFilter6IfaceSocketTest::send(const Iface& iface, uint16_t sockfd, const Pkt6Ptr& pkt) {
138 return (PktFilterIfaceSocketTest::sendCommon(iface, sockfd, pkt));
139}
140
141} // end of isc::dhcp::test namespace
142} // end of isc::dhcp namespace
143} // end of isc namespace

/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/unique_lock.h

1// std::unique_lock implementation -*- C++ -*-
2
3// Copyright (C) 2008-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/unique_lock.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{mutex}
28 */
29
30#ifndef _GLIBCXX_UNIQUE_LOCK_H1
31#define _GLIBCXX_UNIQUE_LOCK_H1 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus201703L < 201103L
38# include <bits/c++0x_warning.h>
39#else
40
41#include <bits/chrono.h>
42#include <bits/error_constants.h> // for std::errc
43#include <bits/move.h> // for std::swap
44#include <bits/std_mutex.h> // for std::defer_lock_t
45
46namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
47{
48_GLIBCXX_BEGIN_NAMESPACE_VERSION
49
50 /** @brief A movable scoped lock type.
51 *
52 * A unique_lock controls mutex ownership within a scope. Ownership of the
53 * mutex can be delayed until after construction and can be transferred
54 * to another unique_lock by move construction or move assignment. If a
55 * mutex lock is owned when the destructor runs ownership will be released.
56 *
57 * @headerfile mutex
58 * @ingroup mutexes
59 * @since C++11
60 */
61 template<typename _Mutex>
62 class unique_lock
63 {
64 public:
65 typedef _Mutex mutex_type;
66
67 unique_lock() noexcept
68 : _M_device(0), _M_owns(false)
69 { }
70
71 [[__nodiscard__]]
72 explicit unique_lock(mutex_type& __m)
73 : _M_device(std::__addressof(__m)), _M_owns(false)
74 {
75 lock();
3
Calling 'unique_lock::lock'
15
Returning from 'unique_lock::lock'
16
Entering critical section for the 1st time here
76 _M_owns = true;
77 }
78
79 unique_lock(mutex_type& __m, defer_lock_t) noexcept
80 : _M_device(std::__addressof(__m)), _M_owns(false)
81 { }
82
83 [[__nodiscard__]]
84 unique_lock(mutex_type& __m, try_to_lock_t)
85 : _M_device(std::__addressof(__m)), _M_owns(_M_device->try_lock())
86 { }
87
88 [[__nodiscard__]]
89 unique_lock(mutex_type& __m, adopt_lock_t) noexcept
90 : _M_device(std::__addressof(__m)), _M_owns(true)
91 {
92 // XXX calling thread owns mutex
93 }
94
95 template<typename _Clock, typename _Duration>
96 [[__nodiscard__]]
97 unique_lock(mutex_type& __m,
98 const chrono::time_point<_Clock, _Duration>& __atime)
99 : _M_device(std::__addressof(__m)),
100 _M_owns(_M_device->try_lock_until(__atime))
101 { }
102
103 template<typename _Rep, typename _Period>
104 [[__nodiscard__]]
105 unique_lock(mutex_type& __m,
106 const chrono::duration<_Rep, _Period>& __rtime)
107 : _M_device(std::__addressof(__m)),
108 _M_owns(_M_device->try_lock_for(__rtime))
109 { }
110
111 ~unique_lock()
112 {
113 if (_M_owns)
114 unlock();
115 }
116
117 unique_lock(const unique_lock&) = delete;
118 unique_lock& operator=(const unique_lock&) = delete;
119
120 unique_lock(unique_lock&& __u) noexcept
121 : _M_device(__u._M_device), _M_owns(__u._M_owns)
122 {
123 __u._M_device = 0;
124 __u._M_owns = false;
125 }
126
127 unique_lock& operator=(unique_lock&& __u) noexcept
128 {
129 // _GLIBCXX_RESOLVE_LIB_DEFECTS
130 // 4172. unique_lock self-move-assignment is broken
131 unique_lock(std::move(__u)).swap(*this);
132 return *this;
133 }
134
135 void
136 lock()
137 {
138 if (!_M_device
3.1
Field '_M_device' is non-null
3.1
Field '_M_device' is non-null
3.1
Field '_M_device' is non-null
3.1
Field '_M_device' is non-null
)
4
Taking false branch
139 __throw_system_error(int(errc::operation_not_permitted));
140 else if (_M_owns
4.1
Field '_M_owns' is false
4.1
Field '_M_owns' is false
4.1
Field '_M_owns' is false
4.1
Field '_M_owns' is false
)
5
Taking false branch
141 __throw_system_error(int(errc::resource_deadlock_would_occur));
142 else
143 {
144 _M_device->lock();
6
Calling 'mutex::lock'
13
Returning from 'mutex::lock'
14
Entering critical section here
145 _M_owns = true;
146 }
147 }
148
149 _GLIBCXX_NODISCARD[[__nodiscard__]]
150 bool
151 try_lock()
152 {
153 if (!_M_device)
154 __throw_system_error(int(errc::operation_not_permitted));
155 else if (_M_owns)
156 __throw_system_error(int(errc::resource_deadlock_would_occur));
157 else
158 {
159 _M_owns = _M_device->try_lock();
160 return _M_owns;
161 }
162 }
163
164 template<typename _Clock, typename _Duration>
165 _GLIBCXX_NODISCARD[[__nodiscard__]]
166 bool
167 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
168 {
169 if (!_M_device)
170 __throw_system_error(int(errc::operation_not_permitted));
171 else if (_M_owns)
172 __throw_system_error(int(errc::resource_deadlock_would_occur));
173 else
174 {
175 _M_owns = _M_device->try_lock_until(__atime);
176 return _M_owns;
177 }
178 }
179
180 template<typename _Rep, typename _Period>
181 _GLIBCXX_NODISCARD[[__nodiscard__]]
182 bool
183 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
184 {
185 if (!_M_device)
186 __throw_system_error(int(errc::operation_not_permitted));
187 else if (_M_owns)
188 __throw_system_error(int(errc::resource_deadlock_would_occur));
189 else
190 {
191 _M_owns = _M_device->try_lock_for(__rtime);
192 return _M_owns;
193 }
194 }
195
196 void
197 unlock()
198 {
199 if (!_M_owns)
200 __throw_system_error(int(errc::operation_not_permitted));
201 else if (_M_device)
202 {
203 _M_device->unlock();
204 _M_owns = false;
205 }
206 }
207
208 void
209 swap(unique_lock& __u) noexcept
210 {
211 std::swap(_M_device, __u._M_device);
212 std::swap(_M_owns, __u._M_owns);
213 }
214
215 mutex_type*
216 release() noexcept
217 {
218 mutex_type* __ret = _M_device;
219 _M_device = 0;
220 _M_owns = false;
221 return __ret;
222 }
223
224 _GLIBCXX_NODISCARD[[__nodiscard__]]
225 bool
226 owns_lock() const noexcept
227 { return _M_owns; }
228
229 explicit operator bool() const noexcept
230 { return owns_lock(); }
231
232 _GLIBCXX_NODISCARD[[__nodiscard__]]
233 mutex_type*
234 mutex() const noexcept
235 { return _M_device; }
236
237 private:
238 mutex_type* _M_device;
239 bool _M_owns;
240 };
241
242 /// Swap overload for unique_lock objects.
243 /// @relates unique_lock
244 template<typename _Mutex>
245 inline void
246 swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) noexcept
247 { __x.swap(__y); }
248
249_GLIBCXX_END_NAMESPACE_VERSION
250} // namespace
251
252#endif // C++11
253#endif // _GLIBCXX_UNIQUE_LOCK_H

/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/std_mutex.h

1// std::mutex implementation -*- C++ -*-
2
3// Copyright (C) 2003-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/std_mutex.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{mutex}
28 */
29
30#ifndef _GLIBCXX_MUTEX_H1
31#define _GLIBCXX_MUTEX_H1 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus201703L < 201103L
38# include <bits/c++0x_warning.h>
39#else
40
41#include <errno(*__errno_location ()).h> // EBUSY
42#include <bits/functexcept.h>
43#include <bits/gthr.h>
44
45namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
46{
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
48
49 /**
50 * @defgroup mutexes Mutexes
51 * @ingroup concurrency
52 *
53 * Classes for mutex support.
54 * @{
55 */
56
57#ifdef _GLIBCXX_HAS_GTHREADS1
58 /// @cond undocumented
59
60 // Common base class for std::mutex and std::timed_mutex
61 class __mutex_base
62 {
63 protected:
64 typedef __gthread_mutex_t __native_type;
65
66#ifdef __GTHREAD_MUTEX_INIT{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { __null, __null
} } }
67 __native_type _M_mutex = __GTHREAD_MUTEX_INIT{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { __null, __null
} } }
;
68
69 constexpr __mutex_base() noexcept = default;
70#else
71 __native_type _M_mutex;
72
73 __mutex_base() noexcept
74 {
75 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
76 __GTHREAD_MUTEX_INIT_FUNCTION__gthread_mutex_init_function(&_M_mutex);
77 }
78
79 ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
80#endif
81
82 __mutex_base(const __mutex_base&) = delete;
83 __mutex_base& operator=(const __mutex_base&) = delete;
84 };
85 /// @endcond
86
87 /** The standard mutex type.
88 *
89 * A simple, non-recursive, non-timed mutex.
90 *
91 * Do not call `lock()` and `unlock()` directly, use a scoped lock type
92 * such as `std::unique_lock`, `std::lock_guard`, or (since C++17)
93 * `std::scoped_lock`.
94 *
95 * @headerfile mutex
96 * @since C++11
97 */
98 class mutex : private __mutex_base
99 {
100 public:
101 typedef __native_type* native_handle_type;
102
103#ifdef __GTHREAD_MUTEX_INIT{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { __null, __null
} } }
104 constexpr
105#endif
106 mutex() noexcept = default;
107 ~mutex() = default;
108
109 mutex(const mutex&) = delete;
110 mutex& operator=(const mutex&) = delete;
111
112 void
113 lock()
114 {
115 int __e = __gthread_mutex_lock(&_M_mutex);
7
Calling '__gthread_mutex_lock'
10
Returning from '__gthread_mutex_lock'
116
117 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
118 if (__e)
11
Assuming '__e' is 0
12
Taking false branch
119 __throw_system_error(__e);
120 }
121
122 _GLIBCXX_NODISCARD[[__nodiscard__]]
123 bool
124 try_lock() noexcept
125 {
126 // XXX EINVAL, EAGAIN, EBUSY
127 return !__gthread_mutex_trylock(&_M_mutex);
128 }
129
130 void
131 unlock()
132 {
133 // XXX EINVAL, EAGAIN, EPERM
134 __gthread_mutex_unlock(&_M_mutex);
135 }
136
137 native_handle_type
138 native_handle() noexcept
139 { return &_M_mutex; }
140 };
141
142 /// @cond undocumented
143
144 // Implementation details for std::condition_variable
145 class __condvar
146 {
147 using timespec = __gthread_time_t;
148
149 public:
150 __condvar() noexcept
151 {
152#ifndef __GTHREAD_COND_INIT{ { {0}, {0}, {0, 0}, 0, 0, {0, 0}, 0, 0 } }
153 __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
154#endif
155 }
156
157 ~__condvar()
158 {
159 int __e __attribute__((__unused__)) = __gthread_cond_destroy(&_M_cond);
160 __glibcxx_assert(__e != EBUSY)do { if (__builtin_expect(!bool(__e != 16), false)) std::__glibcxx_assert_fail
("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/std_mutex.h"
, 160, __PRETTY_FUNCTION__, "__e != 16"); } while (false)
; // threads are still blocked
161 }
162
163 __condvar(const __condvar&) = delete;
164 __condvar& operator=(const __condvar&) = delete;
165
166 __gthread_cond_t* native_handle() noexcept { return &_M_cond; }
167
168 // Expects: Calling thread has locked __m.
169 void
170 wait(mutex& __m)
171 {
172 int __e __attribute__((__unused__))
173 = __gthread_cond_wait(&_M_cond, __m.native_handle());
174 __glibcxx_assert(__e == 0)do { if (__builtin_expect(!bool(__e == 0), false)) std::__glibcxx_assert_fail
("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/std_mutex.h"
, 174, __PRETTY_FUNCTION__, "__e == 0"); } while (false)
;
175 }
176
177 void
178 wait_until(mutex& __m, timespec& __abs_time)
179 {
180 __gthread_cond_timedwait(&_M_cond, __m.native_handle(), &__abs_time);
181 }
182
183#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT1
184 void
185 wait_until(mutex& __m, clockid_t __clock, timespec& __abs_time)
186 {
187 pthread_cond_clockwait(&_M_cond, __m.native_handle(), __clock,
188 &__abs_time);
189 }
190#endif
191
192 void
193 notify_one() noexcept
194 {
195 int __e __attribute__((__unused__)) = __gthread_cond_signal(&_M_cond);
196 __glibcxx_assert(__e == 0)do { if (__builtin_expect(!bool(__e == 0), false)) std::__glibcxx_assert_fail
("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/std_mutex.h"
, 196, __PRETTY_FUNCTION__, "__e == 0"); } while (false)
;
197 }
198
199 void
200 notify_all() noexcept
201 {
202 int __e __attribute__((__unused__)) = __gthread_cond_broadcast(&_M_cond);
203 __glibcxx_assert(__e == 0)do { if (__builtin_expect(!bool(__e == 0), false)) std::__glibcxx_assert_fail
("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/std_mutex.h"
, 203, __PRETTY_FUNCTION__, "__e == 0"); } while (false)
;
204 }
205
206 protected:
207#ifdef __GTHREAD_COND_INIT{ { {0}, {0}, {0, 0}, 0, 0, {0, 0}, 0, 0 } }
208 __gthread_cond_t _M_cond = __GTHREAD_COND_INIT{ { {0}, {0}, {0, 0}, 0, 0, {0, 0}, 0, 0 } };
209#else
210 __gthread_cond_t _M_cond;
211#endif
212 };
213 /// @endcond
214
215#endif // _GLIBCXX_HAS_GTHREADS
216
217 /// Do not acquire ownership of the mutex.
218 struct defer_lock_t { explicit defer_lock_t() = default; };
219
220 /// Try to acquire ownership of the mutex without blocking.
221 struct try_to_lock_t { explicit try_to_lock_t() = default; };
222
223 /// Assume the calling thread has already obtained mutex ownership
224 /// and manage it.
225 struct adopt_lock_t { explicit adopt_lock_t() = default; };
226
227 /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
228 _GLIBCXX17_INLINEinline constexpr defer_lock_t defer_lock { };
229
230 /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
231 _GLIBCXX17_INLINEinline constexpr try_to_lock_t try_to_lock { };
232
233 /// Tag used to make a scoped lock take ownership of a locked mutex.
234 _GLIBCXX17_INLINEinline constexpr adopt_lock_t adopt_lock { };
235
236 /** @brief A simple scoped lock type.
237 *
238 * A lock_guard controls mutex ownership within a scope, releasing
239 * ownership in the destructor.
240 *
241 * @headerfile mutex
242 * @since C++11
243 */
244 template<typename _Mutex>
245 class lock_guard
246 {
247 public:
248 typedef _Mutex mutex_type;
249
250 [[__nodiscard__]]
251 explicit lock_guard(mutex_type& __m) : _M_device(__m)
252 { _M_device.lock(); }
253
254 [[__nodiscard__]]
255 lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
256 { } // calling thread owns mutex
257
258 ~lock_guard()
259 { _M_device.unlock(); }
260
261 lock_guard(const lock_guard&) = delete;
262 lock_guard& operator=(const lock_guard&) = delete;
263
264 private:
265 mutex_type& _M_device;
266 };
267
268 /// @} group mutexes
269_GLIBCXX_END_NAMESPACE_VERSION
270} // namespace
271#endif // C++11
272#endif // _GLIBCXX_MUTEX_H

/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/x86_64-redhat-linux/bits/gthr-default.h

1/* Threads compatibility routines for libgcc2 and libobjc. */
2/* Compile this one with gcc. */
3/* Copyright (C) 1997-2025 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
25
26#ifndef _GLIBCXX_GCC_GTHR_POSIX_H
27#define _GLIBCXX_GCC_GTHR_POSIX_H
28
29/* POSIX threads specific definitions.
30 Easy, since the interface is just one-to-one mapping. */
31
32#define __GTHREADS1 1
33#define __GTHREADS_CXX0X1 1
34
35#include <pthread.h>
36
37#if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \
38 || !defined(_GTHREAD_USE_MUTEX_TIMEDLOCK1))
39# include <unistd.h>
40# if defined(_POSIX_TIMEOUTS200809L) && _POSIX_TIMEOUTS200809L >= 0
41# define _GTHREAD_USE_MUTEX_TIMEDLOCK1 1
42# else
43# define _GTHREAD_USE_MUTEX_TIMEDLOCK1 0
44# endif
45#endif
46
47#ifdef __has_attribute
48# if0 __has_attribute(__always_inline__)1
49# define __GTHREAD_ALWAYS_INLINE __attribute__((__always_inline__))
50# endif
51#endif
52#ifndef __GTHREAD_ALWAYS_INLINE
53# define __GTHREAD_ALWAYS_INLINE
54#endif
55
56#ifdef __cplusplus201703L
57# define __GTHREAD_INLINE inline __GTHREAD_ALWAYS_INLINE
58#else
59# define __GTHREAD_INLINE static inline
60#endif
61
62typedef pthread_t __gthread_t;
63typedef pthread_key_t __gthread_key_t;
64typedef pthread_once_t __gthread_once_t;
65typedef pthread_mutex_t __gthread_mutex_t;
66#ifndef __cplusplus201703L
67typedef pthread_rwlock_t __gthread_rwlock_t;
68#endif
69typedef pthread_mutex_t __gthread_recursive_mutex_t;
70typedef pthread_cond_t __gthread_cond_t;
71typedef struct timespec __gthread_time_t;
72
73/* POSIX like conditional variables are supported. Please look at comments
74 in gthr.h for details. */
75#define __GTHREAD_HAS_COND1 1
76
77#define __GTHREAD_MUTEX_INIT{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { __null, __null
} } }
PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { __null, __null
} } }
78#define __GTHREAD_MUTEX_INIT_FUNCTION__gthread_mutex_init_function __gthread_mutex_init_function
79#ifndef __cplusplus201703L
80#define __GTHREAD_RWLOCK_INIT PTHREAD_RWLOCK_INITIALIZER{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0 }, 0, PTHREAD_RWLOCK_DEFAULT_NP
} }
81#endif
82#define __GTHREAD_ONCE_INIT0 PTHREAD_ONCE_INIT0
83#if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
84#define __GTHREAD_RECURSIVE_MUTEX_INIT{ { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, 0, { __null, __null
} } }
PTHREAD_RECURSIVE_MUTEX_INITIALIZER
85#elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP{ { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, 0, { __null, __null
} } }
)
86#define __GTHREAD_RECURSIVE_MUTEX_INIT{ { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, 0, { __null, __null
} } }
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP{ { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, 0, { __null, __null
} } }
87#else
88#define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
89#endif
90#define __GTHREAD_COND_INIT{ { {0}, {0}, {0, 0}, 0, 0, {0, 0}, 0, 0 } } PTHREAD_COND_INITIALIZER{ { {0}, {0}, {0, 0}, 0, 0, {0, 0}, 0, 0 } }
91#define __GTHREAD_TIME_INIT{0,0} {0,0}
92
93#ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
94# undef __GTHREAD_MUTEX_INIT{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { __null, __null
} } }
95#endif
96#ifdef _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC
97# undef __GTHREAD_RECURSIVE_MUTEX_INIT{ { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, 0, { __null, __null
} } }
98# undef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
99# define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
100#endif
101#ifdef _GTHREAD_USE_COND_INIT_FUNC
102# undef __GTHREAD_COND_INIT{ { {0}, {0}, {0, 0}, 0, 0, {0, 0}, 0, 0 } }
103# define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
104#endif
105
106#if __GXX_WEAK__1 && _GLIBCXX_GTHREAD_USE_WEAK0
107# ifndef __gthrw_pragma
108# define __gthrw_pragma(pragma)
109# endif
110# define __gthrw2(name,name2,type) \
111 static __typeof(type) name \
112 __attribute__ ((__weakref__(#name2), __copy__ (type))); \
113 __gthrw_pragma(weak type)
114# define __gthrw_(name)name __gthrw_ ## name
115#else
116# define __gthrw2(name,name2,type)
117# define __gthrw_(name)name name
118#endif
119
120/* Typically, __gthrw_foo is a weak reference to symbol foo. */
121#define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
122
123__gthrw(pthread_once)
124__gthrw(pthread_getspecific)
125__gthrw(pthread_setspecific)
126
127__gthrw(pthread_create)
128__gthrw(pthread_join)
129__gthrw(pthread_equal)
130__gthrw(pthread_self)
131__gthrw(pthread_detach)
132#ifndef __BIONIC__
133__gthrw(pthread_cancel)
134#endif
135__gthrw(sched_yield)
136
137__gthrw(pthread_mutex_lock)
138__gthrw(pthread_mutex_trylock)
139#if _GTHREAD_USE_MUTEX_TIMEDLOCK1
140__gthrw(pthread_mutex_timedlock)
141#endif
142__gthrw(pthread_mutex_unlock)
143__gthrw(pthread_mutex_init)
144__gthrw(pthread_mutex_destroy)
145
146__gthrw(pthread_cond_init)
147__gthrw(pthread_cond_broadcast)
148__gthrw(pthread_cond_signal)
149__gthrw(pthread_cond_wait)
150__gthrw(pthread_cond_timedwait)
151__gthrw(pthread_cond_destroy)
152
153__gthrw(pthread_key_create)
154__gthrw(pthread_key_delete)
155__gthrw(pthread_mutexattr_init)
156__gthrw(pthread_mutexattr_settype)
157__gthrw(pthread_mutexattr_destroy)
158
159#ifndef __cplusplus201703L
160__gthrw(pthread_rwlock_rdlock)
161__gthrw(pthread_rwlock_tryrdlock)
162__gthrw(pthread_rwlock_wrlock)
163__gthrw(pthread_rwlock_trywrlock)
164__gthrw(pthread_rwlock_unlock)
165#endif
166
167#if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
168/* Objective-C. */
169__gthrw(pthread_exit)
170#ifdef _POSIX_PRIORITY_SCHEDULING200809L
171#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING200809L
172__gthrw(sched_get_priority_max)
173__gthrw(sched_get_priority_min)
174#endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
175#endif /* _POSIX_PRIORITY_SCHEDULING */
176__gthrw(pthread_attr_destroy)
177__gthrw(pthread_attr_init)
178__gthrw(pthread_attr_setdetachstate)
179#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING200809L
180__gthrw(pthread_getschedparam)
181__gthrw(pthread_setschedparam)
182#endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
183#endif /* _LIBOBJC || _LIBOBJC_WEAK */
184
185#if __GXX_WEAK__1 && _GLIBCXX_GTHREAD_USE_WEAK0
186
187/* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
188 -pthreads is not specified. The functions are dummies and most return an
189 error value. However pthread_once returns 0 without invoking the routine
190 it is passed so we cannot pretend that the interface is active if -pthreads
191 is not specified. On Solaris 2.5.1, the interface is not exposed at all so
192 we need to play the usual game with weak symbols. On Solaris 10 and up, a
193 working interface is always exposed. On FreeBSD 6 and later, libc also
194 exposes a dummy POSIX threads interface, similar to what Solaris 2.6 up
195 to 9 does. FreeBSD >= 700014 even provides a pthread_cancel stub in libc,
196 which means the alternate __gthread_active_p below cannot be used there. */
197
198#if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
199
200#pragma GCC visibility push(hidden)
201__GTHREAD_INLINE volatile int *
202__gthread_active (void)
203{
204 static volatile int __gthread_active_var = -1;
205 return &__gthread_active_var;
206}
207#pragma GCC visibility pop
208
209__GTHREAD_INLINE void
210__gthread_trigger (void)
211{
212 *__gthread_active () = 1;
213}
214
215#pragma GCC visibility push(hidden)
216__GTHREAD_INLINE int
217__gthread_active_p (void)
218{
219 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { __null, __null
} } }
;
220 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT0;
221
222 /* Avoid reading __gthread_active twice on the main code path. */
223 int __gthread_active_latest_value = *__gthread_active ();
224
225 /* This test is not protected to avoid taking a lock on the main code
226 path so every update of __gthread_active in a threaded program must
227 be atomic with regard to the result of the test. */
228 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
229 {
230 if (__gthrw_(pthread_once)pthread_once)
231 {
232 /* If this really is a threaded program, then we must ensure that
233 __gthread_active has been set to 1 before exiting this block. */
234 __gthrw_(pthread_mutex_lock)pthread_mutex_lock (&__gthread_active_mutex);
235 __gthrw_(pthread_once)pthread_once (&__gthread_active_once, __gthread_trigger);
236 __gthrw_(pthread_mutex_unlock)pthread_mutex_unlock (&__gthread_active_mutex);
237 }
238
239 /* Make sure we'll never enter this block again. */
240 if (*__gthread_active () < 0)
241 *__gthread_active () = 0;
242
243 __gthread_active_latest_value = *__gthread_active ();
244 }
245
246 return __gthread_active_latest_value != 0;
247}
248#pragma GCC visibility pop
249
250#else /* neither FreeBSD nor Solaris */
251
252/* For a program to be multi-threaded the only thing that it certainly must
253 be using is pthread_create. However, there may be other libraries that
254 intercept pthread_create with their own definitions to wrap pthreads
255 functionality for some purpose. In those cases, pthread_create being
256 defined might not necessarily mean that libpthread is actually linked
257 in.
258
259 For the GNU C library, we can use a known internal name. This is always
260 available in the ABI, but no other library would define it. That is
261 ideal, since any public pthread function might be intercepted just as
262 pthread_create might be. __pthread_key_create is an "internal"
263 implementation symbol, but it is part of the public exported ABI. Also,
264 it's among the symbols that the static libpthread.a always links in
265 whenever pthread_create is used, so there is no danger of a false
266 negative result in any statically-linked, multi-threaded program.
267
268 For others, we choose pthread_cancel as a function that seems unlikely
269 to be redefined by an interceptor library. The bionic (Android) C
270 library does not provide pthread_cancel, so we do use pthread_create
271 there (and interceptor libraries lose). */
272
273#ifdef __GLIBC__2
274__gthrw2(__gthrw_(__pthread_key_create),
275 __pthread_key_create,
276 pthread_key_create)
277# define GTHR_ACTIVE_PROXY __gthrw_(__pthread_key_create)__pthread_key_create
278#elif defined (__BIONIC__)
279# define GTHR_ACTIVE_PROXY __gthrw_(pthread_create)pthread_create
280#else
281# define GTHR_ACTIVE_PROXY __gthrw_(pthread_cancel)pthread_cancel
282#endif
283
284#pragma GCC visibility push(hidden)
285__GTHREAD_INLINE int
286__gthread_active_p (void)
287{
288 static void *const __gthread_active_ptr
289 = __extension__ (void *) &GTHR_ACTIVE_PROXY;
290 return __gthread_active_ptr != 0;
291}
292#pragma GCC visibility pop
293
294#endif /* FreeBSD or Solaris */
295
296#else /* not __GXX_WEAK__ */
297
298/* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
299 calls in shared flavors of the HP-UX C library. Most of the stubs
300 have no functionality. The details are described in the "libc cumulative
301 patch" for each subversion of HP-UX 11. There are two special interfaces
302 provided for checking whether an application is linked to a shared pthread
303 library or not. However, these interfaces aren't available in early
304 libpthread libraries. We also need a test that works for archive
305 libraries. We can't use pthread_once as some libc versions call the
306 init function. We also can't use pthread_create or pthread_attr_init
307 as these create a thread and thereby prevent changing the default stack
308 size. The function pthread_default_stacksize_np is available in both
309 the archive and shared versions of libpthread. It can be used to
310 determine the default pthread stack size. There is a stub in some
311 shared libc versions which returns a zero size if pthreads are not
312 active. We provide an equivalent stub to handle cases where libc
313 doesn't provide one. */
314
315#if defined(__hppa__) && defined(__hpux__)
316
317#pragma GCC visibility push(hidden)
318__GTHREAD_INLINE volatile int *
319__gthread_active (void)
320{
321 static volatile int __gthread_active_var = -1;
322 return &__gthread_active_var;
323}
324#pragma GCC visibility pop
325
326__GTHREAD_INLINE int
327__gthread_active_p (void)
328{
329 /* Avoid reading __gthread_active twice on the main code path. */
330 int __gthread_active_latest_value = *__gthread_active ();
331 size_t __s;
332
333 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
334 {
335 pthread_default_stacksize_np (0, &__s);
336 *__gthread_active () = __s ? 1 : 0;
337 __gthread_active_latest_value = *__gthread_active ();
338 }
339
340 return __gthread_active_latest_value != 0;
341}
342
343#else /* not hppa-hpux */
344
345__GTHREAD_INLINE int
346__gthread_active_p (void)
347{
348 return 1;
349}
350
351#endif /* hppa-hpux */
352
353#endif /* __GXX_WEAK__ */
354
355#ifdef _LIBOBJC
356
357/* This is the config.h file in libobjc/ */
358#include <config.h>
359
360#ifdef HAVE_SCHED_H
361# include <sched.h>
362#endif
363
364/* Key structure for maintaining thread specific storage */
365static pthread_key_t _objc_thread_storage;
366static pthread_attr_t _objc_thread_attribs;
367
368/* Thread local storage for a single thread */
369static void *thread_local_storage = NULL__null;
370
371/* Backend initialization functions */
372
373/* Initialize the threads subsystem. */
374static inline int
375__gthread_objc_init_thread_system (void)
376{
377 if (__gthread_active_p ())
378 {
379 /* Initialize the thread storage key. */
380 if (__gthrw_(pthread_key_create)pthread_key_create (&_objc_thread_storage, NULL__null) == 0)
381 {
382 /* The normal default detach state for threads is
383 * PTHREAD_CREATE_JOINABLE which causes threads to not die
384 * when you think they should. */
385 if (__gthrw_(pthread_attr_init)pthread_attr_init (&_objc_thread_attribs) == 0
386 && __gthrw_(pthread_attr_setdetachstate)pthread_attr_setdetachstate (&_objc_thread_attribs,
387 PTHREAD_CREATE_DETACHEDPTHREAD_CREATE_DETACHED) == 0)
388 return 0;
389 }
390 }
391
392 return -1;
393}
394
395/* Close the threads subsystem. */
396static inline int
397__gthread_objc_close_thread_system (void)
398{
399 if (__gthread_active_p ()
400 && __gthrw_(pthread_key_delete)pthread_key_delete (_objc_thread_storage) == 0
401 && __gthrw_(pthread_attr_destroy)pthread_attr_destroy (&_objc_thread_attribs) == 0)
402 return 0;
403
404 return -1;
405}
406
407/* Backend thread functions */
408
409/* Create a new thread of execution. */
410static inline objc_thread_t
411__gthread_objc_thread_detach (void (*func)(void *), void *arg)
412{
413 objc_thread_t thread_id;
414 pthread_t new_thread_handle;
415
416 if (!__gthread_active_p ())
417 return NULL__null;
418
419 if (!(__gthrw_(pthread_create)pthread_create (&new_thread_handle, &_objc_thread_attribs,
420 (void *) func, arg)))
421 thread_id = (objc_thread_t) new_thread_handle;
422 else
423 thread_id = NULL__null;
424
425 return thread_id;
426}
427
428/* Set the current thread's priority. */
429static inline int
430__gthread_objc_thread_set_priority (int priority)
431{
432 if (!__gthread_active_p ())
433 return -1;
434 else
435 {
436#ifdef _POSIX_PRIORITY_SCHEDULING200809L
437#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING200809L
438 pthread_t thread_id = __gthrw_(pthread_self)pthread_self ();
439 int policy;
440 struct sched_param params;
441 int priority_min, priority_max;
442
443 if (__gthrw_(pthread_getschedparam)pthread_getschedparam (thread_id, &policy, &params) == 0)
444 {
445 if ((priority_max = __gthrw_(sched_get_priority_max)sched_get_priority_max (policy)) == -1)
446 return -1;
447
448 if ((priority_min = __gthrw_(sched_get_priority_min)sched_get_priority_min (policy)) == -1)
449 return -1;
450
451 if (priority > priority_max)
452 priority = priority_max;
453 else if (priority < priority_min)
454 priority = priority_min;
455 params.sched_prioritysched_priority = priority;
456
457 /*
458 * The solaris 7 and several other man pages incorrectly state that
459 * this should be a pointer to policy but pthread.h is universally
460 * at odds with this.
461 */
462 if (__gthrw_(pthread_setschedparam)pthread_setschedparam (thread_id, policy, &params) == 0)
463 return 0;
464 }
465#endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
466#endif /* _POSIX_PRIORITY_SCHEDULING */
467 return -1;
468 }
469}
470
471/* Return the current thread's priority. */
472static inline int
473__gthread_objc_thread_get_priority (void)
474{
475#ifdef _POSIX_PRIORITY_SCHEDULING200809L
476#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING200809L
477 if (__gthread_active_p ())
478 {
479 int policy;
480 struct sched_param params;
481
482 if (__gthrw_(pthread_getschedparam)pthread_getschedparam (__gthrw_(pthread_self)pthread_self (), &policy, &params) == 0)
483 return params.sched_prioritysched_priority;
484 else
485 return -1;
486 }
487 else
488#endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
489#endif /* _POSIX_PRIORITY_SCHEDULING */
490 return OBJC_THREAD_INTERACTIVE_PRIORITY;
491}
492
493/* Yield our process time to another thread. */
494static inline void
495__gthread_objc_thread_yield (void)
496{
497 if (__gthread_active_p ())
498 __gthrw_(sched_yield)sched_yield ();
499}
500
501/* Terminate the current thread. */
502static inline int
503__gthread_objc_thread_exit (void)
504{
505 if (__gthread_active_p ())
506 /* exit the thread */
507 __gthrw_(pthread_exit)pthread_exit (&__objc_thread_exit_status);
508
509 /* Failed if we reached here */
510 return -1;
511}
512
513/* Returns an integer value which uniquely describes a thread. */
514static inline objc_thread_t
515__gthread_objc_thread_id (void)
516{
517 if (__gthread_active_p ())
518 return (objc_thread_t) __gthrw_(pthread_self)pthread_self ();
519 else
520 return (objc_thread_t) 1;
521}
522
523/* Sets the thread's local storage pointer. */
524static inline int
525__gthread_objc_thread_set_data (void *value)
526{
527 if (__gthread_active_p ())
528 return __gthrw_(pthread_setspecific)pthread_setspecific (_objc_thread_storage, value);
529 else
530 {
531 thread_local_storage = value;
532 return 0;
533 }
534}
535
536/* Returns the thread's local storage pointer. */
537static inline void *
538__gthread_objc_thread_get_data (void)
539{
540 if (__gthread_active_p ())
541 return __gthrw_(pthread_getspecific)pthread_getspecific (_objc_thread_storage);
542 else
543 return thread_local_storage;
544}
545
546/* Backend mutex functions */
547
548/* Allocate a mutex. */
549static inline int
550__gthread_objc_mutex_allocate (objc_mutex_t mutex)
551{
552 if (__gthread_active_p ())
553 {
554 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
555
556 if (__gthrw_(pthread_mutex_init)pthread_mutex_init ((pthread_mutex_t *) mutex->backend, NULL__null))
557 {
558 objc_free (mutex->backend);
559 mutex->backend = NULL__null;
560 return -1;
561 }
562 }
563
564 return 0;
565}
566
567/* Deallocate a mutex. */
568static inline int
569__gthread_objc_mutex_deallocate (objc_mutex_t mutex)
570{
571 if (__gthread_active_p ())
572 {
573 int count;
574
575 /*
576 * Posix Threads specifically require that the thread be unlocked
577 * for __gthrw_(pthread_mutex_destroy) to work.
578 */
579
580 do
581 {
582 count = __gthrw_(pthread_mutex_unlock)pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend);
583 if (count < 0)
584 return -1;
585 }
586 while (count);
587
588 if (__gthrw_(pthread_mutex_destroy)pthread_mutex_destroy ((pthread_mutex_t *) mutex->backend))
589 return -1;
590
591 objc_free (mutex->backend);
592 mutex->backend = NULL__null;
593 }
594 return 0;
595}
596
597/* Grab a lock on a mutex. */
598static inline int
599__gthread_objc_mutex_lock (objc_mutex_t mutex)
600{
601 if (__gthread_active_p ()
602 && __gthrw_(pthread_mutex_lock)pthread_mutex_lock ((pthread_mutex_t *) mutex->backend) != 0)
603 {
604 return -1;
605 }
606
607 return 0;
608}
609
610/* Try to grab a lock on a mutex. */
611static inline int
612__gthread_objc_mutex_trylock (objc_mutex_t mutex)
613{
614 if (__gthread_active_p ()
615 && __gthrw_(pthread_mutex_trylock)pthread_mutex_trylock ((pthread_mutex_t *) mutex->backend) != 0)
616 {
617 return -1;
618 }
619
620 return 0;
621}
622
623/* Unlock the mutex */
624static inline int
625__gthread_objc_mutex_unlock (objc_mutex_t mutex)
626{
627 if (__gthread_active_p ()
628 && __gthrw_(pthread_mutex_unlock)pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend) != 0)
629 {
630 return -1;
631 }
632
633 return 0;
634}
635
636/* Backend condition mutex functions */
637
638/* Allocate a condition. */
639static inline int
640__gthread_objc_condition_allocate (objc_condition_t condition)
641{
642 if (__gthread_active_p ())
643 {
644 condition->backend = objc_malloc (sizeof (pthread_cond_t));
645
646 if (__gthrw_(pthread_cond_init)pthread_cond_init ((pthread_cond_t *) condition->backend, NULL__null))
647 {
648 objc_free (condition->backend);
649 condition->backend = NULL__null;
650 return -1;
651 }
652 }
653
654 return 0;
655}
656
657/* Deallocate a condition. */
658static inline int
659__gthread_objc_condition_deallocate (objc_condition_t condition)
660{
661 if (__gthread_active_p ())
662 {
663 if (__gthrw_(pthread_cond_destroy)pthread_cond_destroy ((pthread_cond_t *) condition->backend))
664 return -1;
665
666 objc_free (condition->backend);
667 condition->backend = NULL__null;
668 }
669 return 0;
670}
671
672/* Wait on the condition */
673static inline int
674__gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
675{
676 if (__gthread_active_p ())
677 return __gthrw_(pthread_cond_wait)pthread_cond_wait ((pthread_cond_t *) condition->backend,
678 (pthread_mutex_t *) mutex->backend);
679 else
680 return 0;
681}
682
683/* Wake up all threads waiting on this condition. */
684static inline int
685__gthread_objc_condition_broadcast (objc_condition_t condition)
686{
687 if (__gthread_active_p ())
688 return __gthrw_(pthread_cond_broadcast)pthread_cond_broadcast ((pthread_cond_t *) condition->backend);
689 else
690 return 0;
691}
692
693/* Wake up one thread waiting on this condition. */
694static inline int
695__gthread_objc_condition_signal (objc_condition_t condition)
696{
697 if (__gthread_active_p ())
698 return __gthrw_(pthread_cond_signal)pthread_cond_signal ((pthread_cond_t *) condition->backend);
699 else
700 return 0;
701}
702
703#else /* _LIBOBJC */
704
705__GTHREAD_INLINE int
706__gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
707 void *__args)
708{
709 return __gthrw_(pthread_create)pthread_create (__threadid, NULL__null, __func, __args);
710}
711
712__GTHREAD_INLINE int
713__gthread_join (__gthread_t __threadid, void **__value_ptr)
714{
715 return __gthrw_(pthread_join)pthread_join (__threadid, __value_ptr);
716}
717
718__GTHREAD_INLINE int
719__gthread_detach (__gthread_t __threadid)
720{
721 return __gthrw_(pthread_detach)pthread_detach (__threadid);
722}
723
724__GTHREAD_INLINE int
725__gthread_equal (__gthread_t __t1, __gthread_t __t2)
726{
727 return __gthrw_(pthread_equal)pthread_equal (__t1, __t2);
728}
729
730__GTHREAD_INLINE __gthread_t
731__gthread_self (void)
732{
733 return __gthrw_(pthread_self)pthread_self ();
734}
735
736__GTHREAD_INLINE int
737__gthread_yield (void)
738{
739 return __gthrw_(sched_yield)sched_yield ();
740}
741
742__GTHREAD_INLINE int
743__gthread_once (__gthread_once_t *__once, void (*__func) (void))
744{
745 if (__gthread_active_p ())
746 return __gthrw_(pthread_once)pthread_once (__once, __func);
747 else
748 return -1;
749}
750
751__GTHREAD_INLINE int
752__gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
753{
754 return __gthrw_(pthread_key_create)pthread_key_create (__key, __dtor);
755}
756
757__GTHREAD_INLINE int
758__gthread_key_delete (__gthread_key_t __key)
759{
760 return __gthrw_(pthread_key_delete)pthread_key_delete (__key);
761}
762
763__GTHREAD_INLINE void *
764__gthread_getspecific (__gthread_key_t __key)
765{
766 return __gthrw_(pthread_getspecific)pthread_getspecific (__key);
767}
768
769__GTHREAD_INLINE int
770__gthread_setspecific (__gthread_key_t __key, const void *__ptr)
771{
772 return __gthrw_(pthread_setspecific)pthread_setspecific (__key, __ptr);
773}
774
775__GTHREAD_INLINE void
776__gthread_mutex_init_function (__gthread_mutex_t *__mutex)
777{
778 if (__gthread_active_p ())
779 __gthrw_(pthread_mutex_init)pthread_mutex_init (__mutex, NULL__null);
780}
781
782__GTHREAD_INLINE int
783__gthread_mutex_destroy (__gthread_mutex_t *__mutex)
784{
785 if (__gthread_active_p ())
786 return __gthrw_(pthread_mutex_destroy)pthread_mutex_destroy (__mutex);
787 else
788 return 0;
789}
790
791__GTHREAD_INLINE int
792__gthread_mutex_lock (__gthread_mutex_t *__mutex)
793{
794 if (__gthread_active_p ())
8
Taking true branch
795 return __gthrw_(pthread_mutex_lock)pthread_mutex_lock (__mutex);
9
Entering critical section here
796 else
797 return 0;
798}
799
800__GTHREAD_INLINE int
801__gthread_mutex_trylock (__gthread_mutex_t *__mutex)
802{
803 if (__gthread_active_p ())
804 return __gthrw_(pthread_mutex_trylock)pthread_mutex_trylock (__mutex);
805 else
806 return 0;
807}
808
809#if _GTHREAD_USE_MUTEX_TIMEDLOCK1
810__GTHREAD_INLINE int
811__gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
812 const __gthread_time_t *__abs_timeout)
813{
814 if (__gthread_active_p ())
815 return __gthrw_(pthread_mutex_timedlock)pthread_mutex_timedlock (__mutex, __abs_timeout);
816 else
817 return 0;
818}
819#endif
820
821__GTHREAD_INLINE int
822__gthread_mutex_unlock (__gthread_mutex_t *__mutex)
823{
824 if (__gthread_active_p ())
825 return __gthrw_(pthread_mutex_unlock)pthread_mutex_unlock (__mutex);
826 else
827 return 0;
828}
829
830#if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP{ { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, 0, { __null, __null
} } }
) \
831 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
832__GTHREAD_INLINE int
833__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
834{
835 if (__gthread_active_p ())
836 {
837 pthread_mutexattr_t __attr;
838 int __r;
839
840 __r = __gthrw_(pthread_mutexattr_init)pthread_mutexattr_init (&__attr);
841 if (!__r)
842 __r = __gthrw_(pthread_mutexattr_settype)pthread_mutexattr_settype (&__attr,
843 PTHREAD_MUTEX_RECURSIVE);
844 if (!__r)
845 __r = __gthrw_(pthread_mutex_init)pthread_mutex_init (__mutex, &__attr);
846 if (!__r)
847 __r = __gthrw_(pthread_mutexattr_destroy)pthread_mutexattr_destroy (&__attr);
848 return __r;
849 }
850 return 0;
851}
852#endif
853
854__GTHREAD_INLINE int
855__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
856{
857 return __gthread_mutex_lock (__mutex);
858}
859
860__GTHREAD_INLINE int
861__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
862{
863 return __gthread_mutex_trylock (__mutex);
864}
865
866#if _GTHREAD_USE_MUTEX_TIMEDLOCK1
867__GTHREAD_INLINE int
868__gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
869 const __gthread_time_t *__abs_timeout)
870{
871 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
872}
873#endif
874
875__GTHREAD_INLINE int
876__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
877{
878 return __gthread_mutex_unlock (__mutex);
879}
880
881__GTHREAD_INLINE int
882__gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
883{
884 return __gthread_mutex_destroy (__mutex);
885}
886
887#ifdef _GTHREAD_USE_COND_INIT_FUNC
888__GTHREAD_INLINE void
889__gthread_cond_init_function (__gthread_cond_t *__cond)
890{
891 if (__gthread_active_p ())
892 __gthrw_(pthread_cond_init)pthread_cond_init (__cond, NULL__null);
893}
894#endif
895
896__GTHREAD_INLINE int
897__gthread_cond_broadcast (__gthread_cond_t *__cond)
898{
899 return __gthrw_(pthread_cond_broadcast)pthread_cond_broadcast (__cond);
900}
901
902__GTHREAD_INLINE int
903__gthread_cond_signal (__gthread_cond_t *__cond)
904{
905 return __gthrw_(pthread_cond_signal)pthread_cond_signal (__cond);
906}
907
908__GTHREAD_INLINE int
909__gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
910{
911 return __gthrw_(pthread_cond_wait)pthread_cond_wait (__cond, __mutex);
912}
913
914__GTHREAD_INLINE int
915__gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
916 const __gthread_time_t *__abs_timeout)
917{
918 return __gthrw_(pthread_cond_timedwait)pthread_cond_timedwait (__cond, __mutex, __abs_timeout);
919}
920
921__GTHREAD_INLINE int
922__gthread_cond_wait_recursive (__gthread_cond_t *__cond,
923 __gthread_recursive_mutex_t *__mutex)
924{
925 return __gthread_cond_wait (__cond, __mutex);
926}
927
928__GTHREAD_INLINE int
929__gthread_cond_destroy (__gthread_cond_t* __cond)
930{
931 return __gthrw_(pthread_cond_destroy)pthread_cond_destroy (__cond);
932}
933
934#ifndef __cplusplus201703L
935__GTHREAD_INLINE int
936__gthread_rwlock_rdlock (__gthread_rwlock_t *__rwlock)
937{
938 if (__gthread_active_p ())
939 return __gthrw_(pthread_rwlock_rdlock)pthread_rwlock_rdlock (__rwlock);
940 else
941 return 0;
942}
943
944__GTHREAD_INLINE int
945__gthread_rwlock_tryrdlock (__gthread_rwlock_t *__rwlock)
946{
947 if (__gthread_active_p ())
948 return __gthrw_(pthread_rwlock_tryrdlock)pthread_rwlock_tryrdlock (__rwlock);
949 else
950 return 0;
951}
952
953__GTHREAD_INLINE int
954__gthread_rwlock_wrlock (__gthread_rwlock_t *__rwlock)
955{
956 if (__gthread_active_p ())
957 return __gthrw_(pthread_rwlock_wrlock)pthread_rwlock_wrlock (__rwlock);
958 else
959 return 0;
960}
961
962__GTHREAD_INLINE int
963__gthread_rwlock_trywrlock (__gthread_rwlock_t *__rwlock)
964{
965 if (__gthread_active_p ())
966 return __gthrw_(pthread_rwlock_trywrlock)pthread_rwlock_trywrlock (__rwlock);
967 else
968 return 0;
969}
970
971__GTHREAD_INLINE int
972__gthread_rwlock_unlock (__gthread_rwlock_t *__rwlock)
973{
974 if (__gthread_active_p ())
975 return __gthrw_(pthread_rwlock_unlock)pthread_rwlock_unlock (__rwlock);
976 else
977 return 0;
978}
979#endif
980
981#endif /* _LIBOBJC */
982
983#undef __GTHREAD_INLINE
984#undef __GTHREAD_ALWAYS_INLINE
985
986#endif /* ! _GLIBCXX_GCC_GTHR_POSIX_H */