1 /*
2 * Copyright (C) 2009 Maxime Simon <simon.maxime@gmail.com>
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 COMPUTER, 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 COMPUTER, 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
26 #include "config.h"
27 #include "SharedTimer.h"
28
29 #include <MessageFilter.h>
30 #include <MessageRunner.h>
31 #include <Looper.h>
32 #include <support/Locker.h>
33 #include <support/Autolock.h>
34 #include <wtf/CurrentTime.h>
35
36 #define FIRE_MESSAGE 'fire'
37
38
39 namespace WebCore {
40
41 class SharedTimerHaiku: public BMessageFilter {
42 friend void setSharedTimerFiredFunction(void (*f)());
43 public:
44 static SharedTimerHaiku* instance();
45
46 void start(double);
47 void stop();
48
49 protected:
50 virtual filter_result Filter(BMessage*, BHandler**);
51
52 private:
53 SharedTimerHaiku();
54 ~SharedTimerHaiku();
55
56 void (*m_timerFunction)();
57 bool m_shouldRun;
58 };
59
SharedTimerHaiku()60 SharedTimerHaiku::SharedTimerHaiku()
61 : BMessageFilter(FIRE_MESSAGE)
62 , m_timerFunction(0)
63 , m_shouldRun(false)
64 {
65 }
66
~SharedTimerHaiku()67 SharedTimerHaiku::~SharedTimerHaiku()
68 {
69 }
70
instance()71 SharedTimerHaiku* SharedTimerHaiku::instance()
72 {
73 BLooper* looper = BLooper::LooperForThread(find_thread(0));
74 static SharedTimerHaiku* timer;
75
76 if (!timer) {
77 BAutolock lock(looper);
78 timer = new SharedTimerHaiku();
79 looper->AddCommonFilter(timer);
80 }
81
82 return timer;
83 }
84
start(double fireTime)85 void SharedTimerHaiku::start(double fireTime)
86 {
87 m_shouldRun = true;
88
89 double intervalInSeconds = fireTime - currentTime();
90 bigtime_t intervalInMicroSeconds = intervalInSeconds < 0 ? 0 : intervalInSeconds * 1000000;
91
92 BMessageRunner::StartSending(Looper(), new BMessage(FIRE_MESSAGE), intervalInMicroSeconds, 1);
93 }
94
stop()95 void SharedTimerHaiku::stop()
96 {
97 m_shouldRun = false;
98 }
99
Filter(BMessage *,BHandler **)100 filter_result SharedTimerHaiku::Filter(BMessage*, BHandler**)
101 {
102 if (m_shouldRun && m_timerFunction)
103 m_timerFunction();
104
105 return B_SKIP_MESSAGE;
106 }
107
108 // WebCore functions
setSharedTimerFiredFunction(void (* f)())109 void setSharedTimerFiredFunction(void (*f)())
110 {
111 SharedTimerHaiku::instance()->m_timerFunction = f;
112 }
113
setSharedTimerFireTime(double fireTime)114 void setSharedTimerFireTime(double fireTime)
115 {
116 SharedTimerHaiku::instance()->start(fireTime);
117 }
118
stopSharedTimer()119 void stopSharedTimer()
120 {
121 SharedTimerHaiku::instance()->stop();
122 }
123
124 } // namespace WebCore
125