Skip to content

Commit 844ed14

Browse files
committed
Add mutex and shared_mutex headers back
1 parent d37fa81 commit 844ed14

File tree

6 files changed

+2205
-0
lines changed

6 files changed

+2205
-0
lines changed

include/bits/error_constants.h

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Specific definitions for generic platforms -*- C++ -*-
2+
3+
// Copyright (C) 2007-2024 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/error_constants.h
26+
* This is an internal header file, included by other library headers.
27+
* Do not attempt to use it directly. @headername{system_error}
28+
*/
29+
30+
#ifndef _GLIBCXX_ERROR_CONSTANTS
31+
#define _GLIBCXX_ERROR_CONSTANTS 1
32+
33+
#include <bits/c++config.h>
34+
#include <cerrno>
35+
36+
namespace std _GLIBCXX_VISIBILITY(default)
37+
{
38+
_GLIBCXX_BEGIN_NAMESPACE_VERSION
39+
40+
enum class errc
41+
{
42+
address_family_not_supported = EAFNOSUPPORT,
43+
address_in_use = EADDRINUSE,
44+
address_not_available = EADDRNOTAVAIL,
45+
already_connected = EISCONN,
46+
argument_list_too_long = E2BIG,
47+
argument_out_of_domain = EDOM,
48+
bad_address = EFAULT,
49+
bad_file_descriptor = EBADF,
50+
51+
#ifdef EBADMSG
52+
bad_message = EBADMSG,
53+
#endif
54+
55+
broken_pipe = EPIPE,
56+
connection_aborted = ECONNABORTED,
57+
connection_already_in_progress = EALREADY,
58+
connection_refused = ECONNREFUSED,
59+
connection_reset = ECONNRESET,
60+
cross_device_link = EXDEV,
61+
destination_address_required = EDESTADDRREQ,
62+
device_or_resource_busy = EBUSY,
63+
directory_not_empty = ENOTEMPTY,
64+
executable_format_error = ENOEXEC,
65+
file_exists = EEXIST,
66+
file_too_large = EFBIG,
67+
filename_too_long = ENAMETOOLONG,
68+
function_not_supported = ENOSYS,
69+
host_unreachable = EHOSTUNREACH,
70+
71+
#ifdef EIDRM
72+
identifier_removed = EIDRM,
73+
#endif
74+
75+
illegal_byte_sequence = EILSEQ,
76+
inappropriate_io_control_operation = ENOTTY,
77+
interrupted = EINTR,
78+
invalid_argument = EINVAL,
79+
invalid_seek = ESPIPE,
80+
io_error = EIO,
81+
is_a_directory = EISDIR,
82+
message_size = EMSGSIZE,
83+
network_down = ENETDOWN,
84+
network_reset = ENETRESET,
85+
network_unreachable = ENETUNREACH,
86+
no_buffer_space = ENOBUFS,
87+
no_child_process = ECHILD,
88+
89+
#ifdef ENOLINK
90+
no_link = ENOLINK,
91+
#endif
92+
93+
no_lock_available = ENOLCK,
94+
95+
#ifdef ENODATA
96+
no_message_available = ENODATA,
97+
#endif
98+
99+
no_message = ENOMSG,
100+
no_protocol_option = ENOPROTOOPT,
101+
no_space_on_device = ENOSPC,
102+
103+
#ifdef ENOSR
104+
no_stream_resources = ENOSR,
105+
#endif
106+
107+
no_such_device_or_address = ENXIO,
108+
no_such_device = ENODEV,
109+
no_such_file_or_directory = ENOENT,
110+
no_such_process = ESRCH,
111+
not_a_directory = ENOTDIR,
112+
not_a_socket = ENOTSOCK,
113+
114+
#ifdef ENOSTR
115+
not_a_stream = ENOSTR,
116+
#endif
117+
118+
not_connected = ENOTCONN,
119+
not_enough_memory = ENOMEM,
120+
121+
#ifdef ENOTSUP
122+
not_supported = ENOTSUP,
123+
#endif
124+
125+
#ifdef ECANCELED
126+
operation_canceled = ECANCELED,
127+
#endif
128+
129+
operation_in_progress = EINPROGRESS,
130+
operation_not_permitted = EPERM,
131+
operation_not_supported = EOPNOTSUPP,
132+
operation_would_block = EWOULDBLOCK,
133+
134+
#ifdef EOWNERDEAD
135+
owner_dead = EOWNERDEAD,
136+
#endif
137+
138+
permission_denied = EACCES,
139+
140+
#ifdef EPROTO
141+
protocol_error = EPROTO,
142+
#endif
143+
144+
protocol_not_supported = EPROTONOSUPPORT,
145+
read_only_file_system = EROFS,
146+
resource_deadlock_would_occur = EDEADLK,
147+
resource_unavailable_try_again = EAGAIN,
148+
result_out_of_range = ERANGE,
149+
150+
#ifdef ENOTRECOVERABLE
151+
state_not_recoverable = ENOTRECOVERABLE,
152+
#endif
153+
154+
#ifdef ETIME
155+
stream_timeout = ETIME,
156+
#endif
157+
158+
#ifdef ETXTBSY
159+
text_file_busy = ETXTBSY,
160+
#endif
161+
162+
timed_out = ETIMEDOUT,
163+
too_many_files_open_in_system = ENFILE,
164+
too_many_files_open = EMFILE,
165+
too_many_links = EMLINK,
166+
too_many_symbolic_link_levels = ELOOP,
167+
168+
#ifdef EOVERFLOW
169+
value_too_large = EOVERFLOW,
170+
#elif defined __AVR__
171+
value_too_large = 999,
172+
#endif
173+
174+
wrong_protocol_type = EPROTOTYPE
175+
};
176+
177+
_GLIBCXX_END_NAMESPACE_VERSION
178+
} // namespace
179+
180+
#endif

include/bits/functexcept.h

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include <bits/c++config.h>
4040
#include <bits/exception_defines.h>
41+
#include <bits/error_constants.h>
4142

4243
namespace std _GLIBCXX_VISIBILITY(default)
4344
{
@@ -90,6 +91,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
9091
void
9192
__throw_underflow_error(const char* __s = "") __attribute__((__noreturn__));
9293

94+
// Helpers for exception objects in <system_error>
95+
void
96+
__throw_system_error(int) __attribute__((__noreturn__));
97+
9398
// Helpers for exception objects in <functional>
9499
void
95100
__throw_bad_function_call() __attribute__((__noreturn__));

include/bits/std_mutex.h

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// std::mutex implementation -*- C++ -*-
2+
3+
// Copyright (C) 2003-2020 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_H
31+
#define _GLIBCXX_MUTEX_H 1
32+
33+
#pragma GCC system_header
34+
35+
#if __cplusplus < 201103L
36+
# include <bits/c++0x_warning.h>
37+
#else
38+
39+
#include <bits/functexcept.h>
40+
41+
namespace std _GLIBCXX_VISIBILITY(default)
42+
{
43+
_GLIBCXX_BEGIN_NAMESPACE_VERSION
44+
45+
/**
46+
* @defgroup mutexes Mutexes
47+
* @ingroup concurrency
48+
*
49+
* Classes for mutex support.
50+
* @{
51+
*/
52+
53+
#ifdef _GLIBCXX_HAS_GTHREADS
54+
// Common base class for std::mutex and std::timed_mutex
55+
class __mutex_base
56+
{
57+
protected:
58+
typedef __gthread_mutex_t __native_type;
59+
60+
#ifdef __GTHREAD_MUTEX_INIT
61+
__native_type _M_mutex = __GTHREAD_MUTEX_INIT;
62+
63+
constexpr __mutex_base() noexcept = default;
64+
#else
65+
__native_type _M_mutex;
66+
67+
__mutex_base() noexcept
68+
{
69+
// XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
70+
__GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
71+
}
72+
73+
~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
74+
#endif
75+
76+
__mutex_base(const __mutex_base&) = delete;
77+
__mutex_base& operator=(const __mutex_base&) = delete;
78+
};
79+
80+
/// The standard mutex type.
81+
class mutex : private __mutex_base
82+
{
83+
public:
84+
typedef __native_type* native_handle_type;
85+
86+
#ifdef __GTHREAD_MUTEX_INIT
87+
constexpr
88+
#endif
89+
mutex() noexcept = default;
90+
~mutex() = default;
91+
92+
mutex(const mutex&) = delete;
93+
mutex& operator=(const mutex&) = delete;
94+
95+
void
96+
lock()
97+
{
98+
int __e = __gthread_mutex_lock(&_M_mutex);
99+
100+
// EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
101+
if (__e)
102+
__throw_system_error(__e);
103+
}
104+
105+
bool
106+
try_lock() noexcept
107+
{
108+
// XXX EINVAL, EAGAIN, EBUSY
109+
return !__gthread_mutex_trylock(&_M_mutex);
110+
}
111+
112+
void
113+
unlock()
114+
{
115+
// XXX EINVAL, EAGAIN, EPERM
116+
__gthread_mutex_unlock(&_M_mutex);
117+
}
118+
119+
native_handle_type
120+
native_handle() noexcept
121+
{ return &_M_mutex; }
122+
};
123+
124+
#endif // _GLIBCXX_HAS_GTHREADS
125+
126+
/// Do not acquire ownership of the mutex.
127+
struct defer_lock_t { explicit defer_lock_t() = default; };
128+
129+
/// Try to acquire ownership of the mutex without blocking.
130+
struct try_to_lock_t { explicit try_to_lock_t() = default; };
131+
132+
/// Assume the calling thread has already obtained mutex ownership
133+
/// and manage it.
134+
struct adopt_lock_t { explicit adopt_lock_t() = default; };
135+
136+
/// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
137+
_GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { };
138+
139+
/// Tag used to prevent a scoped lock from blocking if a mutex is locked.
140+
_GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { };
141+
142+
/// Tag used to make a scoped lock take ownership of a locked mutex.
143+
_GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { };
144+
145+
/** @brief A simple scoped lock type.
146+
*
147+
* A lock_guard controls mutex ownership within a scope, releasing
148+
* ownership in the destructor.
149+
*/
150+
template<typename _Mutex>
151+
class lock_guard
152+
{
153+
public:
154+
typedef _Mutex mutex_type;
155+
156+
explicit lock_guard(mutex_type& __m) : _M_device(__m)
157+
{ _M_device.lock(); }
158+
159+
lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
160+
{ } // calling thread owns mutex
161+
162+
~lock_guard()
163+
{ _M_device.unlock(); }
164+
165+
lock_guard(const lock_guard&) = delete;
166+
lock_guard& operator=(const lock_guard&) = delete;
167+
168+
private:
169+
mutex_type& _M_device;
170+
};
171+
172+
// @} group mutexes
173+
_GLIBCXX_END_NAMESPACE_VERSION
174+
} // namespace
175+
#endif // C++11
176+
#endif // _GLIBCXX_MUTEX_H

0 commit comments

Comments
 (0)