Ignore:
Timestamp:
Mar 1, 2017, 12:15:08 PM (8 years ago)
Author:
[email protected]
Message:

[Re-landing] Change JSLock to stash PlatformThread instead of std::thread::id.
https://bugs.webkit.org/show_bug.cgi?id=168996

Reviewed by Filip Pizlo and Saam Barati.

PlatformThread is more useful because it allows us to:

  1. find the MachineThreads::Thread which is associated with it.
  2. suspend / resume threads.
  3. send a signal to a thread.

We can't do those with std::thread::id. We will need one or more of these
capabilities to implement non-polling VM traps later.

Update: Since we don't have a canonical "uninitialized" value for PlatformThread,
we now have a JSLock::m_hasOwnerThread flag that is set to true if and only the
m_ownerThread value is valid. JSLock::currentThreadIsHoldingLock() now checks
JSLock::m_hasOwnerThread before doing the thread identity comparison.

  • JavaScriptCore.xcodeproj/project.pbxproj:
  • heap/MachineStackMarker.cpp:

(JSC::MachineThreads::Thread::createForCurrentThread):
(JSC::MachineThreads::machineThreadForCurrentThread):
(JSC::MachineThreads::removeThread):
(JSC::MachineThreads::Thread::suspend):
(JSC::MachineThreads::tryCopyOtherThreadStacks):
(JSC::getCurrentPlatformThread): Deleted.

  • heap/MachineStackMarker.h:
  • runtime/JSCellInlines.h:

(JSC::JSCell::classInfo):

  • runtime/JSLock.cpp:

(JSC::JSLock::JSLock):
(JSC::JSLock::lock):
(JSC::JSLock::unlock):
(JSC::JSLock::currentThreadIsHoldingLock): Deleted.

  • runtime/JSLock.h:

(JSC::JSLock::ownerThread):
(JSC::JSLock::currentThreadIsHoldingLock):

  • runtime/PlatformThread.h: Added.

(JSC::currentPlatformThread):

  • runtime/VM.cpp:

(JSC::VM::~VM):

  • runtime/VM.h:

(JSC::VM::ownerThread):

  • runtime/Watchdog.cpp:

(JSC::Watchdog::setTimeLimit):
(JSC::Watchdog::shouldTerminate):
(JSC::Watchdog::startTimer):
(JSC::Watchdog::stopTimer):

  • tools/JSDollarVMPrototype.cpp:

(JSC::JSDollarVMPrototype::currentThreadOwnsJSLock):

  • tools/VMInspector.cpp:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/runtime/JSLock.h

    r213231 r213238  
    2121#pragma once
    2222
     23#include "PlatformThread.h"
    2324#include <mutex>
    24 #include <thread>
    2525#include <wtf/Assertions.h>
    2626#include <wtf/Lock.h>
     
    9494    VM* vm() { return m_vm; }
    9595
    96     std::thread::id ownerThread() const { return m_ownerThreadID; }
    97     JS_EXPORT_PRIVATE bool currentThreadIsHoldingLock();
     96    std::optional<PlatformThread> ownerThread() const
     97    {
     98        if (m_hasOwnerThread)
     99            return m_ownerThread;
     100        return { };
     101    }
     102    bool currentThreadIsHoldingLock() { return m_hasOwnerThread && m_ownerThread == currentPlatformThread(); }
    98103
    99104    void willDestroyVM(VM*);
     
    127132
    128133    Lock m_lock;
    129     std::thread::id m_ownerThreadID;
     134    // We cannot make m_ownerThread an optional (instead of pairing it with an explicit
     135    // m_hasOwnerThread) because currentThreadIsHoldingLock() may be called from a
     136    // different thread, and an optional is vulnerable to races.
     137    // See https://bugs.webkit.org/show_bug.cgi?id=169042#c6
     138    bool m_hasOwnerThread { false };
     139    PlatformThread m_ownerThread;
    130140    intptr_t m_lockCount;
    131141    unsigned m_lockDropDepth;
Note: See TracChangeset for help on using the changeset viewer.