io-event 1.10.2 → 1.11.0
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
- checksums.yaml.gz.sig +0 -0
- data/ext/extconf.rb +10 -0
- data/ext/io/event/event.c +5 -1
- data/ext/io/event/event.h +4 -0
- data/ext/io/event/fiber.c +1 -1
- data/ext/io/event/worker_pool.c +464 -0
- data/ext/io/event/worker_pool.h +8 -0
- data/ext/io/event/worker_pool_test.c +200 -0
- data/ext/io/event/worker_pool_test.h +9 -0
- data/lib/io/event/version.rb +1 -1
- data/readme.md +4 -0
- data/releases.md +26 -0
- data.tar.gz.sig +0 -0
- metadata +6 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5601614e57f23e24edb8f34dfdaf5b87ca33e902312c27fa51dc9af375ab76c
|
4
|
+
data.tar.gz: c791965fb7be4525b4894de46f1a14787a19ea68920d4009e89713e3aebc1001
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f662e6208b30d95aeb68d68ea9e3459bd51b6dc920d7bfcaa4794ffa455316443d2da3c6e954805bd8daadd0339341aab09c225054d9e81903866d63b39d9f0
|
7
|
+
data.tar.gz: 0016b73d68044784ec52d270725d7f4a8bde5064bfd54c5fd3d685f36771d235ae16719fa1ca0a3f9d4dd246894e84d74e8e5025605ebcfc86fb9b5d6be39696
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/ext/extconf.rb
CHANGED
@@ -58,6 +58,16 @@ have_func("epoll_pwait2")
|
|
58
58
|
|
59
59
|
have_header("ruby/io/buffer.h")
|
60
60
|
|
61
|
+
# Feature detection for blocking operation support
|
62
|
+
if have_func("rb_fiber_scheduler_blocking_operation_extract")
|
63
|
+
# Feature detection for pthread support (needed for WorkerPool)
|
64
|
+
if have_header("pthread.h")
|
65
|
+
append_cflags(["-DHAVE_IO_EVENT_WORKER_POOL"])
|
66
|
+
$srcs << "io/event/worker_pool.c"
|
67
|
+
$srcs << "io/event/worker_pool_test.c"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
61
71
|
if ENV.key?("RUBY_SANITIZE")
|
62
72
|
$stderr.puts "Enabling sanitizers..."
|
63
73
|
|
data/ext/io/event/event.c
CHANGED
@@ -14,7 +14,11 @@ void Init_IO_Event(void)
|
|
14
14
|
VALUE IO_Event = rb_define_module_under(rb_cIO, "Event");
|
15
15
|
|
16
16
|
Init_IO_Event_Fiber(IO_Event);
|
17
|
-
|
17
|
+
|
18
|
+
#ifdef HAVE_IO_EVENT_WORKER_POOL
|
19
|
+
Init_IO_Event_WorkerPool(IO_Event);
|
20
|
+
#endif
|
21
|
+
|
18
22
|
VALUE IO_Event_Selector = rb_define_module_under(IO_Event, "Selector");
|
19
23
|
Init_IO_Event_Selector(IO_Event_Selector);
|
20
24
|
|
data/ext/io/event/event.h
CHANGED
data/ext/io/event/fiber.c
CHANGED
@@ -35,7 +35,7 @@ VALUE IO_Event_Fiber_raise(VALUE fiber, int argc, VALUE *argv) {
|
|
35
35
|
#ifndef HAVE_RB_FIBER_CURRENT
|
36
36
|
static ID id_current;
|
37
37
|
|
38
|
-
|
38
|
+
VALUE IO_Event_Fiber_current(void) {
|
39
39
|
return rb_funcall(rb_cFiber, id_current, 0);
|
40
40
|
}
|
41
41
|
#endif
|
@@ -0,0 +1,464 @@
|
|
1
|
+
// Released under the MIT License.
|
2
|
+
// Copyright, 2025, by Samuel Williams.
|
3
|
+
|
4
|
+
#include "worker_pool.h"
|
5
|
+
#include "worker_pool_test.h"
|
6
|
+
#include "fiber.h"
|
7
|
+
|
8
|
+
#include <ruby/thread.h>
|
9
|
+
#include <ruby/fiber/scheduler.h>
|
10
|
+
|
11
|
+
#include <pthread.h>
|
12
|
+
#include <stdbool.h>
|
13
|
+
#include <stdlib.h>
|
14
|
+
#include <errno.h>
|
15
|
+
#include <string.h>
|
16
|
+
|
17
|
+
enum {
|
18
|
+
DEBUG = 0,
|
19
|
+
};
|
20
|
+
|
21
|
+
static VALUE IO_Event_WorkerPool;
|
22
|
+
static ID id_maximum_worker_count;
|
23
|
+
|
24
|
+
// Thread pool structure
|
25
|
+
struct IO_Event_WorkerPool_Worker {
|
26
|
+
VALUE thread;
|
27
|
+
|
28
|
+
// Flag to indicate this specific worker should exit:
|
29
|
+
bool interrupted;
|
30
|
+
|
31
|
+
// Currently executing operation:
|
32
|
+
rb_fiber_scheduler_blocking_operation_t *current_blocking_operation;
|
33
|
+
|
34
|
+
struct IO_Event_WorkerPool *pool;
|
35
|
+
struct IO_Event_WorkerPool_Worker *next;
|
36
|
+
};
|
37
|
+
|
38
|
+
// Work item structure
|
39
|
+
struct IO_Event_WorkerPool_Work {
|
40
|
+
rb_fiber_scheduler_blocking_operation_t *blocking_operation;
|
41
|
+
|
42
|
+
bool completed;
|
43
|
+
|
44
|
+
VALUE scheduler;
|
45
|
+
VALUE blocker;
|
46
|
+
VALUE fiber;
|
47
|
+
|
48
|
+
struct IO_Event_WorkerPool_Work *next;
|
49
|
+
};
|
50
|
+
|
51
|
+
// Worker pool structure
|
52
|
+
struct IO_Event_WorkerPool {
|
53
|
+
pthread_mutex_t mutex;
|
54
|
+
pthread_cond_t work_available;
|
55
|
+
|
56
|
+
struct IO_Event_WorkerPool_Work *work_queue;
|
57
|
+
struct IO_Event_WorkerPool_Work *work_queue_tail;
|
58
|
+
|
59
|
+
struct IO_Event_WorkerPool_Worker *workers;
|
60
|
+
size_t current_worker_count;
|
61
|
+
size_t maximum_worker_count;
|
62
|
+
|
63
|
+
size_t call_count;
|
64
|
+
size_t completed_count;
|
65
|
+
size_t cancelled_count;
|
66
|
+
|
67
|
+
bool shutdown;
|
68
|
+
};
|
69
|
+
|
70
|
+
// Free functions for Ruby GC
|
71
|
+
static void worker_pool_free(void *ptr) {
|
72
|
+
struct IO_Event_WorkerPool *pool = (struct IO_Event_WorkerPool *)ptr;
|
73
|
+
|
74
|
+
if (pool) {
|
75
|
+
// Signal shutdown to all workers
|
76
|
+
if (!pool->shutdown) {
|
77
|
+
pthread_mutex_lock(&pool->mutex);
|
78
|
+
pool->shutdown = true;
|
79
|
+
pthread_cond_broadcast(&pool->work_available);
|
80
|
+
pthread_mutex_unlock(&pool->mutex);
|
81
|
+
}
|
82
|
+
|
83
|
+
// Note: We don't free worker structures or wait for threads during GC
|
84
|
+
// as this can cause deadlocks. The Ruby GC will handle the thread objects.
|
85
|
+
// Workers will see the shutdown flag and exit cleanly.
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
// Size functions for Ruby GC
|
90
|
+
static size_t worker_pool_size(const void *ptr) {
|
91
|
+
return sizeof(struct IO_Event_WorkerPool);
|
92
|
+
}
|
93
|
+
|
94
|
+
// Ruby TypedData structures
|
95
|
+
static const rb_data_type_t IO_Event_WorkerPool_type = {
|
96
|
+
"IO::Event::WorkerPool",
|
97
|
+
{0, worker_pool_free, worker_pool_size,},
|
98
|
+
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
99
|
+
};
|
100
|
+
|
101
|
+
// Helper function to enqueue work (must be called with mutex held)
|
102
|
+
static void enqueue_work(struct IO_Event_WorkerPool *pool, struct IO_Event_WorkerPool_Work *work) {
|
103
|
+
if (pool->work_queue_tail) {
|
104
|
+
pool->work_queue_tail->next = work;
|
105
|
+
} else {
|
106
|
+
pool->work_queue = work;
|
107
|
+
}
|
108
|
+
pool->work_queue_tail = work;
|
109
|
+
}
|
110
|
+
|
111
|
+
// Helper function to dequeue work (must be called with mutex held)
|
112
|
+
static struct IO_Event_WorkerPool_Work *dequeue_work(struct IO_Event_WorkerPool *pool) {
|
113
|
+
struct IO_Event_WorkerPool_Work *work = pool->work_queue;
|
114
|
+
if (work) {
|
115
|
+
pool->work_queue = work->next;
|
116
|
+
if (!pool->work_queue) {
|
117
|
+
pool->work_queue_tail = NULL;
|
118
|
+
}
|
119
|
+
work->next = NULL; // Clear the next pointer for safety
|
120
|
+
}
|
121
|
+
return work;
|
122
|
+
}
|
123
|
+
|
124
|
+
// Unblock function to interrupt a specific worker.
|
125
|
+
static void worker_unblock_func(void *_worker) {
|
126
|
+
struct IO_Event_WorkerPool_Worker *worker = (struct IO_Event_WorkerPool_Worker *)_worker;
|
127
|
+
struct IO_Event_WorkerPool *pool = worker->pool;
|
128
|
+
|
129
|
+
// Mark this specific worker as interrupted
|
130
|
+
pthread_mutex_lock(&pool->mutex);
|
131
|
+
worker->interrupted = true;
|
132
|
+
pthread_cond_broadcast(&pool->work_available);
|
133
|
+
pthread_mutex_unlock(&pool->mutex);
|
134
|
+
|
135
|
+
// If there's a currently executing blocking operation, cancel it
|
136
|
+
if (worker->current_blocking_operation) {
|
137
|
+
rb_fiber_scheduler_blocking_operation_cancel(worker->current_blocking_operation);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
// Function to wait for work and execute it without GVL.
|
142
|
+
static void *worker_wait_and_execute(void *_worker) {
|
143
|
+
struct IO_Event_WorkerPool_Worker *worker = (struct IO_Event_WorkerPool_Worker *)_worker;
|
144
|
+
struct IO_Event_WorkerPool *pool = worker->pool;
|
145
|
+
|
146
|
+
while (true) {
|
147
|
+
struct IO_Event_WorkerPool_Work *work = NULL;
|
148
|
+
|
149
|
+
pthread_mutex_lock(&pool->mutex);
|
150
|
+
|
151
|
+
// Wait for work, shutdown, or interruption
|
152
|
+
while (!pool->work_queue && !pool->shutdown && !worker->interrupted) {
|
153
|
+
pthread_cond_wait(&pool->work_available, &pool->mutex);
|
154
|
+
}
|
155
|
+
|
156
|
+
if (pool->shutdown || worker->interrupted) {
|
157
|
+
pthread_mutex_unlock(&pool->mutex);
|
158
|
+
break;
|
159
|
+
}
|
160
|
+
|
161
|
+
work = dequeue_work(pool);
|
162
|
+
|
163
|
+
pthread_mutex_unlock(&pool->mutex);
|
164
|
+
|
165
|
+
// Execute work WITHOUT GVL (this is the whole point!)
|
166
|
+
if (work) {
|
167
|
+
worker->current_blocking_operation = work->blocking_operation;
|
168
|
+
rb_fiber_scheduler_blocking_operation_execute(work->blocking_operation);
|
169
|
+
worker->current_blocking_operation = NULL;
|
170
|
+
}
|
171
|
+
|
172
|
+
return work;
|
173
|
+
}
|
174
|
+
|
175
|
+
return NULL; // Shutdown signal
|
176
|
+
}
|
177
|
+
|
178
|
+
static VALUE worker_thread_func(void *_worker) {
|
179
|
+
struct IO_Event_WorkerPool_Worker *worker = (struct IO_Event_WorkerPool_Worker *)_worker;
|
180
|
+
|
181
|
+
while (true) {
|
182
|
+
// Wait for work and execute it without holding GVL
|
183
|
+
struct IO_Event_WorkerPool_Work *work = (struct IO_Event_WorkerPool_Work *)rb_thread_call_without_gvl(worker_wait_and_execute, worker, worker_unblock_func, worker);
|
184
|
+
|
185
|
+
if (!work) {
|
186
|
+
// Shutdown signal received
|
187
|
+
break;
|
188
|
+
}
|
189
|
+
|
190
|
+
// Protected by GVL:
|
191
|
+
work->completed = true;
|
192
|
+
worker->pool->completed_count++;
|
193
|
+
|
194
|
+
// Work was executed without GVL, now unblock the waiting fiber (we have GVL here)
|
195
|
+
rb_fiber_scheduler_unblock(work->scheduler, work->blocker, work->fiber);
|
196
|
+
}
|
197
|
+
|
198
|
+
return Qnil;
|
199
|
+
}
|
200
|
+
|
201
|
+
// Create a new worker thread
|
202
|
+
static int create_worker_thread(struct IO_Event_WorkerPool *pool) {
|
203
|
+
if (pool->current_worker_count >= pool->maximum_worker_count) {
|
204
|
+
return -1;
|
205
|
+
}
|
206
|
+
|
207
|
+
struct IO_Event_WorkerPool_Worker *worker = malloc(sizeof(struct IO_Event_WorkerPool_Worker));
|
208
|
+
if (!worker) {
|
209
|
+
return -1;
|
210
|
+
}
|
211
|
+
|
212
|
+
worker->pool = pool;
|
213
|
+
worker->interrupted = false;
|
214
|
+
worker->current_blocking_operation = NULL;
|
215
|
+
worker->next = pool->workers;
|
216
|
+
|
217
|
+
worker->thread = rb_thread_create(worker_thread_func, worker);
|
218
|
+
if (NIL_P(worker->thread)) {
|
219
|
+
free(worker);
|
220
|
+
return -1;
|
221
|
+
}
|
222
|
+
|
223
|
+
pool->workers = worker;
|
224
|
+
pool->current_worker_count++;
|
225
|
+
|
226
|
+
return 0;
|
227
|
+
}
|
228
|
+
|
229
|
+
// Ruby constructor for WorkerPool
|
230
|
+
static VALUE worker_pool_initialize(int argc, VALUE *argv, VALUE self) {
|
231
|
+
size_t maximum_worker_count = 1; // Default
|
232
|
+
|
233
|
+
// Extract keyword arguments
|
234
|
+
VALUE kwargs = Qnil;
|
235
|
+
VALUE rb_maximum_worker_count = Qnil;
|
236
|
+
|
237
|
+
rb_scan_args(argc, argv, "0:", &kwargs);
|
238
|
+
|
239
|
+
if (!NIL_P(kwargs)) {
|
240
|
+
VALUE kwvals[1];
|
241
|
+
ID kwkeys[1] = {id_maximum_worker_count};
|
242
|
+
rb_get_kwargs(kwargs, kwkeys, 0, 1, kwvals);
|
243
|
+
rb_maximum_worker_count = kwvals[0];
|
244
|
+
}
|
245
|
+
|
246
|
+
if (!NIL_P(rb_maximum_worker_count)) {
|
247
|
+
maximum_worker_count = NUM2SIZET(rb_maximum_worker_count);
|
248
|
+
if (maximum_worker_count == 0) {
|
249
|
+
rb_raise(rb_eArgError, "maximum_worker_count must be greater than 0!");
|
250
|
+
}
|
251
|
+
}
|
252
|
+
|
253
|
+
// Get the pool that was allocated by worker_pool_allocate
|
254
|
+
struct IO_Event_WorkerPool *pool;
|
255
|
+
TypedData_Get_Struct(self, struct IO_Event_WorkerPool, &IO_Event_WorkerPool_type, pool);
|
256
|
+
|
257
|
+
if (!pool) {
|
258
|
+
rb_raise(rb_eRuntimeError, "WorkerPool allocation failed!");
|
259
|
+
}
|
260
|
+
|
261
|
+
pthread_mutex_init(&pool->mutex, NULL);
|
262
|
+
pthread_cond_init(&pool->work_available, NULL);
|
263
|
+
|
264
|
+
pool->work_queue = NULL;
|
265
|
+
pool->work_queue_tail = NULL;
|
266
|
+
pool->workers = NULL;
|
267
|
+
pool->current_worker_count = 0;
|
268
|
+
pool->maximum_worker_count = maximum_worker_count;
|
269
|
+
pool->call_count = 0;
|
270
|
+
pool->completed_count = 0;
|
271
|
+
pool->cancelled_count = 0;
|
272
|
+
pool->shutdown = false;
|
273
|
+
|
274
|
+
// Create initial workers
|
275
|
+
for (size_t i = 0; i < maximum_worker_count; i++) {
|
276
|
+
if (create_worker_thread(pool) != 0) {
|
277
|
+
// Just set the maximum_worker_count for debugging, don't fail completely
|
278
|
+
// worker_pool_free(pool);
|
279
|
+
// rb_raise(rb_eRuntimeError, "Failed to create workers");
|
280
|
+
break;
|
281
|
+
}
|
282
|
+
}
|
283
|
+
|
284
|
+
return self;
|
285
|
+
}
|
286
|
+
|
287
|
+
static VALUE worker_pool_work_begin(VALUE _work) {
|
288
|
+
struct IO_Event_WorkerPool_Work *work = (void*)_work;
|
289
|
+
|
290
|
+
if (DEBUG) fprintf(stderr, "worker_pool_work_begin:rb_fiber_scheduler_block work=%p\n", work);
|
291
|
+
rb_fiber_scheduler_block(work->scheduler, work->blocker, Qnil);
|
292
|
+
|
293
|
+
return Qnil;
|
294
|
+
}
|
295
|
+
|
296
|
+
// Ruby method to submit work and wait for completion
|
297
|
+
static VALUE worker_pool_call(VALUE self, VALUE _blocking_operation) {
|
298
|
+
struct IO_Event_WorkerPool *pool;
|
299
|
+
TypedData_Get_Struct(self, struct IO_Event_WorkerPool, &IO_Event_WorkerPool_type, pool);
|
300
|
+
|
301
|
+
if (pool->shutdown) {
|
302
|
+
rb_raise(rb_eRuntimeError, "Worker pool is shut down!");
|
303
|
+
}
|
304
|
+
|
305
|
+
// Increment call count (protected by GVL)
|
306
|
+
pool->call_count++;
|
307
|
+
|
308
|
+
// Get current fiber and scheduler
|
309
|
+
VALUE fiber = rb_fiber_current();
|
310
|
+
VALUE scheduler = rb_fiber_scheduler_current();
|
311
|
+
if (NIL_P(scheduler)) {
|
312
|
+
rb_raise(rb_eRuntimeError, "WorkerPool requires a fiber scheduler!");
|
313
|
+
}
|
314
|
+
|
315
|
+
// Extract blocking operation handle
|
316
|
+
rb_fiber_scheduler_blocking_operation_t *blocking_operation = rb_fiber_scheduler_blocking_operation_extract(_blocking_operation);
|
317
|
+
|
318
|
+
if (!blocking_operation) {
|
319
|
+
rb_raise(rb_eArgError, "Invalid blocking operation!");
|
320
|
+
}
|
321
|
+
|
322
|
+
// Create work item
|
323
|
+
struct IO_Event_WorkerPool_Work work = {
|
324
|
+
.blocking_operation = blocking_operation,
|
325
|
+
.completed = false,
|
326
|
+
.scheduler = scheduler,
|
327
|
+
.blocker = self,
|
328
|
+
.fiber = fiber,
|
329
|
+
.next = NULL
|
330
|
+
};
|
331
|
+
|
332
|
+
// Enqueue work:
|
333
|
+
pthread_mutex_lock(&pool->mutex);
|
334
|
+
enqueue_work(pool, &work);
|
335
|
+
pthread_cond_signal(&pool->work_available);
|
336
|
+
pthread_mutex_unlock(&pool->mutex);
|
337
|
+
|
338
|
+
// Block the current fiber until work is completed:
|
339
|
+
int state;
|
340
|
+
while (true) {
|
341
|
+
rb_protect(worker_pool_work_begin, (VALUE)&work, &state);
|
342
|
+
|
343
|
+
if (work.completed) {
|
344
|
+
break;
|
345
|
+
} else {
|
346
|
+
if (DEBUG) fprintf(stderr, "worker_pool_call:rb_fiber_scheduler_blocking_operation_cancel\n");
|
347
|
+
rb_fiber_scheduler_blocking_operation_cancel(blocking_operation);
|
348
|
+
// The work was not completed, we need to wait for it to be completed.
|
349
|
+
}
|
350
|
+
}
|
351
|
+
|
352
|
+
if (state) {
|
353
|
+
rb_jump_tag(state);
|
354
|
+
} else {
|
355
|
+
return Qtrue;
|
356
|
+
}
|
357
|
+
}
|
358
|
+
|
359
|
+
static VALUE worker_pool_allocate(VALUE klass) {
|
360
|
+
struct IO_Event_WorkerPool *pool;
|
361
|
+
VALUE self = TypedData_Make_Struct(klass, struct IO_Event_WorkerPool, &IO_Event_WorkerPool_type, pool);
|
362
|
+
|
363
|
+
// Initialize to NULL/zero so we can detect uninitialized pools
|
364
|
+
memset(pool, 0, sizeof(struct IO_Event_WorkerPool));
|
365
|
+
|
366
|
+
return self;
|
367
|
+
}
|
368
|
+
|
369
|
+
// Ruby method to close the worker pool
|
370
|
+
static VALUE worker_pool_close(VALUE self) {
|
371
|
+
struct IO_Event_WorkerPool *pool;
|
372
|
+
TypedData_Get_Struct(self, struct IO_Event_WorkerPool, &IO_Event_WorkerPool_type, pool);
|
373
|
+
|
374
|
+
if (!pool) {
|
375
|
+
rb_raise(rb_eRuntimeError, "WorkerPool not initialized!");
|
376
|
+
}
|
377
|
+
|
378
|
+
if (pool->shutdown) {
|
379
|
+
return Qnil; // Already closed
|
380
|
+
}
|
381
|
+
|
382
|
+
// Signal shutdown to all workers
|
383
|
+
pthread_mutex_lock(&pool->mutex);
|
384
|
+
pool->shutdown = true;
|
385
|
+
pthread_cond_broadcast(&pool->work_available);
|
386
|
+
pthread_mutex_unlock(&pool->mutex);
|
387
|
+
|
388
|
+
// Wait for all worker threads to finish
|
389
|
+
struct IO_Event_WorkerPool_Worker *worker = pool->workers;
|
390
|
+
while (worker) {
|
391
|
+
if (!NIL_P(worker->thread)) {
|
392
|
+
rb_funcall(worker->thread, rb_intern("join"), 0);
|
393
|
+
}
|
394
|
+
worker = worker->next;
|
395
|
+
}
|
396
|
+
|
397
|
+
// Clean up worker structures
|
398
|
+
worker = pool->workers;
|
399
|
+
while (worker) {
|
400
|
+
struct IO_Event_WorkerPool_Worker *next = worker->next;
|
401
|
+
free(worker);
|
402
|
+
worker = next;
|
403
|
+
}
|
404
|
+
pool->workers = NULL;
|
405
|
+
pool->current_worker_count = 0;
|
406
|
+
|
407
|
+
// Clean up mutex and condition variable
|
408
|
+
pthread_mutex_destroy(&pool->mutex);
|
409
|
+
pthread_cond_destroy(&pool->work_available);
|
410
|
+
|
411
|
+
return Qnil;
|
412
|
+
}
|
413
|
+
|
414
|
+
// Test helper: get pool statistics for debugging/testing
|
415
|
+
static VALUE worker_pool_statistics(VALUE self) {
|
416
|
+
struct IO_Event_WorkerPool *pool;
|
417
|
+
TypedData_Get_Struct(self, struct IO_Event_WorkerPool, &IO_Event_WorkerPool_type, pool);
|
418
|
+
|
419
|
+
if (!pool) {
|
420
|
+
rb_raise(rb_eRuntimeError, "WorkerPool not initialized!");
|
421
|
+
}
|
422
|
+
|
423
|
+
VALUE stats = rb_hash_new();
|
424
|
+
rb_hash_aset(stats, ID2SYM(rb_intern("current_worker_count")), SIZET2NUM(pool->current_worker_count));
|
425
|
+
rb_hash_aset(stats, ID2SYM(rb_intern("maximum_worker_count")), SIZET2NUM(pool->maximum_worker_count));
|
426
|
+
rb_hash_aset(stats, ID2SYM(rb_intern("call_count")), SIZET2NUM(pool->call_count));
|
427
|
+
rb_hash_aset(stats, ID2SYM(rb_intern("completed_count")), SIZET2NUM(pool->completed_count));
|
428
|
+
rb_hash_aset(stats, ID2SYM(rb_intern("cancelled_count")), SIZET2NUM(pool->cancelled_count));
|
429
|
+
rb_hash_aset(stats, ID2SYM(rb_intern("shutdown")), pool->shutdown ? Qtrue : Qfalse);
|
430
|
+
|
431
|
+
// Count work items in queue (only if properly initialized)
|
432
|
+
if (pool->maximum_worker_count > 0) {
|
433
|
+
pthread_mutex_lock(&pool->mutex);
|
434
|
+
size_t current_queue_size = 0;
|
435
|
+
struct IO_Event_WorkerPool_Work *work = pool->work_queue;
|
436
|
+
while (work) {
|
437
|
+
current_queue_size++;
|
438
|
+
work = work->next;
|
439
|
+
}
|
440
|
+
pthread_mutex_unlock(&pool->mutex);
|
441
|
+
rb_hash_aset(stats, ID2SYM(rb_intern("current_queue_size")), SIZET2NUM(current_queue_size));
|
442
|
+
} else {
|
443
|
+
rb_hash_aset(stats, ID2SYM(rb_intern("current_queue_size")), SIZET2NUM(0));
|
444
|
+
}
|
445
|
+
|
446
|
+
return stats;
|
447
|
+
}
|
448
|
+
|
449
|
+
void Init_IO_Event_WorkerPool(VALUE IO_Event) {
|
450
|
+
// Initialize symbols
|
451
|
+
id_maximum_worker_count = rb_intern("maximum_worker_count");
|
452
|
+
|
453
|
+
IO_Event_WorkerPool = rb_define_class_under(IO_Event, "WorkerPool", rb_cObject);
|
454
|
+
rb_define_alloc_func(IO_Event_WorkerPool, worker_pool_allocate);
|
455
|
+
|
456
|
+
rb_define_method(IO_Event_WorkerPool, "initialize", worker_pool_initialize, -1);
|
457
|
+
rb_define_method(IO_Event_WorkerPool, "call", worker_pool_call, 1);
|
458
|
+
rb_define_method(IO_Event_WorkerPool, "close", worker_pool_close, 0);
|
459
|
+
|
460
|
+
rb_define_method(IO_Event_WorkerPool, "statistics", worker_pool_statistics, 0);
|
461
|
+
|
462
|
+
// Initialize test functions
|
463
|
+
Init_IO_Event_WorkerPool_Test(IO_Event_WorkerPool);
|
464
|
+
}
|
@@ -0,0 +1,200 @@
|
|
1
|
+
// worker_pool_test.c - Test functions for WorkerPool cancellation
|
2
|
+
// Released under the MIT License.
|
3
|
+
// Copyright, 2025, by Samuel Williams.
|
4
|
+
|
5
|
+
#include "worker_pool_test.h"
|
6
|
+
|
7
|
+
#include <ruby/thread.h>
|
8
|
+
#include <stdlib.h>
|
9
|
+
#include <string.h>
|
10
|
+
|
11
|
+
#include <unistd.h>
|
12
|
+
#include <sys/select.h>
|
13
|
+
#include <errno.h>
|
14
|
+
#include <time.h>
|
15
|
+
|
16
|
+
static ID id_duration;
|
17
|
+
|
18
|
+
struct BusyOperationData {
|
19
|
+
int read_fd;
|
20
|
+
int write_fd;
|
21
|
+
volatile int cancelled;
|
22
|
+
double duration; // How long to wait (for testing)
|
23
|
+
clock_t start_time;
|
24
|
+
clock_t end_time;
|
25
|
+
int operation_result;
|
26
|
+
VALUE exception;
|
27
|
+
};
|
28
|
+
|
29
|
+
// The actual blocking operation that can be cancelled
|
30
|
+
static void* busy_blocking_operation(void *data) {
|
31
|
+
struct BusyOperationData *busy_data = (struct BusyOperationData*)data;
|
32
|
+
|
33
|
+
// Use select() to wait for the pipe to become readable
|
34
|
+
fd_set read_fds;
|
35
|
+
struct timeval timeout;
|
36
|
+
|
37
|
+
FD_ZERO(&read_fds);
|
38
|
+
FD_SET(busy_data->read_fd, &read_fds);
|
39
|
+
|
40
|
+
// Set timeout based on duration
|
41
|
+
timeout.tv_sec = (long)busy_data->duration;
|
42
|
+
timeout.tv_usec = ((busy_data->duration - timeout.tv_sec) * 1000000);
|
43
|
+
|
44
|
+
// This will block until:
|
45
|
+
// 1. The pipe becomes readable (cancellation)
|
46
|
+
// 2. The timeout expires
|
47
|
+
// 3. An error occurs
|
48
|
+
int result = select(busy_data->read_fd + 1, &read_fds, NULL, NULL, &timeout);
|
49
|
+
|
50
|
+
if (result > 0 && FD_ISSET(busy_data->read_fd, &read_fds)) {
|
51
|
+
// Pipe became readable - we were cancelled
|
52
|
+
char buffer;
|
53
|
+
read(busy_data->read_fd, &buffer, 1); // Consume the byte
|
54
|
+
busy_data->cancelled = 1;
|
55
|
+
return (void*)-1; // Indicate cancellation
|
56
|
+
} else if (result == 0) {
|
57
|
+
// Timeout - operation completed normally
|
58
|
+
return (void*)0; // Indicate success
|
59
|
+
} else {
|
60
|
+
// Error occurred
|
61
|
+
return (void*)-2; // Indicate error
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
// Unblock function that writes to the pipe to cancel the operation
|
66
|
+
static void busy_unblock_function(void *data) {
|
67
|
+
struct BusyOperationData *busy_data = (struct BusyOperationData*)data;
|
68
|
+
|
69
|
+
busy_data->cancelled = 1;
|
70
|
+
|
71
|
+
// Write a byte to the pipe to wake up the select()
|
72
|
+
char wake_byte = 1;
|
73
|
+
write(busy_data->write_fd, &wake_byte, 1);
|
74
|
+
}
|
75
|
+
|
76
|
+
// Function for the main operation execution (for rb_rescue)
|
77
|
+
static VALUE busy_operation_execute(VALUE data_value) {
|
78
|
+
struct BusyOperationData *busy_data = (struct BusyOperationData*)data_value;
|
79
|
+
|
80
|
+
// Record start time
|
81
|
+
busy_data->start_time = clock();
|
82
|
+
|
83
|
+
// Execute the blocking operation
|
84
|
+
void *block_result = rb_nogvl(
|
85
|
+
busy_blocking_operation,
|
86
|
+
busy_data,
|
87
|
+
busy_unblock_function,
|
88
|
+
busy_data,
|
89
|
+
RB_NOGVL_UBF_ASYNC_SAFE | RB_NOGVL_OFFLOAD_SAFE
|
90
|
+
);
|
91
|
+
|
92
|
+
// Record end time
|
93
|
+
busy_data->end_time = clock();
|
94
|
+
|
95
|
+
// Store the operation result
|
96
|
+
busy_data->operation_result = (int)(intptr_t)block_result;
|
97
|
+
|
98
|
+
return Qnil;
|
99
|
+
}
|
100
|
+
|
101
|
+
// Function for exception handling (for rb_rescue)
|
102
|
+
static VALUE busy_operation_rescue(VALUE data_value, VALUE exception) {
|
103
|
+
struct BusyOperationData *busy_data = (struct BusyOperationData*)data_value;
|
104
|
+
|
105
|
+
// Record end time even in case of exception
|
106
|
+
busy_data->end_time = clock();
|
107
|
+
|
108
|
+
// Mark that an exception was caught
|
109
|
+
busy_data->exception = exception;
|
110
|
+
|
111
|
+
return exception;
|
112
|
+
}
|
113
|
+
|
114
|
+
// Ruby method: IO::Event::WorkerPool.busy(duration: 1.0)
|
115
|
+
// This creates a cancellable blocking operation for testing
|
116
|
+
static VALUE worker_pool_test_busy(int argc, VALUE *argv, VALUE self) {
|
117
|
+
double duration = 1.0; // Default 1 second
|
118
|
+
|
119
|
+
// Extract keyword arguments
|
120
|
+
VALUE kwargs = Qnil;
|
121
|
+
VALUE rb_duration = Qnil;
|
122
|
+
|
123
|
+
rb_scan_args(argc, argv, "0:", &kwargs);
|
124
|
+
|
125
|
+
if (!NIL_P(kwargs)) {
|
126
|
+
VALUE kwvals[1];
|
127
|
+
ID kwkeys[1] = {id_duration};
|
128
|
+
rb_get_kwargs(kwargs, kwkeys, 0, 1, kwvals);
|
129
|
+
rb_duration = kwvals[0];
|
130
|
+
}
|
131
|
+
|
132
|
+
if (!NIL_P(rb_duration)) {
|
133
|
+
duration = NUM2DBL(rb_duration);
|
134
|
+
}
|
135
|
+
|
136
|
+
// Create pipe for cancellation
|
137
|
+
int pipe_fds[2];
|
138
|
+
if (pipe(pipe_fds) != 0) {
|
139
|
+
rb_sys_fail("pipe creation failed");
|
140
|
+
}
|
141
|
+
|
142
|
+
// Stack allocate operation data
|
143
|
+
struct BusyOperationData busy_data = {
|
144
|
+
.read_fd = pipe_fds[0],
|
145
|
+
.write_fd = pipe_fds[1],
|
146
|
+
.cancelled = 0,
|
147
|
+
.duration = duration,
|
148
|
+
.start_time = 0,
|
149
|
+
.end_time = 0,
|
150
|
+
.operation_result = 0,
|
151
|
+
.exception = Qnil
|
152
|
+
};
|
153
|
+
|
154
|
+
// Execute the blocking operation with exception handling using function pointers
|
155
|
+
rb_rescue(
|
156
|
+
busy_operation_execute,
|
157
|
+
(VALUE)&busy_data,
|
158
|
+
busy_operation_rescue,
|
159
|
+
(VALUE)&busy_data
|
160
|
+
);
|
161
|
+
|
162
|
+
// Calculate elapsed time from the state stored in busy_data
|
163
|
+
double elapsed = ((double)(busy_data.end_time - busy_data.start_time)) / CLOCKS_PER_SEC;
|
164
|
+
|
165
|
+
// Create result hash using the state from busy_data
|
166
|
+
VALUE result = rb_hash_new();
|
167
|
+
rb_hash_aset(result, ID2SYM(rb_intern("duration")), DBL2NUM(duration));
|
168
|
+
rb_hash_aset(result, ID2SYM(rb_intern("elapsed")), DBL2NUM(elapsed));
|
169
|
+
|
170
|
+
// Determine result based on operation outcome
|
171
|
+
if (busy_data.exception != Qnil) {
|
172
|
+
rb_hash_aset(result, ID2SYM(rb_intern("result")), ID2SYM(rb_intern("exception")));
|
173
|
+
rb_hash_aset(result, ID2SYM(rb_intern("cancelled")), Qtrue);
|
174
|
+
rb_hash_aset(result, ID2SYM(rb_intern("exception")), busy_data.exception);
|
175
|
+
} else if (busy_data.operation_result == -1) {
|
176
|
+
rb_hash_aset(result, ID2SYM(rb_intern("result")), ID2SYM(rb_intern("cancelled")));
|
177
|
+
rb_hash_aset(result, ID2SYM(rb_intern("cancelled")), Qtrue);
|
178
|
+
} else if (busy_data.operation_result == 0) {
|
179
|
+
rb_hash_aset(result, ID2SYM(rb_intern("result")), ID2SYM(rb_intern("completed")));
|
180
|
+
rb_hash_aset(result, ID2SYM(rb_intern("cancelled")), Qfalse);
|
181
|
+
} else {
|
182
|
+
rb_hash_aset(result, ID2SYM(rb_intern("result")), ID2SYM(rb_intern("error")));
|
183
|
+
rb_hash_aset(result, ID2SYM(rb_intern("cancelled")), Qfalse);
|
184
|
+
}
|
185
|
+
|
186
|
+
// Clean up pipe file descriptors
|
187
|
+
close(pipe_fds[0]);
|
188
|
+
close(pipe_fds[1]);
|
189
|
+
|
190
|
+
return result;
|
191
|
+
}
|
192
|
+
|
193
|
+
// Initialize the test functions
|
194
|
+
void Init_IO_Event_WorkerPool_Test(VALUE IO_Event_WorkerPool) {
|
195
|
+
// Initialize symbols
|
196
|
+
id_duration = rb_intern("duration");
|
197
|
+
|
198
|
+
// Add test methods to IO::Event::WorkerPool class
|
199
|
+
rb_define_singleton_method(IO_Event_WorkerPool, "busy", worker_pool_test_busy, -1);
|
200
|
+
}
|
data/lib/io/event/version.rb
CHANGED
data/readme.md
CHANGED
@@ -18,6 +18,10 @@ Please see the [project documentation](https://socketry.github.io/io-event/) for
|
|
18
18
|
|
19
19
|
Please see the [project releases](https://socketry.github.io/io-event/releases/index) for all releases.
|
20
20
|
|
21
|
+
### v1.11.0
|
22
|
+
|
23
|
+
- [Introduce `IO::Event::WorkerPool` for off-loading blocking operations.](https://socketry.github.io/io-event/releases/index#introduce-io::event::workerpool-for-off-loading-blocking-operations.)
|
24
|
+
|
21
25
|
### v1.10.2
|
22
26
|
|
23
27
|
- Improved consistency of handling closed IO when invoking `#select`.
|
data/releases.md
CHANGED
@@ -1,5 +1,31 @@
|
|
1
1
|
# Releases
|
2
2
|
|
3
|
+
## v1.11.0
|
4
|
+
|
5
|
+
### Introduce `IO::Event::WorkerPool` for off-loading blocking operations.
|
6
|
+
|
7
|
+
The {ruby IO::Event::WorkerPool} provides a mechanism for executing blocking operations on separate OS threads while properly integrating with Ruby's fiber scheduler and GVL (Global VM Lock) management. This enables true parallelism for CPU-intensive or blocking operations that would otherwise block the event loop.
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
# Fiber scheduler integration via blocking_operation_wait hook
|
11
|
+
class MyScheduler
|
12
|
+
def initialize
|
13
|
+
@worker_pool = IO::Event::WorkerPool.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def blocking_operation_wait(operation)
|
17
|
+
@worker_pool.call(operation)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Usage with automatic offloading
|
22
|
+
Fiber.set_scheduler(MyScheduler.new)
|
23
|
+
# Automatically offload `rb_nogvl(..., RB_NOGVL_OFFLOAD_SAFE)` to a background thread:
|
24
|
+
result = some_blocking_operation()
|
25
|
+
```
|
26
|
+
|
27
|
+
The implementation uses one or more background threads and a list of pending blocking operations. Those operations either execute through to completion or may be cancelled, which executes the "unblock function" provided to `rb_nogvl`.
|
28
|
+
|
3
29
|
## v1.10.2
|
4
30
|
|
5
31
|
- Improved consistency of handling closed IO when invoking `#select`.
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: io-event
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -75,6 +75,10 @@ files:
|
|
75
75
|
- ext/io/event/selector/uring.h
|
76
76
|
- ext/io/event/time.c
|
77
77
|
- ext/io/event/time.h
|
78
|
+
- ext/io/event/worker_pool.c
|
79
|
+
- ext/io/event/worker_pool.h
|
80
|
+
- ext/io/event/worker_pool_test.c
|
81
|
+
- ext/io/event/worker_pool_test.h
|
78
82
|
- lib/io/event.rb
|
79
83
|
- lib/io/event/debug/selector.rb
|
80
84
|
- lib/io/event/interrupt.rb
|
@@ -109,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
113
|
- !ruby/object:Gem::Version
|
110
114
|
version: '0'
|
111
115
|
requirements: []
|
112
|
-
rubygems_version: 3.7
|
116
|
+
rubygems_version: 3.6.7
|
113
117
|
specification_version: 4
|
114
118
|
summary: An event loop.
|
115
119
|
test_files: []
|
metadata.gz.sig
CHANGED
Binary file
|