aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/mauve_thread.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mauve/mauve_thread.rb')
-rw-r--r--lib/mauve/mauve_thread.rb70
1 files changed, 42 insertions, 28 deletions
diff --git a/lib/mauve/mauve_thread.rb b/lib/mauve/mauve_thread.rb
index f6c0cbc..6409f98 100644
--- a/lib/mauve/mauve_thread.rb
+++ b/lib/mauve/mauve_thread.rb
@@ -5,9 +5,11 @@ module Mauve
class MauveThread
+ attr_reader :state
+
def initialize
@thread = nil
- @stop = true
+ @state = :stopped
end
def logger
@@ -18,62 +20,77 @@ module Mauve
#
# Good to go.
#
- @frozen = false
- @stop = false
-
- logger.debug("Started")
+ self.state = :starting
@sleep_interval ||= interval
- while !@stop do
+ sleep_loops = (@sleep_interval.to_f / 0.1).round.to_i
+ sleep_loops = 1 if sleep_loops.nil? or sleep_loops < 1
+
+ while self.state != :stopping do
+
+ self.state = :started if self.state == :starting
+
#
# Schtop!
#
- if @frozen
- logger.debug("Frozen")
+ if self.state == :freezing
+ self.state = :frozen
Thread.stop
- logger.debug("Thawed")
+ self.state = :started
end
yield
- next if self.should_stop?
+ #
+ # Ah-ha! Sleep with a break clause.
+ #
+ sleep_loops.times do
+
+ break if self.should_stop?
- Kernel.sleep(@sleep_interval)
+ #
+ # This is a rate-limiting step
+ #
+ Kernel.sleep 0.1
+ end
end
- logger.debug("Stopped")
+ self.state = :stopped
end
def should_stop?
- @frozen or @stop
+ [:freezing, :stopping].include?(self.state)
+ end
+
+ def state=(s)
+ raise "Bad state for mauve_thread #{s.inspect}" unless [:stopped, :starting, :started, :freezing, :frozen, :stopping, :killing, :killed].include?(s)
+ unless @state == s
+ @state = s
+ logger.debug(s.to_s.capitalize)
+ end
end
def freeze
- logger.debug("Freezing") unless @frozen
+ self.state = :freezing
- @frozen = true
-
20.times { Kernel.sleep 0.1 ; break if @thread.stop? }
logger.debug("Thread has not frozen!") unless @thread.stop?
end
def frozen?
- self.stop?
+ self.stop? and self.state == :frozen
end
def run
if self.alive?
- if self.stop?
- logger.debug("Thawing") if @frozen
- @frozen = false
+ if self.stop?
@thread.wakeup
end
else
@logger = nil
- logger.debug("Starting") if @stop
- @stop = false
+ self.state = :starting
@thread = Thread.new{ self.run_thread { self.main_loop } }
end
end
@@ -107,9 +124,7 @@ module Mauve
end
def stop
- logger.debug("Stopping") unless @stop
-
- @stop = true
+ self.state = :stopping
10.times do
break unless self.alive?
@@ -127,10 +142,9 @@ module Mauve
alias exit stop
def kill
- logger.debug("Killing")
- @frozen = @stop = true
+ self.state = :killing
@thread.kill
- logger.debug("Killed")
+ self.state = :killed
end
def thread