mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 16:44:01 +02:00
[ruby/reline] Thread safe readline
(https://github.com/ruby/reline/pull/669)
Block until other Reline.readline or Reline.readmultiline finish
ebab2875f1
This commit is contained in:
parent
4cbe4e49c7
commit
4e48d2724e
2 changed files with 51 additions and 29 deletions
|
@ -75,6 +75,7 @@ module Reline
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
self.output = STDOUT
|
self.output = STDOUT
|
||||||
|
@mutex = Mutex.new
|
||||||
@dialog_proc_list = {}
|
@dialog_proc_list = {}
|
||||||
yield self
|
yield self
|
||||||
@completion_quote_character = nil
|
@completion_quote_character = nil
|
||||||
|
@ -254,6 +255,7 @@ module Reline
|
||||||
Reline::DEFAULT_DIALOG_CONTEXT = Array.new
|
Reline::DEFAULT_DIALOG_CONTEXT = Array.new
|
||||||
|
|
||||||
def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
|
def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
|
||||||
|
@mutex.synchronize do
|
||||||
unless confirm_multiline_termination
|
unless confirm_multiline_termination
|
||||||
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
|
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
|
||||||
end
|
end
|
||||||
|
@ -277,8 +279,10 @@ module Reline
|
||||||
whole_buffer
|
whole_buffer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def readline(prompt = '', add_hist = false)
|
def readline(prompt = '', add_hist = false)
|
||||||
|
@mutex.synchronize do
|
||||||
Reline.update_iogate
|
Reline.update_iogate
|
||||||
io_gate.with_raw_input do
|
io_gate.with_raw_input do
|
||||||
inner_readline(prompt, add_hist, false)
|
inner_readline(prompt, add_hist, false)
|
||||||
|
@ -293,6 +297,7 @@ module Reline
|
||||||
line_editor.reset_line if line_editor.line.nil?
|
line_editor.reset_line if line_editor.line.nil?
|
||||||
line
|
line
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private def inner_readline(prompt, add_hist, multiline, &confirm_multiline_termination)
|
private def inner_readline(prompt, add_hist, multiline, &confirm_multiline_termination)
|
||||||
if ENV['RELINE_STDERR_TTY']
|
if ENV['RELINE_STDERR_TTY']
|
||||||
|
|
|
@ -1690,6 +1690,23 @@ begin
|
||||||
EOC
|
EOC
|
||||||
end
|
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)
|
def write_inputrc(content)
|
||||||
File.open(@inputrc_file, 'w') do |f|
|
File.open(@inputrc_file, 'w') do |f|
|
||||||
f.write content
|
f.write content
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue