wires 0.1.8 → 0.1.9

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wires/hub.rb +25 -15
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07b54cd8c5d2566dc1df5dbbba359680e290cb44
4
- data.tar.gz: 32392ed09eae7079456c55ace22840b5d504aa55
3
+ metadata.gz: 457cb2ed2bd4af2a473bdd857e373aee0d3d9266
4
+ data.tar.gz: 2f55be33795d9bccd37e7d90afe95ca0387ceb32
5
5
  SHA512:
6
- metadata.gz: ec3dc587a26c0373cb9b8fd9872208608b6bb5a3f2445dcfd398623ab108b1e7da3205864cb0e15d6911a982ec9ad594e5e06d3975b1bbde075a03532037a3f6
7
- data.tar.gz: 5264ed0c4c84c2063e62e32876b1e40acd38587e52e9c40af05bb301ed9e381555c3d7a3fe588a4b0f48fedf149b44235b54db533d107db0054139844ca97798
6
+ metadata.gz: 396cc5c23cb7b7b0e8cd9c52fa9b438310593dd03ba96e4eaf5731b7d7e1933326a31a89d35cc453301f3746016ec1806e39be2afba08f7c8f09c988c44e0097
7
+ data.tar.gz: dafd5997b103c258f9fd3776c4eb9f62d11d28c41f0b389e95df7f45827492f9035d5389b140e7edb1d2cdb6a2ce4c151cc3f8ecd096b05ea5c580c2c2f3de73
data/lib/wires/hub.rb CHANGED
@@ -7,7 +7,7 @@ def puts(x) $stdout.puts(x) end
7
7
  # get called in new threads in the order received
8
8
  class Hub
9
9
  @queue = Queue.new
10
- @running = false
10
+ @state = [:dead, :alive, :dying][0]
11
11
 
12
12
  @before_kills = Queue.new
13
13
  @after_kills = Queue.new
@@ -15,13 +15,17 @@ class Hub
15
15
  # Operate on the metaclass as a type of singleton pattern
16
16
  class << self
17
17
 
18
- def running?; @running; end
18
+ def dead?; @state==:dead end
19
+ def alive?; @state==:alive end
20
+ def dying?; @state==:dying end
21
+ def state; @state end
22
+
23
+ def clear; @queue.clear end
19
24
 
20
25
  # Start the Hub event loop in a new thread
21
26
  def run
22
- if not @running
27
+ if dead?
23
28
  @thread = Thread.new() do
24
- @running = true
25
29
  self.send(:run_loop)
26
30
  end
27
31
 
@@ -31,17 +35,13 @@ class Hub
31
35
 
32
36
  # Start the Hub event loop in the current thread
33
37
  def run_in_place()
34
- self.send(:run_loop) unless @running
38
+ self.send(:run_loop) if dead?
35
39
  nil end
36
40
 
37
41
  # Kill the Hub event loop (softly)
38
- def kill();
39
- # Call the before kill hooks
40
- while not @before_kills.empty?
41
- @before_kills.shift.call
42
- end
42
+ def kill()
43
43
  # Stop the main event loop
44
- @running=false;
44
+ @state=:dying;
45
45
  end
46
46
 
47
47
  # Register hook to execute before kill - can call multiple times
@@ -64,7 +64,7 @@ class Hub
64
64
 
65
65
  # Put x in the queue, and block until x is processed (if Hub is running)
66
66
  def fire(x)
67
- if @running # yield to event loop thread until awoken by it later
67
+ if not dead? # yield to event loop thread until awoken by it later
68
68
  @queue << [x, Thread.current]
69
69
  sleep
70
70
  else # don't wait if Hub isn't running - would cause lockup
@@ -74,13 +74,23 @@ class Hub
74
74
  def <<(x); fire(x); end
75
75
 
76
76
  private
77
+
78
+ def die
79
+ # Call the before kill hooks # TODO move
80
+ while not @before_kills.empty?
81
+ @before_kills.shift.call
82
+ end
83
+ @state = :dead
84
+ end
77
85
 
78
86
  def run_loop
79
- @running = true
87
+ @state = :alive
80
88
 
81
- while @running
89
+ while not dead?
82
90
  if @queue.empty? then sleep(0)
83
91
  else process_item(@queue.shift) end
92
+
93
+ if dying?; die_thread ||= Thread.new { die } end
84
94
  end
85
95
 
86
96
  while not @after_kills.empty?
@@ -98,7 +108,7 @@ class Hub
98
108
  waiting_thread.wakeup if blocking and waiting_thread
99
109
 
100
110
  rescue Interrupt, SystemExit => e
101
- @running = false
111
+ @state = :dying
102
112
  unhandled_exception(e)
103
113
 
104
114
  rescue Exception => e
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wires
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain