Skip to content

Commit

Permalink
Merge pull request #14 from austb/fix_locking
Browse files Browse the repository at this point in the history
Fix locking
  • Loading branch information
piotrmurach committed Jul 28, 2017
2 parents 11309f6 + 7602c94 commit dcf138d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 7 additions & 5 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,15 +161,17 @@ def start
reset
end

SPINNER_START_LOCK = Mutex.new
# Start automatic spinning animation
#
# @api public
def auto_spin
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 @@ -339,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: 8 additions & 2 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 @@ -36,8 +38,12 @@ def initialize(options = {})
# @api public
def register(pattern, options = {})
spinner = TTY::Spinner.new(pattern, @options.merge(options))
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 dcf138d

Please sign in to comment.