]>
git.saurik.com Git - apple/javascriptcore.git/blob - heap/HeapTimer.cpp
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "HeapTimer.h"
33 #include <wtf/MainThread.h>
34 #include <wtf/Threading.h>
37 #include <QCoreApplication>
38 #include <QMutexLocker>
40 #include <QTimerEvent>
49 const CFTimeInterval
HeapTimer::s_decade
= 60 * 60 * 24 * 365 * 10;
51 static const void* retainAPILock(const void* info
)
53 static_cast<JSLock
*>(const_cast<void*>(info
))->ref();
57 static void releaseAPILock(const void* info
)
59 static_cast<JSLock
*>(const_cast<void*>(info
))->deref();
62 HeapTimer::HeapTimer(VM
* vm
, CFRunLoopRef runLoop
)
66 memset(&m_context
, 0, sizeof(CFRunLoopTimerContext
));
67 m_context
.info
= &vm
->apiLock();
68 m_context
.retain
= retainAPILock
;
69 m_context
.release
= releaseAPILock
;
70 m_timer
= adoptCF(CFRunLoopTimerCreate(0, s_decade
, s_decade
, 0, 0, HeapTimer::timerDidFire
, &m_context
));
71 CFRunLoopAddTimer(m_runLoop
.get(), m_timer
.get(), kCFRunLoopCommonModes
);
74 HeapTimer::~HeapTimer()
76 CFRunLoopRemoveTimer(m_runLoop
.get(), m_timer
.get(), kCFRunLoopCommonModes
);
77 CFRunLoopTimerInvalidate(m_timer
.get());
80 void HeapTimer::timerDidFire(CFRunLoopTimerRef timer
, void* context
)
82 JSLock
* apiLock
= static_cast<JSLock
*>(context
);
85 VM
* vm
= apiLock
->vm();
86 // The VM has been destroyed, so we should just give up.
92 HeapTimer
* heapTimer
= 0;
94 if (vm
->heap
.activityCallback() && vm
->heap
.activityCallback()->m_timer
.get() == timer
)
95 heapTimer
= vm
->heap
.activityCallback();
97 if (vm
->heap
.activityCallback()->m_timer
.get() == timer
)
98 heapTimer
= vm
->heap
.activityCallback();
99 #endif // PLATFORM(IOS)
100 else if (vm
->heap
.sweeper()->m_timer
.get() == timer
)
101 heapTimer
= vm
->heap
.sweeper();
103 RELEASE_ASSERT_NOT_REACHED();
106 APIEntryShim
shim(vm
);
113 #elif PLATFORM(BLACKBERRY)
115 HeapTimer::HeapTimer(VM
* vm
)
117 , m_timer(this, &HeapTimer::timerDidFire
)
119 // FIXME: Implement HeapTimer for other threads.
120 if (WTF::isMainThread() && !m_timer
.tryCreateClient())
124 HeapTimer::~HeapTimer()
128 void HeapTimer::timerDidFire()
133 void HeapTimer::invalidate()
139 HeapTimer::HeapTimer(VM
* vm
)
142 , m_mutex(QMutex::NonRecursive
)
144 // The HeapTimer might be created before the runLoop is started,
145 // but we need to ensure the thread has an eventDispatcher already.
146 QEventLoop
fakeLoop(this);
149 HeapTimer::~HeapTimer()
151 QMutexLocker
lock(&m_mutex
);
155 void HeapTimer::timerEvent(QTimerEvent
*)
157 QMutexLocker
lock(&m_mutex
);
159 // We need to wait with processing until we are on the right thread.
163 APIEntryShim
shim(m_vm
);
167 void HeapTimer::customEvent(QEvent
*)
170 QMutexLocker
lock(&m_mutex
);
171 moveToThread(m_newThread
);
177 HeapTimer::HeapTimer(VM
* vm
)
183 HeapTimer::~HeapTimer()
188 Ecore_Timer
* HeapTimer::add(double delay
, void* agent
)
190 return ecore_timer_add(delay
, reinterpret_cast<Ecore_Task_Cb
>(timerEvent
), agent
);
193 void HeapTimer::stop()
198 ecore_timer_del(m_timer
);
202 bool HeapTimer::timerEvent(void* info
)
204 HeapTimer
* agent
= static_cast<HeapTimer
*>(info
);
206 APIEntryShim
shim(agent
->m_vm
);
210 return ECORE_CALLBACK_CANCEL
;
214 HeapTimer::HeapTimer(VM
* vm
)
219 HeapTimer::~HeapTimer()
223 void HeapTimer::invalidate()