Permalink
Browse files

mutex fix?

  • Loading branch information...
1 parent 425dfb0 commit 7d1665144a3a975c05f1f43902ddaf084e784dbe @mojombo mojombo committed Jan 17, 2008
Showing with 92 additions and 49 deletions.
  1. +39 −0 lib/god.rb
  2. +44 −42 lib/god/hub.rb
  3. +9 −7 lib/god/logger.rb
View
@@ -3,6 +3,45 @@
# rubygems
require 'rubygems'
+begin
+ require 'fastthread'
+rescue RuntimeError => e
+ warn "fastthread not loaded: #{ e.message }"
+rescue LoadError
+ensure
+ require 'thread'
+end
+
+class Mutex
+ def lock
+ while (Thread.critical = true; @locked)
+ @waiting.unshift Thread.current
+ Thread.stop
+ end
+ @locked = true
+ Thread.critical = false
+ self
+ end
+
+ def unlock
+ return unless @locked
+ Thread.critical = true
+ @locked = false
+ begin
+ t = @waiting.pop
+ t.wakeup if t
+ rescue ThreadError
+ retry
+ end
+ Thread.critical = false
+ begin
+ t.run if t
+ rescue ThreadError
+ end
+ self
+ end
+end
+
# core
require 'stringio'
require 'logger'
View
@@ -11,7 +11,7 @@ class << self
end
self.directory = {}
- self.mutex = Monitor.new
+ self.mutex = Mutex.new
# Attach the condition to the hub and schedule/register it
# +condition+ is the Condition to attach
@@ -88,47 +88,49 @@ def self.handle_poll(condition, phase)
watch.mutex.synchronize do
# ensure this condition is still active when we finally get the mutex
if self.directory[condition] && phase == watch.phase
- # run the test
- result = condition.test
-
- # log
- messages = self.log(watch, metric, condition, result)
-
- # notify
- if condition.notify && self.trigger?(metric, result)
- self.notify(condition, messages.last)
- end
-
- # after-condition
- condition.after
-
- # get the destination
- dest =
- if result && condition.transition
- # condition override
- condition.transition
- else
- # regular
- metric.destination && metric.destination[result]
- end
-
- # transition or reschedule
- if dest
- # transition
- begin
- # watch.move(dest)
- Timer.get.schedule(condition)
- rescue EventRegistrationFailedError
- msg = watch.name + ' Event registration failed, moving back to previous state'
- applog(watch, :info, msg)
-
- dest = watch.state
- retry
- end
- else
- # reschedule
- Timer.get.schedule(condition)
- end
+ # # run the test
+ # result = condition.test
+ #
+ # # log
+ # messages = self.log(watch, metric, condition, result)
+ #
+ # # notify
+ # if condition.notify && self.trigger?(metric, result)
+ # self.notify(condition, messages.last)
+ # end
+ #
+ # # after-condition
+ # condition.after
+ #
+ # # get the destination
+ # dest =
+ # if result && condition.transition
+ # # condition override
+ # condition.transition
+ # else
+ # # regular
+ # metric.destination && metric.destination[result]
+ # end
+ #
+ # # transition or reschedule
+ # if dest
+ # # transition
+ # begin
+ # # watch.move(dest)
+ # Timer.get.schedule(condition)
+ # rescue EventRegistrationFailedError
+ # msg = watch.name + ' Event registration failed, moving back to previous state'
+ # applog(watch, :info, msg)
+ #
+ # dest = watch.state
+ # retry
+ # end
+ # else
+ # # reschedule
+ # Timer.get.schedule(condition)
+ # end
+ puts 'reschedule'
+ Timer.get.schedule(condition)
end
end
rescue Exception => e
View
@@ -55,17 +55,19 @@ def load_syslog
#
# Returns nothing
def log(watch, level, text)
+ return
+
# initialize watch log if necessary
self.logs[watch.name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT) if watch
# push onto capture and timeline for the given watch
- @templogio.truncate(0)
- @templogio.rewind
- @templog.send(level, text % [])
- @mutex.synchronize do
- @capture.puts(@templogio.string) if @capture
- self.logs[watch.name] << [Time.now, @templogio.string.dup] if watch
- end
+ # @templogio.truncate(0)
+ # @templogio.rewind
+ # @templog.send(level, text % [])
+ # @mutex.synchronize do
+ # @capture.puts(@templogio.string) if @capture
+ # self.logs[watch.name] << [Time.now, @templogio.string.dup] if watch
+ # end
# send to regular logger
self.send(level, text % [])

0 comments on commit 7d16651

Please sign in to comment.