Skip to content

Commit

Permalink
Attempt to fix locks
Browse files Browse the repository at this point in the history
  • Loading branch information
austb committed Jul 28, 2017
1 parent 43654cb commit 7602c94
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
17 changes: 7 additions & 10 deletions lib/tty/spinner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Spinner
TICK = '✔'.freeze
CROSS = '✖'.freeze

CURSOR_USAGE_LOCK = Monitor.new

# The object that responds to print call defaulting to stderr
#
# @api public
Expand Down Expand Up @@ -159,20 +161,17 @@ def start
reset
end

SPINNER_START_LOCK = Mutex.new
# Start automatic spinning animation
#
# @api public
def auto_spin
# TODO: this should probably be the same lock (will need to be a Monitor
# rather than a Mutex) as execute_on_line, so that a spinner starts
# completely before another tries to redraw itself. Also, spin needs to
# run once before a spinner is fully started, so we should probably run
# it once, and then start the Thread.
SPINNER_START_LOCK.synchronize do
CURSOR_USAGE_LOCK.synchronize do
start
sleep_time = 1.0 / @interval

spin
@thread = Thread.new do
sleep(sleep_time)
while @started_at
spin
sleep(sleep_time)
Expand Down Expand Up @@ -344,11 +343,9 @@ def reset

private

LOCK = Mutex.new

def execute_on_line
if @multispinner
LOCK.synchronize do
CURSOR_USAGE_LOCK.synchronize do
lines_up = @multispinner.count_line_offset(@index)

if @first_run
Expand Down
10 changes: 7 additions & 3 deletions lib/tty/spinner/multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Multi
def initialize(options = {})
@options = options

@create_spinner_lock = Mutex.new

@spinners = []
@callbacks = {
success: [],
Expand All @@ -37,9 +39,11 @@ def initialize(options = {})
def register(pattern, options = {})
spinner = TTY::Spinner.new(pattern, @options.merge(options))

# TODO: If two threads add a spinner at the same time, @spinners.length will be the same for both
spinner.add_multispinner(self, @spinners.length)
@spinners << spinner
@create_spinner_lock.synchronize do
spinner.add_multispinner(self, @spinners.length)
@spinners << spinner
end

spinner
end

Expand Down

0 comments on commit 7602c94

Please sign in to comment.