Skip to content
This repository
Newer
Older
100644 178 lines (159 sloc) 4.07 kb
9598dde6 »
2011-03-05 Get Threads.pm6 working again after the backend rewrite
1 module Threads;
38b3ab29 »
2010-08-14 Add multithreading support
2
3 # Should be a role, since it can be applied to any class with minimal overhead
9598dde6 »
2011-03-05 Get Threads.pm6 working again after the backend rewrite
4 class Monitor is export {
bd64f652 »
2011-03-05 Finish monitor API, add ObjectPipe for moritz++
5 method enter() { Q:CgOp {
6 (rnull (rawscall System.Threading.Monitor.Enter (@ {self})))
7 } }
8 method exit() { Q:CgOp {
9 (rnull (rawscall System.Threading.Monitor.Exit (@ {self})))
10 } }
11 method pulse() { Q:CgOp {
12 (rnull (rawscall System.Threading.Monitor.Pulse (@ {self})))
13 } }
14 method pulse_all() { Q:CgOp {
15 (rnull (rawscall System.Threading.Monitor.PulseAll (@ {self})))
16 } }
17 method try_enter($t) { Q:CgOp {
18 (box Bool (rawscall System.Threading.Monitor.TryEnter (@ {self})
19 (cast int (obj_getnum {$t * 1000}))))
20 } }
21 method wait() { Q:CgOp {
22 (box Bool (rawscall System.Threading.Monitor.Wait (@ {self})))
23 } }
24 method try_wait($t) { Q:CgOp {
25 (box Bool (rawscall System.Threading.Monitor.Wait (@ {self})
26 (cast int (obj_getnum {$t * 1000}))))
27 } }
38b3ab29 »
2010-08-14 Add multithreading support
28 # TODO exception handling
193ca57c »
2011-12-03 [Threads.pm6] Fix build; exception-safe &lock
29 method lock($f) { self.enter; LEAVE self.exit; $f() }
38b3ab29 »
2010-08-14 Add multithreading support
30 }
31
5b8140e4 »
2010-08-14 Fudge Thread & Monitor to be correctly exported
32 sub lock($m,$f) is export { $m.lock($f); }
33
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
34 my class ObjectPipeWriteHandle {
35 has $.op;
36 method put($x) {
37 $.op.put($x);
38 }
39 method dup {
40 return $!op.write_handle();
41 }
42 method DESTROY {
43 $!op.writer_closed(self);
44 }
45 }
46
47 my class ObjectPipeReadHandle {
48 has $.op = 0;
403c7c4d »
2012-01-05 First shot at implementing feeds
49 has $.thread;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
50 method get() {
51 $.op.get();
52 }
53 method dup {
54 return $!op.read_handle();
55 }
56 method DESTROY {
57 $!op.reader_closed(self);
403c7c4d »
2012-01-05 First shot at implementing feeds
58 $!thread.join if $!thread;
59 }
60 }
61
62 my class ObjectPipeReadHandleIter is IterCursor {
63 has $.read;
64 method reify {
8f0740b6 »
2012-01-06 fix ObjectPipeReadHandleIter semantics
65 my $r = $!read.get();
66 if ($r === EMPTY) {
862300b0 »
2012-01-06 First attempt on map {} <== @a.
67 ();
8f0740b6 »
2012-01-06 fix ObjectPipeReadHandleIter semantics
68 } else {
69 ($r, self);
70 }
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
71 }
72 }
73
bd64f652 »
2011-03-05 Finish monitor API, add ObjectPipe for moritz++
74 class ObjectPipe {
75 has $!lock = Monitor.new;
76 has $!queue = [];
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
77 has $!max_buffer_size = 10;
78 has $!writers = 0;
79 has $!readers = 0;
80
81 method read_handle {
a988fa04 »
2012-01-04 Fixes in the implementation of the ObjectPipe
82 $!lock.enter;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
83 $!readers++;
84 my $read = ObjectPipeReadHandle.new();
85 $read.op = self;
a988fa04 »
2012-01-04 Fixes in the implementation of the ObjectPipe
86 $!lock.exit;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
87 return $read;
88 }
89
90 method reader_closed {
91 $!lock.enter;
a988fa04 »
2012-01-04 Fixes in the implementation of the ObjectPipe
92 $!readers--;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
93 $!lock.pulse;
94 $!lock.exit;
95 }
96
97 method write_handle {
a988fa04 »
2012-01-04 Fixes in the implementation of the ObjectPipe
98 $!lock.enter;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
99 $!writers++;
100 my $write = ObjectPipeWriteHandle.new();
101 $write.op = self;
a988fa04 »
2012-01-04 Fixes in the implementation of the ObjectPipe
102 $!lock.exit;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
103 return $write;
104 }
105
106 method writer_closed {
107 $!lock.enter;
a988fa04 »
2012-01-04 Fixes in the implementation of the ObjectPipe
108 $!writers--;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
109 $!lock.pulse;
110 $!lock.exit;
111 }
bd64f652 »
2011-03-05 Finish monitor API, add ObjectPipe for moritz++
112
113 method get() {
114 $!lock.enter;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
115 while (!$!queue && $!writers) {
116 $!lock.wait;
117 }
118 if (!($!queue || $!writers)) {
a988fa04 »
2012-01-04 Fixes in the implementation of the ObjectPipe
119 return EMPTY;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
120 }
bd64f652 »
2011-03-05 Finish monitor API, add ObjectPipe for moritz++
121 my $value = shift $!queue;
27499a69 »
2012-01-03 Add a max_buffer_size to the ObjectPipe
122 $!lock.pulse;
bd64f652 »
2011-03-05 Finish monitor API, add ObjectPipe for moritz++
123 $!lock.exit;
124 $value;
125 }
126
127 method put($x) {
128 $!lock.enter;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
129 while (($!queue.elems >= $!max_buffer_size) && $!readers) {
a988fa04 »
2012-01-04 Fixes in the implementation of the ObjectPipe
130 $!lock.wait;
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
131 }
132 if ($!readers < 1) {
133 die "Object Pipe closed";
134 }
bd64f652 »
2011-03-05 Finish monitor API, add ObjectPipe for moritz++
135 push $!queue, $x;
136 $!lock.pulse;
137 $!lock.exit;
138 }
139 }
140
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
141 sub objectpipe is export {
142 my $op = ObjectPipe.new();
403c7c4d »
2012-01-05 First shot at implementing feeds
143 my $read = $op.read_handle();
144 my $write = $op.write_handle();
145 return ($read, $write);
e593b09a »
2012-01-04 initial sketch of the full ObjectPipe framework
146 }
147
9598dde6 »
2011-03-05 Get Threads.pm6 working again after the backend rewrite
148 class Thread is export {
63628e6c »
2010-09-09 Generate slot mappings at compose time
149 has $!value;
38b3ab29 »
2010-08-14 Add multithreading support
150 method new($func) {
193ca57c »
2011-12-03 [Threads.pm6] Fix build; exception-safe &lock
151 Q:CgOp { (box (@ {Thread}) (start_p6_thread (@ {$func}))) }
38b3ab29 »
2010-08-14 Add multithreading support
152 }
153
154 method join() {
9598dde6 »
2011-03-05 Get Threads.pm6 working again after the backend rewrite
155 Q:CgOp { (rnull (rawcall Join (unbox clr:System.Threading.Thread (@ {self})))) }
38b3ab29 »
2010-08-14 Add multithreading support
156 }
157
3e68017e »
2012-01-03 Remove old/broken Threads.pm6 &sleep, ruoso++
158 method sleep($time) { sleep $time }
38b3ab29 »
2010-08-14 Add multithreading support
159 }
403c7c4d »
2012-01-05 First shot at implementing feeds
160
a17d4eb4 »
2012-01-07 define the precedence for the feed op, now I can use $a <== 1..20
161 sub infix:« <== »(\$output, @input) is Niecza::absprec<f=> is export {
403c7c4d »
2012-01-05 First shot at implementing feeds
162 my ($read, $write) = objectpipe();
862300b0 »
2012-01-06 First attempt on map {} <== @a.
163 my $it = ObjectPipeReadHandleIter.new();
164 $it.read = $read;
403c7c4d »
2012-01-05 First shot at implementing feeds
165 $read.thread = Thread.new({
a17d4eb4 »
2012-01-07 define the precedence for the feed op, now I can use $a <== 1..20
166 for @input -> $val {
403c7c4d »
2012-01-05 First shot at implementing feeds
167 $write.put($val);
168 }
169 $write.DESTROY;
170 });
862300b0 »
2012-01-06 First attempt on map {} <== @a.
171 if ($output ~~ Code) {
172 return $output.(@$it);
173 } else {
174 return $output = $it;
175 }
403c7c4d »
2012-01-05 First shot at implementing feeds
176 }
177
Something went wrong with that request. Please try again.