wires 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/wires/hub.rb +25 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 457cb2ed2bd4af2a473bdd857e373aee0d3d9266
|
4
|
+
data.tar.gz: 2f55be33795d9bccd37e7d90afe95ca0387ceb32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
@
|
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
|
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
|
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)
|
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
|
-
@
|
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
|
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
|
-
@
|
87
|
+
@state = :alive
|
80
88
|
|
81
|
-
while
|
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
|
-
@
|
111
|
+
@state = :dying
|
102
112
|
unhandled_exception(e)
|
103
113
|
|
104
114
|
rescue Exception => e
|