2525#include " lldb/Target/Unwind.h"
2626#include " lldb/Utility/LLDBLog.h"
2727#include " lldb/Utility/Log.h"
28- #include " llvm/ADT/ScopeExit.h"
2928#include " llvm/ADT/SmallPtrSet.h"
3029
3130#include < memory>
@@ -580,9 +579,12 @@ uint32_t StackFrameList::GetNumFrames(bool can_create) {
580579 // Don't allow interrupt or we might not return the correct count
581580 GetFramesUpTo (UINT32_MAX, DoNotAllowInterruption);
582581 }
583- // Formally we should acquire the shared lock here, but since we've forced
584- // fetching all the frames, it can't change anymore so that's not required.
585- return GetVisibleStackFrameIndex (m_frames.size ());
582+ uint32_t frame_idx;
583+ {
584+ std::shared_lock<std::shared_mutex> guard (m_list_mutex);
585+ frame_idx = GetVisibleStackFrameIndex (m_frames.size ());
586+ }
587+ return frame_idx;
586588}
587589
588590void StackFrameList::Dump (Stream *s) {
@@ -624,7 +626,7 @@ StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) {
624626
625627 if (frame_sp)
626628 return frame_sp;
627- }
629+ } // End of reader lock scope
628630
629631 // GetFramesUpTo will fill m_frames with as many frames as you asked for, if
630632 // there are that many. If there weren't then you asked for too many frames.
@@ -635,22 +637,25 @@ StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) {
635637 return {};
636638 }
637639
638- if (idx < m_frames.size ()) {
639- frame_sp = m_frames[idx];
640- } else if (original_idx == 0 ) {
641- // There should ALWAYS be a frame at index 0. If something went wrong with
642- // the CurrentInlinedDepth such that there weren't as many frames as we
643- // thought taking that into account, then reset the current inlined depth
644- // and return the real zeroth frame.
645- if (m_frames.empty ()) {
646- // Why do we have a thread with zero frames, that should not ever
647- // happen...
648- assert (!m_thread.IsValid () && " A valid thread has no frames." );
649- } else {
650- ResetCurrentInlinedDepth ();
651- frame_sp = m_frames[original_idx];
640+ { // Now we're accessing m_frames as a reader, so acquire the reader lock.
641+ std::shared_lock<std::shared_mutex> guard (m_list_mutex);
642+ if (idx < m_frames.size ()) {
643+ frame_sp = m_frames[idx];
644+ } else if (original_idx == 0 ) {
645+ // There should ALWAYS be a frame at index 0. If something went wrong
646+ // with the CurrentInlinedDepth such that there weren't as many frames as
647+ // we thought taking that into account, then reset the current inlined
648+ // depth and return the real zeroth frame.
649+ if (m_frames.empty ()) {
650+ // Why do we have a thread with zero frames, that should not ever
651+ // happen...
652+ assert (!m_thread.IsValid () && " A valid thread has no frames." );
653+ } else {
654+ ResetCurrentInlinedDepth ();
655+ frame_sp = m_frames[original_idx];
656+ }
652657 }
653- }
658+ } // End of reader lock scope
654659
655660 return frame_sp;
656661}
@@ -688,16 +693,10 @@ StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) {
688693 // the shared mutex:
689694 std::shared_lock<std::shared_mutex> guard (m_list_mutex);
690695 // Do a binary search in case the stack frame is already in our cache
691- collection::const_iterator begin = m_frames.begin ();
692- collection::const_iterator end = m_frames.end ();
693- if (begin != end) {
694- collection::const_iterator pos =
695- std::lower_bound (begin, end, stack_id, CompareStackID);
696- if (pos != end) {
697- if ((*pos)->GetStackID () == stack_id)
698- return *pos;
699- }
700- }
696+ collection::const_iterator pos =
697+ llvm::lower_bound (m_frames, stack_id, CompareStackID);
698+ if (pos != m_frames.end () && (*pos)->GetStackID () == stack_id)
699+ return *pos;
701700 }
702701 // If we needed to add more frames, we would get to here.
703702 do {
@@ -801,8 +800,6 @@ StackFrameList::GetSelectedFrameIndex(SelectMostRelevant select_most_relevant) {
801800
802801uint32_t
803802StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame) {
804- uint32_t result = 0 ;
805-
806803 std::shared_lock<std::shared_mutex> guard (m_list_mutex);
807804
808805 const_iterator pos;
@@ -820,8 +817,7 @@ StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) {
820817 }
821818 }
822819 SetDefaultFileAndLineToSelectedFrame ();
823- result = *m_selected_frame_idx;
824- return result;
820+ return *m_selected_frame_idx;
825821}
826822
827823bool StackFrameList::SetSelectedFrameByIndex (uint32_t idx) {
0 commit comments