thread 0.0.6.2 → 0.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c934f1f9fc550daf4578bd312a80851a7ea76074
4
- data.tar.gz: 2d678d694a58381d2f197b902341f213c1b887cb
3
+ metadata.gz: ccee102c708a6f45b30e88830ed170d244be7036
4
+ data.tar.gz: e2ff57960a623ab11f82aefe7372900f589602ac
5
5
  SHA512:
6
- metadata.gz: 9d33610502d9bfc7630b03265743c9fcb5459d115553512b36f41e7d800077a08c96fc79de451c9a5a4f58cca47b7d17a70720e2f7a3d77b201a4eb8ccb9953b
7
- data.tar.gz: 555dee244c8984b1d8e5ee58f68bebeb12784872eb40b3a48fe6ea6dcaa42b75206dc19baf93967a13dcd2b738de717a32987a8c2bcee9b43686ff682404cb3a
6
+ metadata.gz: ed75e1af05833e2860b345a988f75cb985edd06564f78289a69a5bac1243609db45788c6682b55b1c2c313fd32d847ffda72e4b835fca506356b113c4fa06ef8
7
+ data.tar.gz: e0308daa7693621bdf0dd753ec329989c448a61e29e09645db50432db3367e07ce4ae2a0e2c1d5c72fcd2c608271df541fe56f32f8feadbe99a149bcc56b7413
data/README.md CHANGED
@@ -85,6 +85,29 @@ p << 2
85
85
  puts ~p # => 16
86
86
  ```
87
87
 
88
+ Process
89
+ =======
90
+ A process helps reducing programming errors coming from race conditions and the
91
+ like, the only way to interact with a process is through messages.
92
+
93
+ Multiple processes should talk with eachother through messages.
94
+
95
+ Example
96
+ -------
97
+
98
+ ```ruby
99
+ require 'thread/process'
100
+
101
+ p = Thread.process {
102
+ loop {
103
+ puts receive.inspect
104
+ }
105
+ }
106
+
107
+ p << 42
108
+ p << 23
109
+ ```
110
+
88
111
  Promise
89
112
  =======
90
113
  This implements the promise pattern, allowing you to pass around an object
@@ -0,0 +1,72 @@
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/channel'
12
+
13
+ # A process should only interact with the outside the outside through messages,
14
+ # it still uses a thread, but it should make it safer to use than sharing and
15
+ # locks.
16
+ class Thread::Process
17
+ def self.all
18
+ @@processes ||= {}
19
+ end
20
+
21
+ def self.register (name, process)
22
+ all[name] = process
23
+ end
24
+
25
+ def self.unregister (name)
26
+ all.delete(name)
27
+ end
28
+
29
+ def self.[] (name)
30
+ all[name]
31
+ end
32
+
33
+ # Create a new process executing the block.
34
+ def initialize (&block)
35
+ @channel = Thread::Channel.new
36
+
37
+ Thread.new {
38
+ instance_eval(&block)
39
+
40
+ @channel = nil
41
+ }
42
+ end
43
+
44
+ # Send a message to the process.
45
+ def send (what)
46
+ unless @channel
47
+ raise RuntimeError, 'the process has terminated'
48
+ end
49
+
50
+ @channel.send(what)
51
+
52
+ self
53
+ end
54
+
55
+ alias << send
56
+
57
+ private
58
+ def receive
59
+ @channel.receive
60
+ end
61
+
62
+ def receive!
63
+ @channel.receive!
64
+ end
65
+ end
66
+
67
+ class Thread
68
+ # Helper to create a process.
69
+ def self.process (&block)
70
+ Thread::Process.new(&block)
71
+ end
72
+ end
data/thread.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new {|s|
2
2
  s.name = 'thread'
3
- s.version = '0.0.6.2'
3
+ s.version = '0.0.7'
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thread
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6.2
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - meh.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-08 00:00:00.000000000 Z
11
+ date: 2013-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -53,6 +53,7 @@ files:
53
53
  - lib/thread/future.rb
54
54
  - lib/thread/pipe.rb
55
55
  - lib/thread/pool.rb
56
+ - lib/thread/process.rb
56
57
  - lib/thread/promise.rb
57
58
  - lib/thread/recursive_mutex.rb
58
59
  - tests/channel_spec.rb