Skip to content

Commit e1e7d6c

Browse files
committed
Add zend_async_channel_t abstract structure to async API
Introduce Channel communication abstraction following async API patterns: - Inherits from zend_async_event_t for event system integration - Contains send/receive method pointers for implementation flexibility - Uses existing event.stop() method for channel closure - Supports creation parameters: buffer_size, resizable, thread_safe - Provides both ZEND_ASYNC_NEW_CHANNEL and _EX variants - Uses function pointer pattern consistent with async API architecture - Includes stub implementation for disabled async mode Abstract structure enables flexible channel implementations while maintaining consistent API interface for communication primitives.
1 parent d17f623 commit e1e7d6c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Zend/zend_async_API.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ static zend_future_t* future_create_stub(bool thread_safe, size_t extra_size)
5454
return NULL;
5555
}
5656

57+
static zend_async_channel_t* channel_create_stub(size_t buffer_size, bool resizable, bool thread_safe, size_t extra_size)
58+
{
59+
ASYNC_THROW_ERROR("Async API is not enabled");
60+
return NULL;
61+
}
62+
5763
static void add_microtask_stub(zend_async_microtask_t *microtask) {}
5864

5965
static zend_array* get_awaiting_info_stub(zend_coroutine_t *coroutine) { return NULL; }
@@ -104,6 +110,7 @@ zend_async_add_microtask_t zend_async_add_microtask_fn = add_microtask_stub;
104110
zend_async_get_awaiting_info_t zend_async_get_awaiting_info_fn = get_awaiting_info_stub;
105111
zend_async_get_class_ce_t zend_async_get_class_ce_fn = get_class_ce;
106112
zend_async_future_create_t zend_async_future_create_fn = future_create_stub;
113+
zend_async_channel_create_t zend_async_channel_create_fn = channel_create_stub;
107114

108115
static zend_atomic_bool reactor_lock = {0};
109116
static char * reactor_module_name = NULL;

Zend/zend_async_API.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include "zend_fibers.h"
2020
#include "zend_globals.h"
2121

22-
#define ZEND_ASYNC_API "TrueAsync API v0.3.0"
22+
#define ZEND_ASYNC_API "TrueAsync API v0.4.0"
2323
#define ZEND_ASYNC_API_VERSION_MAJOR 0
24-
#define ZEND_ASYNC_API_VERSION_MINOR 3
24+
#define ZEND_ASYNC_API_VERSION_MINOR 4
2525
#define ZEND_ASYNC_API_VERSION_PATCH 0
2626

2727
#define ZEND_ASYNC_API_VERSION_NUMBER \
@@ -124,6 +124,11 @@ typedef struct _zend_coroutine_s zend_coroutine_t;
124124
* zend_future_t is a data structure that represents a future result container.
125125
*/
126126
typedef struct _zend_future_s zend_future_t;
127+
128+
/**
129+
* zend_async_channel_t is a data structure that represents a communication channel.
130+
*/
131+
typedef struct _zend_async_channel_s zend_async_channel_t;
127132
typedef struct _zend_async_context_s zend_async_context_t;
128133
typedef struct _zend_async_waker_s zend_async_waker_t;
129134
typedef struct _zend_async_microtask_s zend_async_microtask_t;
@@ -135,6 +140,10 @@ typedef void (*zend_coroutine_entry_t)(void);
135140
/* Future resolve function type */
136141
typedef void (*zend_future_resolve_t)(zend_future_t *future, zval *value, zend_object *exception, bool transfer_error);
137142

143+
/* Channel method function types */
144+
typedef bool (*zend_channel_send_t)(zend_async_channel_t *channel, zval *value);
145+
typedef bool (*zend_channel_receive_t)(zend_async_channel_t *channel, zval *result);
146+
138147
/* Coroutine Switch Handlers */
139148
typedef struct _zend_coroutine_switch_handler_s zend_coroutine_switch_handler_t;
140149
typedef struct _zend_coroutine_switch_handlers_vector_s zend_coroutine_switch_handlers_vector_t;
@@ -212,6 +221,7 @@ typedef void (*zend_async_add_microtask_t)(zend_async_microtask_t *microtask);
212221
typedef zend_array* (*zend_async_get_awaiting_info_t)(zend_coroutine_t * coroutine);
213222
typedef zend_class_entry* (*zend_async_get_class_ce_t)(zend_async_class type);
214223
typedef zend_future_t* (*zend_async_future_create_t)(bool thread_safe, size_t extra_size);
224+
typedef zend_async_channel_t* (*zend_async_channel_create_t)(size_t buffer_size, bool resizable, bool thread_safe, size_t extra_size);
215225

216226
typedef void (*zend_async_reactor_startup_t)(void);
217227
typedef void (*zend_async_reactor_shutdown_t)(void);
@@ -991,6 +1001,22 @@ struct _zend_future_s {
9911001
zend_future_resolve_t resolve;
9921002
};
9931003

1004+
/**
1005+
* zend_async_channel_t structure represents a communication channel.
1006+
* It inherits from zend_async_event_t to participate in the event system.
1007+
*/
1008+
struct _zend_async_channel_s {
1009+
zend_async_event_t event; /* Event inheritance (first member) */
1010+
1011+
/* Debug information */
1012+
zend_string *filename; /* Creation file */
1013+
uint32_t lineno; /* Creation line */
1014+
1015+
/* Channel-specific method pointers */
1016+
zend_channel_send_t send; /* Send method */
1017+
zend_channel_receive_t receive; /* Receive method */
1018+
};
1019+
9941020
/**
9951021
* The macro evaluates to TRUE if the coroutine is in a waiting state —
9961022
* either waiting for events or waiting in the execution queue.
@@ -1206,6 +1232,7 @@ ZEND_API extern zend_async_add_microtask_t zend_async_add_microtask_fn;
12061232
ZEND_API extern zend_async_get_awaiting_info_t zend_async_get_awaiting_info_fn;
12071233
ZEND_API extern zend_async_get_class_ce_t zend_async_get_class_ce_fn;
12081234
ZEND_API extern zend_async_future_create_t zend_async_future_create_fn;
1235+
ZEND_API extern zend_async_channel_create_t zend_async_channel_create_fn;
12091236

12101237
/* Iterator API */
12111238
ZEND_API extern zend_async_new_iterator_t zend_async_new_iterator_fn;
@@ -1394,6 +1421,10 @@ ZEND_API void zend_async_call_main_coroutine_start_handlers(zend_coroutine_t *ma
13941421
#define ZEND_ASYNC_NEW_FUTURE(thread_safe) zend_async_future_create_fn(thread_safe, 0)
13951422
#define ZEND_ASYNC_NEW_FUTURE_EX(thread_safe, extra_size) zend_async_future_create_fn(thread_safe, extra_size)
13961423

1424+
/* Channel API Functions */
1425+
#define ZEND_ASYNC_NEW_CHANNEL(buffer_size, resizable, thread_safe) zend_async_channel_create_fn(buffer_size, resizable, thread_safe, 0)
1426+
#define ZEND_ASYNC_NEW_CHANNEL_EX(buffer_size, resizable, thread_safe, extra_size) zend_async_channel_create_fn(buffer_size, resizable, thread_safe, extra_size)
1427+
13971428
END_EXTERN_C()
13981429

13991430
#define ZEND_ASYNC_IS_ENABLED() zend_async_is_enabled()

0 commit comments

Comments
 (0)