Changeset 177922 in webkit


Ignore:
Timestamp:
Jan 5, 2015, 12:39:36 PM (11 years ago)
Author:
andersca@apple.com
Message:

Clean up Connection::SyncMessageState
https://bugs.webkit.org/show_bug.cgi?id=140087

Reviewed by Andreas Kling.

  • Platform/IPC/Connection.cpp:

(IPC::Connection::SyncMessageState::syncMessageStateMapMutex):
Change this to return an std::mutex and use std::call_once to initialize it properly.

(IPC::Connection::SyncMessageState::getOrCreate):
Return a Ref.

(IPC::Connection::SyncMessageState::~SyncMessageState):
Use an std::lock_guard.

(IPC::Connection::SyncMessageState::processIncomingMessage):
Replace a bind call with a lambda.

(IPC::Connection::SyncMessageState::dispatchMessages):
ConnectionAndIncomingMessage now holds a Ref<Connection>.

(IPC::Connection::SyncMessageState::dispatchMessageAndResetDidScheduleDispatchMessagesForConnection):
Change Connection to a reference.

(IPC::Connection::processIncomingMessage):
Change Connection to a reference.

Location:
trunk/Source/WebKit2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r177921 r177922  
     12015-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
    1302015-01-05  Anders Carlsson  <andersca@apple.com>
    231
  • trunk/Source/WebKit2/Platform/IPC/Connection.cpp

    r177917 r177922  
    5858class Connection::SyncMessageState : public ThreadSafeRefCounted<Connection::SyncMessageState> {
    5959public:
    60     static PassRefPtr<SyncMessageState> getOrCreate(RunLoop&);
     60    static Ref<SyncMessageState> getOrCreate(RunLoop&);
    6161    ~SyncMessageState();
    6262
     
    7373    // Returns true if this message will be handled on a client thread that is currently
    7474    // waiting for a reply to a synchronous message.
    75     bool processIncomingMessage(Connection*, std::unique_ptr<MessageDecoder>&);
     75    bool processIncomingMessage(Connection&, std::unique_ptr<MessageDecoder>&);
    7676
    7777    // Dispatch pending sync messages. if allowedConnection is not null, will only dispatch messages
     
    8989    }
    9090
    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
    9499        return syncMessageStateMapMutex;
    95100    }
    96101
    97     void dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection*);
     102    void dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection&);
    98103
    99104    RunLoop& m_runLoop;
     
    101106
    102107    // Protects m_didScheduleDispatchMessagesWorkSet and m_messagesToDispatchWhileWaitingForSyncReply.
    103     Mutex m_mutex;
     108    std::mutex m_mutex;
    104109
    105110    // The set of connections for which we've scheduled a call to dispatchMessageAndResetDidScheduleDispatchMessagesForConnection.
     
    107112
    108113    struct ConnectionAndIncomingMessage {
    109         RefPtr<Connection> connection;
     114        Ref<Connection> connection;
    110115        std::unique_ptr<MessageDecoder> message;
    111116    };
     
    122127
    123128
    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();
     129Ref<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;
    138141}
    139142
     
    145148Connection::SyncMessageState::~SyncMessageState()
    146149{
    147     MutexLocker locker(syncMessageStateMapMutex());
    148    
     150    std::lock_guard<std::mutex> lock(syncMessageStateMapMutex());
     151
    149152    ASSERT(syncMessageStateMap().contains(&m_runLoop));
    150153    syncMessageStateMap().remove(&m_runLoop);
     
    153156}
    154157
    155 bool Connection::SyncMessageState::processIncomingMessage(Connection* connection, std::unique_ptr<MessageDecoder>& message)
     158bool Connection::SyncMessageState::processIncomingMessage(Connection& connection, std::unique_ptr<MessageDecoder>& message)
    156159{
    157160    if (!message->shouldDispatchMessageWhenWaitingForSyncReply())
    158161        return false;
    159162
    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);
    166167       
    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        }
    169174
    170175        m_messagesToDispatchWhileWaitingForSyncReply.append(WTF::move(connectionAndIncomingMessage));
     
    183188
    184189    {
    185         MutexLocker locker(m_mutex);
     190        std::lock_guard<std::mutex> lock(m_mutex);
    186191        m_messagesToDispatchWhileWaitingForSyncReply.swap(messagesToDispatchWhileWaitingForSyncReply);
    187192    }
     
    192197        ConnectionAndIncomingMessage& connectionAndIncomingMessage = messagesToDispatchWhileWaitingForSyncReply[i];
    193198
    194         if (allowedConnection && allowedConnection != connectionAndIncomingMessage.connection) {
     199        if (allowedConnection && allowedConnection != connectionAndIncomingMessage.connection.ptr()) {
    195200            // This incoming message belongs to another connection and we don't want to dispatch it now
    196201            // so mark it to be put back in the message queue.
     
    203208
    204209    if (!messagesToPutBack.isEmpty()) {
    205         MutexLocker locker(m_mutex);
     210        std::lock_guard<std::mutex> lock(m_mutex);
    206211
    207212        for (auto& message : messagesToPutBack)
     
    210215}
    211216
    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);
     217void 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);
    221226}
    222227
     
    535540    while (!timedOut) {
    536541        // First, check if we have any messages that we need to process.
    537         m_syncMessageState->dispatchMessages(0);
     542        m_syncMessageState->dispatchMessages(nullptr);
    538543       
    539544        {
     
    653658    // a sync reply. If it is, and we're waiting for a sync reply this message needs to be dispatched.
    654659    // 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))
    656661        return;
    657662
Note: See TracChangeset for help on using the changeset viewer.