thread 0.0.1.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/README.md CHANGED
@@ -66,3 +66,45 @@ loop {
66
66
  sleep 0.5
67
67
  }
68
68
  ```
69
+
70
+ Promise
71
+ =======
72
+ This implements the promise pattern, allowing you to pass around an object
73
+ where you can send a value and extract a value, in a thread-safe way, accessing
74
+ the value will wait for the value to be delivered.
75
+
76
+ Example
77
+ -------
78
+
79
+ ```ruby
80
+ require 'thread/promise'
81
+
82
+ p = promise
83
+
84
+ Thread.new {
85
+ sleep 5
86
+ p << 42
87
+ }
88
+
89
+ puts ~p # => 42
90
+ ```
91
+
92
+ Future
93
+ ======
94
+ A future is somewhat a promise, except you pass it a block to execute in
95
+ another thread.
96
+
97
+ The value returned by the block will be the value of the promise.
98
+
99
+ Example
100
+ -------
101
+
102
+ ```ruby
103
+ require 'thread/future'
104
+
105
+ puts ~future {
106
+ sleep 5
107
+
108
+ 42
109
+ } # => 42
110
+ ```
@@ -0,0 +1,25 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ require 'thread/promise'
12
+
13
+ class Thread::Future < Thread::Promise
14
+ def initialize (&block)
15
+ Thread.new {
16
+ deliver block.call
17
+ }
18
+ end
19
+ end
20
+
21
+ module Kernel
22
+ def future (&block)
23
+ Thread::Future.new(&block)
24
+ end
25
+ end
@@ -18,9 +18,13 @@ class Thread::Pool
18
18
  attr_reader :pool, :timeout, :exception, :thread, :started_at
19
19
 
20
20
  def initialize (pool, *args, &block)
21
- @pool = pool
22
- @arguments = args
23
- @block = block
21
+ @pool = pool
22
+ @arguments = args
23
+ @block = block
24
+ @running = false
25
+ @finished = false
26
+ @timedout = false
27
+ @terminated = false
24
28
  end
25
29
 
26
30
  def running?; @running; end
@@ -221,7 +225,7 @@ class Thread::Pool
221
225
  end
222
226
 
223
227
  def wake_up_timeout
224
- if @pipes
228
+ if defined? @pipes
225
229
  @pipes.last.write_nonblock 'x' rescue nil
226
230
  end
227
231
  end
@@ -0,0 +1,64 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ require 'thread'
12
+
13
+ class Thread::Promise
14
+ def delivered?
15
+ instance_variable_defined? :@value
16
+ end
17
+
18
+ def deliver (value)
19
+ return if delivered?
20
+
21
+ @value = value
22
+
23
+ if cond?
24
+ mutex.synchronize {
25
+ cond.broadcast
26
+ }
27
+ end
28
+
29
+ self
30
+ end
31
+
32
+ alias << deliver
33
+
34
+ def value
35
+ return @value if delivered?
36
+
37
+ mutex.synchronize {
38
+ cond.wait(mutex)
39
+ }
40
+
41
+ return @value
42
+ end
43
+
44
+ alias ~ value
45
+
46
+ private
47
+ def cond?
48
+ instance_variable_defined? :@cond
49
+ end
50
+
51
+ def cond
52
+ @cond ||= ConditionVariable.new
53
+ end
54
+
55
+ def mutex
56
+ @mutex ||= Mutex.new
57
+ end
58
+ end
59
+
60
+ module Kernel
61
+ def promise
62
+ Thread::Promise.new
63
+ end
64
+ end
@@ -1,12 +1,12 @@
1
1
  Gem::Specification.new {|s|
2
2
  s.name = 'thread'
3
- s.version = '0.0.1.1'
3
+ s.version = '0.0.2'
4
4
  s.author = 'meh.'
5
5
  s.email = 'meh@schizofreni.co'
6
6
  s.homepage = 'http://github.com/meh/ruby-thread'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.summary = 'Various extensions to the base thread stdlib.'
9
- s.description = 'Includes a thread pool, message passing capabilities and a recursive mutex.'
9
+ s.description = 'Includes a thread pool, message passing capabilities, a recursive mutex, promise and future.'
10
10
 
11
11
  s.files = `git ls-files`.split("\n")
12
12
  s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
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.1.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-10 00:00:00.000000000 Z
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Includes a thread pool, message passing capabilities and a recursive
15
- mutex.
14
+ description: Includes a thread pool, message passing capabilities, a recursive mutex,
15
+ promise and future.
16
16
  email: meh@schizofreni.co
17
17
  executables: []
18
18
  extensions: []
@@ -20,7 +20,9 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - README.md
22
22
  - lib/thread/channel.rb
23
+ - lib/thread/future.rb
23
24
  - lib/thread/pool.rb
25
+ - lib/thread/promise.rb
24
26
  - lib/thread/recursive_mutex.rb
25
27
  - thread.gemspec
26
28
  homepage: http://github.com/meh/ruby-thread
@@ -43,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
45
  version: '0'
44
46
  requirements: []
45
47
  rubyforge_project:
46
- rubygems_version: 1.8.24
48
+ rubygems_version: 1.8.23
47
49
  signing_key:
48
50
  specification_version: 3
49
51
  summary: Various extensions to the base thread stdlib.