0.2.0-dev Download ZIP
128 lines ·4.5 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* mutex.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "core/typedefs.h" |
| 34 | |
| 35 | #ifdef MINGW_ENABLED |
| 36 | #define MINGW_STDTHREAD_REDUNDANCY_WARNING |
| 37 | #include <thirdparty/mingw-std-threads/mingw.mutex.h> |
| 38 | #define THREADING_NAMESPACE mingw_stdthread |
| 39 | #else |
| 40 | #include <mutex> |
| 41 | #define THREADING_NAMESPACE std |
| 42 | #endif |
| 43 | |
| 44 | #ifdef THREADS_ENABLED |
| 45 | |
| 46 | template <typename MutexT> |
| 47 | class MutexLock; |
| 48 | |
| 49 | template <typename StdMutexT> |
| 50 | class MutexImpl { |
| 51 | friend class MutexLock<MutexImpl<StdMutexT>>; |
| 52 | |
| 53 | using StdMutexType = StdMutexT; |
| 54 | |
| 55 | mutable StdMutexT mutex; |
| 56 | |
| 57 | public: |
| 58 | _ALWAYS_INLINE_ void lock() const { |
| 59 | mutex.lock(); |
| 60 | } |
| 61 | |
| 62 | _ALWAYS_INLINE_ void unlock() const { |
| 63 | mutex.unlock(); |
| 64 | } |
| 65 | |
| 66 | _ALWAYS_INLINE_ bool try_lock() const { |
| 67 | return mutex.try_lock(); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | template <typename MutexT> |
| 72 | class [[nodiscard]] MutexLock { |
| 73 | mutable THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock; |
| 74 | |
| 75 | public: |
| 76 | explicit MutexLock(const MutexT &p_mutex) : |
| 77 | lock(p_mutex.mutex) {} |
| 78 | |
| 79 | // Clarification: all the funny syntax is needed so this function exists only for binary mutexes. |
| 80 | template <typename T = MutexT> |
| 81 | _ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &_get_lock( |
| 82 | typename std::enable_if<std::is_same<T, THREADING_NAMESPACE::mutex>::value> * = nullptr) const { |
| 83 | return lock; |
| 84 | } |
| 85 | |
| 86 | _ALWAYS_INLINE_ void temp_relock() const { |
| 87 | lock.lock(); |
| 88 | } |
| 89 | |
| 90 | _ALWAYS_INLINE_ void temp_unlock() const { |
| 91 | lock.unlock(); |
| 92 | } |
| 93 | |
| 94 | // TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below). |
| 95 | }; |
| 96 | |
| 97 | using Mutex = MutexImpl<THREADING_NAMESPACE::recursive_mutex>; // Recursive, for general use |
| 98 | using BinaryMutex = MutexImpl<THREADING_NAMESPACE::mutex>; // Non-recursive, handle with care |
| 99 | |
| 100 | extern template class MutexImpl<THREADING_NAMESPACE::recursive_mutex>; |
| 101 | extern template class MutexImpl<THREADING_NAMESPACE::mutex>; |
| 102 | extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>; |
| 103 | extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>; |
| 104 | |
| 105 | #else // No threads. |
| 106 | |
| 107 | class MutexImpl { |
| 108 | mutable THREADING_NAMESPACE::mutex mutex; |
| 109 | |
| 110 | public: |
| 111 | void lock() const {} |
| 112 | void unlock() const {} |
| 113 | bool try_lock() const { return true; } |
| 114 | }; |
| 115 | |
| 116 | template <typename MutexT> |
| 117 | class [[nodiscard]] MutexLock { |
| 118 | public: |
| 119 | MutexLock(const MutexT &p_mutex) {} |
| 120 | |
| 121 | void temp_relock() const {} |
| 122 | void temp_unlock() const {} |
| 123 | }; |
| 124 | |
| 125 | using Mutex = MutexImpl; |
| 126 | using BinaryMutex = MutexImpl; |
| 127 | |
| 128 | #endif // THREADS_ENABLED |