Diff
Modified: trunk/Source/WebKit2/ChangeLog (139040 => 139041)
--- trunk/Source/WebKit2/ChangeLog 2013-01-08 08:12:49 UTC (rev 139040)
+++ trunk/Source/WebKit2/ChangeLog 2013-01-08 08:15:16 UTC (rev 139041)
@@ -1,3 +1,13 @@
+2013-01-08 Csaba Osztrogonác <[email protected]>
+
+ [Qt][Win] Unreviewed buildfix, partially revert
+ r139003, because Qt port still uses these files.
+
+ * Platform/CoreIPC/win/BinarySemaphoreWin.cpp: Added.
+ * Platform/CoreIPC/win/ConnectionWin.cpp: Added.
+ * Platform/win/SharedMemoryWin.cpp: Added.
+ * Platform/win/WorkQueueWin.cpp: Added.
+
2013-01-07 Christophe Dumez <[email protected]>
[CoordinatedGraphics] compositing/repaint/resize-repaint.html is failing
Added: trunk/Source/WebKit2/Platform/CoreIPC/win/BinarySemaphoreWin.cpp (0 => 139041)
--- trunk/Source/WebKit2/Platform/CoreIPC/win/BinarySemaphoreWin.cpp (rev 0)
+++ trunk/Source/WebKit2/Platform/CoreIPC/win/BinarySemaphoreWin.cpp 2013-01-08 08:15:16 UTC (rev 139041)
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2011 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "BinarySemaphore.h"
+
+namespace CoreIPC {
+
+BinarySemaphore::BinarySemaphore()
+ : m_event(::CreateEventW(0, FALSE, FALSE, 0))
+{
+}
+
+BinarySemaphore::~BinarySemaphore()
+{
+ ::CloseHandle(m_event);
+}
+
+void BinarySemaphore::signal()
+{
+ ::SetEvent(m_event);
+}
+
+bool BinarySemaphore::wait(double absoluteTime)
+{
+ DWORD interval = absoluteTimeToWaitTimeoutInterval(absoluteTime);
+ if (!interval) {
+ // Consider the wait to have timed out, even if the event has already been signaled, to
+ // match the WTF::ThreadCondition implementation.
+ return false;
+ }
+
+ DWORD result = ::WaitForSingleObjectEx(m_event, interval, FALSE);
+ switch (result) {
+ case WAIT_OBJECT_0:
+ // The event was signaled.
+ return true;
+
+ case WAIT_TIMEOUT:
+ // The wait timed out.
+ return false;
+
+ case WAIT_FAILED:
+ ASSERT_WITH_MESSAGE(false, "::WaitForSingleObjectEx failed with error %lu", ::GetLastError());
+ return false;
+ default:
+ ASSERT_WITH_MESSAGE(false, "::WaitForSingleObjectEx returned unexpected result %lu", result);
+ return false;
+ }
+}
+
+} // namespace CoreIPC
Added: trunk/Source/WebKit2/Platform/CoreIPC/win/ConnectionWin.cpp (0 => 139041)
--- trunk/Source/WebKit2/Platform/CoreIPC/win/ConnectionWin.cpp (rev 0)
+++ trunk/Source/WebKit2/Platform/CoreIPC/win/ConnectionWin.cpp 2013-01-08 08:15:16 UTC (rev 139041)
@@ -0,0 +1,362 @@
+/*
+ * Copyright (C) 2010 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "Connection.h"
+
+#include "BinarySemaphore.h"
+#include "DataReference.h"
+#include <wtf/Functional.h>
+#include <wtf/RandomNumber.h>
+#include <wtf/text/WTFString.h>
+
+using namespace std;
+
+namespace CoreIPC {
+
+// FIXME: Rename this or use a different constant on windows.
+static const size_t inlineMessageMaxSize = 4096;
+
+bool Connection::createServerAndClientIdentifiers(HANDLE& serverIdentifier, HANDLE& clientIdentifier)
+{
+ String pipeName;
+
+ while (true) {
+ unsigned uniqueID = randomNumber() * std::numeric_limits<unsigned>::max();
+ pipeName = String::format("\\\\.\\pipe\\com.apple.WebKit.%x", uniqueID);
+
+ serverIdentifier = ::CreateNamedPipe(pipeName.charactersWithNullTermination(),
+ PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,
+ PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, inlineMessageMaxSize, inlineMessageMaxSize,
+ 0, 0);
+ if (!serverIdentifier && ::GetLastError() == ERROR_PIPE_BUSY) {
+ // There was already a pipe with this name, try again.
+ continue;
+ }
+
+ break;
+ }
+
+ if (!serverIdentifier)
+ return false;
+
+ clientIdentifier = ::CreateFileW(pipeName.charactersWithNullTermination(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
+ if (!clientIdentifier) {
+ ::CloseHandle(serverIdentifier);
+ return false;
+ }
+
+ DWORD mode = PIPE_READMODE_MESSAGE;
+ if (!::SetNamedPipeHandleState(clientIdentifier, &mode, 0, 0)) {
+ ::CloseHandle(serverIdentifier);
+ ::CloseHandle(clientIdentifier);
+ return false;
+ }
+
+ return true;
+}
+
+void Connection::platformInitialize(Identifier identifier)
+{
+ memset(&m_readState, 0, sizeof(m_readState));
+ m_readState.hEvent = ::CreateEventW(0, FALSE, FALSE, 0);
+
+ memset(&m_writeState, 0, sizeof(m_writeState));
+ m_writeState.hEvent = ::CreateEventW(0, FALSE, FALSE, 0);
+
+ m_connectionPipe = identifier;
+}
+
+void Connection::platformInvalidate()
+{
+ if (m_connectionPipe == INVALID_HANDLE_VALUE)
+ return;
+
+ m_isConnected = false;
+
+ m_connectionQueue.unregisterAndCloseHandle(m_readState.hEvent);
+ m_readState.hEvent = 0;
+
+ m_connectionQueue.unregisterAndCloseHandle(m_writeState.hEvent);
+ m_writeState.hEvent = 0;
+
+ ::CloseHandle(m_connectionPipe);
+ m_connectionPipe = INVALID_HANDLE_VALUE;
+}
+
+void Connection::readEventHandler()
+{
+ if (m_connectionPipe == INVALID_HANDLE_VALUE)
+ return;
+
+ while (true) {
+ // Check if we got some data.
+ DWORD numberOfBytesRead = 0;
+ if (!::GetOverlappedResult(m_connectionPipe, &m_readState, &numberOfBytesRead, FALSE)) {
+ DWORD error = ::GetLastError();
+
+ switch (error) {
+ case ERROR_BROKEN_PIPE:
+ connectionDidClose();
+ return;
+ case ERROR_MORE_DATA: {
+ // Read the rest of the message out of the pipe.
+
+ DWORD bytesToRead = 0;
+ if (!::PeekNamedPipe(m_connectionPipe, 0, 0, 0, 0, &bytesToRead)) {
+ DWORD error = ::GetLastError();
+ if (error == ERROR_BROKEN_PIPE) {
+ connectionDidClose();
+ return;
+ }
+ ASSERT_NOT_REACHED();
+ return;
+ }
+
+ // ::GetOverlappedResult told us there's more data. ::PeekNamedPipe shouldn't
+ // contradict it!
+ ASSERT(bytesToRead);
+ if (!bytesToRead)
+ break;
+
+ m_readBuffer.grow(m_readBuffer.size() + bytesToRead);
+ if (!::ReadFile(m_connectionPipe, m_readBuffer.data() + numberOfBytesRead, bytesToRead, 0, &m_readState)) {
+ DWORD error = ::GetLastError();
+ ASSERT_NOT_REACHED();
+ return;
+ }
+ continue;
+ }
+
+ // FIXME: We should figure out why we're getting this error.
+ case ERROR_IO_INCOMPLETE:
+ return;
+ default:
+ ASSERT_NOT_REACHED();
+ }
+ }
+
+ if (!m_readBuffer.isEmpty()) {
+ // We have a message, let's dispatch it.
+
+ // The messageID is encoded at the end of the buffer.
+ // Note that we assume here that the message is the same size as m_readBuffer. We can
+ // assume this because we always size m_readBuffer to exactly match the size of the message,
+ // either when receiving ERROR_MORE_DATA from ::GetOverlappedResult above or when
+ // ::PeekNamedPipe tells us the size below. We never set m_readBuffer to a size larger
+ // than the message.
+ ASSERT(m_readBuffer.size() >= sizeof(MessageID));
+ size_t realBufferSize = m_readBuffer.size() - sizeof(MessageID);
+
+ unsigned messageID = *reinterpret_cast<unsigned*>(m_readBuffer.data() + realBufferSize);
+
+ OwnPtr<MessageDecoder> decoder = MessageDecoder::create(DataReference(m_readBuffer.data(), realBufferSize));
+ processIncomingMessage(MessageID::fromInt(messageID), decoder.release());
+ }
+
+ // Find out the size of the next message in the pipe (if there is one) so that we can read
+ // it all in one operation. (This is just an optimization to avoid an extra pass through the
+ // loop (if we chose a buffer size that was too small) or allocating extra memory (if we
+ // chose a buffer size that was too large).)
+ DWORD bytesToRead = 0;
+ if (!::PeekNamedPipe(m_connectionPipe, 0, 0, 0, 0, &bytesToRead)) {
+ DWORD error = ::GetLastError();
+ if (error == ERROR_BROKEN_PIPE) {
+ connectionDidClose();
+ return;
+ }
+ ASSERT_NOT_REACHED();
+ }
+ if (!bytesToRead) {
+ // There's no message waiting in the pipe. Schedule a read of the first byte of the
+ // next message. We'll find out the message's actual size when it arrives. (If we
+ // change this to read more than a single byte for performance reasons, we'll have to
+ // deal with m_readBuffer potentially being larger than the message we read after
+ // calling ::GetOverlappedResult above.)
+ bytesToRead = 1;
+ }
+
+ m_readBuffer.resize(bytesToRead);
+
+ // Either read the next available message (which should occur synchronously), or start an
+ // asynchronous read of the next message that becomes available.
+ BOOL result = ::ReadFile(m_connectionPipe, m_readBuffer.data(), m_readBuffer.size(), 0, &m_readState);
+ if (result) {
+ // There was already a message waiting in the pipe, and we read it synchronously.
+ // Process it.
+ continue;
+ }
+
+ DWORD error = ::GetLastError();
+
+ if (error == ERROR_IO_PENDING) {
+ // There are no messages in the pipe currently. readEventHandler will be called again once there is a message.
+ return;
+ }
+
+ if (error == ERROR_MORE_DATA) {
+ // Either a message is available when we didn't think one was, or the message is larger
+ // than ::PeekNamedPipe told us. The former seems far more likely. Probably the message
+ // became available between our calls to ::PeekNamedPipe and ::ReadFile above. Go back
+ // to the top of the loop to use ::GetOverlappedResult to retrieve the available data.
+ continue;
+ }
+
+ // FIXME: We need to handle other errors here.
+ ASSERT_NOT_REACHED();
+ }
+}
+
+void Connection::writeEventHandler()
+{
+ if (m_connectionPipe == INVALID_HANDLE_VALUE)
+ return;
+
+ DWORD numberOfBytesWritten = 0;
+ if (!::GetOverlappedResult(m_connectionPipe, &m_writeState, &numberOfBytesWritten, FALSE)) {
+ DWORD error = ::GetLastError();
+ if (error == ERROR_IO_INCOMPLETE) {
+ // FIXME: We should figure out why we're getting this error.
+ return;
+ }
+ if (error == ERROR_BROKEN_PIPE) {
+ connectionDidClose();
+ return;
+ }
+ ASSERT_NOT_REACHED();
+ }
+
+ // The pending write has finished, so we are now done with its encoder. Clearing this member
+ // will allow us to send messages again.
+ m_pendingWriteEncoder = nullptr;
+
+ // Now that the pending write has finished, we can try to send a new message.
+ sendOutgoingMessages();
+}
+
+bool Connection::open()
+{
+ // We connected the two ends of the pipe in createServerAndClientIdentifiers.
+ m_isConnected = true;
+
+ // Start listening for read and write state events.
+ m_connectionQueue.registerHandle(m_readState.hEvent, bind(&Connection::readEventHandler, this));
+ m_connectionQueue.registerHandle(m_writeState.hEvent, bind(&Connection::writeEventHandler, this));
+
+ // Schedule a read.
+ m_connectionQueue.dispatch(bind(&Connection::readEventHandler, this));
+
+ return true;
+}
+
+bool Connection::platformCanSendOutgoingMessages() const
+{
+ // We only allow sending one asynchronous message at a time. If we wanted to send more than one
+ // at once, we'd have to use multiple OVERLAPPED structures and hold onto multiple pending
+ // MessageEncoders (one of each for each simultaneous asynchronous message).
+ return !m_pendingWriteEncoder;
+}
+
+bool Connection::sendOutgoingMessage(MessageID messageID, PassOwnPtr<MessageEncoder> encoder)
+{
+ ASSERT(!m_pendingWriteEncoder);
+
+ // Just bail if the handle has been closed.
+ if (m_connectionPipe == INVALID_HANDLE_VALUE)
+ return false;
+
+ // We put the message ID last.
+ encoder->encode(static_cast<uint32_t>(messageID.toInt()));
+
+ // Write the outgoing message.
+
+ if (::WriteFile(m_connectionPipe, encoder->buffer(), encoder->bufferSize(), 0, &m_writeState)) {
+ // We successfully sent this message.
+ return true;
+ }
+
+ DWORD error = ::GetLastError();
+
+ if (error == ERROR_NO_DATA) {
+ // The pipe is being closed.
+ connectionDidClose();
+ return false;
+ }
+
+ if (error != ERROR_IO_PENDING) {
+ ASSERT_NOT_REACHED();
+ return false;
+ }
+
+ // The message will be sent soon. Hold onto the encoder so that it won't be destroyed
+ // before the write completes.
+ m_pendingWriteEncoder = encoder;
+
+ // We can only send one asynchronous message at a time (see comment in platformCanSendOutgoingMessages).
+ return false;
+}
+
+bool Connection::dispatchSentMessagesUntil(const Vector<HWND>& windows, CoreIPC::BinarySemaphore& semaphore, double absoluteTime)
+{
+ if (windows.isEmpty())
+ return semaphore.wait(absoluteTime);
+
+ HANDLE handle = semaphore.event();
+ DWORD handleCount = 1;
+
+ while (true) {
+ DWORD interval = absoluteTimeToWaitTimeoutInterval(absoluteTime);
+ if (!interval) {
+ // Consider the wait to have timed out, even if the semaphore is currently signaled.
+ // This matches the WTF::ThreadCondition implementation of BinarySemaphore::wait.
+ return false;
+ }
+
+ DWORD result = ::MsgWaitForMultipleObjectsEx(handleCount, &handle, interval, QS_SENDMESSAGE, 0);
+ if (result == WAIT_OBJECT_0) {
+ // The semaphore was signaled.
+ return true;
+ }
+ if (result == WAIT_TIMEOUT) {
+ // absoluteTime was reached.
+ return false;
+ }
+ if (result == WAIT_OBJECT_0 + handleCount) {
+ // One or more sent messages are available. Process sent messages for all the windows
+ // we were given, since we don't have a way of knowing which window has available sent
+ // messages.
+ for (size_t i = 0; i < windows.size(); ++i) {
+ MSG message;
+ ::PeekMessageW(&message, windows[i], 0, 0, PM_NOREMOVE | PM_QS_SENDMESSAGE);
+ }
+ continue;
+ }
+ ASSERT_WITH_MESSAGE(result != WAIT_FAILED, "::MsgWaitForMultipleObjectsEx failed with error %lu", ::GetLastError());
+ ASSERT_WITH_MESSAGE(false, "::MsgWaitForMultipleObjectsEx returned unexpected result %lu", result);
+ return false;
+ }
+}
+
+} // namespace CoreIPC
Added: trunk/Source/WebKit2/Platform/win/SharedMemoryWin.cpp (0 => 139041)
--- trunk/Source/WebKit2/Platform/win/SharedMemoryWin.cpp (rev 0)
+++ trunk/Source/WebKit2/Platform/win/SharedMemoryWin.cpp 2013-01-08 08:15:16 UTC (rev 139041)
@@ -0,0 +1,241 @@
+/*
+ * Copyright (C) 2010 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "SharedMemory.h"
+
+#include "ArgumentDecoder.h"
+#include "ArgumentEncoder.h"
+#include <wtf/RefPtr.h>
+
+namespace WebKit {
+
+SharedMemory::Handle::Handle()
+ : m_handle(0)
+ , m_size(0)
+{
+}
+
+SharedMemory::Handle::~Handle()
+{
+ if (!m_handle)
+ return;
+
+ ::CloseHandle(m_handle);
+}
+
+bool SharedMemory::Handle::isNull() const
+{
+ return !m_handle;
+}
+
+void SharedMemory::Handle::encode(CoreIPC::ArgumentEncoder& encoder) const
+{
+ encoder.encode(static_cast<uint64_t>(m_size));
+
+ // Hand off ownership of our HANDLE to the receiving process. It will close it for us.
+ // FIXME: If the receiving process crashes before it receives the memory, the memory will be
+ // leaked. See <http://webkit.org/b/47502>.
+ encoder.encode(reinterpret_cast<uint64_t>(m_handle));
+ m_handle = 0;
+
+ // Send along our PID so that the receiving process can duplicate the HANDLE for its own use.
+ encoder.encode(static_cast<uint32_t>(::GetCurrentProcessId()));
+}
+
+static bool getDuplicatedHandle(HANDLE sourceHandle, DWORD sourcePID, HANDLE& duplicatedHandle)
+{
+ duplicatedHandle = 0;
+ if (!sourceHandle)
+ return true;
+
+ HANDLE sourceProcess = ::OpenProcess(PROCESS_DUP_HANDLE, FALSE, sourcePID);
+ if (!sourceProcess)
+ return false;
+
+ // Copy the handle into our process and close the handle that the sending process created for us.
+ BOOL success = ::DuplicateHandle(sourceProcess, sourceHandle, ::GetCurrentProcess(), &duplicatedHandle, 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
+ ASSERT_WITH_MESSAGE(success, "::DuplicateHandle failed with error %lu", ::GetLastError());
+
+ ::CloseHandle(sourceProcess);
+
+ return success;
+}
+
+bool SharedMemory::Handle::decode(CoreIPC::ArgumentDecoder* decoder, Handle& handle)
+{
+ ASSERT_ARG(handle, !handle.m_handle);
+ ASSERT_ARG(handle, !handle.m_size);
+
+ uint64_t size;
+ if (!decoder->decodeUInt64(size))
+ return false;
+
+ uint64_t sourceHandle;
+ if (!decoder->decodeUInt64(sourceHandle))
+ return false;
+
+ uint32_t sourcePID;
+ if (!decoder->decodeUInt32(sourcePID))
+ return false;
+
+ HANDLE duplicatedHandle;
+ if (!getDuplicatedHandle(reinterpret_cast<HANDLE>(sourceHandle), sourcePID, duplicatedHandle))
+ return false;
+
+ handle.m_handle = duplicatedHandle;
+ handle.m_size = size;
+ return true;
+}
+
+PassRefPtr<SharedMemory> SharedMemory::create(size_t size)
+{
+ HANDLE handle = ::CreateFileMappingW(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, 0);
+ if (!handle)
+ return 0;
+
+ void* baseAddress = ::MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
+ if (!baseAddress) {
+ ::CloseHandle(handle);
+ return 0;
+ }
+
+ RefPtr<SharedMemory> memory = adoptRef(new SharedMemory);
+ memory->m_size = size;
+ memory->m_data = baseAddress;
+ memory->m_handle = handle;
+
+ return memory.release();
+}
+
+static DWORD accessRights(SharedMemory::Protection protection)
+{
+ switch (protection) {
+ case SharedMemory::ReadOnly:
+ return FILE_MAP_READ;
+ case SharedMemory::ReadWrite:
+ return FILE_MAP_READ | FILE_MAP_WRITE;
+ }
+
+ ASSERT_NOT_REACHED();
+ return 0;
+}
+
+PassRefPtr<SharedMemory> SharedMemory::create(const Handle& handle, Protection protection)
+{
+ RefPtr<SharedMemory> memory = adopt(handle.m_handle, handle.m_size, protection);
+ if (!memory)
+ return 0;
+
+ // The SharedMemory object now owns the HANDLE.
+ handle.m_handle = 0;
+
+ return memory.release();
+}
+
+PassRefPtr<SharedMemory> SharedMemory::adopt(HANDLE handle, size_t size, Protection protection)
+{
+ if (!handle)
+ return 0;
+
+ DWORD desiredAccess = accessRights(protection);
+
+ void* baseAddress = ::MapViewOfFile(handle, desiredAccess, 0, 0, size);
+ ASSERT_WITH_MESSAGE(baseAddress, "::MapViewOfFile failed with error %lu", ::GetLastError());
+ if (!baseAddress)
+ return 0;
+
+ RefPtr<SharedMemory> memory = adoptRef(new SharedMemory);
+ memory->m_size = size;
+ memory->m_data = baseAddress;
+ memory->m_handle = handle;
+
+ return memory.release();
+}
+
+SharedMemory::~SharedMemory()
+{
+ ASSERT(m_data);
+ ASSERT(m_handle);
+
+ ::UnmapViewOfFile(m_data);
+ ::CloseHandle(m_handle);
+}
+
+bool SharedMemory::createHandle(Handle& handle, Protection protection)
+{
+ ASSERT_ARG(handle, !handle.m_handle);
+ ASSERT_ARG(handle, !handle.m_size);
+
+ HANDLE processHandle = ::GetCurrentProcess();
+
+ HANDLE duplicatedHandle;
+ if (!::DuplicateHandle(processHandle, m_handle, processHandle, &duplicatedHandle, accessRights(protection), FALSE, 0))
+ return false;
+
+ handle.m_handle = duplicatedHandle;
+ handle.m_size = m_size;
+ return true;
+}
+
+PassRefPtr<SharedMemory> SharedMemory::createCopyOnWriteCopy(size_t size) const
+{
+ ASSERT_ARG(size, size <= this->size());
+
+ HANDLE duplicatedHandle;
+ BOOL result = ::DuplicateHandle(::GetCurrentProcess(), m_handle, ::GetCurrentProcess(), &duplicatedHandle, 0, FALSE, DUPLICATE_SAME_ACCESS);
+ ASSERT_WITH_MESSAGE(result, "::DuplicateHandle failed with error %lu", ::GetLastError());
+ if (!result)
+ return 0;
+
+ void* newMapping = ::MapViewOfFile(duplicatedHandle, FILE_MAP_COPY, 0, 0, size);
+ ASSERT_WITH_MESSAGE(newMapping, "::MapViewOfFile failed with error %lu", ::GetLastError());
+ if (!newMapping) {
+ ::CloseHandle(duplicatedHandle);
+ return 0;
+ }
+
+ RefPtr<SharedMemory> memory = adoptRef(new SharedMemory);
+ memory->m_size = size;
+ memory->m_data = newMapping;
+ memory->m_handle = duplicatedHandle;
+
+ return memory.release();
+}
+
+unsigned SharedMemory::systemPageSize()
+{
+ static unsigned pageSize = 0;
+
+ if (!pageSize) {
+ SYSTEM_INFO systemInfo;
+ ::GetSystemInfo(&systemInfo);
+ pageSize = systemInfo.dwPageSize;
+ }
+
+ return pageSize;
+}
+
+} // namespace WebKit
Added: trunk/Source/WebKit2/Platform/win/WorkQueueWin.cpp (0 => 139041)
--- trunk/Source/WebKit2/Platform/win/WorkQueueWin.cpp (rev 0)
+++ trunk/Source/WebKit2/Platform/win/WorkQueueWin.cpp 2013-01-08 08:15:16 UTC (rev 139041)
@@ -0,0 +1,290 @@
+/*
+ * Copyright (C) 2010 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "WorkQueue.h"
+
+#include <wtf/Threading.h>
+
+inline WorkQueue::WorkItemWin::WorkItemWin(const Function<void()>& function, WorkQueue* queue)
+ : m_function(function)
+ , m_queue(queue)
+{
+}
+
+PassRefPtr<WorkQueue::WorkItemWin> WorkQueue::WorkItemWin::create(const Function<void()>& function, WorkQueue* queue)
+{
+ return adoptRef(new WorkItemWin(function, queue));
+}
+
+WorkQueue::WorkItemWin::~WorkItemWin()
+{
+}
+
+inline WorkQueue::HandleWorkItem::HandleWorkItem(HANDLE handle, const Function<void()>& function, WorkQueue* queue)
+ : WorkItemWin(function, queue)
+ , m_handle(handle)
+ , m_waitHandle(0)
+{
+ ASSERT_ARG(handle, handle);
+}
+
+PassRefPtr<WorkQueue::HandleWorkItem> WorkQueue::HandleWorkItem::createByAdoptingHandle(HANDLE handle, const Function<void()>& function, WorkQueue* queue)
+{
+ return adoptRef(new HandleWorkItem(handle, function, queue));
+}
+
+WorkQueue::HandleWorkItem::~HandleWorkItem()
+{
+ ::CloseHandle(m_handle);
+}
+
+void WorkQueue::handleCallback(void* context, BOOLEAN timerOrWaitFired)
+{
+ ASSERT_ARG(context, context);
+ ASSERT_ARG(timerOrWaitFired, !timerOrWaitFired);
+
+ WorkItemWin* item = static_cast<WorkItemWin*>(context);
+ WorkQueue* queue = item->queue();
+
+ {
+ MutexLocker lock(queue->m_workItemQueueLock);
+ queue->m_workItemQueue.append(item);
+
+ // If no other thread is performing work, we can do it on this thread.
+ if (!queue->tryRegisterAsWorkThread()) {
+ // Some other thread is performing work. Since we hold the queue lock, we can be sure
+ // that the work thread is not exiting due to an empty queue and will process the work
+ // item we just added to it. If we weren't holding the lock we'd have to signal
+ // m_performWorkEvent to make sure the work item got picked up.
+ return;
+ }
+ }
+
+ queue->performWorkOnRegisteredWorkThread();
+}
+
+void WorkQueue::registerHandle(HANDLE handle, const Function<void()>& function)
+{
+ RefPtr<HandleWorkItem> handleItem = HandleWorkItem::createByAdoptingHandle(handle, function, this);
+
+ {
+ MutexLocker lock(m_handlesLock);
+ ASSERT_ARG(handle, !m_handles.contains(handle));
+ m_handles.set(handle, handleItem);
+ }
+
+ HANDLE waitHandle;
+ if (!::RegisterWaitForSingleObject(&waitHandle, handle, handleCallback, handleItem.get(), INFINITE, WT_EXECUTEDEFAULT)) {
+ DWORD error = ::GetLastError();
+ ASSERT_NOT_REACHED();
+ }
+ handleItem->setWaitHandle(waitHandle);
+}
+
+void WorkQueue::unregisterAndCloseHandle(HANDLE handle)
+{
+ RefPtr<HandleWorkItem> item;
+ {
+ MutexLocker locker(m_handlesLock);
+ ASSERT_ARG(handle, m_handles.contains(handle));
+ item = m_handles.take(handle);
+ }
+
+ unregisterWaitAndDestroyItemSoon(item.release());
+}
+
+DWORD WorkQueue::workThreadCallback(void* context)
+{
+ ASSERT_ARG(context, context);
+
+ WorkQueue* queue = static_cast<WorkQueue*>(context);
+
+ if (!queue->tryRegisterAsWorkThread())
+ return 0;
+
+ queue->performWorkOnRegisteredWorkThread();
+ return 0;
+}
+
+void WorkQueue::performWorkOnRegisteredWorkThread()
+{
+ ASSERT(m_isWorkThreadRegistered);
+
+ bool isValid = true;
+
+ m_workItemQueueLock.lock();
+
+ while (isValid && !m_workItemQueue.isEmpty()) {
+ Vector<RefPtr<WorkItemWin> > workItemQueue;
+ m_workItemQueue.swap(workItemQueue);
+
+ // Allow more work to be scheduled while we're not using the queue directly.
+ m_workItemQueueLock.unlock();
+ for (size_t i = 0; i < workItemQueue.size(); ++i) {
+ MutexLocker locker(m_isValidMutex);
+ isValid = m_isValid;
+ if (!isValid)
+ break;
+ workItemQueue[i]->function()();
+ }
+ m_workItemQueueLock.lock();
+ }
+
+ // One invariant we maintain is that any work scheduled while a work thread is registered will
+ // be handled by that work thread. Unregister as the work thread while the queue lock is still
+ // held so that no work can be scheduled while we're still registered.
+ unregisterAsWorkThread();
+
+ m_workItemQueueLock.unlock();
+}
+
+void WorkQueue::platformInitialize(const char* name)
+{
+ m_isWorkThreadRegistered = 0;
+ m_timerQueue = ::CreateTimerQueue();
+ ASSERT_WITH_MESSAGE(m_timerQueue, "::CreateTimerQueue failed with error %lu", ::GetLastError());
+}
+
+bool WorkQueue::tryRegisterAsWorkThread()
+{
+ LONG result = ::InterlockedCompareExchange(&m_isWorkThreadRegistered, 1, 0);
+ ASSERT(!result || result == 1);
+ return !result;
+}
+
+void WorkQueue::unregisterAsWorkThread()
+{
+ LONG result = ::InterlockedCompareExchange(&m_isWorkThreadRegistered, 0, 1);
+ ASSERT_UNUSED(result, result == 1);
+}
+
+void WorkQueue::platformInvalidate()
+{
+#if !ASSERT_DISABLED
+ MutexLocker lock(m_handlesLock);
+ ASSERT(m_handles.isEmpty());
+#endif
+
+ // FIXME: We need to ensure that any timer-queue timers that fire after this point don't try to
+ // access this WorkQueue <http://webkit.org/b/44690>.
+ ::DeleteTimerQueueEx(m_timerQueue, 0);
+}
+
+void WorkQueue::dispatch(const Function<void()>& function)
+{
+ MutexLocker locker(m_workItemQueueLock);
+
+ m_workItemQueue.append(WorkItemWin::create(function, this));
+
+ // Spawn a work thread to perform the work we just added. As an optimization, we avoid
+ // spawning the thread if a work thread is already registered. This prevents multiple work
+ // threads from being spawned in most cases. (Note that when a work thread has been spawned but
+ // hasn't registered itself yet, m_isWorkThreadRegistered will be false and we'll end up
+ // spawning a second work thread here. But work thread registration process will ensure that
+ // only one thread actually ends up performing work.)
+ if (!m_isWorkThreadRegistered)
+ ::QueueUserWorkItem(workThreadCallback, this, WT_EXECUTEDEFAULT);
+}
+
+struct TimerContext : public ThreadSafeRefCounted<TimerContext> {
+ static PassRefPtr<TimerContext> create() { return adoptRef(new TimerContext); }
+
+ WorkQueue* queue;
+ Function<void()> function;
+ Mutex timerMutex;
+ HANDLE timer;
+
+private:
+ TimerContext() : queue(0), timer(0) { }
+};
+
+void WorkQueue::timerCallback(void* context, BOOLEAN timerOrWaitFired)
+{
+ ASSERT_ARG(context, context);
+ ASSERT_UNUSED(timerOrWaitFired, timerOrWaitFired);
+
+ // Balanced by leakRef in scheduleWorkAfterDelay.
+ RefPtr<TimerContext> timerContext = adoptRef(static_cast<TimerContext*>(context));
+
+ timerContext->queue->dispatch(timerContext->function);
+
+ MutexLocker lock(timerContext->timerMutex);
+ ASSERT(timerContext->timer);
+ ASSERT(timerContext->queue->m_timerQueue);
+ if (!::DeleteTimerQueueTimer(timerContext->queue->m_timerQueue, timerContext->timer, 0))
+ ASSERT_WITH_MESSAGE(false, "::DeleteTimerQueueTimer failed with error %lu", ::GetLastError());
+}
+
+void WorkQueue::dispatchAfterDelay(const Function<void()>& function, double delay)
+{
+ ASSERT(m_timerQueue);
+
+ RefPtr<TimerContext> context = TimerContext::create();
+ context->queue = this;
+ context->function = function;
+
+ {
+ // The timer callback could fire before ::CreateTimerQueueTimer even returns, so we protect
+ // context->timer with a mutex to ensure the timer callback doesn't access it before the
+ // timer handle has been stored in it.
+ MutexLocker lock(context->timerMutex);
+
+ // Since our timer callback is quick, we can execute in the timer thread itself and avoid
+ // an extra thread switch over to a worker thread.
+ if (!::CreateTimerQueueTimer(&context->timer, m_timerQueue, timerCallback, context.get(), delay * 1000, 0, WT_EXECUTEINTIMERTHREAD)) {
+ ASSERT_WITH_MESSAGE(false, "::CreateTimerQueueTimer failed with error %lu", ::GetLastError());
+ return;
+ }
+ }
+
+ // The timer callback will handle destroying context.
+ context.release().leakRef();
+}
+
+void WorkQueue::unregisterWaitAndDestroyItemSoon(PassRefPtr<HandleWorkItem> item)
+{
+ // We're going to make a blocking call to ::UnregisterWaitEx before closing the handle. (The
+ // blocking version of ::UnregisterWaitEx is much simpler than the non-blocking version.) If we
+ // do this on the current thread, we'll deadlock if we're currently in a callback function for
+ // the wait we're unregistering. So instead we do it asynchronously on some other worker thread.
+
+ ::QueueUserWorkItem(unregisterWaitAndDestroyItemCallback, item.leakRef(), WT_EXECUTEDEFAULT);
+}
+
+DWORD WINAPI WorkQueue::unregisterWaitAndDestroyItemCallback(void* context)
+{
+ ASSERT_ARG(context, context);
+ RefPtr<HandleWorkItem> item = adoptRef(static_cast<HandleWorkItem*>(context));
+
+ // Now that we know we're not in a callback function for the wait we're unregistering, we can
+ // make a blocking call to ::UnregisterWaitEx.
+ if (!::UnregisterWaitEx(item->waitHandle(), INVALID_HANDLE_VALUE)) {
+ DWORD error = ::GetLastError();
+ ASSERT_NOT_REACHED();
+ }
+
+ return 0;
+}