WatchdogManx.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2014 Sony Computer Entertainment Inc.
  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 SONY COMPUTER ENTERTAINMENT INC. ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SONY COMPUTER ENTERTAINMENT INC.
  17. * OR 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. #include "config.h"
  26. #include "Watchdog.h"
  27. #include <manx/RunLoop.h>
  28. #include <wtf/Threading.h>
  29. #include <wtf/ThreadingPrimitives.h>
  30. #include <cstdio>
  31. namespace JSC {
  32. class Watchdog::WatchdogTimerHandler : public Manx::RunLoop::Function {
  33. public:
  34. WatchdogTimerHandler(Watchdog*);
  35. ~WatchdogTimerHandler();
  36. void startTimer(double timeout);
  37. void stopTimer();
  38. virtual void call() OVERRIDE;
  39. private:
  40. static void watchdogThread(void*);
  41. Watchdog* m_watchdog;
  42. ThreadIdentifier m_watchdogThreadId;
  43. Mutex m_runLoopMutex;
  44. ThreadCondition m_runLoopCreatedCondition;
  45. Manx::RunLoop* m_watchdogRunLoop;
  46. Manx::RunLoop::Timer* m_watchdogTimer;
  47. };
  48. Watchdog::WatchdogTimerHandler::WatchdogTimerHandler(Watchdog* watchdog)
  49. : m_watchdog(watchdog)
  50. , m_watchdogThreadId(0)
  51. #if USE(MANX_COND_INIT)
  52. , m_runLoopCreatedCondition(m_runLoopMutex)
  53. #endif
  54. , m_watchdogRunLoop(0)
  55. , m_watchdogTimer(0)
  56. {
  57. MutexLocker ml(m_runLoopMutex);
  58. m_watchdogThreadId = createThread(&watchdogThread, this, "JavaScriptCore::Watchdog");
  59. if (m_watchdogThreadId) {
  60. m_runLoopCreatedCondition.wait(m_runLoopMutex);
  61. ASSERT(m_watchdogRunLoop);
  62. } else {
  63. LOG_ERROR("WatchdogTimerHandler: couldn't create the JSC watchdog handler thread!");
  64. }
  65. }
  66. Watchdog::WatchdogTimerHandler::~WatchdogTimerHandler()
  67. {
  68. if (m_watchdogRunLoop) {
  69. delete m_watchdogTimer;
  70. // m_watchdogRunLoop will self-delete when the thread exits
  71. m_watchdogRunLoop->quit();
  72. waitForThreadCompletion(m_watchdogThreadId);
  73. }
  74. }
  75. void Watchdog::WatchdogTimerHandler::startTimer(double timeout)
  76. {
  77. if (!m_watchdogRunLoop)
  78. return;
  79. if (!m_watchdogTimer)
  80. m_watchdogTimer = m_watchdogRunLoop->createTimer(this);
  81. m_watchdogTimer->startOneShot(timeout);
  82. }
  83. void Watchdog::WatchdogTimerHandler::stopTimer()
  84. {
  85. if (!m_watchdogTimer)
  86. return;
  87. m_watchdogTimer->stop();
  88. }
  89. void Watchdog::WatchdogTimerHandler::call()
  90. {
  91. m_watchdog->m_timerDidFire = true;
  92. }
  93. void Watchdog::WatchdogTimerHandler::watchdogThread(void* context)
  94. {
  95. WatchdogTimerHandler* thiz = reinterpret_cast<WatchdogTimerHandler*>(context);
  96. {
  97. MutexLocker ml(thiz->m_runLoopMutex);
  98. thiz->m_watchdogRunLoop = Manx::RunLoop::current();
  99. thiz->m_runLoopCreatedCondition.signal();
  100. }
  101. Manx::RunLoop::run();
  102. }
  103. void Watchdog::initTimer()
  104. {
  105. m_timerHandler = 0;
  106. }
  107. void Watchdog::destroyTimer()
  108. {
  109. delete m_timerHandler;
  110. }
  111. void Watchdog::startTimer(double timeout)
  112. {
  113. if (!m_timerHandler)
  114. m_timerHandler = new WatchdogTimerHandler(this);
  115. m_timerHandler->startTimer(timeout);
  116. }
  117. void Watchdog::stopTimer()
  118. {
  119. if (m_timerHandler)
  120. m_timerHandler->stopTimer();
  121. }
  122. } // namespace JSC