Skip to content

Commit d17f623

Browse files
committed
Add zend_future_t structure to async API
Introduce Future data structure following coroutine architecture patterns: - Inherits from zend_async_event_t for event system integration - Contains result/exception storage with debug location tracking - Provides resolve method pointer for flexible resolution strategies - Supports thread-safe creation parameter - Uses function pointer pattern consistent with existing async API - Includes stub implementation for disabled async mode Structure enables passive result containers that integrate with existing callback and replay mechanisms
1 parent 6b98adb commit d17f623

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Zend/zend_async_API.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ static void shutdown_stub(void) {}
4848

4949
static zend_array* get_coroutines_stub(void) { return NULL; }
5050

51+
static zend_future_t* future_create_stub(bool thread_safe, size_t extra_size)
52+
{
53+
ASYNC_THROW_ERROR("Async API is not enabled");
54+
return NULL;
55+
}
56+
5157
static void add_microtask_stub(zend_async_microtask_t *microtask) {}
5258

5359
static zend_array* get_awaiting_info_stub(zend_coroutine_t *coroutine) { return NULL; }
@@ -97,6 +103,7 @@ zend_async_get_coroutines_t zend_async_get_coroutines_fn = get_coroutines_stub;
97103
zend_async_add_microtask_t zend_async_add_microtask_fn = add_microtask_stub;
98104
zend_async_get_awaiting_info_t zend_async_get_awaiting_info_fn = get_awaiting_info_stub;
99105
zend_async_get_class_ce_t zend_async_get_class_ce_fn = get_class_ce;
106+
zend_async_future_create_t zend_async_future_create_fn = future_create_stub;
100107

101108
static zend_atomic_bool reactor_lock = {0};
102109
static char * reactor_module_name = NULL;

Zend/zend_async_API.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ typedef enum
119119
* zend_coroutine_t is a Basic data structure that represents a coroutine in the Zend Engine.
120120
*/
121121
typedef struct _zend_coroutine_s zend_coroutine_t;
122+
123+
/**
124+
* zend_future_t is a data structure that represents a future result container.
125+
*/
126+
typedef struct _zend_future_s zend_future_t;
122127
typedef struct _zend_async_context_s zend_async_context_t;
123128
typedef struct _zend_async_waker_s zend_async_waker_t;
124129
typedef struct _zend_async_microtask_s zend_async_microtask_t;
@@ -127,6 +132,9 @@ typedef struct _zend_async_iterator_s zend_async_iterator_t;
127132
typedef struct _zend_fcall_s zend_fcall_t;
128133
typedef void (*zend_coroutine_entry_t)(void);
129134

135+
/* Future resolve function type */
136+
typedef void (*zend_future_resolve_t)(zend_future_t *future, zval *value, zend_object *exception, bool transfer_error);
137+
130138
/* Coroutine Switch Handlers */
131139
typedef struct _zend_coroutine_switch_handler_s zend_coroutine_switch_handler_t;
132140
typedef struct _zend_coroutine_switch_handlers_vector_s zend_coroutine_switch_handlers_vector_t;
@@ -203,6 +211,7 @@ typedef zend_array* (*zend_async_get_coroutines_t)(void);
203211
typedef void (*zend_async_add_microtask_t)(zend_async_microtask_t *microtask);
204212
typedef zend_array* (*zend_async_get_awaiting_info_t)(zend_coroutine_t * coroutine);
205213
typedef zend_class_entry* (*zend_async_get_class_ce_t)(zend_async_class type);
214+
typedef zend_future_t* (*zend_async_future_create_t)(bool thread_safe, size_t extra_size);
206215

207216
typedef void (*zend_async_reactor_startup_t)(void);
208217
typedef void (*zend_async_reactor_shutdown_t)(void);
@@ -963,6 +972,25 @@ struct _zend_coroutine_s {
963972
zend_coroutine_switch_handlers_vector_t *switch_handlers;
964973
};
965974

975+
/**
976+
* zend_future_t structure represents a future result container.
977+
* It inherits from zend_async_event_t to participate in the event system.
978+
*/
979+
struct _zend_future_s {
980+
zend_async_event_t event; /* Event inheritance (first member) */
981+
zval result; /* Result value (UNDEF = pending) */
982+
zend_object *exception; /* Exception object (NULL = no error) */
983+
984+
/* Debug information */
985+
zend_string *filename; /* Creation file */
986+
uint32_t lineno; /* Creation line */
987+
zend_string *resolved_filename; /* Resolution file */
988+
uint32_t resolved_lineno; /* Resolution line */
989+
990+
/* Resolution method */
991+
zend_future_resolve_t resolve;
992+
};
993+
966994
/**
967995
* The macro evaluates to TRUE if the coroutine is in a waiting state —
968996
* either waiting for events or waiting in the execution queue.
@@ -1177,6 +1205,7 @@ ZEND_API extern zend_async_get_coroutines_t zend_async_get_coroutines_fn;
11771205
ZEND_API extern zend_async_add_microtask_t zend_async_add_microtask_fn;
11781206
ZEND_API extern zend_async_get_awaiting_info_t zend_async_get_awaiting_info_fn;
11791207
ZEND_API extern zend_async_get_class_ce_t zend_async_get_class_ce_fn;
1208+
ZEND_API extern zend_async_future_create_t zend_async_future_create_fn;
11801209

11811210
/* Iterator API */
11821211
ZEND_API extern zend_async_new_iterator_t zend_async_new_iterator_fn;
@@ -1361,6 +1390,10 @@ ZEND_API void zend_async_add_main_coroutine_start_handler(
13611390

13621391
ZEND_API void zend_async_call_main_coroutine_start_handlers(zend_coroutine_t *main_coroutine);
13631392

1393+
/* Future API Functions */
1394+
#define ZEND_ASYNC_NEW_FUTURE(thread_safe) zend_async_future_create_fn(thread_safe, 0)
1395+
#define ZEND_ASYNC_NEW_FUTURE_EX(thread_safe, extra_size) zend_async_future_create_fn(thread_safe, extra_size)
1396+
13641397
END_EXTERN_C()
13651398

13661399
#define ZEND_ASYNC_IS_ENABLED() zend_async_is_enabled()

0 commit comments

Comments
 (0)