Util_Watchdog.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /************************************************************************************
  2. Filename : Util_Watchdog.h
  3. Content : Deadlock reaction
  4. Created : June 27, 2013
  5. Authors : Kevin Jenkins, 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_Watchdog_h
  20. #define OVR_Util_Watchdog_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. namespace OVR { namespace Util {
  28. //-----------------------------------------------------------------------------
  29. // WatchDog
  30. class WatchDog : public NewOverrideBase
  31. {
  32. friend class WatchDogObserver;
  33. public:
  34. WatchDog(const String& threadName);
  35. ~WatchDog();
  36. void Disable();
  37. void Enable();
  38. void Feed(int threshold);
  39. protected:
  40. // Use 32 bit int so assignment and comparison is atomic
  41. AtomicInt<uint32_t> WhenLastFedMilliseconds;
  42. AtomicInt<int> ThreshholdMilliseconds;
  43. String ThreadName;
  44. bool Listed;
  45. };
  46. //-----------------------------------------------------------------------------
  47. // WatchDogObserver
  48. class WatchDogObserver : public Thread, public SystemSingletonBase<WatchDogObserver>
  49. {
  50. OVR_DECLARE_SINGLETON(WatchDogObserver);
  51. virtual void OnThreadDestroy();
  52. friend class WatchDog;
  53. public:
  54. // Uses the exception logger to write deadlock reports
  55. void EnableReporting(const String organization = String(),
  56. const String application = String());
  57. // Disables deadlock reports
  58. void DisableReporting();
  59. protected:
  60. Lock ListLock;
  61. Array< WatchDog* > DogList;
  62. static const int WakeupInterval = 1000; // milliseconds between checks
  63. Event TerminationEvent;
  64. // Used in deadlock reporting.
  65. bool IsReporting;
  66. // On Windows, deadlock logs are stored in %AppData%\OrganizationName\ApplicationName\.
  67. // See ExceptionHandler::ReportDeadlock() for how these are used.
  68. String ApplicationName;
  69. String OrganizationName;
  70. protected:
  71. virtual int Run();
  72. void Add(WatchDog* dog);
  73. void Remove(WatchDog* dog);
  74. public:
  75. static bool IsExitingOnDeadlock();
  76. static void SetExitingOnDeadlock(bool enabled);
  77. };
  78. }} // namespace OVR::Util
  79. #endif // OVR_Util_Watchdog_h