Skip to content

Commit 964eca0

Browse files
committed
add coroutine priority support to TrueAsync API
Implements priority-based coroutine scheduling where higher priority values result in coroutines being queued at the front of the execution queue. ## Changes ### Circular Buffer Enhancement - Add `circular_buffer_push_front()` function to insert elements at buffer head - Fallback to normal push when front insertion is not possible (buffer full) ### API Extension - Update `zend_async_spawn_t` typedef to include `int32_t priority` parameter - Add new macros: - `ZEND_ASYNC_SPAWN_WITH_PRIORITY(priority)` - `ZEND_ASYNC_SPAWN_WITH_SCOPE_EX(scope, priority)` - Maintain backward compatibility by updating existing macros to pass priority=0 ### Priority Logic - Priority > 0: coroutine queued at front (high priority) - Priority ≤ 0: coroutine queued at back (normal/low priority) - Preserves existing behavior for all current code ### Files Modified - `ext/async/internal/circular_buffer.{h,c}` - Buffer front insertion - `Zend/zend_async_API.{h,c}` - API definitions and stub functions - `ext/async/async_API.c` - Priority-aware spawn implementation ## Backward Compatibility All existing spawn macros unchanged in behavior, new priority parameter defaults to 0 for normal scheduling order.
1 parent a234299 commit 964eca0

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Zend/zend_async_API.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ zend_async_globals_t zend_async_globals_api = {0};
2929
/* Forward declarations */
3030
static void zend_async_main_handlers_shutdown(void);
3131

32-
static zend_coroutine_t * spawn(zend_async_scope_t *scope, zend_object *scope_provider)
32+
static zend_coroutine_t * spawn(zend_async_scope_t *scope, zend_object *scope_provider, int32_t priority)
3333
{
3434
ASYNC_THROW_ERROR("Async API is not enabled");
3535
return NULL;

Zend/zend_async_API.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ typedef struct _zend_async_task_s zend_async_task_t;
184184

185185
typedef zend_coroutine_t * (*zend_async_new_coroutine_t)(zend_async_scope_t *scope);
186186
typedef zend_async_scope_t * (*zend_async_new_scope_t)(zend_async_scope_t * parent_scope, bool with_zend_object);
187-
typedef zend_coroutine_t * (*zend_async_spawn_t)(zend_async_scope_t *scope, zend_object *scope_provider);
187+
typedef zend_coroutine_t * (*zend_async_spawn_t)(zend_async_scope_t *scope, zend_object *scope_provider, int32_t priority);
188188
typedef void (*zend_async_suspend_t)(bool from_main);
189189
typedef void (*zend_async_enqueue_coroutine_t)(zend_coroutine_t *coroutine);
190190
typedef void (*zend_async_resume_t)(zend_coroutine_t *coroutine, zend_object * error, const bool transfer_error);
@@ -1231,9 +1231,11 @@ ZEND_API void zend_async_call_main_coroutine_start_handlers(zend_coroutine_t *ma
12311231
END_EXTERN_C()
12321232

12331233
#define ZEND_ASYNC_IS_ENABLED() zend_async_is_enabled()
1234-
#define ZEND_ASYNC_SPAWN() zend_async_spawn_fn(NULL, NULL)
1235-
#define ZEND_ASYNC_SPAWN_WITH(scope) zend_async_spawn_fn(scope, NULL)
1236-
#define ZEND_ASYNC_SPAWN_WITH_PROVIDER(scope_provider) zend_async_spawn_fn(NULL, scope_provider)
1234+
#define ZEND_ASYNC_SPAWN() zend_async_spawn_fn(NULL, NULL, 0)
1235+
#define ZEND_ASYNC_SPAWN_WITH(scope) zend_async_spawn_fn(scope, NULL, 0)
1236+
#define ZEND_ASYNC_SPAWN_WITH_PROVIDER(scope_provider) zend_async_spawn_fn(NULL, scope_provider, 0)
1237+
#define ZEND_ASYNC_SPAWN_WITH_PRIORITY(priority) zend_async_spawn_fn(NULL, NULL, priority)
1238+
#define ZEND_ASYNC_SPAWN_WITH_SCOPE_EX(scope, priority) zend_async_spawn_fn(scope, NULL, priority)
12371239
#define ZEND_ASYNC_NEW_COROUTINE(scope) zend_async_new_coroutine_fn(scope)
12381240
#define ZEND_ASYNC_NEW_SCOPE(parent) zend_async_new_scope_fn(parent, false)
12391241
#define ZEND_ASYNC_NEW_SCOPE_WITH_OBJECT(parent) zend_async_new_scope_fn(parent, true)

0 commit comments

Comments
 (0)