Util_LongPollThread.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /************************************************************************************
  2. Filename : Util_LongPollThread.cpp
  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. #include "Util_LongPollThread.h"
  20. #include "Util_Watchdog.h"
  21. OVR_DEFINE_SINGLETON(OVR::Util::LongPollThread);
  22. namespace OVR { namespace Util {
  23. void LongPollThread::AddPollFunc(CallbackListener<PollFunc>* func)
  24. {
  25. PollSubject.AddListener(func);
  26. }
  27. LongPollThread::LongPollThread() :
  28. Terminated(false)
  29. {
  30. Start();
  31. // Must be at end of function
  32. PushDestroyCallbacks();
  33. }
  34. LongPollThread::~LongPollThread()
  35. {
  36. fireTermination();
  37. Join();
  38. }
  39. void LongPollThread::OnThreadDestroy()
  40. {
  41. fireTermination();
  42. }
  43. void LongPollThread::Wake()
  44. {
  45. WakeEvent.SetEvent();
  46. }
  47. void LongPollThread::fireTermination()
  48. {
  49. Terminated = true;
  50. Wake();
  51. }
  52. void LongPollThread::OnSystemDestroy()
  53. {
  54. Release();
  55. }
  56. int LongPollThread::Run()
  57. {
  58. SetThreadName("LongPoll");
  59. WatchDog watchdog("LongPoll");
  60. // While not terminated,
  61. do
  62. {
  63. watchdog.Feed(20000);
  64. PollSubject.Call();
  65. WakeEvent.Wait(WakeupInterval);
  66. WakeEvent.ResetEvent();
  67. } while (!Terminated);
  68. return 0;
  69. }
  70. }} // namespace OVR::Util