channels 0.0.1 → 0.0.2
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.
- data/lib/channel.rb +28 -6
- data/lib/channels.rb +1 -0
- metadata +2 -2
data/lib/channel.rb
CHANGED
@@ -1,21 +1,36 @@
|
|
1
1
|
class Channel
|
2
|
-
attr_reader :thread
|
2
|
+
attr_reader :thread, :sender, :channel_out
|
3
3
|
|
4
4
|
def initialize(*params, &block)
|
5
5
|
@writing_thread = nil
|
6
6
|
@reading_thread = nil
|
7
|
+
@channel_out = params.last || self
|
7
8
|
@mutex = Mutex.new
|
9
|
+
@sender = nil
|
8
10
|
@value = []
|
9
11
|
@write_mutex = Mutex.new
|
10
|
-
|
11
|
-
yield self
|
12
|
+
if block_given?
|
13
|
+
@thread = new_thread { yield self }
|
14
|
+
else
|
15
|
+
raise "Must implement run" unless respond_to?(:run)
|
16
|
+
@thread = new_thread { run }
|
12
17
|
end
|
13
18
|
end
|
14
19
|
|
15
|
-
def
|
20
|
+
def new_thread(&block)
|
21
|
+
@thread = Thread.new {
|
22
|
+
begin
|
23
|
+
yield block
|
24
|
+
rescue => e
|
25
|
+
puts "error running thread #{e}"
|
26
|
+
end
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def write(value, sender = nil)
|
16
31
|
@write_mutex.synchronize {
|
17
32
|
@mutex.synchronize {
|
18
|
-
@value.push value
|
33
|
+
@value.push(:value => value, :sender => sender)
|
19
34
|
if @reading_thread.nil?
|
20
35
|
@writing_thread = Thread.current
|
21
36
|
@mutex.sleep
|
@@ -29,6 +44,10 @@ class Channel
|
|
29
44
|
}
|
30
45
|
end
|
31
46
|
|
47
|
+
def write_out(value)
|
48
|
+
@channel_out.write(value, self)
|
49
|
+
end
|
50
|
+
|
32
51
|
def read
|
33
52
|
@mutex.synchronize {
|
34
53
|
if @writing_thread
|
@@ -39,10 +58,13 @@ class Channel
|
|
39
58
|
@mutex.sleep
|
40
59
|
@reading_thread = nil
|
41
60
|
end
|
42
|
-
@value.shift
|
61
|
+
response = @value.shift
|
62
|
+
@sender = response[:sender]
|
63
|
+
response[:value]
|
43
64
|
}
|
44
65
|
end
|
45
66
|
|
67
|
+
|
46
68
|
def kill
|
47
69
|
@thread.kill
|
48
70
|
end
|
data/lib/channels.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: channels
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Go-lang style channels in ruby
|
15
15
|
email: towski@gmail.com
|