thread 0.0.4.1 → 0.0.4.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/thread/channel.rb +13 -4
- data/lib/thread/delay.rb +0 -1
- data/lib/thread/future.rb +15 -5
- data/lib/thread/promise.rb +15 -3
- data/thread.gemspec +1 -1
- metadata +2 -2
data/lib/thread/channel.rb
CHANGED
@@ -19,7 +19,6 @@ class Thread::Channel
|
|
19
19
|
def initialize (messages = [], &block)
|
20
20
|
@messages = []
|
21
21
|
@mutex = Mutex.new
|
22
|
-
@cond = ConditionVariable.new
|
23
22
|
@check = block
|
24
23
|
|
25
24
|
messages.each {|o|
|
@@ -38,7 +37,8 @@ class Thread::Channel
|
|
38
37
|
|
39
38
|
@mutex.synchronize {
|
40
39
|
@messages << what
|
41
|
-
|
40
|
+
|
41
|
+
cond.broadcast if cond?
|
42
42
|
}
|
43
43
|
|
44
44
|
self
|
@@ -59,14 +59,14 @@ class Thread::Channel
|
|
59
59
|
message = @messages.delete_at(index)
|
60
60
|
found = true
|
61
61
|
else
|
62
|
-
|
62
|
+
cond.wait @mutex
|
63
63
|
end
|
64
64
|
}
|
65
65
|
end
|
66
66
|
else
|
67
67
|
@mutex.synchronize {
|
68
68
|
if @messages.empty?
|
69
|
-
|
69
|
+
cond.wait @mutex
|
70
70
|
end
|
71
71
|
|
72
72
|
message = @messages.shift
|
@@ -86,4 +86,13 @@ class Thread::Channel
|
|
86
86
|
@messages.shift
|
87
87
|
end
|
88
88
|
end
|
89
|
+
|
90
|
+
private
|
91
|
+
def cond?
|
92
|
+
instance_variable_defined? :@cond
|
93
|
+
end
|
94
|
+
|
95
|
+
def cond
|
96
|
+
@cond ||= ConditionVariable.new
|
97
|
+
end
|
89
98
|
end
|
data/lib/thread/delay.rb
CHANGED
data/lib/thread/future.rb
CHANGED
@@ -20,9 +20,7 @@ class Thread::Future
|
|
20
20
|
def initialize (&block)
|
21
21
|
raise ArgumentError, 'no block given' unless block
|
22
22
|
|
23
|
-
@mutex
|
24
|
-
@cond = ConditionVariable.new
|
25
|
-
|
23
|
+
@mutex = Mutex.new
|
26
24
|
@thread = Thread.new {
|
27
25
|
begin
|
28
26
|
deliver block.call
|
@@ -77,13 +75,16 @@ class Thread::Future
|
|
77
75
|
#
|
78
76
|
# In case the block raises an exception, it will be raised, the exception is cached
|
79
77
|
# and will be raised every time you access the value.
|
78
|
+
#
|
79
|
+
# An optional timeout can be passed which will return nil if nothing has been
|
80
|
+
# delivered.
|
80
81
|
def value (timeout = nil)
|
81
82
|
raise @exception if exception?
|
82
83
|
|
83
84
|
return @value if delivered?
|
84
85
|
|
85
86
|
@mutex.synchronize {
|
86
|
-
|
87
|
+
cond.wait(@mutex, *timeout)
|
87
88
|
}
|
88
89
|
|
89
90
|
if exception?
|
@@ -107,12 +108,21 @@ class Thread::Future
|
|
107
108
|
alias ! value!
|
108
109
|
|
109
110
|
private
|
111
|
+
def cond?
|
112
|
+
instance_variable_defined? :@cond
|
113
|
+
end
|
114
|
+
|
115
|
+
def cond
|
116
|
+
@cond ||= ConditionVariable.new
|
117
|
+
end
|
118
|
+
|
110
119
|
def deliver (value)
|
111
120
|
return if delivered?
|
112
121
|
|
113
122
|
@mutex.synchronize {
|
114
123
|
@value = value
|
115
|
-
|
124
|
+
|
125
|
+
cond.broadcast if cond?
|
116
126
|
}
|
117
127
|
|
118
128
|
self
|
data/lib/thread/promise.rb
CHANGED
@@ -15,7 +15,6 @@ class Thread::Promise
|
|
15
15
|
# Create a promise.
|
16
16
|
def initialize
|
17
17
|
@mutex = Mutex.new
|
18
|
-
@cond = ConditionVariable.new
|
19
18
|
end
|
20
19
|
|
21
20
|
# Check if a value has been delivered.
|
@@ -33,7 +32,8 @@ class Thread::Promise
|
|
33
32
|
|
34
33
|
@mutex.synchronize {
|
35
34
|
@value = value
|
36
|
-
|
35
|
+
|
36
|
+
cond.broadcast if cond?
|
37
37
|
}
|
38
38
|
|
39
39
|
self
|
@@ -43,17 +43,29 @@ class Thread::Promise
|
|
43
43
|
|
44
44
|
# Get the value that's been delivered, if none has been delivered yet the call
|
45
45
|
# will block until one is delivered.
|
46
|
+
#
|
47
|
+
# An optional timeout can be passed which will return nil if nothing has been
|
48
|
+
# delivered.
|
46
49
|
def value (timeout = nil)
|
47
50
|
return @value if delivered?
|
48
51
|
|
49
52
|
@mutex.synchronize {
|
50
|
-
|
53
|
+
cond.wait(@mutex, *timeout)
|
51
54
|
}
|
52
55
|
|
53
56
|
return @value if delivered?
|
54
57
|
end
|
55
58
|
|
56
59
|
alias ~ value
|
60
|
+
|
61
|
+
private
|
62
|
+
def cond?
|
63
|
+
instance_variable_defined? :@cond
|
64
|
+
end
|
65
|
+
|
66
|
+
def cond
|
67
|
+
@cond ||= ConditionVariable.new
|
68
|
+
end
|
57
69
|
end
|
58
70
|
|
59
71
|
module Kernel
|
data/thread.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thread
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.4.
|
4
|
+
version: 0.0.4.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: 2013-03-
|
12
|
+
date: 2013-03-12 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Includes a thread pool, message passing capabilities, a recursive mutex,
|
15
15
|
promise, future and delay.
|