Changeset 177922 in webkit
- Timestamp:
- Jan 5, 2015, 12:39:36 PM (11 years ago)
- Location:
- trunk/Source/WebKit2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebKit2/ChangeLog
r177921 r177922 1 2015-01-05 Anders Carlsson <andersca@apple.com> 2 3 Clean up Connection::SyncMessageState 4 https://bugs.webkit.org/show_bug.cgi?id=140087 5 6 Reviewed by Andreas Kling. 7 8 * Platform/IPC/Connection.cpp: 9 (IPC::Connection::SyncMessageState::syncMessageStateMapMutex): 10 Change this to return an std::mutex and use std::call_once to initialize it properly. 11 12 (IPC::Connection::SyncMessageState::getOrCreate): 13 Return a Ref. 14 15 (IPC::Connection::SyncMessageState::~SyncMessageState): 16 Use an std::lock_guard. 17 18 (IPC::Connection::SyncMessageState::processIncomingMessage): 19 Replace a bind call with a lambda. 20 21 (IPC::Connection::SyncMessageState::dispatchMessages): 22 ConnectionAndIncomingMessage now holds a Ref<Connection>. 23 24 (IPC::Connection::SyncMessageState::dispatchMessageAndResetDidScheduleDispatchMessagesForConnection): 25 Change Connection to a reference. 26 27 (IPC::Connection::processIncomingMessage): 28 Change Connection to a reference. 29 1 30 2015-01-05 Anders Carlsson <andersca@apple.com> 2 31 -
trunk/Source/WebKit2/Platform/IPC/Connection.cpp
r177917 r177922 58 58 class Connection::SyncMessageState : public ThreadSafeRefCounted<Connection::SyncMessageState> { 59 59 public: 60 static PassRefPtr<SyncMessageState> getOrCreate(RunLoop&);60 static Ref<SyncMessageState> getOrCreate(RunLoop&); 61 61 ~SyncMessageState(); 62 62 … … 73 73 // Returns true if this message will be handled on a client thread that is currently 74 74 // waiting for a reply to a synchronous message. 75 bool processIncomingMessage(Connection *, std::unique_ptr<MessageDecoder>&);75 bool processIncomingMessage(Connection&, std::unique_ptr<MessageDecoder>&); 76 76 77 77 // Dispatch pending sync messages. if allowedConnection is not null, will only dispatch messages … … 89 89 } 90 90 91 static Mutex& syncMessageStateMapMutex() 92 { 93 static NeverDestroyed<Mutex> syncMessageStateMapMutex; 91 static std::mutex& syncMessageStateMapMutex() 92 { 93 static LazyNeverDestroyed<std::mutex> syncMessageStateMapMutex; 94 static std::once_flag onceFlag; 95 std::call_once(onceFlag, [] { 96 syncMessageStateMapMutex.construct(); 97 }); 98 94 99 return syncMessageStateMapMutex; 95 100 } 96 101 97 void dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection *);102 void dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection&); 98 103 99 104 RunLoop& m_runLoop; … … 101 106 102 107 // Protects m_didScheduleDispatchMessagesWorkSet and m_messagesToDispatchWhileWaitingForSyncReply. 103 Mutex m_mutex;108 std::mutex m_mutex; 104 109 105 110 // The set of connections for which we've scheduled a call to dispatchMessageAndResetDidScheduleDispatchMessagesForConnection. … … 107 112 108 113 struct ConnectionAndIncomingMessage { 109 Ref Ptr<Connection> connection;114 Ref<Connection> connection; 110 115 std::unique_ptr<MessageDecoder> message; 111 116 }; … … 122 127 123 128 124 PassRefPtr<Connection::SyncMessageState> Connection::SyncMessageState::getOrCreate(RunLoop& runLoop) 125 { 126 MutexLocker locker(syncMessageStateMapMutex()); 127 SyncMessageStateMap::AddResult result = syncMessageStateMap().add(&runLoop, nullptr); 128 129 if (!result.isNewEntry) { 130 ASSERT(result.iterator->value); 131 return result.iterator->value; 132 } 133 134 RefPtr<SyncMessageState> syncMessageState = adoptRef(new SyncMessageState(runLoop)); 135 result.iterator->value = syncMessageState.get(); 136 137 return syncMessageState.release(); 129 Ref<Connection::SyncMessageState> Connection::SyncMessageState::getOrCreate(RunLoop& runLoop) 130 { 131 std::lock_guard<std::mutex> lock(syncMessageStateMapMutex()); 132 133 auto& slot = syncMessageStateMap().add(&runLoop, nullptr).iterator->value; 134 if (slot) 135 return *slot; 136 137 Ref<SyncMessageState> syncMessageState = adoptRef(*new SyncMessageState(runLoop)); 138 slot = syncMessageState.ptr(); 139 140 return syncMessageState; 138 141 } 139 142 … … 145 148 Connection::SyncMessageState::~SyncMessageState() 146 149 { 147 MutexLocker locker(syncMessageStateMapMutex());148 150 std::lock_guard<std::mutex> lock(syncMessageStateMapMutex()); 151 149 152 ASSERT(syncMessageStateMap().contains(&m_runLoop)); 150 153 syncMessageStateMap().remove(&m_runLoop); … … 153 156 } 154 157 155 bool Connection::SyncMessageState::processIncomingMessage(Connection *connection, std::unique_ptr<MessageDecoder>& message)158 bool Connection::SyncMessageState::processIncomingMessage(Connection& connection, std::unique_ptr<MessageDecoder>& message) 156 159 { 157 160 if (!message->shouldDispatchMessageWhenWaitingForSyncReply()) 158 161 return false; 159 162 160 ConnectionAndIncomingMessage connectionAndIncomingMessage; 161 connectionAndIncomingMessage.connection = connection; 162 connectionAndIncomingMessage.message = WTF::move(message); 163 164 { 165 MutexLocker locker(m_mutex); 163 ConnectionAndIncomingMessage connectionAndIncomingMessage { connection, WTF::move(message) }; 164 165 { 166 std::lock_guard<std::mutex> lock(m_mutex); 166 167 167 if (m_didScheduleDispatchMessagesWorkSet.add(connection).isNewEntry) 168 m_runLoop.dispatch(bind(&SyncMessageState::dispatchMessageAndResetDidScheduleDispatchMessagesForConnection, this, RefPtr<Connection>(connection))); 168 if (m_didScheduleDispatchMessagesWorkSet.add(&connection).isNewEntry) { 169 RefPtr<Connection> protectedConnection(&connection); 170 m_runLoop.dispatch([this, protectedConnection] { 171 dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(*protectedConnection); 172 }); 173 } 169 174 170 175 m_messagesToDispatchWhileWaitingForSyncReply.append(WTF::move(connectionAndIncomingMessage)); … … 183 188 184 189 { 185 MutexLocker locker(m_mutex);190 std::lock_guard<std::mutex> lock(m_mutex); 186 191 m_messagesToDispatchWhileWaitingForSyncReply.swap(messagesToDispatchWhileWaitingForSyncReply); 187 192 } … … 192 197 ConnectionAndIncomingMessage& connectionAndIncomingMessage = messagesToDispatchWhileWaitingForSyncReply[i]; 193 198 194 if (allowedConnection && allowedConnection != connectionAndIncomingMessage.connection ) {199 if (allowedConnection && allowedConnection != connectionAndIncomingMessage.connection.ptr()) { 195 200 // This incoming message belongs to another connection and we don't want to dispatch it now 196 201 // so mark it to be put back in the message queue. … … 203 208 204 209 if (!messagesToPutBack.isEmpty()) { 205 MutexLocker locker(m_mutex);210 std::lock_guard<std::mutex> lock(m_mutex); 206 211 207 212 for (auto& message : messagesToPutBack) … … 210 215 } 211 216 212 void Connection::SyncMessageState::dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection *connection)213 { 214 { 215 MutexLocker locker(m_mutex);216 ASSERT(m_didScheduleDispatchMessagesWorkSet.contains( connection));217 m_didScheduleDispatchMessagesWorkSet.remove( connection);218 } 219 220 dispatchMessages( connection);217 void Connection::SyncMessageState::dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection& connection) 218 { 219 { 220 std::lock_guard<std::mutex> lock(m_mutex); 221 ASSERT(m_didScheduleDispatchMessagesWorkSet.contains(&connection)); 222 m_didScheduleDispatchMessagesWorkSet.remove(&connection); 223 } 224 225 dispatchMessages(&connection); 221 226 } 222 227 … … 535 540 while (!timedOut) { 536 541 // First, check if we have any messages that we need to process. 537 m_syncMessageState->dispatchMessages( 0);542 m_syncMessageState->dispatchMessages(nullptr); 538 543 539 544 { … … 653 658 // a sync reply. If it is, and we're waiting for a sync reply this message needs to be dispatched. 654 659 // If we don't we'll end up with a deadlock where both sync message senders are stuck waiting for a reply. 655 if (m_syncMessageState->processIncomingMessage( this, message))660 if (m_syncMessageState->processIncomingMessage(*this, message)) 656 661 return; 657 662
Note:
See TracChangeset
for help on using the changeset viewer.