From f24791b72e764ada576901c0e866bf7768773a16 Mon Sep 17 00:00:00 2001 From: Tankut Baris Aktemur Date: Mon, 13 Dec 2021 12:22:48 +0100 Subject: [PATCH] gdbserver: introduce scoped_restore_current_thread and switch_to_thread Introduce a class for restoring the current thread and a function to switch to the given thread. This is a preparation for a refactoring that aims to remove direct assignments to 'current_thread'. --- gdbserver/gdbthread.h | 22 ++++++++++++++++++++++ gdbserver/inferiors.cc | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h index 7c293b1f89d..315a4da5167 100644 --- a/gdbserver/gdbthread.h +++ b/gdbserver/gdbthread.h @@ -233,4 +233,26 @@ lwpid_of (const thread_info *thread) return thread->id.lwp (); } +/* Switch the current thread. */ + +void switch_to_thread (thread_info *thread); + +/* Save/restore current thread. */ + +class scoped_restore_current_thread +{ +public: + scoped_restore_current_thread (); + ~scoped_restore_current_thread (); + + DISABLE_COPY_AND_ASSIGN (scoped_restore_current_thread); + + /* Cancel restoring on scope exit. */ + void dont_restore () { m_dont_restore = true; } + +private: + bool m_dont_restore = false; + thread_info *m_thread; +}; + #endif /* GDBSERVER_GDBTHREAD_H */ diff --git a/gdbserver/inferiors.cc b/gdbserver/inferiors.cc index a636266c798..d44e40a10db 100644 --- a/gdbserver/inferiors.cc +++ b/gdbserver/inferiors.cc @@ -218,6 +218,14 @@ switch_to_thread (process_stratum_target *ops, ptid_t ptid) current_thread = find_thread_ptid (ptid); } +/* See gdbthread.h. */ + +void +switch_to_thread (thread_info *thread) +{ + current_thread = thread; +} + /* See inferiors.h. */ void @@ -243,3 +251,16 @@ set_inferior_cwd (std::string cwd) { current_inferior_cwd = std::move (cwd); } + +scoped_restore_current_thread::scoped_restore_current_thread () +{ + m_thread = current_thread; +} + +scoped_restore_current_thread::~scoped_restore_current_thread () +{ + if (m_dont_restore) + return; + + switch_to_thread (m_thread); +} -- 2.30.2