Skip to content

Commit 6a74634

Browse files
Direct dispatch: bypass worker threads, execute blocking op inline
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 218d53a commit 6a74634

1 file changed

Lines changed: 3 additions & 47 deletions

File tree

ext/io/event/worker_pool.c

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -332,54 +332,10 @@ static VALUE worker_pool_call(VALUE self, VALUE _blocking_operation) {
332332
rb_raise(rb_eArgError, "Invalid blocking operation!");
333333
}
334334

335-
// Create work item
336-
struct IO_Event_WorkerPool_Work work = {
337-
.blocking_operation = blocking_operation,
338-
.completed = false,
339-
.scheduler = scheduler,
340-
.blocker = self,
341-
.fiber = fiber,
342-
.next = NULL
343-
};
344-
345-
// Enqueue work:
346-
pthread_mutex_lock(&pool->mutex);
347-
enqueue_work(pool, &work);
348-
pthread_cond_signal(&pool->work_available);
349-
pthread_mutex_unlock(&pool->mutex);
350-
351-
// Block the current fiber until work is completed:
352-
int state = 0;
353-
while (true) {
354-
int current_state = 0;
355-
rb_protect(worker_pool_work_begin, (VALUE)&work, &current_state);
356-
if (DEBUG) fprintf(stderr, "-- worker_pool_call:work completed=%d, current_state=%d, state=%d\n", work.completed, current_state, state);
357-
358-
// Store the first exception state:
359-
if (!state) {
360-
state = current_state;
361-
}
362-
363-
// If the work is still in the queue, we must wait for a worker to complete it (even if cancelled):
364-
if (work.completed) {
365-
// The work was completed, we can exit the loop:
366-
break;
367-
} else {
368-
if (DEBUG) fprintf(stderr, "worker_pool_call:rb_fiber_scheduler_blocking_operation_cancel\n");
369-
// Ensure the blocking operation is cancelled:
370-
rb_fiber_scheduler_blocking_operation_cancel(blocking_operation);
371-
372-
// The work was not completed, we need to wait for it to be completed, so we go around the loop again.
373-
}
374-
}
335+
// Direct dispatch: execute the blocking operation inline, bypassing worker threads.
336+
rb_fiber_scheduler_blocking_operation_execute(blocking_operation);
375337

376-
if (DEBUG) fprintf(stderr, "<- worker_pool_call:work completed=%d, state=%d\n", work.completed, state);
377-
378-
if (state) {
379-
rb_jump_tag(state);
380-
} else {
381-
return Qtrue;
382-
}
338+
return Qtrue;
383339
}
384340

385341
static VALUE worker_pool_allocate(VALUE klass) {

0 commit comments

Comments
 (0)