OVR_ThreadCommandQueue.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /************************************************************************************
  2. PublicHeader: None
  3. Filename : OVR_ThreadCommandQueue.h
  4. Content : Command queue for operations executed on a thread
  5. Created : October 29, 2012
  6. Author : Michael Antonov
  7. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. ************************************************************************************/
  20. #ifndef OVR_ThreadCommandQueue_h
  21. #define OVR_ThreadCommandQueue_h
  22. #include "OVR_Types.h"
  23. #include "OVR_List.h"
  24. #include "OVR_Atomic.h"
  25. #include "OVR_Threads.h"
  26. namespace OVR {
  27. class ThreadCommand;
  28. class ThreadCommandQueue;
  29. //-------------------------------------------------------------------------------------
  30. // ***** ThreadCommand
  31. // ThreadCommand is a base class implementation for commands stored in ThreadCommandQueue.
  32. class ThreadCommand
  33. {
  34. public:
  35. // NotifyEvent is used by ThreadCommandQueue::PushCallAndWait to notify the
  36. // calling (producer) thread when command is completed or queue slot is available.
  37. class NotifyEvent : public ListNode<NotifyEvent>, public NewOverrideBase
  38. {
  39. Event E;
  40. public:
  41. NotifyEvent() { }
  42. void Wait() { E.Wait(); }
  43. void PulseEvent() { E.PulseEvent(); }
  44. };
  45. // ThreadCommand::PopBuffer is temporary storage for a command popped off
  46. // by ThreadCommandQueue::PopCommand.
  47. class PopBuffer
  48. {
  49. enum { MaxSize = 256 };
  50. size_t Size;
  51. union {
  52. uint8_t Buffer[MaxSize];
  53. size_t Align;
  54. };
  55. ThreadCommand* toCommand() const { return (ThreadCommand*)Buffer; }
  56. public:
  57. PopBuffer() : Size(0) { }
  58. ~PopBuffer();
  59. void InitFromBuffer(void* data);
  60. bool HasCommand() const { return Size != 0; }
  61. size_t GetSize() const { return Size; }
  62. bool NeedsWait() const { return toCommand()->NeedsWait(); }
  63. NotifyEvent* GetEvent() const { return toCommand()->pEvent; }
  64. // Execute the command and also notifies caller to finish waiting,
  65. // if necessary.
  66. void Execute();
  67. };
  68. uint16_t Size;
  69. bool WaitFlag;
  70. bool ExitFlag; // Marks the last exit command.
  71. NotifyEvent* pEvent;
  72. ThreadCommand(size_t size, bool waitFlag, bool exitFlag = false)
  73. : Size((uint16_t)size), WaitFlag(waitFlag), ExitFlag(exitFlag), pEvent(0) { }
  74. virtual ~ThreadCommand() { }
  75. bool NeedsWait() const { return WaitFlag; }
  76. size_t GetSize() const { return Size; }
  77. virtual void Execute() const = 0;
  78. // Copy constructor used for serializing this to memory buffer.
  79. virtual ThreadCommand* CopyConstruct(void* p) const = 0;
  80. };
  81. //-------------------------------------------------------------------------------------
  82. // CleanType is a template that strips 'const' and '&' modifiers from the argument type;
  83. // for example, typename CleanType<A&>::Type is equivalent to A.
  84. template<class T> struct CleanType { typedef T Type; };
  85. template<class T> struct CleanType<T&> { typedef T Type; };
  86. template<class T> struct CleanType<const T> { typedef T Type; };
  87. template<class T> struct CleanType<const T&> { typedef T Type; };
  88. // SelfType is a template that yields the argument type. This helps avoid conflicts with
  89. // automatic template argument deduction for function calls when identical argument
  90. // is already defined.
  91. template<class T> struct SelfType { typedef T Type; };
  92. //-------------------------------------------------------------------------------------
  93. // ThreadCommand specializations for member functions with different number of
  94. // arguments and argument types.
  95. // Used to return nothing from a ThreadCommand, to avoid problems with 'void'.
  96. struct Void
  97. {
  98. Void() {}
  99. Void(int) {}
  100. };
  101. // ThreadCommand for member function with 0 arguments.
  102. template<class C, class R>
  103. class ThreadCommandMF0 : public ThreadCommand
  104. {
  105. typedef R (C::*FnPtr)();
  106. C* pClass;
  107. FnPtr pFn;
  108. R* pRet;
  109. void executeImpl() const
  110. {
  111. pRet ? (void)(*pRet = (pClass->*pFn)()) :
  112. (void)(pClass->*pFn)();
  113. }
  114. public:
  115. ThreadCommandMF0(C* pclass, FnPtr fn, R* ret, bool needsWait)
  116. : ThreadCommand(sizeof(ThreadCommandMF0), needsWait),
  117. pClass(pclass), pFn(fn), pRet(ret) { }
  118. virtual void Execute() const { executeImpl(); }
  119. virtual ThreadCommand* CopyConstruct(void* p) const
  120. { return Construct<ThreadCommandMF0>(p, *this); }
  121. };
  122. // ThreadCommand for member function with 1 argument.
  123. template<class C, class R, class A0>
  124. class ThreadCommandMF1 : public ThreadCommand
  125. {
  126. typedef R (C::*FnPtr)(A0);
  127. C* pClass;
  128. FnPtr pFn;
  129. R* pRet;
  130. typename CleanType<A0>::Type AVal0;
  131. void executeImpl() const
  132. {
  133. pRet ? (void)(*pRet = (pClass->*pFn)(AVal0)) :
  134. (void)(pClass->*pFn)(AVal0);
  135. }
  136. public:
  137. ThreadCommandMF1(C* pclass, FnPtr fn, R* ret, A0 a0, bool needsWait)
  138. : ThreadCommand(sizeof(ThreadCommandMF1), needsWait),
  139. pClass(pclass), pFn(fn), pRet(ret), AVal0(a0) { }
  140. virtual void Execute() const { executeImpl(); }
  141. virtual ThreadCommand* CopyConstruct(void* p) const
  142. { return Construct<ThreadCommandMF1>(p, *this); }
  143. };
  144. // ThreadCommand for member function with 2 arguments.
  145. template<class C, class R, class A0, class A1>
  146. class ThreadCommandMF2 : public ThreadCommand
  147. {
  148. typedef R (C::*FnPtr)(A0, A1);
  149. C* pClass;
  150. FnPtr pFn;
  151. R* pRet;
  152. typename CleanType<A0>::Type AVal0;
  153. typename CleanType<A1>::Type AVal1;
  154. void executeImpl() const
  155. {
  156. pRet ? (void)(*pRet = (pClass->*pFn)(AVal0, AVal1)) :
  157. (void)(pClass->*pFn)(AVal0, AVal1);
  158. }
  159. public:
  160. ThreadCommandMF2(C* pclass, FnPtr fn, R* ret, A0 a0, A1 a1, bool needsWait)
  161. : ThreadCommand(sizeof(ThreadCommandMF2), needsWait),
  162. pClass(pclass), pFn(fn), pRet(ret), AVal0(a0), AVal1(a1) { }
  163. virtual void Execute() const { executeImpl(); }
  164. virtual ThreadCommand* CopyConstruct(void* p) const
  165. { return Construct<ThreadCommandMF2>(p, *this); }
  166. };
  167. //-------------------------------------------------------------------------------------
  168. // ***** ThreadCommandQueue
  169. // ThreadCommandQueue is a queue of executable function-call commands intended to be
  170. // serviced by a single consumer thread. Commands are added to the queue with PushCall
  171. // and removed with PopCall; they are processed in FIFO order. Multiple producer threads
  172. // are supported and will be blocked if internal data buffer is full.
  173. class ThreadCommandQueue
  174. {
  175. public:
  176. ThreadCommandQueue();
  177. virtual ~ThreadCommandQueue();
  178. // Pops the next command from the thread queue, if any is available.
  179. // The command should be executed by calling popBuffer->Execute().
  180. // Returns 'false' if no command is available at the time of the call.
  181. bool PopCommand(ThreadCommand::PopBuffer* popBuffer);
  182. // Generic implementaion of PushCommand; enqueues a command for execution.
  183. // Returns 'false' if push failed, usually indicating thread shutdown.
  184. bool PushCommand(const ThreadCommand& command);
  185. //
  186. void PushExitCommand(bool wait);
  187. // Returns 'true' once ExitCommand has been processed, so the thread can shut down.
  188. bool IsExiting() const;
  189. // These two virtual functions serve as notifications for derived
  190. // thread waiting.
  191. virtual void OnPushNonEmpty_Locked() { }
  192. virtual void OnPopEmpty_Locked() { }
  193. // *** PushCall with no result
  194. // Enqueue a member function of 'this' class to be called on consumer thread.
  195. // By default the function returns immediately; set 'wait' argument to 'true' to
  196. // wait for completion.
  197. template<class C, class R>
  198. bool PushCall(R (C::*fn)(), bool wait = false)
  199. { return PushCommand(ThreadCommandMF0<C,R>(static_cast<C*>(this), fn, 0, wait)); }
  200. template<class C, class R, class A0>
  201. bool PushCall(R (C::*fn)(A0), typename SelfType<A0>::Type a0, bool wait = false)
  202. { return PushCommand(ThreadCommandMF1<C,R,A0>(static_cast<C*>(this), fn, 0, a0, wait)); }
  203. template<class C, class R, class A0, class A1>
  204. bool PushCall(R (C::*fn)(A0, A1),
  205. typename SelfType<A0>::Type a0, typename SelfType<A1>::Type a1, bool wait = false)
  206. { return PushCommand(ThreadCommandMF2<C,R,A0,A1>(static_cast<C*>(this), fn, 0, a0, a1, wait)); }
  207. // Enqueue a specified member function call of class C.
  208. // By default the function returns immediately; set 'wait' argument to 'true' to
  209. // wait for completion.
  210. template<class C, class R>
  211. bool PushCall(C* p, R (C::*fn)(), bool wait = false)
  212. { return PushCommand(ThreadCommandMF0<C,R>(p, fn, 0, wait)); }
  213. template<class C, class R, class A0>
  214. bool PushCall(C* p, R (C::*fn)(A0), typename SelfType<A0>::Type a0, bool wait = false)
  215. { return PushCommand(ThreadCommandMF1<C,R,A0>(p, fn, 0, a0, wait)); }
  216. template<class C, class R, class A0, class A1>
  217. bool PushCall(C* p, R (C::*fn)(A0, A1),
  218. typename SelfType<A0>::Type a0, typename SelfType<A1>::Type a1, bool wait = false)
  219. { return PushCommand(ThreadCommandMF2<C,R,A0,A1>(p, fn, 0, a0, a1, wait)); }
  220. // *** PushCall with Result
  221. // Enqueue a member function of 'this' class call and wait for call to complete
  222. // on consumer thread before returning.
  223. template<class C, class R>
  224. bool PushCallAndWaitResult(R (C::*fn)(), R* ret)
  225. { return PushCommand(ThreadCommandMF0<C,R>(static_cast<C*>(this), fn, ret, true)); }
  226. template<class C, class R, class A0>
  227. bool PushCallAndWaitResult(R (C::*fn)(A0), R* ret, typename SelfType<A0>::Type a0)
  228. { return PushCommand(ThreadCommandMF1<C,R,A0>(static_cast<C*>(this), fn, ret, a0, true)); }
  229. template<class C, class R, class A0, class A1>
  230. bool PushCallAndWaitResult(R (C::*fn)(A0, A1), R* ret,
  231. typename SelfType<A0>::Type a0, typename SelfType<A1>::Type a1)
  232. { return PushCommand(ThreadCommandMF2<C,R,A0,A1>(static_cast<C*>(this), fn, ret, a0, a1, true)); }
  233. // Enqueue a member function call for class C and wait for the call to complete
  234. // on consumer thread before returning.
  235. template<class C, class R>
  236. bool PushCallAndWaitResult(C* p, R (C::*fn)(), R* ret)
  237. { return PushCommand(ThreadCommandMF0<C,R>(p, fn, ret, true)); }
  238. template<class C, class R, class A0>
  239. bool PushCallAndWaitResult(C* p, R (C::*fn)(A0), R* ret, typename SelfType<A0>::Type a0)
  240. { return PushCommand(ThreadCommandMF1<C,R,A0>(p, fn, ret, a0, true)); }
  241. template<class C, class R, class A0, class A1>
  242. bool PushCallAndWaitResult(C* p, R (C::*fn)(A0, A1), R* ret,
  243. typename SelfType<A0>::Type a0, typename SelfType<A1>::Type a1)
  244. { return PushCommand(ThreadCommandMF2<C,R,A0,A1>(p, fn, ret, a0, a1, true)); }
  245. private:
  246. class ThreadCommandQueueImpl* pImpl;
  247. };
  248. } // namespace OVR
  249. #endif // OVR_ThreadCommandQueue_h