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