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_PSSYNC_H |
31 | #define PSFOUNDATION_PSSYNC_H |
32 | |
33 | #include "PsAllocator.h" |
34 | |
35 | namespace physx |
36 | { |
37 | namespace shdfnd |
38 | { |
39 | /*! |
40 | Implementation notes: |
41 | * - Calling set() on an already signaled Sync does not change its state. |
42 | * - Calling reset() on an already reset Sync does not change its state. |
43 | * - Calling set() on a reset Sync wakes all waiting threads (potential for thread contention). |
44 | * - Calling wait() on an already signaled Sync will return true immediately. |
45 | * - NOTE: be careful when pulsing an event with set() followed by reset(), because a |
46 | * thread that is not waiting on the event will miss the signal. |
47 | */ |
48 | class PX_FOUNDATION_API SyncImpl |
49 | { |
50 | public: |
51 | static const uint32_t waitForever = 0xffffffff; |
52 | |
53 | SyncImpl(); |
54 | |
55 | ~SyncImpl(); |
56 | |
57 | /** Wait on the object for at most the given number of ms. Returns |
58 | * true if the object is signaled. Sync::waitForever will block forever |
59 | * or until the object is signaled. |
60 | */ |
61 | |
62 | bool wait(uint32_t milliseconds = waitForever); |
63 | |
64 | /** Signal the synchronization object, waking all threads waiting on it */ |
65 | |
66 | void set(); |
67 | |
68 | /** Reset the synchronization object */ |
69 | |
70 | void reset(); |
71 | |
72 | /** |
73 | Size of this class. |
74 | */ |
75 | static uint32_t getSize(); |
76 | }; |
77 | |
78 | /*! |
79 | Implementation notes: |
80 | * - Calling set() on an already signaled Sync does not change its state. |
81 | * - Calling reset() on an already reset Sync does not change its state. |
82 | * - Calling set() on a reset Sync wakes all waiting threads (potential for thread contention). |
83 | * - Calling wait() on an already signaled Sync will return true immediately. |
84 | * - NOTE: be careful when pulsing an event with set() followed by reset(), because a |
85 | * thread that is not waiting on the event will miss the signal. |
86 | */ |
87 | template <typename Alloc = ReflectionAllocator<SyncImpl> > |
88 | class SyncT : protected Alloc |
89 | { |
90 | public: |
91 | static const uint32_t waitForever = SyncImpl::waitForever; |
92 | |
93 | SyncT(const Alloc& alloc = Alloc()) : Alloc(alloc) |
94 | { |
95 | mImpl = reinterpret_cast<SyncImpl*>(Alloc::allocate(SyncImpl::getSize(), __FILE__, __LINE__)); |
96 | PX_PLACEMENT_NEW(mImpl, SyncImpl)(); |
97 | } |
98 | |
99 | ~SyncT() |
100 | { |
101 | mImpl->~SyncImpl(); |
102 | Alloc::deallocate(mImpl); |
103 | } |
104 | |
105 | /** Wait on the object for at most the given number of ms. Returns |
106 | * true if the object is signaled. Sync::waitForever will block forever |
107 | * or until the object is signaled. |
108 | */ |
109 | |
110 | bool wait(uint32_t milliseconds = SyncImpl::waitForever) |
111 | { |
112 | return mImpl->wait(milliseconds); |
113 | } |
114 | |
115 | /** Signal the synchronization object, waking all threads waiting on it */ |
116 | |
117 | void set() |
118 | { |
119 | mImpl->set(); |
120 | } |
121 | |
122 | /** Reset the synchronization object */ |
123 | |
124 | void reset() |
125 | { |
126 | mImpl->reset(); |
127 | } |
128 | |
129 | private: |
130 | class SyncImpl* mImpl; |
131 | }; |
132 | |
133 | typedef SyncT<> Sync; |
134 | |
135 | } // namespace shdfnd |
136 | } // namespace physx |
137 | |
138 | #endif // #ifndef PSFOUNDATION_PSSYNC_H |
139 | |