Skip to content

Commit

Permalink
Make Listen.to / Listen.stop thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhefner committed Jul 21, 2020
1 parent ec274a1 commit 63eff89
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/listen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Listen::Logger.info "Listen version: #{Listen::VERSION}"

module Listen
@listeners = []
@listeners = Queue.new

class << self
# Listens to file system modifications on a either single directory or
Expand All @@ -36,7 +36,7 @@ class << self
#
def to(*args, &block)
Listener.new(*args, &block).tap do |listener|
@listeners << WeakRef.new(listener)
@listeners.enq(WeakRef.new(listener))
end
end

Expand All @@ -45,13 +45,13 @@ def to(*args, &block)
def stop
Internals::ThreadPool.stop

# TODO: should use a mutex for this
while (listener = @listeners.pop)
while (listener = @listeners.deq(true))
begin
listener.stop
rescue WeakRef::RefError
end
end
rescue ThreadError
end
end
end

0 comments on commit 63eff89

Please sign in to comment.