lounger 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f572c4da7be5b5c1b915afb600078b1c12735b6
4
- data.tar.gz: 56d0fe7aa98d512c2fb0c9dd6e105002811d1735
3
+ metadata.gz: 11ac13fa2d6f6eb0b5154ed80d986d867764e151
4
+ data.tar.gz: 41ceb2e39c847fff0b12de6d374db13d6ded06e9
5
5
  SHA512:
6
- metadata.gz: f0af08df01820670c9b69a3bd08c8df9192534adbdfc4a36808a8834120599cc87c706e7c6e1cb85b259aa71f62d4ae63578dfdeefffad96dca96fa5a26e0043
7
- data.tar.gz: 79bc106a9c71ac210d0a3abe09c8e0effc3b5aad92c28fdcc3c296fe06a0aa19ececf507950601ee19b92fba73fdc3bd1cb73b203a9fc285288d6edd1e26d535
6
+ metadata.gz: d04aa8779babc1c568af75bb6d49e055da458805e10b4938354f31499b6f6f2cbc5eea03daa6c94e5d132f1f12e7329b82b0631945726d2c0d1347264945acc5
7
+ data.tar.gz: f67a0e8d973557924b912fc348c89a41416735fb527bd65388a2aa917cb481414288690eb43907f5cfa2bf855398173248002041f3b615558b4773371d2e2b31
data/lib/lounger.rb CHANGED
@@ -4,26 +4,57 @@ class Lounger
4
4
  SIGNALS = ["INT", "TERM", "EXIT", "USR1", "QUIT"]
5
5
 
6
6
  def initialize(include_signals: [], exclude_signals: [])
7
- @lock = Mutex.new
8
- @condition = ConditionVariable.new
7
+ @lock = Mutex.new
8
+ @condition = ConditionVariable.new
9
+ @pending_signals = 0
10
+ @buffer = []
11
+ @idle = false
9
12
 
10
- (SIGNALS + include_signals - exclude_signals).each do |signal|
11
- Signal.trap(signal) { wakeup! }
13
+ (SIGNALS + include_signals - exclude_signals).each do |s|
14
+ prev = Signal.trap(s) do
15
+ @condition.signal
16
+ prev.call unless prev.is_a?(String)
17
+ end
12
18
  end
13
19
  end
14
20
 
15
- def idle
16
- @lock.synchronize { @condition.wait(@lock) }
21
+ def idle(ignore_pending: false)
22
+ result = nil
23
+ @lock.synchronize do
24
+ @pending_signals = 0 if ignore_pending
25
+ @idle = true
26
+
27
+ if @pending_signals > 0
28
+ @pending_signals -= 1
29
+ else
30
+ @condition.wait(@lock)
31
+ @pending_signals -= 1
32
+ end
33
+
34
+ @idle = false
35
+ result = @buffer.shift
36
+ end
37
+
38
+ return result
17
39
  end
18
40
 
19
- def wakeup!
20
- @condition.signal
41
+ def idle?
42
+ return @idle
43
+ end
44
+
45
+ def wakeup!(value = nil)
46
+ @lock.synchronize do
47
+ @pending_signals += 1
48
+ @buffer << value
49
+ @condition.signal
50
+ end
21
51
  end
22
52
 
23
53
  def self.idle
24
54
  Lounger.new.idle
25
55
  end
26
56
 
57
+ alias_method :waiting?, :idle?
27
58
  alias_method :wait, :idle
28
59
  alias_method :signal, :wakeup!
29
60
  end
@@ -1,3 +1,3 @@
1
1
  class Lounger
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lounger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Groza Sergiu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-23 00:00:00.000000000 Z
11
+ date: 2017-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.4.6
95
+ rubygems_version: 2.6.10
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: A simple Ruby gem for current thread idling