Skip to content

Commit a0ed682

Browse files
committed
+ Refactoring async_coroutine_cancel
1 parent 8ee66aa commit a0ed682

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

Zend/zend_async_API.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,19 +711,30 @@ ZEND_API zend_async_waker_t * zend_async_waker_new_with_timeout(
711711
return waker;
712712
}
713713

714-
ZEND_API bool zend_async_waker_apply_error(zend_async_waker_t *waker, zend_object *error, bool override, bool for_cancellation)
714+
ZEND_API bool zend_async_waker_apply_error(
715+
zend_async_waker_t *waker, zend_object *error, bool tranfer_error, bool override, bool for_cancellation
716+
)
715717
{
716718
if (UNEXPECTED(waker == NULL)) {
719+
if (tranfer_error) {
720+
OBJ_RELEASE(error);
721+
}
717722
return false;
718723
}
719724

720725
if (EXPECTED(waker->error == NULL)) {
721726
waker->error = error;
727+
if (false == tranfer_error) {
728+
GC_ADDREF(error);
729+
}
722730
return true;
723731
}
724732

725733
if (for_cancellation && instanceof_function(waker->error->ce, zend_ce_cancellation_exception)) {
726734
// If the waker already has a cancellation exception, we do not override it
735+
if (tranfer_error) {
736+
OBJ_RELEASE(error);
737+
}
727738
return false;
728739
}
729740

@@ -734,6 +745,10 @@ ZEND_API bool zend_async_waker_apply_error(zend_async_waker_t *waker, zend_objec
734745
zend_exception_set_previous(waker->error, error);
735746
}
736747

748+
if (false == tranfer_error) {
749+
GC_ADDREF(error);
750+
}
751+
737752
return true;
738753
}
739754

Zend/zend_async_API.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ typedef zend_coroutine_t * (*zend_async_spawn_t)(zend_async_scope_t *scope, zend
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);
191-
typedef void (*zend_async_cancel_t)(zend_coroutine_t *coroutine, zend_object * error, const bool transfer_error, const bool is_safely);
191+
typedef void (*zend_async_cancel_t)(zend_coroutine_t *coroutine, zend_object * error, bool transfer_error, const bool is_safely);
192192
typedef void (*zend_async_shutdown_t)(void);
193193
typedef zend_array* (*zend_async_get_coroutines_t)(void);
194194
typedef void (*zend_async_add_microtask_t)(zend_async_microtask_t *microtask);
@@ -1124,13 +1124,15 @@ ZEND_API zend_async_waker_t *zend_async_waker_new(zend_coroutine_t *coroutine);
11241124
ZEND_API zend_async_waker_t * zend_async_waker_new_with_timeout(
11251125
zend_coroutine_t * coroutine, const zend_ulong timeout, zend_async_event_t *cancellation
11261126
);
1127-
ZEND_API bool zend_async_waker_apply_error(zend_async_waker_t *waker, zend_object *error, bool override, bool for_cancellation);
1127+
ZEND_API bool zend_async_waker_apply_error(
1128+
zend_async_waker_t *waker, zend_object *error, bool transfer_error, bool override, bool for_cancellation
1129+
);
11281130
ZEND_API void zend_async_waker_destroy(zend_coroutine_t *coroutine);
11291131
ZEND_API void zend_async_waker_add_triggered_event(zend_coroutine_t *coroutine, zend_async_event_t *event);
11301132

1131-
#define ZEND_ASYNC_WAKER_APPLY_ERROR(waker, error) zend_async_waker_apply_error((waker), (error), true, false)
1132-
#define ZEND_ASYNC_WAKER_APPEND_ERROR(waker, error) zend_async_waker_apply_error((waker), (error), false, false)
1133-
#define ZEND_ASYNC_WAKER_APPLY_CANCELLATION(waker, error) zend_async_waker_apply_error((waker), (error), true, true)
1133+
#define ZEND_ASYNC_WAKER_APPLY_ERROR(waker, error, transfer) zend_async_waker_apply_error((waker), (error), (transfer), true, false)
1134+
#define ZEND_ASYNC_WAKER_APPEND_ERROR(waker, error, transfer) zend_async_waker_apply_error((waker), (error), (transfer), false, false)
1135+
#define ZEND_ASYNC_WAKER_APPLY_CANCELLATION(waker, error, transfer) zend_async_waker_apply_error((waker), (error), (transfer), true, true)
11341136

11351137
ZEND_API void zend_async_resume_when(
11361138
zend_coroutine_t *coroutine,

0 commit comments

Comments
 (0)