RavEngine
Loading...
Searching...
No Matches
PxMutex.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_MUTEX_H
30#define PX_MUTEX_H
31
32#include "foundation/PxAllocator.h"
33
34/*
35 * This <new> inclusion is a best known fix for gcc 4.4.1 error:
36 * Creating object file for apex/src/PsAllocator.cpp ...
37 * In file included from apex/include/PsFoundation.h:30,
38 * from apex/src/PsAllocator.cpp:26:
39 * apex/include/PsMutex.h: In constructor 'physx::PxMutexT<Alloc>::MutexT(const Alloc&)':
40 * apex/include/PsMutex.h:92: error: no matching function for call to 'operator new(unsigned int,
41 * physx::PxMutexImpl*&)'
42 * <built-in>:0: note: candidates are: void* operator new(unsigned int)
43 */
44#include <new>
45
46#if !PX_DOXYGEN
47namespace physx
48{
49#endif
50class PX_FOUNDATION_API PxMutexImpl
51{
52 public:
57
62
68 void lock();
69
74 bool trylock();
75
79 void unlock();
80
84 static uint32_t getSize();
85};
86
87template <typename Alloc = PxReflectionAllocator<PxMutexImpl> >
88class PxMutexT : protected Alloc
89{
90 PX_NOCOPY(PxMutexT)
91 public:
93 {
94 PxMutexT<Alloc>& mMutex;
95 PX_NOCOPY(ScopedLock)
96 public:
97 PX_INLINE ScopedLock(PxMutexT<Alloc>& mutex) : mMutex(mutex)
98 {
99 mMutex.lock();
100 }
102 {
103 mMutex.unlock();
104 }
105 };
106
110 PxMutexT(const Alloc& alloc = Alloc()) : Alloc(alloc)
111 {
112 mImpl = reinterpret_cast<PxMutexImpl*>(Alloc::allocate(PxMutexImpl::getSize(), __FILE__, __LINE__));
113 PX_PLACEMENT_NEW(mImpl, PxMutexImpl)();
114 }
115
120 {
121 mImpl->~PxMutexImpl();
122 Alloc::deallocate(mImpl);
123 }
124
131 {
132 mImpl->lock();
133 }
134
141 {
142 return mImpl->trylock();
143 }
144
150 {
151 mImpl->unlock();
152 }
153
154 private:
155 PxMutexImpl* mImpl;
156};
157
158class PX_FOUNDATION_API PxReadWriteLock
159{
160 PX_NOCOPY(PxReadWriteLock)
161 public:
164
165 // "takeLock" can only be false if the thread already holds the mutex, e.g. if it already acquired the write lock
166 void lockReader(bool takeLock);
167 void lockWriter();
168
169 void unlockReader();
170 void unlockWriter();
171
172 private:
173 class ReadWriteLockImpl* mImpl;
174};
175
176typedef PxMutexT<> PxMutex;
177
178#if !PX_DOXYGEN
179} // namespace physx
180#endif
181
182#endif
183
Definition PxMutex.h:51
static uint32_t getSize()
Definition FdUnixMutex.cpp:146
~PxMutexImpl()
Definition FdUnixMutex.cpp:104
bool trylock()
Definition FdUnixMutex.cpp:120
void lock()
Definition FdUnixMutex.cpp:109
void unlock()
Definition FdUnixMutex.cpp:130
Definition PxMutex.h:93
Definition PxMutex.h:89
PX_FORCE_INLINE void unlock() const
Definition PxMutex.h:149
PX_FORCE_INLINE bool trylock() const
Definition PxMutex.h:140
PxMutexT(const Alloc &alloc=Alloc())
Definition PxMutex.h:110
PX_FORCE_INLINE void lock() const
Definition PxMutex.h:130
~PxMutexT()
Definition PxMutex.h:119
Definition PxMutex.h:159
Definition FdUnixMutex.cpp:152
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39