ovr_sdk

view LibOVR/Src/Kernel/OVR_ThreadCommandQueue.h @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line source
1 /************************************************************************************
3 PublicHeader: None
4 Filename : OVR_ThreadCommandQueue.h
5 Content : Command queue for operations executed on a thread
6 Created : October 29, 2012
7 Author : Michael Antonov
9 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
11 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
13 which is provided at the time of installation or download, or which
14 otherwise accompanies this software in either electronic or hard copy form.
16 You may obtain a copy of the License at
18 http://www.oculusvr.com/licenses/LICENSE-3.2
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
21 distributed under the License is distributed on an "AS IS" BASIS,
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License for the specific language governing permissions and
24 limitations under the License.
26 ************************************************************************************/
28 #ifndef OVR_ThreadCommandQueue_h
29 #define OVR_ThreadCommandQueue_h
31 #include "../Kernel/OVR_Types.h"
32 #include "../Kernel/OVR_List.h"
33 #include "../Kernel/OVR_Atomic.h"
34 #include "../Kernel/OVR_Threads.h"
36 namespace OVR {
38 class ThreadCommand;
39 class ThreadCommandQueue;
42 //-------------------------------------------------------------------------------------
43 // ***** ThreadCommand
45 // ThreadCommand is a base class implementation for commands stored in ThreadCommandQueue.
46 class ThreadCommand
47 {
48 public:
49 // NotifyEvent is used by ThreadCommandQueue::PushCallAndWait to notify the
50 // calling (producer) thread when command is completed or queue slot is available.
51 class NotifyEvent : public ListNode<NotifyEvent>, public NewOverrideBase
52 {
53 Event E;
54 public:
55 NotifyEvent() { }
57 void Wait() { E.Wait(); }
58 void PulseEvent() { E.PulseEvent(); }
59 };
61 // ThreadCommand::PopBuffer is temporary storage for a command popped off
62 // by ThreadCommandQueue::PopCommand.
63 class PopBuffer
64 {
65 enum { MaxSize = 256 };
67 size_t Size;
68 union {
69 uint8_t Buffer[MaxSize];
70 size_t Align;
71 };
73 ThreadCommand* toCommand() const { return (ThreadCommand*)Buffer; }
75 public:
76 PopBuffer() : Size(0) { }
77 ~PopBuffer();
79 void InitFromBuffer(void* data);
81 bool HasCommand() const { return Size != 0; }
82 size_t GetSize() const { return Size; }
83 bool NeedsWait() const { return toCommand()->NeedsWait(); }
84 NotifyEvent* GetEvent() const { return toCommand()->pEvent; }
86 // Execute the command and also notifies caller to finish waiting,
87 // if necessary.
88 void Execute();
89 };
91 uint16_t Size;
92 bool WaitFlag;
93 bool ExitFlag; // Marks the last exit command.
94 NotifyEvent* pEvent;
96 ThreadCommand(size_t size, bool waitFlag, bool exitFlag = false)
97 : Size((uint16_t)size), WaitFlag(waitFlag), ExitFlag(exitFlag), pEvent(0) { }
98 virtual ~ThreadCommand() { }
100 bool NeedsWait() const { return WaitFlag; }
101 size_t GetSize() const { return Size; }
103 virtual void Execute() const = 0;
104 // Copy constructor used for serializing this to memory buffer.
105 virtual ThreadCommand* CopyConstruct(void* p) const = 0;
106 };
109 //-------------------------------------------------------------------------------------
111 // CleanType is a template that strips 'const' and '&' modifiers from the argument type;
112 // for example, typename CleanType<A&>::Type is equivalent to A.
113 template<class T> struct CleanType { typedef T Type; };
114 template<class T> struct CleanType<T&> { typedef T Type; };
115 template<class T> struct CleanType<const T> { typedef T Type; };
116 template<class T> struct CleanType<const T&> { typedef T Type; };
118 // SelfType is a template that yields the argument type. This helps avoid conflicts with
119 // automatic template argument deduction for function calls when identical argument
120 // is already defined.
121 template<class T> struct SelfType { typedef T Type; };
125 //-------------------------------------------------------------------------------------
126 // ThreadCommand specializations for member functions with different number of
127 // arguments and argument types.
129 // Used to return nothing from a ThreadCommand, to avoid problems with 'void'.
130 struct Void
131 {
132 Void() {}
133 Void(int) {}
134 };
136 // ThreadCommand for member function with 0 arguments.
137 template<class C, class R>
138 class ThreadCommandMF0 : public ThreadCommand
139 {
140 typedef R (C::*FnPtr)();
141 C* pClass;
142 FnPtr pFn;
143 R* pRet;
145 void executeImpl() const
146 {
147 pRet ? (void)(*pRet = (pClass->*pFn)()) :
148 (void)(pClass->*pFn)();
149 }
151 public:
152 ThreadCommandMF0(C* pclass, FnPtr fn, R* ret, bool needsWait)
153 : ThreadCommand(sizeof(ThreadCommandMF0), needsWait),
154 pClass(pclass), pFn(fn), pRet(ret) { }
156 virtual void Execute() const { executeImpl(); }
157 virtual ThreadCommand* CopyConstruct(void* p) const
158 { return Construct<ThreadCommandMF0>(p, *this); }
159 };
162 // ThreadCommand for member function with 1 argument.
163 template<class C, class R, class A0>
164 class ThreadCommandMF1 : public ThreadCommand
165 {
166 typedef R (C::*FnPtr)(A0);
167 C* pClass;
168 FnPtr pFn;
169 R* pRet;
170 typename CleanType<A0>::Type AVal0;
172 void executeImpl() const
173 {
174 pRet ? (void)(*pRet = (pClass->*pFn)(AVal0)) :
175 (void)(pClass->*pFn)(AVal0);
176 }
178 public:
179 ThreadCommandMF1(C* pclass, FnPtr fn, R* ret, A0 a0, bool needsWait)
180 : ThreadCommand(sizeof(ThreadCommandMF1), needsWait),
181 pClass(pclass), pFn(fn), pRet(ret), AVal0(a0) { }
183 virtual void Execute() const { executeImpl(); }
184 virtual ThreadCommand* CopyConstruct(void* p) const
185 { return Construct<ThreadCommandMF1>(p, *this); }
186 };
188 // ThreadCommand for member function with 2 arguments.
189 template<class C, class R, class A0, class A1>
190 class ThreadCommandMF2 : public ThreadCommand
191 {
192 typedef R (C::*FnPtr)(A0, A1);
193 C* pClass;
194 FnPtr pFn;
195 R* pRet;
196 typename CleanType<A0>::Type AVal0;
197 typename CleanType<A1>::Type AVal1;
199 void executeImpl() const
200 {
201 pRet ? (void)(*pRet = (pClass->*pFn)(AVal0, AVal1)) :
202 (void)(pClass->*pFn)(AVal0, AVal1);
203 }
205 public:
206 ThreadCommandMF2(C* pclass, FnPtr fn, R* ret, A0 a0, A1 a1, bool needsWait)
207 : ThreadCommand(sizeof(ThreadCommandMF2), needsWait),
208 pClass(pclass), pFn(fn), pRet(ret), AVal0(a0), AVal1(a1) { }
210 virtual void Execute() const { executeImpl(); }
211 virtual ThreadCommand* CopyConstruct(void* p) const
212 { return Construct<ThreadCommandMF2>(p, *this); }
213 };
216 //-------------------------------------------------------------------------------------
217 // ***** ThreadCommandQueue
219 // ThreadCommandQueue is a queue of executable function-call commands intended to be
220 // serviced by a single consumer thread. Commands are added to the queue with PushCall
221 // and removed with PopCall; they are processed in FIFO order. Multiple producer threads
222 // are supported and will be blocked if internal data buffer is full.
224 class ThreadCommandQueue
225 {
226 public:
228 ThreadCommandQueue();
229 virtual ~ThreadCommandQueue();
232 // Pops the next command from the thread queue, if any is available.
233 // The command should be executed by calling popBuffer->Execute().
234 // Returns 'false' if no command is available at the time of the call.
235 bool PopCommand(ThreadCommand::PopBuffer* popBuffer);
237 // Generic implementaion of PushCommand; enqueues a command for execution.
238 // Returns 'false' if push failed, usually indicating thread shutdown.
239 bool PushCommand(const ThreadCommand& command);
241 //
242 void PushExitCommand(bool wait);
244 // Returns 'true' once ExitCommand has been processed, so the thread can shut down.
245 bool IsExiting() const;
248 // These two virtual functions serve as notifications for derived
249 // thread waiting.
250 virtual void OnPushNonEmpty_Locked() { }
251 virtual void OnPopEmpty_Locked() { }
254 // *** PushCall with no result
256 // Enqueue a member function of 'this' class to be called on consumer thread.
257 // By default the function returns immediately; set 'wait' argument to 'true' to
258 // wait for completion.
259 template<class C, class R>
260 bool PushCall(R (C::*fn)(), bool wait = false)
261 { return PushCommand(ThreadCommandMF0<C,R>(static_cast<C*>(this), fn, 0, wait)); }
262 template<class C, class R, class A0>
263 bool PushCall(R (C::*fn)(A0), typename SelfType<A0>::Type a0, bool wait = false)
264 { return PushCommand(ThreadCommandMF1<C,R,A0>(static_cast<C*>(this), fn, 0, a0, wait)); }
265 template<class C, class R, class A0, class A1>
266 bool PushCall(R (C::*fn)(A0, A1),
267 typename SelfType<A0>::Type a0, typename SelfType<A1>::Type a1, bool wait = false)
268 { return PushCommand(ThreadCommandMF2<C,R,A0,A1>(static_cast<C*>(this), fn, 0, a0, a1, wait)); }
269 // Enqueue a specified member function call of class C.
270 // By default the function returns immediately; set 'wait' argument to 'true' to
271 // wait for completion.
272 template<class C, class R>
273 bool PushCall(C* p, R (C::*fn)(), bool wait = false)
274 { return PushCommand(ThreadCommandMF0<C,R>(p, fn, 0, wait)); }
275 template<class C, class R, class A0>
276 bool PushCall(C* p, R (C::*fn)(A0), typename SelfType<A0>::Type a0, bool wait = false)
277 { return PushCommand(ThreadCommandMF1<C,R,A0>(p, fn, 0, a0, wait)); }
278 template<class C, class R, class A0, class A1>
279 bool PushCall(C* p, R (C::*fn)(A0, A1),
280 typename SelfType<A0>::Type a0, typename SelfType<A1>::Type a1, bool wait = false)
281 { return PushCommand(ThreadCommandMF2<C,R,A0,A1>(p, fn, 0, a0, a1, wait)); }
284 // *** PushCall with Result
286 // Enqueue a member function of 'this' class call and wait for call to complete
287 // on consumer thread before returning.
288 template<class C, class R>
289 bool PushCallAndWaitResult(R (C::*fn)(), R* ret)
290 { return PushCommand(ThreadCommandMF0<C,R>(static_cast<C*>(this), fn, ret, true)); }
291 template<class C, class R, class A0>
292 bool PushCallAndWaitResult(R (C::*fn)(A0), R* ret, typename SelfType<A0>::Type a0)
293 { return PushCommand(ThreadCommandMF1<C,R,A0>(static_cast<C*>(this), fn, ret, a0, true)); }
294 template<class C, class R, class A0, class A1>
295 bool PushCallAndWaitResult(R (C::*fn)(A0, A1), R* ret,
296 typename SelfType<A0>::Type a0, typename SelfType<A1>::Type a1)
297 { return PushCommand(ThreadCommandMF2<C,R,A0,A1>(static_cast<C*>(this), fn, ret, a0, a1, true)); }
298 // Enqueue a member function call for class C and wait for the call to complete
299 // on consumer thread before returning.
300 template<class C, class R>
301 bool PushCallAndWaitResult(C* p, R (C::*fn)(), R* ret)
302 { return PushCommand(ThreadCommandMF0<C,R>(p, fn, ret, true)); }
303 template<class C, class R, class A0>
304 bool PushCallAndWaitResult(C* p, R (C::*fn)(A0), R* ret, typename SelfType<A0>::Type a0)
305 { return PushCommand(ThreadCommandMF1<C,R,A0>(p, fn, ret, a0, true)); }
306 template<class C, class R, class A0, class A1>
307 bool PushCallAndWaitResult(C* p, R (C::*fn)(A0, A1), R* ret,
308 typename SelfType<A0>::Type a0, typename SelfType<A1>::Type a1)
309 { return PushCommand(ThreadCommandMF2<C,R,A0,A1>(p, fn, ret, a0, a1, true)); }
311 private:
312 class ThreadCommandQueueImpl* pImpl;
313 };
316 } // namespace OVR
318 #endif // OVR_ThreadCommandQueue_h