1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/*
Copyright (C) 2012 Samsung Electronics
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WorkQueue.h"
#include <sys/timerfd.h>
#include <wtf/Assertions.h>
#include <wtf/CurrentTime.h>
static const int invalidSocketDescriptor = -1;
static const int threadMessageSize = 1;
static const char finishThreadMessage[] = "F";
static const char wakupThreadMessage[] = "W";
PassOwnPtr<WorkQueue::TimerWorkItem> WorkQueue::TimerWorkItem::create(Function<void()> function, double expireTime)
{
if (expireTime < 0)
return nullptr;
return adoptPtr(new TimerWorkItem(function, expireTime));
}
WorkQueue::TimerWorkItem::TimerWorkItem(Function<void()> function, double expireTime)
: m_function(function)
, m_expireTime(expireTime)
{
}
void WorkQueue::platformInitialize(const char* name)
{
int fds[2];
if (pipe(fds))
ASSERT_NOT_REACHED();
m_readFromPipeDescriptor = fds[0];
m_writeToPipeDescriptor = fds[1];
FD_ZERO(&m_fileDescriptorSet);
FD_SET(m_readFromPipeDescriptor, &m_fileDescriptorSet);
m_maxFileDescriptor = m_readFromPipeDescriptor;
m_socketDescriptor = invalidSocketDescriptor;
m_threadLoop = true;
createThread(reinterpret_cast<WTF::ThreadFunction>(&WorkQueue::workQueueThread), this, name);
}
void WorkQueue::platformInvalidate()
{
sendMessageToThread(finishThreadMessage);
}
void WorkQueue::performWork()
{
while (true) {
Vector<Function<void()> > workItemQueue;
{
MutexLocker locker(m_workItemQueueLock);
if (m_workItemQueue.isEmpty())
return;
m_workItemQueue.swap(workItemQueue);
}
for (size_t i = 0; i < workItemQueue.size(); ++i) {
workItemQueue[i]();
deref();
}
}
}
void WorkQueue::performFileDescriptorWork()
{
fd_set readFileDescriptorSet = m_fileDescriptorSet;
if (select(m_maxFileDescriptor + 1, &readFileDescriptorSet, 0, 0, getNextTimeOut()) >= 0) {
if (FD_ISSET(m_readFromPipeDescriptor, &readFileDescriptorSet)) {
char readBuf[threadMessageSize];
if (read(m_readFromPipeDescriptor, readBuf, threadMessageSize) == -1)
LOG_ERROR("Failed to read from WorkQueueThread pipe");
if (!strncmp(readBuf, finishThreadMessage, threadMessageSize))
m_threadLoop = false;
}
if (m_socketDescriptor != invalidSocketDescriptor && FD_ISSET(m_socketDescriptor, &readFileDescriptorSet))
m_socketEventHandler();
}
}
struct timeval* WorkQueue::getNextTimeOut()
{
MutexLocker locker(m_timerWorkItemsLock);
if (m_timerWorkItems.isEmpty())
return 0;
static struct timeval timeValue;
timeValue.tv_sec = 0;
timeValue.tv_usec = 0;
double timeOut = m_timerWorkItems[0]->expireTime() - currentTime();
if (timeOut > 0) {
timeValue.tv_sec = static_cast<long>(timeOut);
timeValue.tv_usec = static_cast<long>((timeOut - timeValue.tv_sec) * 1000000);
}
return &timeValue;
}
void WorkQueue::insertTimerWorkItem(PassOwnPtr<TimerWorkItem> item)
{
if (!item)
return;
size_t position = 0;
MutexLocker locker(m_timerWorkItemsLock);
// m_timerWorkItems should be ordered by expire time.
for (; position < m_timerWorkItems.size(); ++position)
if (item->expireTime() < m_timerWorkItems[position]->expireTime())
break;
m_timerWorkItems.insert(position, item);
}
void WorkQueue::performTimerWork()
{
Vector<OwnPtr<TimerWorkItem> > timerWorkItems;
{
// Protects m_timerWorkItems.
MutexLocker locker(m_timerWorkItemsLock);
if (m_timerWorkItems.isEmpty())
return;
// Copies all the timer work items in m_timerWorkItems to local vector.
m_timerWorkItems.swap(timerWorkItems);
}
double current = currentTime();
for (size_t i = 0; i < timerWorkItems.size(); ++i) {
if (!timerWorkItems[i]->expired(current)) {
// If a timer work item does not expired, keep it to the m_timerWorkItems.
// m_timerWorkItems should be ordered by expire time.
insertTimerWorkItem(timerWorkItems[i].release());
continue;
}
// If a timer work item expired, dispatch the function of the work item.
timerWorkItems[i]->dispatch();
deref();
}
}
void WorkQueue::sendMessageToThread(const char* message)
{
MutexLocker locker(m_writeToPipeDescriptorLock);
if (write(m_writeToPipeDescriptor, message, threadMessageSize) == -1)
LOG_ERROR("Failed to wake up WorkQueue Thread");
}
void* WorkQueue::workQueueThread(WorkQueue* workQueue)
{
while (workQueue->m_threadLoop) {
workQueue->performWork();
workQueue->performTimerWork();
workQueue->performFileDescriptorWork();
}
close(workQueue->m_readFromPipeDescriptor);
close(workQueue->m_writeToPipeDescriptor);
return 0;
}
void WorkQueue::registerSocketEventHandler(int fileDescriptor, const Function<void()>& function)
{
if (m_socketDescriptor != invalidSocketDescriptor)
LOG_ERROR("%d is already registerd.", fileDescriptor);
m_socketDescriptor = fileDescriptor;
m_socketEventHandler = function;
if (fileDescriptor > m_maxFileDescriptor)
m_maxFileDescriptor = fileDescriptor;
FD_SET(fileDescriptor, &m_fileDescriptorSet);
}
void WorkQueue::unregisterSocketEventHandler(int fileDescriptor)
{
m_socketDescriptor = invalidSocketDescriptor;
if (fileDescriptor == m_maxFileDescriptor)
m_maxFileDescriptor = m_readFromPipeDescriptor;
FD_CLR(fileDescriptor, &m_fileDescriptorSet);
}
void WorkQueue::dispatch(const Function<void()>& function)
{
ref();
{
MutexLocker locker(m_workItemQueueLock);
m_workItemQueue.append(function);
}
sendMessageToThread(wakupThreadMessage);
}
void WorkQueue::dispatchAfterDelay(const Function<void()>& function, double delay)
{
if (delay < 0)
return;
OwnPtr<TimerWorkItem> timerWorkItem = TimerWorkItem::create(function, currentTime() + delay);
if (!timerWorkItem)
return;
ref();
insertTimerWorkItem(timerWorkItem.release());
sendMessageToThread(wakupThreadMessage);
}
|