Skip to content

[🍒 swift/release/6.2] [lldb] Remove ProcessRunLock::TrySetRunning (#135455) #10522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lldb/include/lldb/Host/ProcessRunLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ class ProcessRunLock {

bool ReadTryLock();
bool ReadUnlock();

/// Set the process to running. Returns true if the process was stopped.
/// Return false if the process was running.
bool SetRunning();
bool TrySetRunning();

/// Set the process to stopped. Returns true if the process was running.
/// Returns false if the process was stopped.
bool SetStopped();

class ProcessRunLocker {
Expand Down
21 changes: 6 additions & 15 deletions lldb/source/Host/common/ProcessRunLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,20 @@ bool ProcessRunLock::ReadUnlock() {

bool ProcessRunLock::SetRunning() {
::pthread_rwlock_wrlock(&m_rwlock);
bool was_stopped = !m_running;
m_running = true;
::pthread_rwlock_unlock(&m_rwlock);
return true;
}

bool ProcessRunLock::TrySetRunning() {
bool r;

if (::pthread_rwlock_trywrlock(&m_rwlock) == 0) {
r = !m_running;
m_running = true;
::pthread_rwlock_unlock(&m_rwlock);
return r;
}
return false;
return was_stopped;
}

bool ProcessRunLock::SetStopped() {
::pthread_rwlock_wrlock(&m_rwlock);
bool was_running = m_running;
m_running = false;
::pthread_rwlock_unlock(&m_rwlock);
return true;
}
return was_running;
}

} // namespace lldb_private

#endif
16 changes: 4 additions & 12 deletions lldb/source/Host/windows/ProcessRunLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,16 @@ bool ProcessRunLock::ReadUnlock() { return ::ReadUnlock(m_rwlock); }

bool ProcessRunLock::SetRunning() {
WriteLock(m_rwlock);
bool was_stopped = !m_running;
m_running = true;
WriteUnlock(m_rwlock);
return true;
}

bool ProcessRunLock::TrySetRunning() {
if (WriteTryLock(m_rwlock)) {
bool was_running = m_running;
m_running = true;
WriteUnlock(m_rwlock);
return !was_running;
}
return false;
return was_stopped;
}

bool ProcessRunLock::SetStopped() {
WriteLock(m_rwlock);
bool was_running = m_running;
m_running = false;
WriteUnlock(m_rwlock);
return true;
return was_running;
}
63 changes: 23 additions & 40 deletions lldb/source/Target/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,7 @@ void Process::Finalize(bool destructing) {
// contain events that have ProcessSP values in them which can keep this
// process around forever. These events need to be cleared out.
m_private_state_listener_sp->Clear();
m_public_run_lock.TrySetRunning(); // This will do nothing if already locked
m_public_run_lock.SetStopped();
m_private_run_lock.TrySetRunning(); // This will do nothing if already locked
m_private_run_lock.SetStopped();
m_structured_data_plugin_map.clear();
}
Expand Down Expand Up @@ -1466,11 +1464,11 @@ void Process::SetPublicState(StateType new_state, bool restarted) {
Status Process::Resume() {
Log *log(GetLog(LLDBLog::State | LLDBLog::Process));
LLDB_LOGF(log, "(plugin = %s) -- locking run lock", GetPluginName().data());
if (!m_public_run_lock.TrySetRunning()) {
LLDB_LOGF(log, "(plugin = %s) -- TrySetRunning failed, not resuming.",
GetPluginName().data());
if (!m_public_run_lock.SetRunning()) {
LLDB_LOGF(log, "(plugin = %s) -- SetRunning failed, not resuming.",
GetPluginName().data());
return Status::FromErrorString(
"Resume request failed - process still running.");
"resume request failed - process already running");
}
Status error = PrivateResume();
if (!error.Success()) {
Expand All @@ -1483,10 +1481,10 @@ Status Process::Resume() {
Status Process::ResumeSynchronous(Stream *stream) {
Log *log(GetLog(LLDBLog::State | LLDBLog::Process));
LLDB_LOGF(log, "Process::ResumeSynchronous -- locking run lock");
if (!m_public_run_lock.TrySetRunning()) {
LLDB_LOGF(log, "Process::Resume: -- TrySetRunning failed, not resuming.");
if (!m_public_run_lock.SetRunning()) {
LLDB_LOGF(log, "Process::Resume: -- SetRunning failed, not resuming.");
return Status::FromErrorString(
"Resume request failed - process still running.");
"resume request failed: process already running");
}

ListenerSP listener_sp(
Expand Down Expand Up @@ -2818,13 +2816,8 @@ Status Process::LaunchPrivate(ProcessLaunchInfo &launch_info, StateType &state,
SetPublicState(eStateLaunching, restarted);
m_should_detach = false;

if (m_public_run_lock.TrySetRunning()) {
// Now launch using these arguments.
error = DoLaunch(exe_module, launch_info);
} else {
// This shouldn't happen
error = Status::FromErrorString("failed to acquire process run lock");
}
m_public_run_lock.SetRunning();
error = DoLaunch(exe_module, launch_info);

if (error.Fail()) {
if (GetID() != LLDB_INVALID_PROCESS_ID) {
Expand Down Expand Up @@ -3089,17 +3082,12 @@ Status Process::Attach(ProcessAttachInfo &attach_info) {
if (wait_for_launch) {
error = WillAttachToProcessWithName(process_name, wait_for_launch);
if (error.Success()) {
if (m_public_run_lock.TrySetRunning()) {
m_should_detach = true;
const bool restarted = false;
SetPublicState(eStateAttaching, restarted);
// Now attach using these arguments.
error = DoAttachToProcessWithName(process_name, attach_info);
} else {
// This shouldn't happen
error =
Status::FromErrorString("failed to acquire process run lock");
}
m_public_run_lock.SetRunning();
m_should_detach = true;
const bool restarted = false;
SetPublicState(eStateAttaching, restarted);
// Now attach using these arguments.
error = DoAttachToProcessWithName(process_name, attach_info);

if (error.Fail()) {
if (GetID() != LLDB_INVALID_PROCESS_ID) {
Expand Down Expand Up @@ -3160,17 +3148,13 @@ Status Process::Attach(ProcessAttachInfo &attach_info) {
if (attach_pid != LLDB_INVALID_PROCESS_ID) {
error = WillAttachToProcessWithID(attach_pid);
if (error.Success()) {
m_public_run_lock.SetRunning();

if (m_public_run_lock.TrySetRunning()) {
// Now attach using these arguments.
m_should_detach = true;
const bool restarted = false;
SetPublicState(eStateAttaching, restarted);
error = DoAttachToProcessWithID(attach_pid, attach_info);
} else {
// This shouldn't happen
error = Status::FromErrorString("failed to acquire process run lock");
}
// Now attach using these arguments.
m_should_detach = true;
const bool restarted = false;
SetPublicState(eStateAttaching, restarted);
error = DoAttachToProcessWithID(attach_pid, attach_info);

if (error.Success()) {
SetNextEventAction(new Process::AttachCompletionHandler(
Expand Down Expand Up @@ -5951,10 +5935,9 @@ void Process::ClearPreResumeAction(PreResumeActionCallback callback, void *baton
}

ProcessRunLock &Process::GetRunLock() {
if (m_private_state_thread.EqualsThread(Host::GetCurrentThread()))
if (Process::CurrentThreadIsPrivateStateThread())
return m_private_run_lock;
else
return m_public_run_lock;
return m_public_run_lock;
}

bool Process::CurrentThreadIsPrivateStateThread()
Expand Down