| 1 | // |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions |
| 4 | // are met: |
| 5 | // * Redistributions of source code must retain the above copyright |
| 6 | // notice, this list of conditions and the following disclaimer. |
| 7 | // * Redistributions in binary form must reproduce the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer in the |
| 9 | // documentation and/or other materials provided with the distribution. |
| 10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
| 11 | // contributors may be used to endorse or promote products derived |
| 12 | // from this software without specific prior written permission. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
| 15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
| 27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
| 28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
| 29 | |
| 30 | #ifndef PSFOUNDATION_PSMUTEX_H |
| 31 | #define PSFOUNDATION_PSMUTEX_H |
| 32 | |
| 33 | #include "PsAllocator.h" |
| 34 | |
| 35 | /* |
| 36 | * This <new> inclusion is a best known fix for gcc 4.4.1 error: |
| 37 | * Creating object file for apex/src/PsAllocator.cpp ... |
| 38 | * In file included from apex/include/PsFoundation.h:30, |
| 39 | * from apex/src/PsAllocator.cpp:26: |
| 40 | * apex/include/PsMutex.h: In constructor 'physx::shdfnd::MutexT<Alloc>::MutexT(const Alloc&)': |
| 41 | * apex/include/PsMutex.h:92: error: no matching function for call to 'operator new(unsigned int, |
| 42 | * physx::shdfnd::MutexImpl*&)' |
| 43 | * <built-in>:0: note: candidates are: void* operator new(unsigned int) |
| 44 | */ |
| 45 | #include <new> |
| 46 | |
| 47 | namespace physx |
| 48 | { |
| 49 | namespace shdfnd |
| 50 | { |
| 51 | class PX_FOUNDATION_API MutexImpl |
| 52 | { |
| 53 | public: |
| 54 | /** |
| 55 | The constructor for Mutex creates a mutex. It is initially unlocked. |
| 56 | */ |
| 57 | MutexImpl(); |
| 58 | |
| 59 | /** |
| 60 | The destructor for Mutex deletes the mutex. |
| 61 | */ |
| 62 | ~MutexImpl(); |
| 63 | |
| 64 | /** |
| 65 | Acquire (lock) the mutex. If the mutex is already locked |
| 66 | by another thread, this method blocks until the mutex is |
| 67 | unlocked. |
| 68 | */ |
| 69 | void lock(); |
| 70 | |
| 71 | /** |
| 72 | Acquire (lock) the mutex. If the mutex is already locked |
| 73 | by another thread, this method returns false without blocking. |
| 74 | */ |
| 75 | bool trylock(); |
| 76 | |
| 77 | /** |
| 78 | Release (unlock) the mutex. |
| 79 | */ |
| 80 | void unlock(); |
| 81 | |
| 82 | /** |
| 83 | Size of this class. |
| 84 | */ |
| 85 | static uint32_t getSize(); |
| 86 | }; |
| 87 | |
| 88 | template <typename Alloc = ReflectionAllocator<MutexImpl> > |
| 89 | class MutexT : protected Alloc |
| 90 | { |
| 91 | PX_NOCOPY(MutexT) |
| 92 | public: |
| 93 | class ScopedLock |
| 94 | { |
| 95 | MutexT<Alloc>& mMutex; |
| 96 | PX_NOCOPY(ScopedLock) |
| 97 | public: |
| 98 | PX_INLINE ScopedLock(MutexT<Alloc>& mutex) : mMutex(mutex) |
| 99 | { |
| 100 | mMutex.lock(); |
| 101 | } |
| 102 | PX_INLINE ~ScopedLock() |
| 103 | { |
| 104 | mMutex.unlock(); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | The constructor for Mutex creates a mutex. It is initially unlocked. |
| 110 | */ |
| 111 | MutexT(const Alloc& alloc = Alloc()) : Alloc(alloc) |
| 112 | { |
| 113 | mImpl = reinterpret_cast<MutexImpl*>(Alloc::allocate(MutexImpl::getSize(), __FILE__, __LINE__)); |
| 114 | PX_PLACEMENT_NEW(mImpl, MutexImpl)(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | The destructor for Mutex deletes the mutex. |
| 119 | */ |
| 120 | ~MutexT() |
| 121 | { |
| 122 | mImpl->~MutexImpl(); |
| 123 | Alloc::deallocate(mImpl); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | Acquire (lock) the mutex. If the mutex is already locked |
| 128 | by another thread, this method blocks until the mutex is |
| 129 | unlocked. |
| 130 | */ |
| 131 | void lock() const |
| 132 | { |
| 133 | mImpl->lock(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | Acquire (lock) the mutex. If the mutex is already locked |
| 138 | by another thread, this method returns false without blocking, |
| 139 | returns true if lock is successfully acquired |
| 140 | */ |
| 141 | bool trylock() const |
| 142 | { |
| 143 | return mImpl->trylock(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | Release (unlock) the mutex, the calling thread must have |
| 148 | previously called lock() or method will error |
| 149 | */ |
| 150 | void unlock() const |
| 151 | { |
| 152 | mImpl->unlock(); |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | MutexImpl* mImpl; |
| 157 | }; |
| 158 | |
| 159 | class PX_FOUNDATION_API ReadWriteLock |
| 160 | { |
| 161 | PX_NOCOPY(ReadWriteLock) |
| 162 | public: |
| 163 | ReadWriteLock(); |
| 164 | ~ReadWriteLock(); |
| 165 | |
| 166 | // "takeLock" can only be false if the thread already holds the mutex, e.g. if it already acquired the write lock |
| 167 | void lockReader(bool takeLock); |
| 168 | void lockWriter(); |
| 169 | |
| 170 | void unlockReader(); |
| 171 | void unlockWriter(); |
| 172 | |
| 173 | private: |
| 174 | class ReadWriteLockImpl* mImpl; |
| 175 | }; |
| 176 | |
| 177 | typedef MutexT<> Mutex; |
| 178 | |
| 179 | } // namespace shdfnd |
| 180 | } // namespace physx |
| 181 | |
| 182 | #endif // #ifndef PSFOUNDATION_PSMUTEX_H |
| 183 | |