]> git.saurik.com Git - apple/javascriptcore.git/blame - heap/HeapTimer.cpp
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / heap / HeapTimer.cpp
CommitLineData
93a37866
A
1/*
2 * Copyright (C) 2012 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
26#include "config.h"
27#include "HeapTimer.h"
28
81345200 29#include "IncrementalSweeper.h"
93a37866
A
30#include "JSObject.h"
31#include "JSString.h"
81345200 32#include "JSCInlines.h"
93a37866
A
33#include <wtf/MainThread.h>
34#include <wtf/Threading.h>
35
81345200 36#if PLATFORM(EFL)
93a37866
A
37#include <Ecore.h>
38#endif
39
40namespace JSC {
41
42#if USE(CF)
43
44const CFTimeInterval HeapTimer::s_decade = 60 * 60 * 24 * 365 * 10;
45
46static const void* retainAPILock(const void* info)
47{
48 static_cast<JSLock*>(const_cast<void*>(info))->ref();
49 return info;
50}
51
52static void releaseAPILock(const void* info)
53{
54 static_cast<JSLock*>(const_cast<void*>(info))->deref();
55}
56
57HeapTimer::HeapTimer(VM* vm, CFRunLoopRef runLoop)
58 : m_vm(vm)
59 , m_runLoop(runLoop)
60{
61 memset(&m_context, 0, sizeof(CFRunLoopTimerContext));
62 m_context.info = &vm->apiLock();
63 m_context.retain = retainAPILock;
64 m_context.release = releaseAPILock;
65 m_timer = adoptCF(CFRunLoopTimerCreate(0, s_decade, s_decade, 0, 0, HeapTimer::timerDidFire, &m_context));
66 CFRunLoopAddTimer(m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes);
67}
68
69HeapTimer::~HeapTimer()
70{
71 CFRunLoopRemoveTimer(m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes);
72 CFRunLoopTimerInvalidate(m_timer.get());
73}
74
75void HeapTimer::timerDidFire(CFRunLoopTimerRef timer, void* context)
76{
77 JSLock* apiLock = static_cast<JSLock*>(context);
78 apiLock->lock();
79
80 VM* vm = apiLock->vm();
81 // The VM has been destroyed, so we should just give up.
82 if (!vm) {
83 apiLock->unlock();
84 return;
85 }
86
87 HeapTimer* heapTimer = 0;
81345200
A
88 if (vm->heap.fullActivityCallback() && vm->heap.fullActivityCallback()->m_timer.get() == timer)
89 heapTimer = vm->heap.fullActivityCallback();
90 else if (vm->heap.edenActivityCallback() && vm->heap.edenActivityCallback()->m_timer.get() == timer)
91 heapTimer = vm->heap.edenActivityCallback();
93a37866
A
92 else if (vm->heap.sweeper()->m_timer.get() == timer)
93 heapTimer = vm->heap.sweeper();
94 else
95 RELEASE_ASSERT_NOT_REACHED();
96
97 {
81345200 98 JSLockHolder locker(vm);
93a37866
A
99 heapTimer->doWork();
100 }
101
102 apiLock->unlock();
103}
104
93a37866
A
105#elif PLATFORM(EFL)
106
107HeapTimer::HeapTimer(VM* vm)
108 : m_vm(vm)
109 , m_timer(0)
110{
111}
112
113HeapTimer::~HeapTimer()
114{
115 stop();
116}
117
118Ecore_Timer* HeapTimer::add(double delay, void* agent)
119{
120 return ecore_timer_add(delay, reinterpret_cast<Ecore_Task_Cb>(timerEvent), agent);
121}
122
123void HeapTimer::stop()
124{
125 if (!m_timer)
126 return;
127
128 ecore_timer_del(m_timer);
129 m_timer = 0;
130}
131
132bool HeapTimer::timerEvent(void* info)
133{
134 HeapTimer* agent = static_cast<HeapTimer*>(info);
135
81345200 136 JSLockHolder locker(agent->m_vm);
93a37866
A
137 agent->doWork();
138 agent->m_timer = 0;
139
140 return ECORE_CALLBACK_CANCEL;
141}
93a37866
A
142#else
143HeapTimer::HeapTimer(VM* vm)
144 : m_vm(vm)
145{
146}
147
148HeapTimer::~HeapTimer()
149{
150}
151
152void HeapTimer::invalidate()
153{
154}
155
156#endif
157
158
159} // namespace JSC