|
1 /* |
|
2 * Copyright (C) 2010 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. 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. |
|
24 */ |
|
25 |
|
26 #include "WorkQueue.h" |
|
27 |
|
28 #include <process.h> |
|
29 #include <wtf/Threading.h> |
|
30 |
|
31 void WorkQueue::registerHandle(HANDLE handle, PassOwnPtr<WorkItem> item) |
|
32 { |
|
33 // Add the item. |
|
34 { |
|
35 MutexLocker locker(m_handlesLock); |
|
36 m_handles.set(handle, item.leakPtr()); |
|
37 } |
|
38 |
|
39 // Set the work event. |
|
40 ::SetEvent(m_performWorkEvent); |
|
41 } |
|
42 |
|
43 void* WorkQueue::workQueueThreadBody(void *context) |
|
44 { |
|
45 static_cast<WorkQueue*>(context)->workQueueThreadBody(); |
|
46 return 0; |
|
47 } |
|
48 |
|
49 void WorkQueue::workQueueThreadBody() |
|
50 { |
|
51 while (true) { |
|
52 Vector<HANDLE> handles; |
|
53 { |
|
54 // Copy the handles to our handles vector. |
|
55 MutexLocker locker(m_handlesLock); |
|
56 copyKeysToVector(m_handles, handles); |
|
57 } |
|
58 |
|
59 // Add the "perform work" event handle. |
|
60 handles.append(m_performWorkEvent); |
|
61 |
|
62 // Now we wait. |
|
63 DWORD result = ::WaitForMultipleObjects(handles.size(), handles.data(), FALSE, INFINITE); |
|
64 |
|
65 if (result == handles.size() - 1) |
|
66 performWork(); |
|
67 else { |
|
68 // FIXME: If we ever decide to support unregistering handles we would need to copy the hash map. |
|
69 WorkItem* workItem; |
|
70 HANDLE handle = handles[result]; |
|
71 |
|
72 { |
|
73 MutexLocker locker(m_handlesLock); |
|
74 workItem = m_handles.get(handle); |
|
75 } |
|
76 |
|
77 // Execute the work item. |
|
78 workItem->execute(); |
|
79 } |
|
80 |
|
81 // Check if this queue is invalid. |
|
82 { |
|
83 MutexLocker locker(m_isValidMutex); |
|
84 if (!m_isValid) |
|
85 break; |
|
86 } |
|
87 } |
|
88 } |
|
89 |
|
90 void WorkQueue::platformInitialize(const char* name) |
|
91 { |
|
92 // Create our event. |
|
93 m_performWorkEvent = ::CreateEvent(0, false, false, 0); |
|
94 |
|
95 m_workQueueThread = createThread(&WorkQueue::workQueueThreadBody, this, name); |
|
96 } |
|
97 |
|
98 void WorkQueue::platformInvalidate() |
|
99 { |
|
100 ::CloseHandle(m_performWorkEvent); |
|
101 |
|
102 // FIXME: Stop the thread and do other cleanup. |
|
103 } |
|
104 |
|
105 void WorkQueue::scheduleWork(PassOwnPtr<WorkItem> item) |
|
106 { |
|
107 MutexLocker locker(m_workItemQueueLock); |
|
108 m_workItemQueue.append(item.leakPtr()); |
|
109 |
|
110 // Set the work event. |
|
111 ::SetEvent(m_performWorkEvent); |
|
112 } |
|
113 |
|
114 void WorkQueue::performWork() |
|
115 { |
|
116 Vector<WorkItem*> workItemQueue; |
|
117 { |
|
118 MutexLocker locker(m_workItemQueueLock); |
|
119 m_workItemQueue.swap(workItemQueue); |
|
120 } |
|
121 |
|
122 for (size_t i = 0; i < workItemQueue.size(); ++i) { |
|
123 OwnPtr<WorkItem> item(workItemQueue[i]); |
|
124 |
|
125 MutexLocker locker(m_isValidMutex); |
|
126 if (m_isValid) |
|
127 item->execute(); |
|
128 } |
|
129 } |