thread 0.0.6 → 0.0.6.1
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 +4 -4
- data/lib/thread/pipe.rb +37 -16
- data/tests/pipe_spec.rb +10 -0
- data/thread.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3ff37def7bb5f33d4edbfe721d2fd4ed76736c8
|
4
|
+
data.tar.gz: da06d07f29d4da2ad5ae0c965dd3b857ea2f4693
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee229921f532e80cdcbccd1c3f68222b61b262e2bb46ecd0c47a149a5a37d43015705066108ca8a190b7d400b63a9b8be4ac1479669817e02e735cad876c77a0
|
7
|
+
data.tar.gz: 905962ca00d67d8acbc8eaefcc5be048da9fe799fb0a7f8cc1aa4fbd3c0e52dd88b61a30e9bf175c83dd7625a6bab3428978adb5fb0e9ea4661724185f281a82
|
data/lib/thread/pipe.rb
CHANGED
@@ -14,25 +14,37 @@ require 'thread'
|
|
14
14
|
# each datum inserted in the pipe is passed along through queues to the various
|
15
15
|
# functions composing the pipe, the final result is inserted in the final queue.
|
16
16
|
class Thread::Pipe
|
17
|
+
# A task incapsulates a part of the pipe.
|
17
18
|
class Task
|
18
19
|
attr_accessor :input, :output
|
19
20
|
|
21
|
+
# Create a Task which will call the passed function and get input
|
22
|
+
# from the optional parameter and put output in the optional parameter.
|
20
23
|
def initialize (func, input = Queue.new, output = Queue.new)
|
21
|
-
@input
|
22
|
-
@output
|
24
|
+
@input = input
|
25
|
+
@output = output
|
26
|
+
@handling = false
|
23
27
|
|
24
28
|
@thread = Thread.new {
|
25
29
|
while true
|
30
|
+
value = @input.deq
|
31
|
+
|
32
|
+
@handling = true
|
26
33
|
begin
|
27
|
-
value = @input.deq
|
28
34
|
value = func.call(value)
|
29
|
-
|
30
35
|
@output.enq value
|
31
36
|
rescue Exception; end
|
37
|
+
@handling = false
|
32
38
|
end
|
33
39
|
}
|
34
40
|
end
|
35
41
|
|
42
|
+
# Check if the task has nothing to do.
|
43
|
+
def empty?
|
44
|
+
!@handling && @input.empty? && @output.empty?
|
45
|
+
end
|
46
|
+
|
47
|
+
# Stop the task.
|
36
48
|
def kill
|
37
49
|
@thread.raise
|
38
50
|
end
|
@@ -51,21 +63,13 @@ class Thread::Pipe
|
|
51
63
|
ObjectSpace.define_finalizer(self, self.class.finalize(@tasks))
|
52
64
|
end
|
53
65
|
|
66
|
+
# @private
|
54
67
|
def self.finalize (tasks)
|
55
68
|
proc {
|
56
69
|
tasks.each(&:kill)
|
57
70
|
}
|
58
71
|
end
|
59
72
|
|
60
|
-
# Insert data in the pipe.
|
61
|
-
def << (data)
|
62
|
-
return if @tasks.empty?
|
63
|
-
|
64
|
-
@input.enq data
|
65
|
-
|
66
|
-
self
|
67
|
-
end
|
68
|
-
|
69
73
|
# Add a task to the pipe, it must respond to #call and #arity,
|
70
74
|
# and #arity must return 1.
|
71
75
|
def | (func)
|
@@ -81,13 +85,30 @@ class Thread::Pipe
|
|
81
85
|
self
|
82
86
|
end
|
83
87
|
|
88
|
+
# Check if the pipe is empty.
|
89
|
+
def empty?
|
90
|
+
@input.empty? && @output.empty? && @tasks.all?(&:empty?)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Insert data in the pipe.
|
94
|
+
def enq (data)
|
95
|
+
return if @tasks.empty?
|
96
|
+
|
97
|
+
@input.enq data
|
98
|
+
|
99
|
+
self
|
100
|
+
end
|
101
|
+
|
102
|
+
alias push enq
|
103
|
+
alias << enq
|
104
|
+
|
84
105
|
# Get an element from the output queue.
|
85
|
-
def
|
106
|
+
def deq (non_block = false)
|
86
107
|
@output.deq(non_block)
|
87
108
|
end
|
88
109
|
|
89
|
-
alias deq
|
90
|
-
alias ~
|
110
|
+
alias pop deq
|
111
|
+
alias ~ deq
|
91
112
|
end
|
92
113
|
|
93
114
|
class Thread
|
data/tests/pipe_spec.rb
CHANGED
@@ -12,4 +12,14 @@ describe Thread::Pipe do
|
|
12
12
|
p.deq.should == 16
|
13
13
|
p.deq.should == 32
|
14
14
|
end
|
15
|
+
|
16
|
+
it 'empty works properly' do
|
17
|
+
p = Thread |-> d { sleep 0.2; d * 2 } |-> d { d * 4 }
|
18
|
+
|
19
|
+
p.empty?.should == true
|
20
|
+
p.enq 42
|
21
|
+
p.empty?.should == false
|
22
|
+
p.deq
|
23
|
+
p.empty?.should == true
|
24
|
+
end
|
15
25
|
end
|
data/thread.gemspec
CHANGED