Skip to content

Commit

Permalink
[ruby/reline] Thread safe readline
Browse files Browse the repository at this point in the history
(ruby/reline#669)

Block until other Reline.readline or Reline.readmultiline finish

ruby/reline@ebab2875f1
  • Loading branch information
tompng authored and hsbt committed Apr 5, 2024
1 parent 4cbe4e4 commit 4e48d27
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
63 changes: 34 additions & 29 deletions lib/reline.rb
Expand Up @@ -75,6 +75,7 @@ class Core

def initialize
self.output = STDOUT
@mutex = Mutex.new
@dialog_proc_list = {}
yield self
@completion_quote_character = nil
Expand Down Expand Up @@ -254,44 +255,48 @@ def get_screen_size
Reline::DEFAULT_DIALOG_CONTEXT = Array.new

def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
unless confirm_multiline_termination
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
end
@mutex.synchronize do
unless confirm_multiline_termination
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
end

Reline.update_iogate
io_gate.with_raw_input do
inner_readline(prompt, add_hist, true, &confirm_multiline_termination)
end
Reline.update_iogate
io_gate.with_raw_input do
inner_readline(prompt, add_hist, true, &confirm_multiline_termination)
end

whole_buffer = line_editor.whole_buffer.dup
whole_buffer.taint if RUBY_VERSION < '2.7'
if add_hist and whole_buffer and whole_buffer.chomp("\n").size > 0
Reline::HISTORY << whole_buffer
end
whole_buffer = line_editor.whole_buffer.dup
whole_buffer.taint if RUBY_VERSION < '2.7'
if add_hist and whole_buffer and whole_buffer.chomp("\n").size > 0
Reline::HISTORY << whole_buffer
end

if line_editor.eof?
line_editor.reset_line
# Return nil if the input is aborted by C-d.
nil
else
whole_buffer
if line_editor.eof?
line_editor.reset_line
# Return nil if the input is aborted by C-d.
nil
else
whole_buffer
end
end
end

def readline(prompt = '', add_hist = false)
Reline.update_iogate
io_gate.with_raw_input do
inner_readline(prompt, add_hist, false)
end
@mutex.synchronize do
Reline.update_iogate
io_gate.with_raw_input do
inner_readline(prompt, add_hist, false)
end

line = line_editor.line.dup
line.taint if RUBY_VERSION < '2.7'
if add_hist and line and line.chomp("\n").size > 0
Reline::HISTORY << line.chomp("\n")
end
line = line_editor.line.dup
line.taint if RUBY_VERSION < '2.7'
if add_hist and line and line.chomp("\n").size > 0
Reline::HISTORY << line.chomp("\n")
end

line_editor.reset_line if line_editor.line.nil?
line
line_editor.reset_line if line_editor.line.nil?
line
end
end

private def inner_readline(prompt, add_hist, multiline, &confirm_multiline_termination)
Expand Down
17 changes: 17 additions & 0 deletions test/reline/yamatanooroti/test_rendering.rb
Expand Up @@ -1690,6 +1690,23 @@ def test_exit_with_ctrl_d
EOC
end

def test_thread_safe
start_terminal(6, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.')
write("[Thread.new{Reline.readline'>'},Thread.new{Reline.readmultiline('>'){true}}].map(&:join).size\n")
write("exit\n")
write("exit\n")
write("42\n")
close
assert_screen(<<~EOC)
>exit
>exit
=> 2
prompt> 42
=> 42
prompt>
EOC
end

def write_inputrc(content)
File.open(@inputrc_file, 'w') do |f|
f.write content
Expand Down

0 comments on commit 4e48d27

Please sign in to comment.