⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 248288 in webkit


Ignore:
Timestamp:
Aug 5, 2019, 8:45:40 PM (7 years ago)
Author:
youenn@apple.com
Message:

Make Logger::log thread safe so that it can be used from background threads
https://bugs.webkit.org/show_bug.cgi?id=200448

Reviewed by Eric Carlson.

Source/WebCore:

No change of behavior.

  • dom/Document.cpp:

(WebCore::crossThreadCopy):
(WebCore::Document::didLogMessage):
Make sure to hop to the main thread if needed.

  • platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp:

(WebCore::RealtimeIncomingAudioSourceCocoa::OnData):
Remove hopping to the main thread.

  • platform/mediastream/mac/RealtimeIncomingVideoSourceCocoa.mm:

(WebCore::RealtimeIncomingVideoSourceCocoa::OnFrame):
Remove hopping to the main thread.

Source/WTF:

Add a lock to ensure calling log is thread-safe.

  • wtf/Logger.h:

(WTF::Logger::addObserver):
(WTF::Logger::removeObserver):
(WTF::Logger::log):
(WTF::Logger::observerLock):

Location:
trunk/Source
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r248268 r248288  
     12019-08-05  Youenn Fablet  <youenn@apple.com>
     2
     3        Make Logger::log thread safe so that it can be used from background threads
     4        https://bugs.webkit.org/show_bug.cgi?id=200448
     5
     6        Reviewed by Eric Carlson.
     7
     8        Add a lock to ensure calling log is thread-safe.
     9
     10        * wtf/Logger.h:
     11        (WTF::Logger::addObserver):
     12        (WTF::Logger::removeObserver):
     13        (WTF::Logger::log):
     14        (WTF::Logger::observerLock):
     15
    1162019-08-05  Takashi Komori  <Takashi.Komori@sony.com>
    217
  • trunk/Source/WTF/wtf/Logger.h

    r248043 r248288  
    2626#pragma once
    2727
     28#include <wtf/Lock.h>
    2829#include <wtf/text/StringBuilder.h>
    2930
     
    113114    public:
    114115        virtual ~Observer() = default;
     116        // Can be called on any thread.
    115117        virtual void didLogMessage(const WTFLogChannel&, WTFLogLevel, Vector<JSONLogValue>&&) = 0;
    116118    };
     
    217219    static inline void addObserver(Observer& observer)
    218220    {
     221        auto lock = holdLock(observerLock());
    219222        observers().append(observer);
    220223    }
    221224    static inline void removeObserver(Observer& observer)
    222225    {
     226        auto lock = holdLock(observerLock());
    223227        observers().removeFirstMatching([&observer](auto anObserver) {
    224228            return &anObserver.get() == &observer;
     
    248252            return;
    249253
     254        auto lock = tryHoldLock(observerLock());
     255        if (!lock)
     256            return;
     257
    250258        for (Observer& observer : observers())
    251259            observer.didLogMessage(channel, level, { ConsoleLogValue<Argument>::toValue(arguments)... });
     
    258266    }
    259267
     268    static Lock& observerLock()
     269    {
     270        static NeverDestroyed<Lock> observerLock;
     271        return observerLock;
     272    }
     273
     274
    260275    bool m_enabled { true };
    261276    const void* m_owner;
  • trunk/Source/WebCore/ChangeLog

    r248287 r248288  
     12019-08-05  Youenn Fablet  <youenn@apple.com>
     2
     3        Make Logger::log thread safe so that it can be used from background threads
     4        https://bugs.webkit.org/show_bug.cgi?id=200448
     5
     6        Reviewed by Eric Carlson.
     7
     8        No change of behavior.
     9
     10        * dom/Document.cpp:
     11        (WebCore::crossThreadCopy):
     12        (WebCore::Document::didLogMessage):
     13        Make sure to hop to the main thread if needed.
     14        * platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp:
     15        (WebCore::RealtimeIncomingAudioSourceCocoa::OnData):
     16        Remove hopping to the main thread.
     17        * platform/mediastream/mac/RealtimeIncomingVideoSourceCocoa.mm:
     18        (WebCore::RealtimeIncomingVideoSourceCocoa::OnFrame):
     19        Remove hopping to the main thread.
     20
    1212019-08-05  Devin Rousso  <drousso@apple.com>
    222
  • trunk/Source/WebCore/dom/Document.cpp

    r248170 r248288  
    80218021}
    80228022
     8023static inline Vector<JSONLogValue> crossThreadCopy(Vector<JSONLogValue>&& source)
     8024{
     8025    auto values = WTFMove(source);
     8026    for (auto& value : values)
     8027        value.value = crossThreadCopy(WTFMove(value.value));
     8028    return values;
     8029}
     8030
    80238031void Document::didLogMessage(const WTFLogChannel& channel, WTFLogLevel level, Vector<JSONLogValue>&& logMessages)
    80248032{
     8033    if (!isMainThread()) {
     8034        postTask([this, channel, level, logMessages = crossThreadCopy(WTFMove(logMessages))](auto&) mutable {
     8035            didLogMessage(channel, level, WTFMove(logMessages));
     8036        });
     8037        return;
     8038    }
    80258039    if (!page())
    80268040        return;
  • trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp

    r247480 r248288  
    8888
    8989#if !RELEASE_LOG_DISABLED
    90     if (!(++m_chunksReceived % 200)) {
    91         callOnMainThread([this, protectedThis = makeRef(*this)] {
    92             ALWAYS_LOG_IF(loggerPtr(), LOGIDENTIFIER, "chunk ", m_chunksReceived);
    93         });
    94     }
     90    ALWAYS_LOG_IF(loggerPtr() && !(++m_chunksReceived % 200), LOGIDENTIFIER, "chunk ", m_chunksReceived);
    9591#endif
    9692
  • trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingVideoSourceCocoa.mm

    r248190 r248288  
    159159
    160160#if !RELEASE_LOG_DISABLED
    161     if (!(++m_numberOfFrames % 60)) {
    162         callOnMainThread([this, protectedThis = makeRef(*this), numberOfFrames = m_numberOfFrames] {
    163             ALWAYS_LOG_IF(loggerPtr(), LOGIDENTIFIER, "frame ", numberOfFrames);
    164         });
    165     }
     161    ALWAYS_LOG_IF(loggerPtr() && !(++m_numberOfFrames % 60), LOGIDENTIFIER, "frame ", m_numberOfFrames);
    166162#endif
    167163
    168164    auto pixelBuffer = pixelBufferFromVideoFrame(frame);
    169165    if (!pixelBuffer) {
    170         callOnMainThread([this, protectedThis = makeRef(*this)] {
    171             ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, "Failed to get a pixel buffer from a frame");
    172         });
     166        ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, "Failed to get a pixel buffer from a frame");
    173167        return;
    174168    }
     
    184178    OSStatus ostatus = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, (CVImageBufferRef)pixelBuffer, &formatDescription);
    185179    if (ostatus != noErr) {
    186         callOnMainThread([this, protectedThis = makeRef(*this), ostatus] {
    187             ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, "Failed to initialize CMVideoFormatDescription with error ", static_cast<int>(ostatus));
    188         });
     180        ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, "Failed to initialize CMVideoFormatDescription with error ", static_cast<int>(ostatus));
    189181        return;
    190182    }
     
    194186    CFRelease(formatDescription);
    195187    if (ostatus != noErr) {
    196         callOnMainThread([this, protectedThis = makeRef(*this), ostatus] {
    197             ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, "Failed to create the sample buffer with error ", static_cast<int>(ostatus));
    198         });
     188        ERROR_LOG_IF(loggerPtr(), LOGIDENTIFIER, "Failed to create the sample buffer with error ", static_cast<int>(ostatus));
    199189        return;
    200190    }
Note: See TracChangeset for help on using the changeset viewer.