Chromium Code Reviews| Index: base/message_pump_win.cc |
| diff --git a/base/message_pump_win.cc b/base/message_pump_win.cc |
| index 2b2a10e07534afcf7c7dd8de14567bd4c794d5cd..0ed44c1446b70ba180b7599f5ac9e9bb903c6ff3 100644 |
| --- a/base/message_pump_win.cc |
| +++ b/base/message_pump_win.cc |
| @@ -475,11 +475,26 @@ void MessagePumpForIO::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
| void MessagePumpForIO::RegisterIOHandler(HANDLE file_handle, |
| IOHandler* handler) { |
| - ULONG_PTR key = reinterpret_cast<ULONG_PTR>(handler); |
| + ULONG_PTR key = HandlerToKey(handler, true); |
| HANDLE port = CreateIoCompletionPort(file_handle, port_, key, 1); |
| DPCHECK(port); |
| } |
| +bool MessagePumpForIO::RegisterJobObject(HANDLE job_handle, |
| + IOHandler* handler) { |
| + // Job object notifications use the OVERLAPPED pointer to carry the message |
| + // data. Mark the completion key correspondingly, so we will not try to |
| + // convert OVERLAPPED* to IOContext*. |
| + ULONG_PTR key = HandlerToKey(handler, false); |
| + JOBOBJECT_ASSOCIATE_COMPLETION_PORT info; |
| + info.CompletionKey = reinterpret_cast<void*>(key); |
| + info.CompletionPort = port_; |
| + return SetInformationJobObject(job_handle, |
| + JobObjectAssociateCompletionPortInformation, |
| + &info, |
| + sizeof(info)) != FALSE; |
| +} |
| + |
| //----------------------------------------------------------------------------- |
| // MessagePumpForIO private: |
| @@ -546,12 +561,16 @@ bool MessagePumpForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { |
| return true; |
| } |
| - if (item.context->handler) { |
| + // If |item.has_valid_io_context| is false then the context does not point |
|
Wez
2012/08/16 00:06:33
nit: the context -> |item.context|
(Sorry, I ham-
alexeypa (please no reviews)
2012/08/16 00:34:10
Done.
|
| + // to a context structure, and so should not be dereferenced, although it may |
| + // still hold valid non-pointer data. |
| + if (!item.has_valid_io_context || item.context->handler) { |
| if (filter && item.handler != filter) { |
| // Save this item for later |
| completed_io_.push_back(item); |
| } else { |
| - DCHECK_EQ(item.context->handler, item.handler); |
| + DCHECK(!item.has_valid_io_context || |
| + (item.context->handler == item.handler)); |
| WillProcessIOEvent(); |
| item.handler->OnIOCompleted(item.context, item.bytes_transfered, |
| item.error); |
| @@ -577,7 +596,7 @@ bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { |
| item->bytes_transfered = 0; |
| } |
| - item->handler = reinterpret_cast<IOHandler*>(key); |
| + item->handler = KeyToHandler(key, &item->has_valid_io_context); |
| item->context = reinterpret_cast<IOContext*>(overlapped); |
| return true; |
| } |
| @@ -623,4 +642,28 @@ void MessagePumpForIO::DidProcessIOEvent() { |
| FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
| } |
| +// static |
| +ULONG_PTR MessagePumpForIO::HandlerToKey(IOHandler* handler, |
| + bool has_valid_io_context) { |
| + ULONG_PTR key = reinterpret_cast<ULONG_PTR>(handler); |
| + |
| + // |IOHandler| is at least pointer-size aligned, so the lowest two bits are |
| + // always cleared. We use the lowest bit to distinguish completion keys with |
| + // and without the associated |IOContext|. |
| + DCHECK((key & 1) == 0); |
| + |
| + // Mark the completion key as context-less. |
| + if (!has_valid_io_context) |
| + key = key | 1; |
| + return key; |
| +} |
| + |
| +// static |
| +MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
| + ULONG_PTR key, |
| + bool* has_valid_io_context) { |
| + *has_valid_io_context = ((key & 1) == 0); |
| + return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
| +} |
| + |
| } // namespace base |