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 */
 
   current_thread = find_thread_ptid (ptid);
 }
 
+/* See gdbthread.h.  */
+
+void
+switch_to_thread (thread_info *thread)
+{
+  current_thread = thread;
+}
+
 /* See inferiors.h.  */
 
 void
 {
   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);
+}