RunLoopManx.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. * Copyright (C) 2012 Sony Computer Entertainment Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  18. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  24. * THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "RunLoop.h"
  28. #include <wtf/CurrentTime.h>
  29. #include <wtf/OwnPtr.h>
  30. #include <wtf/PassOwnPtr.h>
  31. using namespace std;
  32. namespace WebCore {
  33. // TimerFunction
  34. class TimerFunction : public Manx::RunLoop::Function {
  35. public:
  36. TimerFunction(WTF::Function<void()> function) : m_function(function) { }
  37. virtual ~TimerFunction() { }
  38. virtual void call() { m_function(); }
  39. private:
  40. WTF::Function<void()> m_function;
  41. };
  42. // RunLoop
  43. RunLoop::RunLoop()
  44. {
  45. m_manxRunLoop = Manx::RunLoop::current();
  46. m_wakeUpTimer = adoptPtr(m_manxRunLoop->createTimer(new TimerFunction(bind(&RunLoop::performWork, this))));
  47. }
  48. RunLoop::~RunLoop()
  49. {
  50. }
  51. void RunLoop::wakeUp()
  52. {
  53. m_wakeUpTimer->startOneShot(0);
  54. }
  55. // RunLoop::Timer
  56. RunLoop::TimerBase::TimerBase(RunLoop* runLoop)
  57. : m_runLoop(runLoop)
  58. {
  59. m_timer = adoptPtr(m_runLoop->m_manxRunLoop->createTimer(new TimerFunction(bind(&RunLoop::TimerBase::fired, this))));
  60. }
  61. RunLoop::TimerBase::~TimerBase()
  62. {
  63. }
  64. void RunLoop::TimerBase::start(double nextFireInterval, bool repeat)
  65. {
  66. if (repeat)
  67. m_timer->startRepeating(nextFireInterval);
  68. else
  69. m_timer->startOneShot(nextFireInterval);
  70. }
  71. void RunLoop::TimerBase::stop()
  72. {
  73. m_timer->stop();
  74. }
  75. bool RunLoop::TimerBase::isActive() const
  76. {
  77. return m_timer->isActive();
  78. }
  79. void RunLoop::run()
  80. {
  81. Manx::RunLoop::run();
  82. }
  83. void RunLoop::stop()
  84. {
  85. m_manxRunLoop->quit();
  86. }
  87. } // namespace WebCore