polyphony 0.99.4 → 0.99.6
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/.rubocop.yml +11 -0
- data/.yardopts +0 -2
- data/README.md +1 -1
- data/docs/readme.md +1 -1
- data/docs/tutorial.md +2 -2
- data/examples/pipes/gzip_http_server.rb +2 -2
- data/examples/pipes/http_server.rb +1 -1
- data/examples/pipes/tcp_proxy.rb +1 -1
- data/ext/polyphony/backend_common.c +4 -4
- data/ext/polyphony/backend_io_uring.c +8 -8
- data/ext/polyphony/backend_libev.c +5 -5
- data/ext/polyphony/fiber.c +33 -42
- data/ext/polyphony/io_extensions.c +50 -37
- data/ext/polyphony/pipe.c +6 -20
- data/ext/polyphony/polyphony.c +72 -144
- data/ext/polyphony/queue.c +23 -63
- data/ext/polyphony/thread.c +4 -13
- data/lib/polyphony/adapters/process.rb +2 -5
- data/lib/polyphony/adapters/sequel.rb +2 -2
- data/lib/polyphony/core/debug.rb +1 -4
- data/lib/polyphony/core/exceptions.rb +1 -5
- data/lib/polyphony/core/resource_pool.rb +7 -8
- data/lib/polyphony/core/sync.rb +5 -8
- data/lib/polyphony/core/thread_pool.rb +3 -10
- data/lib/polyphony/core/throttler.rb +1 -5
- data/lib/polyphony/core/timer.rb +23 -30
- data/lib/polyphony/extensions/fiber.rb +513 -543
- data/lib/polyphony/extensions/io.rb +5 -14
- data/lib/polyphony/extensions/object.rb +283 -2
- data/lib/polyphony/extensions/openssl.rb +5 -26
- data/lib/polyphony/extensions/pipe.rb +6 -17
- data/lib/polyphony/extensions/socket.rb +24 -118
- data/lib/polyphony/extensions/thread.rb +3 -18
- data/lib/polyphony/extensions/timeout.rb +0 -1
- data/lib/polyphony/net.rb +5 -9
- data/lib/polyphony/version.rb +1 -1
- data/lib/polyphony.rb +2 -6
- data/test/test_io.rb +221 -221
- data/test/test_socket.rb +3 -3
- data/test/test_trace.rb +2 -2
- metadata +5 -9
- data/docs/index.md +0 -94
- data/docs/link_rewriter.rb +0 -17
- data/docs/main-concepts/index.md +0 -9
- data/lib/polyphony/core/global_api.rb +0 -309
- /data/{assets → docs/assets}/echo-fibers.svg +0 -0
- /data/{assets → docs/assets}/polyphony-logo.png +0 -0
- /data/{assets → docs/assets}/sleeping-fiber.svg +0 -0
data/ext/polyphony/polyphony.c
CHANGED
@@ -27,31 +27,23 @@ ID ID_R;
|
|
27
27
|
ID ID_W;
|
28
28
|
ID ID_RW;
|
29
29
|
|
30
|
-
/*
|
31
|
-
* snooze
|
32
|
-
* Polyphony.snooze
|
33
|
-
*
|
34
|
-
* Switches to the next fiber in the current thread's runqueue after adding the
|
30
|
+
/* Switches to the next fiber in the current thread's runqueue after adding the
|
35
31
|
* current fiber to the runqueue. This lets other fibers run, letting the
|
36
32
|
* current fiber eventually continue its work. This call is useful when
|
37
33
|
* performing long-running calculations in order to keep the program responsive.
|
38
|
-
*
|
39
|
-
* @return [
|
34
|
+
*
|
35
|
+
* @return [any] resume value
|
40
36
|
*/
|
41
37
|
|
42
38
|
VALUE Polyphony_snooze(VALUE self) {
|
43
39
|
return Backend_snooze(BACKEND());
|
44
40
|
}
|
45
41
|
|
46
|
-
/*
|
47
|
-
* suspend
|
48
|
-
* Polyphony.suspend
|
49
|
-
*
|
50
|
-
* Switches to the next fiber in the current thread's runqueue without
|
42
|
+
/* Switches to the next fiber in the current thread's runqueue without
|
51
43
|
* rescheduling the current fiber. This is useful if the current fiber does not
|
52
44
|
* need to continue or will be scheduled by other means eventually.
|
53
|
-
*
|
54
|
-
* @return [
|
45
|
+
*
|
46
|
+
* @return [any] resume value
|
55
47
|
*/
|
56
48
|
|
57
49
|
static VALUE Polyphony_suspend(VALUE self) {
|
@@ -62,15 +54,12 @@ static VALUE Polyphony_suspend(VALUE self) {
|
|
62
54
|
return ret;
|
63
55
|
}
|
64
56
|
|
65
|
-
/*
|
66
|
-
* Polyphony.backend_accept(server_socket, socket_class) -> socket
|
67
|
-
*
|
68
|
-
* Accepts an incoming connection on the given server socket, returning an
|
57
|
+
/* Accepts an incoming connection on the given server socket, returning an
|
69
58
|
* instance of the given socket class.
|
70
|
-
*
|
59
|
+
*
|
71
60
|
* @param server_socket [Socket] socket to accept on
|
72
61
|
* @param socket_class [Class] class of the socket to instantiate for the accepted connection
|
73
|
-
*
|
62
|
+
*
|
74
63
|
* @return [Socket] accepted connection
|
75
64
|
*/
|
76
65
|
|
@@ -78,16 +67,13 @@ VALUE Polyphony_backend_accept(VALUE self, VALUE server_socket, VALUE socket_cla
|
|
78
67
|
return Backend_accept(BACKEND(), server_socket, socket_class);
|
79
68
|
}
|
80
69
|
|
81
|
-
/*
|
82
|
-
* Polyphony.backend_accept_loop(server_socket, socket_class) { |socket| ... }
|
83
|
-
*
|
84
|
-
* Runs an infinite loop accepting connections on the given server socket,
|
70
|
+
/* Runs an infinite loop accepting connections on the given server socket,
|
85
71
|
* returning an instance of the given socket class.
|
86
|
-
*
|
72
|
+
*
|
87
73
|
* @param server_socket [Socket] socket to accept on
|
88
74
|
* @param socket_class [Class] class of the socket to instantiate for the accepted connection
|
89
|
-
* @yield [Socket] accepted connection
|
90
|
-
* @return [
|
75
|
+
* @yield [Socket] accepted connection
|
76
|
+
* @return [nil]
|
91
77
|
*/
|
92
78
|
|
93
79
|
VALUE Polyphony_backend_accept_loop(VALUE self, VALUE server_socket, VALUE socket_class) {
|
@@ -95,14 +81,11 @@ VALUE Polyphony_backend_accept_loop(VALUE self, VALUE server_socket, VALUE socke
|
|
95
81
|
}
|
96
82
|
|
97
83
|
#ifdef HAVE_IO_URING_PREP_MULTISHOT_ACCEPT
|
98
|
-
/*
|
99
|
-
* Polyphony.backend_multishot_accept(server_socket) { ... }
|
100
|
-
*
|
101
|
-
* Starts a multishot accept operation on the given server socket. This API is
|
84
|
+
/* Starts a multishot accept operation on the given server socket. This API is
|
102
85
|
* available only for the io_uring backend.
|
103
|
-
*
|
86
|
+
*
|
104
87
|
* @param server_socket [Socket] socket to accept on
|
105
|
-
* @return [
|
88
|
+
* @return [any] block return value
|
106
89
|
*/
|
107
90
|
|
108
91
|
VALUE Polyphony_backend_multishot_accept(VALUE self, VALUE server_socket) {
|
@@ -111,15 +94,12 @@ VALUE Polyphony_backend_multishot_accept(VALUE self, VALUE server_socket) {
|
|
111
94
|
#endif
|
112
95
|
|
113
96
|
|
114
|
-
/*
|
115
|
-
* Polyphony.backend_connect(socket, addr, port) -> socket
|
97
|
+
/* Connects the given socket to the given address and port.
|
116
98
|
*
|
117
|
-
* Connects the given socket to the given address and port.
|
118
|
-
*
|
119
99
|
* @param io [Socket] socket to connect
|
120
100
|
* @param addr [String] address to connect to
|
121
101
|
* @param port [Integer] port to connect to
|
122
|
-
*
|
102
|
+
*
|
123
103
|
* @return [Socket] accepted connection
|
124
104
|
*/
|
125
105
|
|
@@ -127,36 +107,29 @@ VALUE Polyphony_backend_connect(VALUE self, VALUE io, VALUE addr, VALUE port) {
|
|
127
107
|
return Backend_connect(BACKEND(), io, addr, port);
|
128
108
|
}
|
129
109
|
|
130
|
-
/*
|
131
|
-
*
|
110
|
+
/* Runs a feed loop, reading data from the given io, feeding it to the receiver
|
111
|
+
* with the given method. The loop terminates when EOF is encountered. If a
|
112
|
+
* block is given, it is used as the block for the method call to the receiver.
|
132
113
|
*
|
133
|
-
* Runs a feed loop, reading data from the given io, feeding it to the receiver
|
134
|
-
* with the given method, and passing the receiver's output to the given block.
|
135
|
-
* The loop terminates when EOF is encountered.
|
136
|
-
*
|
137
114
|
* @param io [IO] io to read from
|
138
115
|
* @param receiver [any] an object receiving the data
|
139
116
|
* @param method [Symbol] method used to feed the data to the receiver
|
140
|
-
*
|
141
|
-
*
|
142
|
-
* @return [void]
|
117
|
+
*
|
118
|
+
* @return [IO] io
|
143
119
|
*/
|
144
120
|
|
145
121
|
VALUE Polyphony_backend_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method) {
|
146
122
|
return Backend_feed_loop(BACKEND(), io, receiver, method);
|
147
123
|
}
|
148
124
|
|
149
|
-
/*
|
150
|
-
* Polyphony.backend_read(io, buffer, length, to_eof, pos) -> buffer
|
125
|
+
/* Reads from the given io.
|
151
126
|
*
|
152
|
-
* Reads from the given io.
|
153
|
-
*
|
154
127
|
* @param io [IO] io to read from
|
155
128
|
* @param buffer [String, nil] buffer to read into
|
156
129
|
* @param length [Integer] maximum bytes to read
|
157
130
|
* @param to_eof [boolean] whether to read to EOF
|
158
131
|
* @param pos [Integer] Position in the buffer to read into
|
159
|
-
*
|
132
|
+
*
|
160
133
|
* @return [String] buffer
|
161
134
|
*/
|
162
135
|
|
@@ -164,32 +137,26 @@ VALUE Polyphony_backend_read(VALUE self, VALUE io, VALUE buffer, VALUE length, V
|
|
164
137
|
return Backend_read(BACKEND(), io, buffer, length, to_eof, pos);
|
165
138
|
}
|
166
139
|
|
167
|
-
/*
|
168
|
-
* Polyphony.backend_read_loop(io, max_len)
|
169
|
-
*
|
170
|
-
* Performs an infinite loop reading data from the given io. The loop terminates
|
140
|
+
/* Performs an infinite loop reading data from the given io. The loop terminates
|
171
141
|
* when EOF is encountered.
|
172
|
-
*
|
142
|
+
*
|
173
143
|
* @param io [IO] io to read from
|
174
144
|
* @param maxlen [Integer] maximum bytes to read
|
175
|
-
*
|
176
|
-
* @return [
|
145
|
+
*
|
146
|
+
* @return [IO] io
|
177
147
|
*/
|
178
148
|
|
179
149
|
VALUE Polyphony_backend_read_loop(VALUE self, VALUE io, VALUE maxlen) {
|
180
150
|
return Backend_read_loop(BACKEND(), io, maxlen);
|
181
151
|
}
|
182
152
|
|
183
|
-
/*
|
184
|
-
* Polyphony.backend_recv(io, buffer, length, pos) -> buffer
|
153
|
+
/* Receives data on the given io.
|
185
154
|
*
|
186
|
-
* Receives data on the given io.
|
187
|
-
*
|
188
155
|
* @param io [Socket] io to receive on
|
189
156
|
* @param buffer [String, nil] buffer to read into
|
190
157
|
* @param length [Integer] maximum bytes to read
|
191
158
|
* @param pos [Integer] Position in the buffer to read into
|
192
|
-
*
|
159
|
+
*
|
193
160
|
* @return [String] buffer
|
194
161
|
*/
|
195
162
|
|
@@ -197,11 +164,8 @@ VALUE Polyphony_backend_recv(VALUE self, VALUE io, VALUE buffer, VALUE length, V
|
|
197
164
|
return Backend_recv(BACKEND(), io, buffer, length, pos);
|
198
165
|
}
|
199
166
|
|
200
|
-
/*
|
201
|
-
* Polyphony.backend_recvmsg(socket, buffer, maxlen, pos, flags, maxcontrollen, opts) -> buffer
|
167
|
+
/* Receives a message on the given socket.
|
202
168
|
*
|
203
|
-
* Receives a message on the given socket.
|
204
|
-
*
|
205
169
|
* @param socket [UDPSocket] io to receive on
|
206
170
|
* @param buffer [String, nil] buffer to read into
|
207
171
|
* @param maxlen [Integer] maximum bytes to read
|
@@ -209,7 +173,6 @@ VALUE Polyphony_backend_recv(VALUE self, VALUE io, VALUE buffer, VALUE length, V
|
|
209
173
|
* @param flags [Integer] Flags
|
210
174
|
* @param maxcontrollen [Integer] Maximum control bytes
|
211
175
|
* @param opts [Hash] Options
|
212
|
-
*
|
213
176
|
* @return [String] buffer
|
214
177
|
*/
|
215
178
|
|
@@ -217,50 +180,41 @@ VALUE Polyphony_backend_recvmsg(VALUE self, VALUE socket, VALUE buffer, VALUE ma
|
|
217
180
|
return Backend_recvmsg(BACKEND(), socket, buffer, maxlen, pos, flags, maxcontrollen, opts);
|
218
181
|
}
|
219
182
|
|
220
|
-
/*
|
221
|
-
*
|
183
|
+
/* Performs an infinite loop receiving data on the given socket. The loop
|
184
|
+
* terminates when the socket is closed.
|
222
185
|
*
|
223
|
-
* Performs an infinite loop receiving data on the given socket. The loop terminates
|
224
|
-
* when the socket is closed.
|
225
|
-
*
|
226
186
|
* @param socket [Socket] socket to receive on
|
227
187
|
* @param maxlen [Integer] maximum bytes to read
|
228
|
-
*
|
229
|
-
* @return [
|
188
|
+
* @yield [data] received data
|
189
|
+
* @return [Socket] socket
|
230
190
|
*/
|
231
191
|
|
232
192
|
VALUE Polyphony_backend_recv_loop(VALUE self, VALUE socket, VALUE maxlen) {
|
233
193
|
return Backend_recv_loop(BACKEND(), socket, maxlen);
|
234
194
|
}
|
235
195
|
|
236
|
-
/*
|
237
|
-
*
|
196
|
+
/* Runs a feed loop, receiving data on the given socket, feeding it to the
|
197
|
+
* receiver with the given method. The loop terminates when EOF is encountered.
|
198
|
+
* If a block is given, it is used as the block for the method call to the
|
199
|
+
* receiver.
|
238
200
|
*
|
239
|
-
* Runs a feed loop, receiving data on the given socket, feeding it to the receiver
|
240
|
-
* with the given method, and passing the receiver's output to the given block.
|
241
|
-
* The loop terminates when EOF is encountered.
|
242
|
-
*
|
243
201
|
* @param socket [Socket] socket to receive on
|
244
202
|
* @param receiver [any] an object receiving the data
|
245
203
|
* @param method [Symbol] method used to feed the data to the receiver
|
246
|
-
*
|
247
|
-
*
|
248
|
-
* @return [void]
|
204
|
+
*
|
205
|
+
* @return [Socket] socket
|
249
206
|
*/
|
250
207
|
|
251
208
|
VALUE Polyphony_backend_recv_feed_loop(VALUE self, VALUE socket, VALUE receiver, VALUE method) {
|
252
209
|
return Backend_recv_feed_loop(BACKEND(), socket, receiver, method);
|
253
210
|
}
|
254
211
|
|
255
|
-
/*
|
256
|
-
* Polyphony.backend_send(socket, msg, flags) -> bytes_sent
|
212
|
+
/* Sends data on the given socket, returning the number of bytes sent.
|
257
213
|
*
|
258
|
-
* Sends data on the given socket, returning the number of bytes sent.
|
259
|
-
*
|
260
214
|
* @param socket [Socket] socket to read from
|
261
215
|
* @param msg [String] data to be sent
|
262
216
|
* @param flags [Integer] Flags
|
263
|
-
*
|
217
|
+
*
|
264
218
|
* @return [Integer] number of bytes sent
|
265
219
|
*/
|
266
220
|
|
@@ -268,11 +222,8 @@ VALUE Polyphony_backend_send(VALUE self, VALUE socket, VALUE msg, VALUE flags) {
|
|
268
222
|
return Backend_send(BACKEND(), socket, msg, flags);
|
269
223
|
}
|
270
224
|
|
271
|
-
/*
|
272
|
-
* Polyphony.backend_sendmsg(socket, msg, flags, dest_sockaddr, controls) -> bytes_sent
|
225
|
+
/* Sends data on the given socket, returning the number of bytes sent.
|
273
226
|
*
|
274
|
-
* Sends data on the given socket, returning the number of bytes sent.
|
275
|
-
*
|
276
227
|
* @param socket [Socket] socket to read from
|
277
228
|
* @param msg [String] data to be sent
|
278
229
|
* @param flags [Integer] Flags
|
@@ -285,11 +236,9 @@ VALUE Polyphony_backend_sendmsg(VALUE self, VALUE socket, VALUE msg, VALUE flags
|
|
285
236
|
return Backend_sendmsg(BACKEND(), socket, msg, flags, dest_sockaddr, controls);
|
286
237
|
}
|
287
238
|
|
288
|
-
/*
|
289
|
-
*
|
239
|
+
/* Sends multiple strings on the given socket, returning the number of bytes
|
240
|
+
* sent.
|
290
241
|
*
|
291
|
-
* Sends multiple strings on the given socket, returning the number of bytes sent.
|
292
|
-
*
|
293
242
|
* @param socket [Socket] socket to read from
|
294
243
|
* @param ary [Array<String>] data to be sent
|
295
244
|
* @param flags [Integer] Flags
|
@@ -300,25 +249,19 @@ VALUE Polyphony_backend_sendv(VALUE self, VALUE socket, VALUE ary, VALUE flags)
|
|
300
249
|
return Backend_sendv(BACKEND(), socket, ary, flags);
|
301
250
|
}
|
302
251
|
|
303
|
-
/*
|
304
|
-
* Polyphony.backend_sleep(duration)
|
252
|
+
/* Sleeps for the given duration, yielding execution to other fibers.
|
305
253
|
*
|
306
|
-
* Sleeps for the given duration, yielding execution to other fibers.
|
307
|
-
*
|
308
254
|
* @param duration [Number] duration in seconds
|
309
|
-
* @return [
|
255
|
+
* @return [nil]
|
310
256
|
*/
|
311
257
|
|
312
258
|
VALUE Polyphony_backend_sleep(VALUE self, VALUE duration) {
|
313
259
|
return Backend_sleep(BACKEND(), duration);
|
314
260
|
}
|
315
261
|
|
316
|
-
/*
|
317
|
-
* Polyphony.backend_splice(src, dest, maxlen) -> bytes_spliced
|
318
|
-
*
|
319
|
-
* Splices data from the given source to the given destination, returning the
|
262
|
+
/* Splices data from the given source to the given destination, returning the
|
320
263
|
* number of bytes spliced.
|
321
|
-
*
|
264
|
+
*
|
322
265
|
* @param src [IO] source
|
323
266
|
* @param dest [IO] destination
|
324
267
|
* @param maxlen [Integer] Maximum bytes to splice
|
@@ -345,69 +288,57 @@ VALUE Polyphony_backend_tee(VALUE self, VALUE src, VALUE dest, VALUE chunksize)
|
|
345
288
|
}
|
346
289
|
#endif
|
347
290
|
|
348
|
-
/*
|
349
|
-
* Polyphony.backend_timeout(duration) { ... }
|
350
|
-
* Polyphony.backend_timeout(duration, exception_class) { ... }
|
351
|
-
*
|
352
|
-
* Runs the given block, raising an exception if the block has not finished
|
291
|
+
/* Runs the given block, raising an exception if the block has not finished
|
353
292
|
* running before a timeout has elapsed, using the given duration. If an
|
354
293
|
* exception class is not given, a TimeoutError is raised.
|
355
|
-
*
|
294
|
+
*
|
295
|
+
* @overload backend_timeout(duration)
|
296
|
+
* @param duration [Number] timeout duration in seconds
|
297
|
+
* @return [any] return value of block
|
298
|
+
* @overload backend_timeout(duration, exception_class)
|
299
|
+
* @param duration [Number] timeout duration in seconds
|
300
|
+
* @param exception_class [Class] exception class to raise in case of timeout
|
301
|
+
* @return [any] return value of block
|
356
302
|
*/
|
357
303
|
|
358
304
|
VALUE Polyphony_backend_timeout(int argc,VALUE *argv, VALUE self) {
|
359
305
|
return Backend_timeout(argc, argv, BACKEND());
|
360
306
|
}
|
361
307
|
|
362
|
-
/*
|
363
|
-
* Polyphony.backend_timer_loop(interval) { ... }
|
308
|
+
/* Runs an infinite loop that calls the given block at the specified time interval.
|
364
309
|
*
|
365
|
-
* Runs an infinite loop that calls the given block at the specified time interval.
|
366
|
-
*
|
367
310
|
* @param interval [Number] interval in seconds
|
368
|
-
* @yield [] code to run
|
369
|
-
* @return [void]
|
370
311
|
*/
|
371
312
|
|
372
313
|
VALUE Polyphony_backend_timer_loop(VALUE self, VALUE interval) {
|
373
314
|
return Backend_timer_loop(BACKEND(), interval);
|
374
315
|
}
|
375
316
|
|
376
|
-
/*
|
377
|
-
* Polyphony.backend_wait_event(raise)
|
378
|
-
*
|
379
|
-
* For for the current fiber to be rescheduled, resuming the fiber with its
|
317
|
+
/* For for the current fiber to be rescheduled, resuming the fiber with its
|
380
318
|
* resumed value. If raise is true and the resumed value is an exception, an
|
381
319
|
* exception will be raised.
|
382
|
-
*
|
320
|
+
*
|
383
321
|
* @param raise [boolean]
|
384
|
-
* @return [any] resumed value
|
385
322
|
*/
|
386
323
|
|
387
324
|
VALUE Polyphony_backend_wait_event(VALUE self, VALUE raise) {
|
388
325
|
return Backend_wait_event(BACKEND(), raise);
|
389
326
|
}
|
390
327
|
|
391
|
-
/*
|
392
|
-
* Polyphony.backend_wait_io(io, read_or_write)
|
393
|
-
*
|
394
|
-
* Waits for the given IO to be readable or writeable, according to the
|
328
|
+
/* Waits for the given IO to be readable or writeable, according to the
|
395
329
|
* read_or_write parameter.
|
396
|
-
*
|
330
|
+
*
|
397
331
|
* @param io [IO]
|
398
332
|
* @param write [boolean] false for read, true for write
|
399
|
-
* @return [
|
333
|
+
* @return [nil]
|
400
334
|
*/
|
401
335
|
|
402
336
|
VALUE Polyphony_backend_wait_io(VALUE self, VALUE io, VALUE write) {
|
403
337
|
return Backend_wait_io(BACKEND(), io, write);
|
404
338
|
}
|
405
339
|
|
406
|
-
/*
|
407
|
-
* Polyphony.backend_wait_pid(pid) -> exit code
|
340
|
+
/* Waits for the given process to terminate, returning its exit code.
|
408
341
|
*
|
409
|
-
* Waits for the given process to terminate, returning its exit code.
|
410
|
-
*
|
411
342
|
* @param pid [Integer] pid
|
412
343
|
* @return [Integer] exit code
|
413
344
|
*/
|
@@ -416,10 +347,7 @@ VALUE Polyphony_backend_waitpid(VALUE self, VALUE pid) {
|
|
416
347
|
return Backend_waitpid(BACKEND(), pid);
|
417
348
|
}
|
418
349
|
|
419
|
-
/*
|
420
|
-
* Polyphony.backend_write(io, data, ...) -> bytes_written
|
421
|
-
*
|
422
|
-
* Writes one or more strings to the given io, returning the total number of
|
350
|
+
/* Writes one or more strings to the given io, returning the total number of
|
423
351
|
* bytes written.
|
424
352
|
*/
|
425
353
|
|
@@ -450,7 +378,7 @@ VALUE Polyphony_raw_buffer_get(int argc, VALUE *argv, VALUE self) {
|
|
450
378
|
|
451
379
|
struct buffer_spec *buffer_spec = FIX2PTR(buf);
|
452
380
|
int length = (len == Qnil) ? buffer_spec->len : FIX2INT(len);
|
453
|
-
|
381
|
+
|
454
382
|
if (length > buffer_spec->len) length = buffer_spec->len;
|
455
383
|
return rb_utf8_str_new((char *)buffer_spec->ptr, length);
|
456
384
|
}
|
@@ -462,7 +390,7 @@ VALUE Polyphony_raw_buffer_set(VALUE self, VALUE buffer, VALUE str) {
|
|
462
390
|
int len = RSTRING_LEN(str);
|
463
391
|
if (len > buffer_spec->len)
|
464
392
|
rb_raise(rb_eRuntimeError, "Given string does not fit in given buffer");
|
465
|
-
|
393
|
+
|
466
394
|
memcpy(buffer_spec->ptr, RSTRING_PTR(str), len);
|
467
395
|
buffer_spec->len = len;
|
468
396
|
return self;
|
@@ -504,7 +432,7 @@ void Init_Polyphony(void) {
|
|
504
432
|
rb_define_singleton_method(mPolyphony, "backend_sendv", Polyphony_backend_sendv, 3);
|
505
433
|
rb_define_singleton_method(mPolyphony, "backend_sleep", Polyphony_backend_sleep, 1);
|
506
434
|
rb_define_singleton_method(mPolyphony, "backend_splice", Polyphony_backend_splice, 3);
|
507
|
-
|
435
|
+
|
508
436
|
#ifdef POLYPHONY_BACKEND_LIBURING
|
509
437
|
rb_define_singleton_method(mPolyphony, "backend_double_splice", Polyphony_backend_double_splice, 2);
|
510
438
|
#endif
|
@@ -512,7 +440,7 @@ void Init_Polyphony(void) {
|
|
512
440
|
#ifdef POLYPHONY_LINUX
|
513
441
|
rb_define_singleton_method(mPolyphony, "backend_tee", Polyphony_backend_tee, 3);
|
514
442
|
#endif
|
515
|
-
|
443
|
+
|
516
444
|
rb_define_singleton_method(mPolyphony, "backend_timeout", Polyphony_backend_timeout, -1);
|
517
445
|
rb_define_singleton_method(mPolyphony, "backend_timer_loop", Polyphony_backend_timer_loop, 1);
|
518
446
|
rb_define_singleton_method(mPolyphony, "backend_wait_event", Polyphony_backend_wait_event, 1);
|
data/ext/polyphony/queue.c
CHANGED
@@ -61,16 +61,14 @@ static VALUE Queue_allocate(VALUE klass) {
|
|
61
61
|
#define GetQueue(obj, queue) \
|
62
62
|
TypedData_Get_Struct((obj), Queue_t, &Queue_type, (queue))
|
63
63
|
|
64
|
-
/*
|
65
|
-
* Queue.new -> queue
|
66
|
-
* Queue.new(capacity) -> queue
|
67
|
-
*
|
68
|
-
* Initializes a queue instance. If the capacity is given, the queue becomes
|
64
|
+
/* Initializes a queue instance. If the capacity is given, the queue becomes
|
69
65
|
* capped, i.e. it cannot contain more elements than its capacity. When trying
|
70
66
|
* to add items to a capped queue that is full, the current fiber will block
|
71
67
|
* until at least one item is removed from the queue.
|
72
|
-
*
|
73
|
-
* @
|
68
|
+
*
|
69
|
+
* @overload new()
|
70
|
+
* @overload new(capacity)
|
71
|
+
* @param capacity [Integer] maximum items in queue
|
74
72
|
*/
|
75
73
|
|
76
74
|
static VALUE Queue_initialize(int argc, VALUE *argv, VALUE self) {
|
@@ -131,7 +129,7 @@ static inline void capped_queue_block_push(Queue_t *queue) {
|
|
131
129
|
*
|
132
130
|
* Adds the given value to the queue's end. If the queue is capped and full, the
|
133
131
|
* call will block until a value is removed from the queue.
|
134
|
-
*
|
132
|
+
*
|
135
133
|
* @param value [any] value to be added to the queue
|
136
134
|
* @return [Queue] self
|
137
135
|
*/
|
@@ -151,12 +149,9 @@ VALUE Queue_push(VALUE self, VALUE value) {
|
|
151
149
|
return self;
|
152
150
|
}
|
153
151
|
|
154
|
-
/*
|
155
|
-
* queue.unshift(value) -> queue
|
156
|
-
*
|
157
|
-
* Adds the given value to the queue's beginning. If the queue is capped and
|
152
|
+
/* Adds the given value to the queue's beginning. If the queue is capped and
|
158
153
|
* full, the call will block until a value is removed from the queue.
|
159
|
-
*
|
154
|
+
*
|
160
155
|
* @param value [any] value to be added to the queue
|
161
156
|
* @return [Queue] self
|
162
157
|
*/
|
@@ -230,6 +225,7 @@ VALUE Queue_shift_block(Queue_t *queue) {
|
|
230
225
|
* the queue is empty, a ThreadError exception is raised. In blocking mode, if
|
231
226
|
* the queue is empty, the call will block until an item is added to the queue.
|
232
227
|
*
|
228
|
+
* @param nonblock [boolean] non-blocking mode
|
233
229
|
* @return [any] first value in queue
|
234
230
|
*/
|
235
231
|
|
@@ -243,10 +239,7 @@ VALUE Queue_shift(int argc,VALUE *argv, VALUE self) {
|
|
243
239
|
Queue_shift_block(queue);
|
244
240
|
}
|
245
241
|
|
246
|
-
/*
|
247
|
-
* queue.delete(value) -> queue
|
248
|
-
*
|
249
|
-
* Removes the given value from the queue.
|
242
|
+
/* Removes the given value from the queue.
|
250
243
|
*
|
251
244
|
* @return [Queue] self
|
252
245
|
*/
|
@@ -263,10 +256,7 @@ VALUE Queue_delete(VALUE self, VALUE value) {
|
|
263
256
|
return self;
|
264
257
|
}
|
265
258
|
|
266
|
-
/*
|
267
|
-
* queue.cap(capacity) -> queue
|
268
|
-
*
|
269
|
-
* Sets the capacity for the queue to the given value. If 0 or nil is given, the
|
259
|
+
/* Sets the capacity for the queue to the given value. If 0 or nil is given, the
|
270
260
|
* queue becomes uncapped.
|
271
261
|
*
|
272
262
|
* @param cap [Integer, nil] new capacity
|
@@ -287,10 +277,7 @@ VALUE Queue_cap(VALUE self, VALUE cap) {
|
|
287
277
|
return self;
|
288
278
|
}
|
289
279
|
|
290
|
-
/*
|
291
|
-
* queue.capped? -> bool
|
292
|
-
*
|
293
|
-
* Returns true if the queue is capped.
|
280
|
+
/* Returns true if the queue is capped.
|
294
281
|
*
|
295
282
|
* @return [boolean] is the queue capped
|
296
283
|
*/
|
@@ -302,10 +289,7 @@ VALUE Queue_capped_p(VALUE self) {
|
|
302
289
|
return queue->capacity ? INT2FIX(queue->capacity) : Qnil;
|
303
290
|
}
|
304
291
|
|
305
|
-
/*
|
306
|
-
* queue.clear -> queue
|
307
|
-
*
|
308
|
-
* Removes all values from the queue.
|
292
|
+
/* Removes all values from the queue.
|
309
293
|
*
|
310
294
|
* @return [Queue] self
|
311
295
|
*/
|
@@ -327,13 +311,10 @@ long Queue_len(VALUE self) {
|
|
327
311
|
return queue->values.count;
|
328
312
|
}
|
329
313
|
|
330
|
-
/*
|
331
|
-
* queue.shift_each { |value| do_something(value) } -> queue
|
332
|
-
*
|
333
|
-
* Iterates over all values in the queue, removing each item and passing it to
|
314
|
+
/* Iterates over all values in the queue, removing each item and passing it to
|
334
315
|
* the given block.
|
335
316
|
*
|
336
|
-
* @yield [any]
|
317
|
+
* @yield [any] removed item
|
337
318
|
* @return [Queue] self
|
338
319
|
*/
|
339
320
|
|
@@ -346,10 +327,7 @@ VALUE Queue_shift_each(VALUE self) {
|
|
346
327
|
return self;
|
347
328
|
}
|
348
329
|
|
349
|
-
/*
|
350
|
-
* queue.shift_all -> array
|
351
|
-
*
|
352
|
-
* Returns all values currently in the queue, clearing the queue.
|
330
|
+
/* Returns all values currently in the queue, clearing the queue.
|
353
331
|
*
|
354
332
|
* @return [Array] all values
|
355
333
|
*/
|
@@ -365,10 +343,7 @@ VALUE Queue_shift_all(VALUE self) {
|
|
365
343
|
return result;
|
366
344
|
}
|
367
345
|
|
368
|
-
/*
|
369
|
-
* queue.flush_waiters -> queue
|
370
|
-
*
|
371
|
-
* Flushes all fibers currently blocked waiting to remove items from the queue,
|
346
|
+
/* Flushes all fibers currently blocked waiting to remove items from the queue,
|
372
347
|
* resuming them with the given value.
|
373
348
|
*
|
374
349
|
* @param value [any] value to resome all waiting fibers with
|
@@ -389,10 +364,7 @@ VALUE Queue_flush_waiters(VALUE self, VALUE value) {
|
|
389
364
|
return self;
|
390
365
|
}
|
391
366
|
|
392
|
-
/*
|
393
|
-
* queue.empty? -> bool
|
394
|
-
*
|
395
|
-
* Returns true if the queue is empty.
|
367
|
+
/* Returns true if the queue is empty.
|
396
368
|
*
|
397
369
|
* @return [boolean]
|
398
370
|
*/
|
@@ -404,10 +376,7 @@ VALUE Queue_empty_p(VALUE self) {
|
|
404
376
|
return (!queue->values.count) ? Qtrue : Qfalse;
|
405
377
|
}
|
406
378
|
|
407
|
-
/*
|
408
|
-
* queue.pending? -> bool
|
409
|
-
*
|
410
|
-
* Returns true if any fibers are currently waiting to remove items from the
|
379
|
+
/* Returns true if any fibers are currently waiting to remove items from the
|
411
380
|
* queue.
|
412
381
|
*
|
413
382
|
* @return [boolean]
|
@@ -420,10 +389,7 @@ VALUE Queue_pending_p(VALUE self) {
|
|
420
389
|
return (queue->shift_queue.count) ? Qtrue : Qfalse;
|
421
390
|
}
|
422
391
|
|
423
|
-
/*
|
424
|
-
* queue.num_waiting -> integer
|
425
|
-
*
|
426
|
-
* Returns the number of fibers currently waiting to remove items from the
|
392
|
+
/* Returns the number of fibers currently waiting to remove items from the
|
427
393
|
* queue.
|
428
394
|
*
|
429
395
|
* @return [Integer]
|
@@ -438,7 +404,7 @@ VALUE Queue_num_waiting(VALUE self) {
|
|
438
404
|
|
439
405
|
/* call-seq:
|
440
406
|
* queue.size -> integer
|
441
|
-
* queue.length -> integer
|
407
|
+
* queue.length -> integer
|
442
408
|
*
|
443
409
|
* Returns the number of values currently in the queue.
|
444
410
|
*
|
@@ -452,10 +418,7 @@ VALUE Queue_size_m(VALUE self) {
|
|
452
418
|
return INT2FIX(queue->values.count);
|
453
419
|
}
|
454
420
|
|
455
|
-
/*
|
456
|
-
* queue.closed? -> bool
|
457
|
-
*
|
458
|
-
* Returns true if the queue has been closed.
|
421
|
+
/* Returns true if the queue has been closed.
|
459
422
|
*
|
460
423
|
* @return [boolean]
|
461
424
|
*/
|
@@ -467,10 +430,7 @@ VALUE Queue_closed_p(VALUE self) {
|
|
467
430
|
return (queue->closed) ? Qtrue : Qfalse;
|
468
431
|
}
|
469
432
|
|
470
|
-
/*
|
471
|
-
* queue.close -> queue
|
472
|
-
*
|
473
|
-
* Marks the queue as closed. Any fibers currently waiting on the queue are
|
433
|
+
/* Marks the queue as closed. Any fibers currently waiting on the queue are
|
474
434
|
* resumed with a `nil` value. After the queue is closed, trying to remove items
|
475
435
|
* from the queue will cause a `ClosedQueueError` to be raised.
|
476
436
|
*
|