Permalink
Browse files

Add thread-safety to starting and stopping all adapters

  • Loading branch information...
1 parent d880ca6 commit 62ad649e0995ade17084804aef4d64606e388343 @Maher4Ever Maher4Ever committed Apr 3, 2012
Showing with 40 additions and 8 deletions.
  1. +10 −2 lib/listen/adapters/darwin.rb
  2. +10 −2 lib/listen/adapters/linux.rb
  3. +10 −2 lib/listen/adapters/polling.rb
  4. +10 −2 lib/listen/adapters/windows.rb
@@ -19,7 +19,11 @@ def initialize(directories, options = {}, &callback)
# @param [Boolean] blocking whether or not to block the current thread after starting
#
def start(blocking = true)
- super
+ @mutex.synchronize do
+ return if @stop == false
+ super
+ end
+
@workers_pool = @workers.map { |w| Thread.new { w.run } }
@poll_thread = Thread.new { poll_changed_dirs }
@@ -33,7 +37,11 @@ def start(blocking = true)
# Stops the adapter.
#
def stop
- super
+ @mutex.synchronize do
+ return if @stop == true
+ super
+ end
+
@workers.map(&:stop)
@workers_pool.map { |t| Thread.kill(t) if t }
@poll_thread.join
@@ -24,7 +24,11 @@ def initialize(directories, options = {}, &callback)
# @param [Boolean] blocking whether or not to block the current thread after starting
#
def start(blocking = true)
- super
+ @mutex.synchronize do
+ return if @stop == false
+ super
+ end
+
@worker_thread = Thread.new { @worker.run }
@poll_thread = Thread.new { poll_changed_dirs }
@poll_thread.join if blocking
@@ -33,7 +37,11 @@ def start(blocking = true)
# Stops the adapter.
#
def stop
- super
+ @mutex.synchronize do
+ return if @stop == true
+ super
+ end
+
@worker.stop
Thread.kill(@worker_thread) if @worker_thread
@poll_thread.join
@@ -23,15 +23,23 @@ def initialize(directories, options = {}, &callback)
# @param [Boolean] blocking whether or not to block the current thread after starting
#
def start(blocking = true)
- super
+ @mutex.synchronize do
+ return if @stop == false
+ super
+ end
+
@poll_thread = Thread.new { poll }
@poll_thread.join if blocking
end
# Stop the adapter.
#
def stop
- super
+ @mutex.synchronize do
+ return if @stop == true
+ super
+ end
+
@poll_thread.join
end
@@ -19,7 +19,11 @@ def initialize(directories, options = {}, &callback)
# @param [Boolean] blocking whether or not to block the current thread after starting
#
def start(blocking = true)
- super
+ @mutex.synchronize do
+ return if @stop == false
+ super
+ end
+
@worker_thread = Thread.new { @worker.run }
@poll_thread = Thread.new { poll_changed_dirs(true) }
@poll_thread.join if blocking
@@ -28,7 +32,11 @@ def start(blocking = true)
# Stops the adapter.
#
def stop
- super
+ @mutex.synchronize do
+ return if @stop == true
+ super
+ end
+
@worker.stop
Thread.kill(@worker_thread) if @worker_thread
@poll_thread.join

0 comments on commit 62ad649

Please sign in to comment.