thread 0.0.4.1 → 0.0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- @cond.broadcast
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
- @cond.wait @mutex
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
- @cond.wait @mutex
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
@@ -16,7 +16,6 @@ class Thread::Delay
16
16
  raise ArgumentError, 'no block given' unless block
17
17
 
18
18
  @mutex = Mutex.new
19
-
20
19
  @block = block
21
20
  end
22
21
 
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 = Mutex.new
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
- @cond.wait(@mutex, *timeout)
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
- @cond.broadcast
124
+
125
+ cond.broadcast if cond?
116
126
  }
117
127
 
118
128
  self
@@ -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
- @cond.broadcast
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
- @cond.wait(@mutex, *timeout)
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new {|s|
2
2
  s.name = 'thread'
3
- s.version = '0.0.4.1'
3
+ s.version = '0.0.4.2'
4
4
  s.author = 'meh.'
5
5
  s.email = 'meh@schizofreni.co'
6
6
  s.homepage = 'http://github.com/meh/ruby-thread'
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.1
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-01 00:00:00.000000000 Z
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.