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

Changeset 295740 in webkit


Ignore:
Timestamp:
Jun 22, 2022, 11:35:44 AM (4 years ago)
Author:
achristensen@apple.com
Message:

Make sure return value of dispatch_source_create is memory managed correctly
https://bugs.webkit.org/show_bug.cgi?id=241836
rdar://95625990

Reviewed by Chris Dumez.

  • Source/WTF/wtf/cocoa/MemoryPressureHandlerCocoa.mm:

(WTF::memoryPressureEventSource):
(WTF::timerEventSource):
(WTF::MemoryPressureHandler::install):
(WTF::MemoryPressureHandler::uninstall):
(WTF::MemoryPressureHandler::holdOff):

  • Source/WTF/wtf/threads/Signals.cpp:

(WTF::startMachExceptionHandlerThread):

  • Source/WebKit/Shared/EntryPointUtilities/Cocoa/XPCService/XPCServiceEntryPoint.mm:

(WebKit::setOSTransaction):

Canonical link: https://commits.webkit.org/251745@main

Location:
trunk/Source
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/wtf/cocoa/MemoryPressureHandlerCocoa.mm

    r285648 r295740  
    5050}
    5151
    52 static dispatch_source_t memoryPressureEventSource = nullptr;
    53 static dispatch_source_t timerEventSource = nullptr;
     52static OSObjectPtr<dispatch_source_t>& memoryPressureEventSource()
     53{
     54    static NeverDestroyed<OSObjectPtr<dispatch_source_t>> source;
     55    return source.get();
     56}
     57
     58static OSObjectPtr<dispatch_source_t>& timerEventSource()
     59{
     60    static NeverDestroyed<OSObjectPtr<dispatch_source_t>> source;
     61    return source.get();
     62}
     63
    5464static int notifyTokens[3];
    5565
     
    6777void MemoryPressureHandler::install()
    6878{
    69     if (m_installed || timerEventSource)
     79    if (m_installed || timerEventSource())
    7080        return;
    7181
    7282    dispatch_async(m_dispatchQueue.get(), ^{
    7383        auto memoryStatusFlags = DISPATCH_MEMORYPRESSURE_NORMAL | DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL | DISPATCH_MEMORYPRESSURE_PROC_LIMIT_WARN | DISPATCH_MEMORYPRESSURE_PROC_LIMIT_CRITICAL;
    74         memoryPressureEventSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, memoryStatusFlags, m_dispatchQueue.get());
    75 
    76         dispatch_source_set_event_handler(memoryPressureEventSource, ^{
    77             auto status = dispatch_source_get_data(memoryPressureEventSource);
     84        memoryPressureEventSource() = adoptOSObject(dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, memoryStatusFlags, m_dispatchQueue.get()));
     85
     86        dispatch_source_set_event_handler(memoryPressureEventSource().get(), ^{
     87            auto status = dispatch_source_get_data(memoryPressureEventSource().get());
    7888            switch (status) {
    7989            // VM pressure events.
     
    102112                RELEASE_LOG(MemoryPressure, "Received memory pressure event %lu vm pressure %d", status, isUnderMemoryPressure());
    103113        });
    104         dispatch_resume(memoryPressureEventSource);
     114        dispatch_resume(memoryPressureEventSource().get());
    105115    });
    106116
     
    141151
    142152    dispatch_async(m_dispatchQueue.get(), ^{
    143         if (memoryPressureEventSource) {
    144             dispatch_source_cancel(memoryPressureEventSource);
    145             memoryPressureEventSource = nullptr;
     153        if (memoryPressureEventSource()) {
     154            dispatch_source_cancel(memoryPressureEventSource().get());
     155            memoryPressureEventSource() = nullptr;
    146156        }
    147157
    148         if (timerEventSource) {
    149             dispatch_source_cancel(timerEventSource);
    150             timerEventSource = nullptr;
     158        if (timerEventSource()) {
     159            dispatch_source_cancel(timerEventSource().get());
     160            timerEventSource() = nullptr;
    151161        }
    152162    });
     
    161171{
    162172    dispatch_async(m_dispatchQueue.get(), ^{
    163         timerEventSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, m_dispatchQueue.get());
    164         if (timerEventSource) {
    165             dispatch_set_context(timerEventSource, this);
     173        timerEventSource() = adoptOSObject(dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, m_dispatchQueue.get()));
     174        if (timerEventSource()) {
     175            dispatch_set_context(timerEventSource().get(), this);
    166176            // FIXME: The final argument `s_minimumHoldOffTime.seconds()` seems wrong.
    167177            // https://bugs.webkit.org/show_bug.cgi?id=183277
    168             dispatch_source_set_timer(timerEventSource, dispatch_time(DISPATCH_TIME_NOW, seconds.seconds() * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, s_minimumHoldOffTime.seconds());
    169             dispatch_source_set_event_handler(timerEventSource, ^{
    170                 if (timerEventSource) {
    171                     dispatch_source_cancel(timerEventSource);
    172                     timerEventSource = nullptr;
     178            dispatch_source_set_timer(timerEventSource().get(), dispatch_time(DISPATCH_TIME_NOW, seconds.seconds() * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, s_minimumHoldOffTime.seconds());
     179            dispatch_source_set_event_handler(timerEventSource().get(), ^{
     180                if (timerEventSource().get()) {
     181                    dispatch_source_cancel(timerEventSource().get());
     182                    timerEventSource() = nullptr;
    173183                }
    174184                MemoryPressureHandler::singleton().install();
    175185            });
    176             dispatch_resume(timerEventSource);
     186            dispatch_resume(timerEventSource().get());
    177187        }
    178188    });
  • trunk/Source/WebKit/Shared/EntryPointUtilities/Cocoa/XPCService/XPCServiceEntryPoint.mm

    r295270 r295740  
    159159{
    160160    static NeverDestroyed<OSObjectPtr<os_transaction_t>> globalTransaction;
     161    static NeverDestroyed<OSObjectPtr<dispatch_source_t>> globalSource;
    161162
    162163    // Because we don't use RunningBoard on macOS, we leak an OS transaction to control the lifetime of our XPC
     
    168169    static dispatch_once_t flag;
    169170    dispatch_once(&flag, ^{
    170         auto sigTermSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
    171         dispatch_source_set_event_handler(sigTermSource, ^{
     171        globalSource.get() = adoptOSObject(dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue()));
     172        dispatch_source_set_event_handler(globalSource.get().get(), ^{
    172173            exit(0);
    173174        });
    174         dispatch_resume(sigTermSource);
     175        dispatch_resume(globalSource.get().get());
    175176    });
    176177
Note: See TracChangeset for help on using the changeset viewer.