Watchdog.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef Watchdog_h
  26. #define Watchdog_h
  27. #if PLATFORM(MAC) || PLATFORM(IOS)
  28. #include <dispatch/dispatch.h>
  29. #endif
  30. namespace JSC {
  31. class ExecState;
  32. class VM;
  33. class Watchdog {
  34. public:
  35. class Scope;
  36. Watchdog();
  37. ~Watchdog();
  38. typedef bool (*ShouldTerminateCallback)(ExecState*, void* data1, void* data2);
  39. void setTimeLimit(VM&, double seconds, ShouldTerminateCallback = 0, void* data1 = 0, void* data2 = 0);
  40. // This version of didFire() will check the elapsed CPU time and call the
  41. // callback (if needed) to determine if the watchdog should fire.
  42. bool didFire(ExecState*);
  43. bool isEnabled();
  44. // This version of didFire() is a more efficient version for when we want
  45. // to know if the watchdog has fired in the past, and not whether it should
  46. // fire right now.
  47. JS_EXPORT_PRIVATE bool didFire() { return m_didFire; }
  48. JS_EXPORT_PRIVATE void fire();
  49. void* timerDidFireAddress() { return &m_timerDidFire; }
  50. private:
  51. void arm();
  52. void disarm();
  53. void startCountdownIfNeeded();
  54. void startCountdown(double limit);
  55. void stopCountdown();
  56. bool isArmed() { return !!m_reentryCount; }
  57. // Platform specific timer implementation:
  58. void initTimer();
  59. void destroyTimer();
  60. void startTimer(double limit);
  61. void stopTimer();
  62. // m_timerDidFire (above) indicates whether the timer fired. The Watchdog
  63. // still needs to check if the allowed CPU time has elapsed. If so, then
  64. // the Watchdog fires and m_didFire will be set.
  65. // NOTE: m_timerDidFire is only set by the platform specific timer
  66. // (probably from another thread) but is only cleared in the script thread.
  67. bool m_timerDidFire;
  68. bool m_didFire;
  69. // All time units are in seconds.
  70. double m_limit;
  71. double m_startTime;
  72. double m_elapsedTime;
  73. int m_reentryCount;
  74. bool m_isStopped;
  75. ShouldTerminateCallback m_callback;
  76. void* m_callbackData1;
  77. void* m_callbackData2;
  78. #if PLATFORM(MAC) || PLATFORM(IOS)
  79. dispatch_queue_t m_queue;
  80. dispatch_source_t m_timer;
  81. #endif
  82. #if PLATFORM(MANX)
  83. class WatchdogTimerHandler;
  84. WatchdogTimerHandler* m_timerHandler;
  85. #endif
  86. friend class Watchdog::Scope;
  87. friend class LLIntOffsetsExtractor;
  88. };
  89. class Watchdog::Scope {
  90. public:
  91. Scope(Watchdog&);
  92. ~Scope();
  93. private:
  94. Watchdog& m_watchdog;
  95. };
  96. } // namespace JSC
  97. #endif // Watchdog_h