lib/lounger.rb in lounger-0.2.0 vs lib/lounger.rb in lounger-0.3.1
- old
+ new
@@ -2,28 +2,59 @@
class Lounger
SIGNALS = ["INT", "TERM", "EXIT", "USR1", "QUIT"]
def initialize(include_signals: [], exclude_signals: [])
- @lock = Mutex.new
- @condition = ConditionVariable.new
+ @lock = Mutex.new
+ @condition = ConditionVariable.new
+ @pending_signals = 0
+ @buffer = []
+ @idle = false
- (SIGNALS + include_signals - exclude_signals).each do |signal|
- Signal.trap(signal) { wakeup! }
+ (SIGNALS + include_signals - exclude_signals).each do |s|
+ prev = Signal.trap(s) do
+ @condition.signal
+ prev.call unless prev.is_a?(String)
+ end
end
end
- def idle
- @lock.synchronize { @condition.wait(@lock) }
+ def idle(ignore_pending: false)
+ result = nil
+ @lock.synchronize do
+ @pending_signals = 0 if ignore_pending
+ @idle = true
+
+ if @pending_signals > 0
+ @pending_signals -= 1
+ else
+ @condition.wait(@lock)
+ @pending_signals -= 1
+ end
+
+ @idle = false
+ result = @buffer.shift
+ end
+
+ return result
end
- def wakeup!
- @condition.signal
+ def idle?
+ return @idle
end
+ def wakeup!(value = nil)
+ @lock.synchronize do
+ @pending_signals += 1
+ @buffer << value
+ @condition.signal
+ end
+ end
+
def self.idle
Lounger.new.idle
end
+ alias_method :waiting?, :idle?
alias_method :wait, :idle
alias_method :signal, :wakeup!
end
\ No newline at end of file