[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 |
|
---|
| 32 | #include <thread>
|
---|
| 33 |
|
---|
| 34 | extern "C" {
|
---|
| 35 | #include <fibril_synch.h>
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | namespace std
|
---|
| 39 | {
|
---|
| 40 | /**
|
---|
| 41 | * 20.4.1.2.1, class mutex:
|
---|
| 42 | */
|
---|
| 43 |
|
---|
| 44 | class mutex
|
---|
| 45 | {
|
---|
| 46 | public:
|
---|
| 47 | constexpr mutex() noexcept;
|
---|
| 48 | ~mutex();
|
---|
| 49 |
|
---|
| 50 | mutex(const mutex&) = delete;
|
---|
| 51 | mutex& operator=(const mutex&) = delete;
|
---|
| 52 |
|
---|
| 53 | void lock();
|
---|
| 54 | bool try_lock();
|
---|
| 55 | void unlock();
|
---|
| 56 |
|
---|
| 57 | using native_handle_type = fibril_mutex_t;
|
---|
| 58 | native_handle_type native_handle();
|
---|
| 59 |
|
---|
| 60 | private:
|
---|
| 61 | native_handle_type mtx_;
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * 30.4.1.2.2, class recursive_mutex:
|
---|
| 66 | */
|
---|
| 67 |
|
---|
| 68 | class recursive_mutex
|
---|
| 69 | {
|
---|
| 70 | public:
|
---|
| 71 | constexpr recursive_mutex() noexcept;
|
---|
| 72 | ~recursive_mutex();
|
---|
| 73 |
|
---|
| 74 | recursive_mutex(const recursive_mutex&) = delete;
|
---|
| 75 | recursive_mutex& operator=(const recursive_mutex&) = delete;
|
---|
| 76 |
|
---|
| 77 | void lock();
|
---|
| 78 | bool try_lock();
|
---|
| 79 | void unlock();
|
---|
| 80 |
|
---|
| 81 | using native_handle_type = fibril_mutex_t;
|
---|
| 82 | native_handle_type native_handle();
|
---|
| 83 |
|
---|
| 84 | private:
|
---|
| 85 | native_handle_type mtx_;
|
---|
| 86 | size_t lock_level_;
|
---|
| 87 | thread::id owner_;
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * 30.4.1.3.1, class timed_mutex:
|
---|
| 92 | */
|
---|
| 93 |
|
---|
| 94 | // TODO: implement
|
---|
| 95 | class timed_mutex;
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * 30.4.1.3.2, class recursive_timed_mutex:
|
---|
| 99 | */
|
---|
| 100 |
|
---|
| 101 | // TODO: implement
|
---|
| 102 | class recursive_timed_mutex;
|
---|
| 103 |
|
---|
| 104 | struct defer_lock_t
|
---|
| 105 | { /* DUMMY BODY */ };
|
---|
| 106 |
|
---|
| 107 | struct try_to_lock_t
|
---|
| 108 | { /* DUMMY BODY */ };
|
---|
| 109 |
|
---|
| 110 | struct adopt_lock_t
|
---|
| 111 | { /* DUMMY BODY */ };
|
---|
| 112 |
|
---|
| 113 | constexpr defer_lock_t defer_lock
|
---|
| 114 | { /* DUMMY BODY */ };
|
---|
| 115 |
|
---|
| 116 | constexpr try_to_lock_t try_to_lock
|
---|
| 117 | { /* DUMMY BODY */ };
|
---|
| 118 |
|
---|
| 119 | constexpr adopt_lock_t adopt_lock
|
---|
| 120 | { /* DUMMY BODY */ };
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * 30.4.2.1, class template lock_guard:
|
---|
| 124 | */
|
---|
| 125 |
|
---|
| 126 | template<class Mutex>
|
---|
| 127 | class lock_guard
|
---|
| 128 | {
|
---|
| 129 | public:
|
---|
| 130 | using mutex_type = Mutex;
|
---|
| 131 |
|
---|
| 132 | explicit lock_guard(mutex_type& mtx)
|
---|
| 133 | : mtx_{mtx}
|
---|
| 134 | {
|
---|
| 135 | mtx.lock();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | lock_guard(mutex_type& mtx, adopt_lock_t)
|
---|
| 139 | : mtx_{mtx}
|
---|
| 140 | { /* DUMMY BODY */ }
|
---|
| 141 |
|
---|
| 142 | ~lock_guard()
|
---|
| 143 | {
|
---|
| 144 | mtx_.unlock();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | lock_guard(const lock_guard&) = delete;
|
---|
| 148 | lock_guard& operator=(const lock_guard&) = delete;
|
---|
| 149 |
|
---|
| 150 | private:
|
---|
| 151 | mutex_type& mtx_;
|
---|
| 152 | };
|
---|
| 153 |
|
---|
| 154 | template<class Mutex>
|
---|
| 155 | class unique_lock;
|
---|
| 156 |
|
---|
| 157 | template<class Mutex>
|
---|
| 158 | void swap(unique_lock<Mutex>& lhs, unique_lock<Mutex>& rhs) noexcept;
|
---|
| 159 |
|
---|
| 160 | template<class L1, class L2, class... L3>
|
---|
| 161 | int try_lock(L1& l1, L2& l2, L3&... ls);
|
---|
| 162 |
|
---|
| 163 | template<class L1, class L2, class... L3>
|
---|
| 164 | void lock(L1& l1, L2& l2, L3&... ls);
|
---|
| 165 |
|
---|
| 166 | struct once_flag
|
---|
| 167 | {
|
---|
| 168 | constexpr once_flag() noexcept;
|
---|
| 169 |
|
---|
| 170 | once_flag(const once_flag&) = delete;
|
---|
| 171 | once_flag& operator=(const once_flag&) = delete;
|
---|
| 172 | };
|
---|
| 173 |
|
---|
| 174 | template<class Callable, class... Args>
|
---|
| 175 | void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | #endif
|
---|