Util_LongPollThread.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /************************************************************************************
  2. Filename : Util_LongPollThread.h
  3. Content : Allows us to do all long polling tasks from a single thread to minimize deadlock risk
  4. Created : June 30, 2013
  5. Authors : Chris Taylor
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. *************************************************************************************/
  19. #ifndef OVR_Util_LongPollThread_h
  20. #define OVR_Util_LongPollThread_h
  21. #include "Kernel/OVR_Timer.h"
  22. #include "Kernel/OVR_Atomic.h"
  23. #include "Kernel/OVR_Allocator.h"
  24. #include "Kernel/OVR_String.h"
  25. #include "Kernel/OVR_System.h"
  26. #include "Kernel/OVR_Threads.h"
  27. #include "Kernel/OVR_Callbacks.h"
  28. namespace OVR { namespace Util {
  29. //-----------------------------------------------------------------------------
  30. // LongPollThread
  31. // This thread runs long-polling subsystems that wake up every second or so
  32. // The motivation is to reduce the number of threads that are running to minimize the risk of deadlock
  33. class LongPollThread : public Thread, public SystemSingletonBase<LongPollThread>
  34. {
  35. OVR_DECLARE_SINGLETON(LongPollThread);
  36. virtual void OnThreadDestroy();
  37. public:
  38. typedef Delegate0<void> PollFunc;
  39. static const int WakeupInterval = 1000; // milliseconds
  40. void AddPollFunc(CallbackListener<PollFunc>* func);
  41. void Wake();
  42. protected:
  43. CallbackEmitter<PollFunc> PollSubject;
  44. bool Terminated;
  45. Event WakeEvent;
  46. void fireTermination();
  47. virtual int Run();
  48. };
  49. }} // namespace OVR::Util
  50. #endif // OVR_Util_LongPollThread_h