@@ -79,6 +79,19 @@ using namespace lldb_private::line_editor;
79
79
80
80
#endif // #if LLDB_EDITLINE_USE_WCHAR
81
81
82
+ template <typename T> class ScopedOptional {
83
+ public:
84
+ template <typename ... Args>
85
+ ScopedOptional (std::optional<T> &optional, Args &&...args)
86
+ : m_optional(optional) {
87
+ m_optional.emplace (std::forward<Args>(args)...);
88
+ }
89
+ ~ScopedOptional () { m_optional.reset (); }
90
+
91
+ private:
92
+ std::optional<T> &m_optional;
93
+ };
94
+
82
95
bool IsOnlySpaces (const EditLineStringType &content) {
83
96
for (wchar_t ch : content) {
84
97
if (ch != EditLineCharType (' ' ))
@@ -541,9 +554,10 @@ int Editline::GetCharacter(EditLineGetCharType *c) {
541
554
// Paint a ANSI formatted version of the desired prompt over the version
542
555
// libedit draws. (will only be requested if colors are supported)
543
556
if (m_needs_prompt_repaint) {
544
- LockedStreamFile locked_stream = m_output_stream_sp->Lock ();
557
+ ScopedOptional<LockedStreamFile> scope (m_locked_output,
558
+ m_output_stream_sp->Lock ());
545
559
MoveCursor (CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
546
- fprintf (locked_stream. GetFile ().GetStream (),
560
+ fprintf (m_locked_output-> GetFile ().GetStream (),
547
561
" %s"
548
562
" %s"
549
563
" %s" ,
@@ -581,10 +595,10 @@ int Editline::GetCharacter(EditLineGetCharType *c) {
581
595
// indefinitely. This gives a chance for someone to interrupt us. After
582
596
// Read returns, immediately lock the mutex again and check if we were
583
597
// interrupted.
584
- m_output_stream_sp-> GetMutex (). unlock ();
598
+ m_locked_output. reset ();
585
599
int read_count =
586
600
m_input_connection.Read (&ch, 1 , std::nullopt, status, nullptr );
587
- m_output_stream_sp->GetMutex (). lock ( );
601
+ m_locked_output. emplace ( m_output_stream_sp->Lock () );
588
602
if (m_editor_status == EditorStatus::Interrupted) {
589
603
while (read_count > 0 && status == lldb::eConnectionStatusSuccess)
590
604
read_count =
@@ -1599,7 +1613,8 @@ bool Editline::GetLine(std::string &line, bool &interrupted) {
1599
1613
m_input_lines = std::vector<EditLineStringType>();
1600
1614
m_input_lines.insert (m_input_lines.begin (), EditLineConstString (" " ));
1601
1615
1602
- LockedStreamFile locked_stream = m_output_stream_sp->Lock ();
1616
+ ScopedOptional<LockedStreamFile> scope (m_locked_output,
1617
+ m_output_stream_sp->Lock ());
1603
1618
1604
1619
lldbassert (m_editor_status != EditorStatus::Editing);
1605
1620
if (m_editor_status == EditorStatus::Interrupted) {
@@ -1619,7 +1634,7 @@ bool Editline::GetLine(std::string &line, bool &interrupted) {
1619
1634
interrupted = m_editor_status == EditorStatus::Interrupted;
1620
1635
if (!interrupted) {
1621
1636
if (input == nullptr ) {
1622
- fprintf (locked_stream. GetFile ().GetStream (), " \n " );
1637
+ fprintf (m_locked_output-> GetFile ().GetStream (), " \n " );
1623
1638
m_editor_status = EditorStatus::EndOfInput;
1624
1639
} else {
1625
1640
m_history_sp->Enter (input);
@@ -1644,7 +1659,8 @@ bool Editline::GetLines(int first_line_number, StringList &lines,
1644
1659
m_input_lines = std::vector<EditLineStringType>();
1645
1660
m_input_lines.insert (m_input_lines.begin (), EditLineConstString (" " ));
1646
1661
1647
- LockedStreamFile locked_stream = m_output_stream_sp->Lock ();
1662
+ ScopedOptional<LockedStreamFile> scope (m_locked_output,
1663
+ m_output_stream_sp->Lock ());
1648
1664
1649
1665
// Begin the line editing loop
1650
1666
DisplayInput ();
0 commit comments