channels 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/channel.rb +49 -0
- data/lib/channels.rb +1 -0
- metadata +47 -0
data/lib/channel.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
class Channel
|
2
|
+
attr_reader :thread
|
3
|
+
|
4
|
+
def initialize(*params, &block)
|
5
|
+
@writing_thread = nil
|
6
|
+
@reading_thread = nil
|
7
|
+
@mutex = Mutex.new
|
8
|
+
@value = []
|
9
|
+
@write_mutex = Mutex.new
|
10
|
+
@thread = Thread.new do
|
11
|
+
yield self
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def write(value)
|
16
|
+
@write_mutex.synchronize {
|
17
|
+
@mutex.synchronize {
|
18
|
+
@value.push value
|
19
|
+
if @reading_thread.nil?
|
20
|
+
@writing_thread = Thread.current
|
21
|
+
@mutex.sleep
|
22
|
+
@write_mutex.unlock
|
23
|
+
@writing_thread = nil
|
24
|
+
else
|
25
|
+
@reading_thread.wakeup
|
26
|
+
@reading_thread = nil
|
27
|
+
end
|
28
|
+
}
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
def read
|
33
|
+
@mutex.synchronize {
|
34
|
+
if @writing_thread
|
35
|
+
@writing_thread.wakeup
|
36
|
+
@writing_thread = nil
|
37
|
+
else
|
38
|
+
@reading_thread = Thread.current
|
39
|
+
@mutex.sleep
|
40
|
+
@reading_thread = nil
|
41
|
+
end
|
42
|
+
@value.shift
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def kill
|
47
|
+
@thread.kill
|
48
|
+
end
|
49
|
+
end
|
data/lib/channels.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'channel'
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: channels
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthew Wastrodowski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2010-01-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Go-lang style channels in ruby
|
15
|
+
email: towski@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/channel.rb
|
21
|
+
- lib/channels.rb
|
22
|
+
homepage: http://rubygems.org/gems/channels
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.25
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Channels
|
47
|
+
test_files: []
|