2 * Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WorkQueue.h"
29 #include <WebCore/NotImplemented.h>
30 #include <wtf/Threading.h>
32 inline WorkQueue::WorkItemWin::WorkItemWin(PassOwnPtr<WorkItem> item, WorkQueue* queue)
38 PassRefPtr<WorkQueue::WorkItemWin> WorkQueue::WorkItemWin::create(PassOwnPtr<WorkItem> item, WorkQueue* queue)
40 return adoptRef(new WorkItemWin(item, queue));
43 WorkQueue::WorkItemWin::~WorkItemWin()
47 inline WorkQueue::HandleWorkItem::HandleWorkItem(HANDLE handle, PassOwnPtr<WorkItem> item, WorkQueue* queue)
48 : WorkItemWin(item, queue)
52 ASSERT_ARG(handle, handle);
55 PassRefPtr<WorkQueue::HandleWorkItem> WorkQueue::HandleWorkItem::createByAdoptingHandle(HANDLE handle, PassOwnPtr<WorkItem> item, WorkQueue* queue)
57 return adoptRef(new HandleWorkItem(handle, item, queue));
60 WorkQueue::HandleWorkItem::~HandleWorkItem()
62 ::CloseHandle(m_handle);
65 void WorkQueue::handleCallback(void* context, BOOLEAN timerOrWaitFired)
67 ASSERT_ARG(context, context);
68 ASSERT_ARG(timerOrWaitFired, !timerOrWaitFired);
70 WorkItemWin* item = static_cast<WorkItemWin*>(context);
71 WorkQueue* queue = item->queue();
74 MutexLocker lock(queue->m_workItemQueueLock);
75 queue->m_workItemQueue.append(item);
77 // If no other thread is performing work, we can do it on this thread.
78 if (!queue->tryRegisterAsWorkThread()) {
79 // Some other thread is performing work. Since we hold the queue lock, we can be sure
80 // that the work thread is not exiting due to an empty queue and will process the work
81 // item we just added to it. If we weren't holding the lock we'd have to signal
82 // m_performWorkEvent to make sure the work item got picked up.
87 queue->performWorkOnRegisteredWorkThread();
90 void WorkQueue::registerHandle(HANDLE handle, PassOwnPtr<WorkItem> item)
92 RefPtr<HandleWorkItem> handleItem = HandleWorkItem::createByAdoptingHandle(handle, item, this);
95 MutexLocker lock(m_handlesLock);
96 ASSERT_ARG(handle, !m_handles.contains(handle));
97 m_handles.set(handle, handleItem);
101 if (!::RegisterWaitForSingleObject(&waitHandle, handle, handleCallback, handleItem.get(), INFINITE, WT_EXECUTEDEFAULT)) {
102 DWORD error = ::GetLastError();
103 ASSERT_NOT_REACHED();
105 handleItem->setWaitHandle(waitHandle);
108 void WorkQueue::unregisterAndCloseHandle(HANDLE handle)
110 RefPtr<HandleWorkItem> item;
112 MutexLocker locker(m_handlesLock);
113 ASSERT_ARG(handle, m_handles.contains(handle));
114 item = m_handles.take(handle);
117 unregisterWaitAndDestroyItemSoon(item.release());
120 DWORD WorkQueue::workThreadCallback(void* context)
122 ASSERT_ARG(context, context);
124 WorkQueue* queue = static_cast<WorkQueue*>(context);
126 if (!queue->tryRegisterAsWorkThread())
129 queue->performWorkOnRegisteredWorkThread();
133 void WorkQueue::performWorkOnRegisteredWorkThread()
135 ASSERT(m_isWorkThreadRegistered);
139 m_workItemQueueLock.lock();
141 while (isValid && !m_workItemQueue.isEmpty()) {
142 Vector<RefPtr<WorkItemWin> > workItemQueue;
143 m_workItemQueue.swap(workItemQueue);
145 // Allow more work to be scheduled while we're not using the queue directly.
146 m_workItemQueueLock.unlock();
147 for (size_t i = 0; i < workItemQueue.size(); ++i) {
148 MutexLocker locker(m_isValidMutex);
152 workItemQueue[i]->item()->execute();
154 m_workItemQueueLock.lock();
157 // One invariant we maintain is that any work scheduled while a work thread is registered will
158 // be handled by that work thread. Unregister as the work thread while the queue lock is still
159 // held so that no work can be scheduled while we're still registered.
160 unregisterAsWorkThread();
162 m_workItemQueueLock.unlock();
165 void WorkQueue::platformInitialize(const char* name)
167 m_isWorkThreadRegistered = 0;
168 m_timerQueue = ::CreateTimerQueue();
169 ASSERT_WITH_MESSAGE(m_timerQueue, "::CreateTimerQueue failed with error %lu", ::GetLastError());
172 bool WorkQueue::tryRegisterAsWorkThread()
174 LONG result = ::InterlockedCompareExchange(&m_isWorkThreadRegistered, 1, 0);
175 ASSERT(!result || result == 1);
179 void WorkQueue::unregisterAsWorkThread()
181 LONG result = ::InterlockedCompareExchange(&m_isWorkThreadRegistered, 0, 1);
182 ASSERT_UNUSED(result, result == 1);
185 void WorkQueue::platformInvalidate()
188 MutexLocker lock(m_handlesLock);
189 ASSERT(m_handles.isEmpty());
192 // FIXME: We need to ensure that any timer-queue timers that fire after this point don't try to
193 // access this WorkQueue <http://webkit.org/b/44690>.
194 ::DeleteTimerQueueEx(m_timerQueue, 0);
197 void WorkQueue::scheduleWork(PassOwnPtr<WorkItem> item)
199 MutexLocker locker(m_workItemQueueLock);
201 m_workItemQueue.append(WorkItemWin::create(item, this));
203 // Spawn a work thread to perform the work we just added. As an optimization, we avoid
204 // spawning the thread if a work thread is already registered. This prevents multiple work
205 // threads from being spawned in most cases. (Note that when a work thread has been spawned but
206 // hasn't registered itself yet, m_isWorkThreadRegistered will be false and we'll end up
207 // spawning a second work thread here. But work thread registration process will ensure that
208 // only one thread actually ends up performing work.)
209 if (!m_isWorkThreadRegistered)
210 ::QueueUserWorkItem(workThreadCallback, this, WT_EXECUTEDEFAULT);
213 struct TimerContext : public ThreadSafeRefCounted<TimerContext> {
214 static PassRefPtr<TimerContext> create() { return adoptRef(new TimerContext); }
217 OwnPtr<WorkItem> item;
222 TimerContext() : queue(0), timer(0) { }
225 void WorkQueue::timerCallback(void* context, BOOLEAN timerOrWaitFired)
227 ASSERT_ARG(context, context);
228 ASSERT_UNUSED(timerOrWaitFired, timerOrWaitFired);
230 // Balanced by leakRef in scheduleWorkAfterDelay.
231 RefPtr<TimerContext> timerContext = adoptRef(static_cast<TimerContext*>(context));
233 timerContext->queue->scheduleWork(timerContext->item.release());
235 MutexLocker lock(timerContext->timerMutex);
236 ASSERT(timerContext->timer);
237 ASSERT(timerContext->queue->m_timerQueue);
238 if (!::DeleteTimerQueueTimer(timerContext->queue->m_timerQueue, timerContext->timer, 0))
239 ASSERT_WITH_MESSAGE(false, "::DeleteTimerQueueTimer failed with error %lu", ::GetLastError());
242 void WorkQueue::scheduleWorkAfterDelay(PassOwnPtr<WorkItem> item, double delay)
244 ASSERT(m_timerQueue);
246 RefPtr<TimerContext> context = TimerContext::create();
247 context->queue = this;
248 context->item = item;
251 // The timer callback could fire before ::CreateTimerQueueTimer even returns, so we protect
252 // context->timer with a mutex to ensure the timer callback doesn't access it before the
253 // timer handle has been stored in it.
254 MutexLocker lock(context->timerMutex);
256 // Since our timer callback is quick, we can execute in the timer thread itself and avoid
257 // an extra thread switch over to a worker thread.
258 if (!::CreateTimerQueueTimer(&context->timer, m_timerQueue, timerCallback, context.get(), delay * 1000, 0, WT_EXECUTEINTIMERTHREAD)) {
259 ASSERT_WITH_MESSAGE(false, "::CreateTimerQueueTimer failed with error %lu", ::GetLastError());
264 // The timer callback will handle destroying context.
265 context.release().leakRef();
268 void WorkQueue::unregisterWaitAndDestroyItemSoon(PassRefPtr<HandleWorkItem> item)
270 // We're going to make a blocking call to ::UnregisterWaitEx before closing the handle. (The
271 // blocking version of ::UnregisterWaitEx is much simpler than the non-blocking version.) If we
272 // do this on the current thread, we'll deadlock if we're currently in a callback function for
273 // the wait we're unregistering. So instead we do it asynchronously on some other worker thread.
275 ::QueueUserWorkItem(unregisterWaitAndDestroyItemCallback, item.leakRef(), WT_EXECUTEDEFAULT);
278 DWORD WINAPI WorkQueue::unregisterWaitAndDestroyItemCallback(void* context)
280 ASSERT_ARG(context, context);
281 RefPtr<HandleWorkItem> item = adoptRef(static_cast<HandleWorkItem*>(context));
283 // Now that we know we're not in a callback function for the wait we're unregistering, we can
284 // make a blocking call to ::UnregisterWaitEx.
285 if (!::UnregisterWaitEx(item->waitHandle(), INVALID_HANDLE_VALUE)) {
286 DWORD error = ::GetLastError();
287 ASSERT_NOT_REACHED();