[a75f3e49] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_MUTEX
|
---|
| 30 | #define LIBCPP_MUTEX
|
---|
| 31 |
|
---|
[5e5498e] | 32 | #include <internal/common.hpp>
|
---|
[9283830] | 33 | #include <internal/thread.hpp>
|
---|
[a75f3e49] | 34 | #include <thread>
|
---|
| 35 |
|
---|
| 36 | namespace std
|
---|
| 37 | {
|
---|
| 38 | /**
|
---|
| 39 | * 20.4.1.2.1, class mutex:
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | class mutex
|
---|
| 43 | {
|
---|
| 44 | public:
|
---|
[9283830] | 45 | constexpr mutex() noexcept
|
---|
| 46 | : mtx_{}
|
---|
| 47 | {
|
---|
| 48 | aux::threading::mutex::init(mtx_);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[a75f3e49] | 51 | ~mutex();
|
---|
| 52 |
|
---|
| 53 | mutex(const mutex&) = delete;
|
---|
| 54 | mutex& operator=(const mutex&) = delete;
|
---|
| 55 |
|
---|
| 56 | void lock();
|
---|
| 57 | bool try_lock();
|
---|
| 58 | void unlock();
|
---|
| 59 |
|
---|
[9283830] | 60 | using native_handle_type = aux::mutex_t*;
|
---|
[a75f3e49] | 61 | native_handle_type native_handle();
|
---|
| 62 |
|
---|
| 63 | private:
|
---|
[9283830] | 64 | aux::mutex_t mtx_;
|
---|
[a75f3e49] | 65 | };
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * 30.4.1.2.2, class recursive_mutex:
|
---|
| 69 | */
|
---|
| 70 |
|
---|
| 71 | class recursive_mutex
|
---|
| 72 | {
|
---|
| 73 | public:
|
---|
| 74 | constexpr recursive_mutex() noexcept;
|
---|
| 75 | ~recursive_mutex();
|
---|
| 76 |
|
---|
| 77 | recursive_mutex(const recursive_mutex&) = delete;
|
---|
| 78 | recursive_mutex& operator=(const recursive_mutex&) = delete;
|
---|
| 79 |
|
---|
| 80 | void lock();
|
---|
| 81 | bool try_lock();
|
---|
| 82 | void unlock();
|
---|
| 83 |
|
---|
[9283830] | 84 | using native_handle_type = aux::mutex_t*;
|
---|
[a75f3e49] | 85 | native_handle_type native_handle();
|
---|
| 86 |
|
---|
| 87 | private:
|
---|
[9283830] | 88 | aux::mutex_t mtx_;
|
---|
[a75f3e49] | 89 | size_t lock_level_;
|
---|
| 90 | thread::id owner_;
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * 30.4.1.3.1, class timed_mutex:
|
---|
| 95 | */
|
---|
| 96 |
|
---|
| 97 | // TODO: implement
|
---|
| 98 | class timed_mutex;
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * 30.4.1.3.2, class recursive_timed_mutex:
|
---|
| 102 | */
|
---|
| 103 |
|
---|
| 104 | // TODO: implement
|
---|
| 105 | class recursive_timed_mutex;
|
---|
| 106 |
|
---|
| 107 | struct defer_lock_t
|
---|
| 108 | { /* DUMMY BODY */ };
|
---|
| 109 |
|
---|
| 110 | struct try_to_lock_t
|
---|
| 111 | { /* DUMMY BODY */ };
|
---|
| 112 |
|
---|
| 113 | struct adopt_lock_t
|
---|
| 114 | { /* DUMMY BODY */ };
|
---|
| 115 |
|
---|
| 116 | constexpr defer_lock_t defer_lock
|
---|
| 117 | { /* DUMMY BODY */ };
|
---|
| 118 |
|
---|
| 119 | constexpr try_to_lock_t try_to_lock
|
---|
| 120 | { /* DUMMY BODY */ };
|
---|
| 121 |
|
---|
| 122 | constexpr adopt_lock_t adopt_lock
|
---|
| 123 | { /* DUMMY BODY */ };
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * 30.4.2.1, class template lock_guard:
|
---|
| 127 | */
|
---|
| 128 |
|
---|
| 129 | template<class Mutex>
|
---|
| 130 | class lock_guard
|
---|
| 131 | {
|
---|
| 132 | public:
|
---|
| 133 | using mutex_type = Mutex;
|
---|
| 134 |
|
---|
| 135 | explicit lock_guard(mutex_type& mtx)
|
---|
| 136 | : mtx_{mtx}
|
---|
| 137 | {
|
---|
| 138 | mtx.lock();
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | lock_guard(mutex_type& mtx, adopt_lock_t)
|
---|
| 142 | : mtx_{mtx}
|
---|
| 143 | { /* DUMMY BODY */ }
|
---|
| 144 |
|
---|
| 145 | ~lock_guard()
|
---|
| 146 | {
|
---|
| 147 | mtx_.unlock();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | lock_guard(const lock_guard&) = delete;
|
---|
| 151 | lock_guard& operator=(const lock_guard&) = delete;
|
---|
| 152 |
|
---|
| 153 | private:
|
---|
| 154 | mutex_type& mtx_;
|
---|
| 155 | };
|
---|
| 156 |
|
---|
| 157 | template<class Mutex>
|
---|
| 158 | class unique_lock;
|
---|
| 159 |
|
---|
| 160 | template<class Mutex>
|
---|
| 161 | void swap(unique_lock<Mutex>& lhs, unique_lock<Mutex>& rhs) noexcept;
|
---|
| 162 |
|
---|
| 163 | template<class L1, class L2, class... L3>
|
---|
| 164 | int try_lock(L1& l1, L2& l2, L3&... ls);
|
---|
| 165 |
|
---|
| 166 | template<class L1, class L2, class... L3>
|
---|
| 167 | void lock(L1& l1, L2& l2, L3&... ls);
|
---|
| 168 |
|
---|
| 169 | struct once_flag
|
---|
| 170 | {
|
---|
| 171 | constexpr once_flag() noexcept;
|
---|
| 172 |
|
---|
| 173 | once_flag(const once_flag&) = delete;
|
---|
| 174 | once_flag& operator=(const once_flag&) = delete;
|
---|
| 175 | };
|
---|
| 176 |
|
---|
| 177 | template<class Callable, class... Args>
|
---|
| 178 | void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | #endif
|
---|