Skip to content

Commit 36114a1

Browse files
committed
#53: + Apply code format
1 parent 20027c7 commit 36114a1

File tree

11 files changed

+108
-94
lines changed

11 files changed

+108
-94
lines changed

async.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,8 @@ static PHP_GINIT_FUNCTION(async)
910910
}
911911

912912
/* {{{ PHP_GSHUTDOWN_FUNCTION */
913-
static PHP_GSHUTDOWN_FUNCTION(async){
913+
static PHP_GSHUTDOWN_FUNCTION(async)
914+
{
914915
#ifdef PHP_WIN32
915916
#endif
916917
} /* }}} */

async_API.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,8 @@ static void await_iterator_dispose(async_await_iterator_t *iterator, async_itera
610610
iterator->zend_iterator = NULL;
611611

612612
// When the iterator has finished, it’s now possible to specify the exact number of elements since it’s known.
613-
iterator->await_context->total = iterator->await_context->futures_count +
614-
iterator->await_context->resolved_count;
613+
iterator->await_context->total =
614+
iterator->await_context->futures_count + iterator->await_context->resolved_count;
615615

616616
// Scenario: the iterator has already finished, and there’s nothing left to await.
617617
// In that case, the coroutine needs to be terminated.

coroutine.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ static void coroutine_event_start(zend_async_event_t *event);
4949
static void coroutine_event_stop(zend_async_event_t *event);
5050
static void coroutine_add_callback(zend_async_event_t *event, zend_async_event_callback_t *callback);
5151
static void coroutine_del_callback(zend_async_event_t *event, zend_async_event_callback_t *callback);
52-
static bool coroutine_replay(zend_async_event_t *event, zend_async_event_callback_t *callback, zval *result, zend_object **exception);
52+
static bool coroutine_replay(zend_async_event_t *event,
53+
zend_async_event_callback_t *callback,
54+
zval *result,
55+
zend_object **exception);
5356
static zend_string *coroutine_info(zend_async_event_t *event);
5457
static void coroutine_dispose(zend_async_event_t *event);
5558

@@ -286,7 +289,8 @@ static HashTable *async_coroutine_object_gc(zend_object *object, zval **table, i
286289
async_fiber_context_t *fiber_context = coroutine->fiber_context;
287290

288291
/* Check if we should traverse execution stack (similar to fibers) */
289-
if (fiber_context == NULL || (fiber_context->context.status != ZEND_FIBER_STATUS_SUSPENDED || !fiber_context->execute_data)) {
292+
if (fiber_context == NULL ||
293+
(fiber_context->context.status != ZEND_FIBER_STATUS_SUSPENDED || !fiber_context->execute_data)) {
290294
zend_get_gc_buffer_use(buf, table, num);
291295
return NULL;
292296
}
@@ -394,7 +398,7 @@ ZEND_STACK_ALIGNED void async_coroutine_execute(async_coroutine_t *coroutine)
394398

395399
coroutine->coroutine.event.dispose(&coroutine->coroutine.event);
396400

397-
if(EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
401+
if (EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
398402
ZEND_ASYNC_CURRENT_COROUTINE = NULL;
399403
}
400404

@@ -405,7 +409,7 @@ ZEND_STACK_ALIGNED void async_coroutine_execute(async_coroutine_t *coroutine)
405409
zend_error(E_ERROR, "Attempt to resume a coroutine that has not been resolved");
406410
coroutine->coroutine.event.dispose(&coroutine->coroutine.event);
407411

408-
if(EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
412+
if (EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
409413
ZEND_ASYNC_CURRENT_COROUTINE = NULL;
410414
}
411415

@@ -460,7 +464,7 @@ ZEND_STACK_ALIGNED void async_coroutine_execute(async_coroutine_t *coroutine)
460464
}
461465
zend_end_try();
462466

463-
if(EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
467+
if (EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
464468
ZEND_ASYNC_CURRENT_COROUTINE = NULL;
465469
}
466470

@@ -901,8 +905,7 @@ static zend_string *coroutine_info(zend_async_event_t *event)
901905
coroutine->std.handle,
902906
coroutine->coroutine.filename ? ZSTR_VAL(coroutine->coroutine.filename) : "",
903907
coroutine->coroutine.lineno,
904-
coroutine->waker.filename ? ZSTR_VAL(coroutine->waker.filename)
905-
: "",
908+
coroutine->waker.filename ? ZSTR_VAL(coroutine->waker.filename) : "",
906909
coroutine->waker.lineno,
907910
ZSTR_VAL(zend_coroutine_name));
908911
} else {
@@ -1348,8 +1351,7 @@ METHOD(getSuspendLocation)
13481351
async_coroutine_t *coroutine = THIS_COROUTINE;
13491352

13501353
if (coroutine->waker.filename) {
1351-
RETURN_STR(zend_strpprintf(
1352-
0, "%s:%d", ZSTR_VAL(coroutine->waker.filename), coroutine->waker.lineno));
1354+
RETURN_STR(zend_strpprintf(0, "%s:%d", ZSTR_VAL(coroutine->waker.filename), coroutine->waker.lineno));
13531355
} else {
13541356
RETURN_STRING("unknown");
13551357
}

coroutine.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct _async_fiber_context_s
2929

3030
/* Active fiber VM stack */
3131
zend_vm_stack vm_stack;
32-
32+
3333
/* Current Zend VM execute data */
3434
zend_execute_data *execute_data;
3535

@@ -47,7 +47,7 @@ struct _async_coroutine_s
4747

4848
/* Basic structure for coroutine. */
4949
zend_coroutine_t coroutine;
50-
50+
5151
/* Embedded waker (always allocated, no malloc needed) */
5252
zend_async_waker_t waker;
5353

exceptions.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ PHP_ASYNC_API ZEND_COLD zend_object *async_new_composite_exception(void)
201201
return Z_OBJ(composite);
202202
}
203203

204-
PHP_ASYNC_API void async_composite_exception_add_exception(zend_object *composite, zend_object *exception, bool transfer)
204+
PHP_ASYNC_API void
205+
async_composite_exception_add_exception(zend_object *composite, zend_object *exception, bool transfer)
205206
{
206207
if (composite == NULL || exception == NULL) {
207208
return;
@@ -308,10 +309,11 @@ zend_object *async_extract_exception(void)
308309
*/
309310
void async_apply_exception(zend_object **to_exception)
310311
{
311-
if (UNEXPECTED(EG(exception) &&
312-
false ==
313-
(instanceof_function(EG(exception)->ce, ZEND_ASYNC_GET_CE(ZEND_ASYNC_EXCEPTION_CANCELLATION)) ||
314-
zend_is_graceful_exit(EG(exception)) || zend_is_unwind_exit(EG(exception))))) {
312+
if (UNEXPECTED(
313+
EG(exception) &&
314+
false ==
315+
(instanceof_function(EG(exception)->ce, ZEND_ASYNC_GET_CE(ZEND_ASYNC_EXCEPTION_CANCELLATION)) ||
316+
zend_is_graceful_exit(EG(exception)) || zend_is_unwind_exit(EG(exception))))) {
315317

316318
zend_object *exception = async_extract_exception();
317319

exceptions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ PHP_ASYNC_API ZEND_COLD zend_object *async_throw_input_output(const char *format
3939
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_timeout(const char *format, const zend_long timeout);
4040
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_poll(const char *format, ...);
4141
PHP_ASYNC_API ZEND_COLD zend_object *async_new_composite_exception(void);
42-
PHP_ASYNC_API void async_composite_exception_add_exception(zend_object *composite, zend_object *exception, bool transfer);
42+
PHP_ASYNC_API void
43+
async_composite_exception_add_exception(zend_object *composite, zend_object *exception, bool transfer);
4344
bool async_spawn_and_throw(zend_object *exception, zend_async_scope_t *scope, int32_t priority);
4445
void async_apply_exception_to_context(zend_object *exception);
4546
zend_object *async_extract_exception(void);

internal/circular_buffer.h

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,46 +83,51 @@ zend_result zval_circular_buffer_pop(circular_buffer_t *buffer, zval *value);
8383
/* Inline optimized functions - placed after all declarations */
8484

8585
/* Inline version for hot path performance */
86-
static zend_always_inline bool circular_buffer_is_not_empty(const circular_buffer_t *buffer) {
87-
return buffer->head != buffer->tail;
86+
static zend_always_inline bool circular_buffer_is_not_empty(const circular_buffer_t *buffer)
87+
{
88+
return buffer->head != buffer->tail;
8889
}
8990

90-
static zend_always_inline void circular_buffer_clean(circular_buffer_t *buffer) {
91+
static zend_always_inline void circular_buffer_clean(circular_buffer_t *buffer)
92+
{
9193
buffer->head = buffer->tail;
9294
}
9395

9496
/* Fast specialized version for pointer push (8 bytes) */
95-
static zend_always_inline zend_result circular_buffer_push_ptr(circular_buffer_t *buffer, void *ptr) {
96-
// Check if buffer is full using bitwise AND (capacity is power of 2)
97-
if (EXPECTED(((buffer->head + 1) & (buffer->capacity - 1)) != buffer->tail)) {
98-
// Direct pointer assignment - no memcpy overhead
99-
*(void**)((char*)buffer->data + buffer->head * sizeof(void*)) = ptr;
100-
buffer->head = (buffer->head + 1) & (buffer->capacity - 1);
101-
return SUCCESS;
102-
}
103-
return FAILURE;
97+
static zend_always_inline zend_result circular_buffer_push_ptr(circular_buffer_t *buffer, void *ptr)
98+
{
99+
// Check if buffer is full using bitwise AND (capacity is power of 2)
100+
if (EXPECTED(((buffer->head + 1) & (buffer->capacity - 1)) != buffer->tail)) {
101+
// Direct pointer assignment - no memcpy overhead
102+
*(void **) ((char *) buffer->data + buffer->head * sizeof(void *)) = ptr;
103+
buffer->head = (buffer->head + 1) & (buffer->capacity - 1);
104+
return SUCCESS;
105+
}
106+
return FAILURE;
104107
}
105108

106109
/* Fast specialized version for pointer pop (8 bytes) */
107-
static zend_always_inline zend_result circular_buffer_pop_ptr(circular_buffer_t *buffer, void **ptr) {
108-
// Check if buffer is empty
109-
if (EXPECTED(buffer->head != buffer->tail)) {
110-
// Direct pointer read - no memcpy overhead
111-
*ptr = *(void**)((char*)buffer->data + buffer->tail * sizeof(void*));
112-
buffer->tail = (buffer->tail + 1) & (buffer->capacity - 1);
113-
return SUCCESS;
114-
}
115-
return FAILURE;
110+
static zend_always_inline zend_result circular_buffer_pop_ptr(circular_buffer_t *buffer, void **ptr)
111+
{
112+
// Check if buffer is empty
113+
if (EXPECTED(buffer->head != buffer->tail)) {
114+
// Direct pointer read - no memcpy overhead
115+
*ptr = *(void **) ((char *) buffer->data + buffer->tail * sizeof(void *));
116+
buffer->tail = (buffer->tail + 1) & (buffer->capacity - 1);
117+
return SUCCESS;
118+
}
119+
return FAILURE;
116120
}
117121

118122
/* Smart wrapper for pointer push with resize fallback */
119-
static zend_always_inline zend_result circular_buffer_push_ptr_with_resize(circular_buffer_t *buffer, void *ptr) {
120-
// Try fast path first (no resize)
121-
if (EXPECTED(circular_buffer_push_ptr(buffer, ptr) == SUCCESS)) {
122-
return SUCCESS;
123-
}
124-
// Fallback to slow path with resize - need address of ptr for memcpy
125-
return circular_buffer_push(buffer, &ptr, true);
123+
static zend_always_inline zend_result circular_buffer_push_ptr_with_resize(circular_buffer_t *buffer, void *ptr)
124+
{
125+
// Try fast path first (no resize)
126+
if (EXPECTED(circular_buffer_push_ptr(buffer, ptr) == SUCCESS)) {
127+
return SUCCESS;
128+
}
129+
// Fallback to slow path with resize - need address of ptr for memcpy
130+
return circular_buffer_push(buffer, &ptr, true);
126131
}
127132

128133
#endif // ASYNC_CIRCULAR_BUFFER_V2_H

libuv_reactor.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static void libuv_close_handle_cb(uv_handle_t *handle)
199199
/* {{{ libuv_close_poll_handle_cb */
200200
static void libuv_close_poll_handle_cb(uv_handle_t *handle)
201201
{
202-
async_poll_event_t *poll = (async_poll_event_t *)handle->data;
202+
async_poll_event_t *poll = (async_poll_event_t *) handle->data;
203203

204204
/* Check if PHP requested descriptor closure after event cleanup */
205205
if (ZEND_ASYNC_EVENT_SHOULD_CLOSE_FD(&poll->event.base)) {
@@ -213,7 +213,7 @@ static void libuv_close_poll_handle_cb(uv_handle_t *handle)
213213
} else if (!poll->event.is_socket && poll->event.file != ZEND_FD_NULL) {
214214
/* File descriptor cleanup */
215215
#ifdef PHP_WIN32
216-
CloseHandle((HANDLE)poll->event.file);
216+
CloseHandle((HANDLE) poll->event.file);
217217
#else
218218
close(poll->event.file);
219219
#endif
@@ -246,7 +246,8 @@ static void libuv_remove_callback(zend_async_event_t *event, zend_async_event_ca
246246
//////////////////////////////////////////////////////////////////////////////
247247

248248
/* Forward declaration */
249-
static zend_always_inline void async_poll_notify_proxies(async_poll_event_t *poll, async_poll_event triggered_events, zend_object *exception);
249+
static zend_always_inline void
250+
async_poll_notify_proxies(async_poll_event_t *poll, async_poll_event triggered_events, zend_object *exception);
250251

251252
/* {{{ on_poll_event */
252253
static void on_poll_event(uv_poll_t *handle, int status, int events)
@@ -359,7 +360,8 @@ static void libuv_poll_dispose(zend_async_event_t *event)
359360
/* }}} */
360361

361362
/* {{{ async_poll_notify_proxies */
362-
static zend_always_inline void async_poll_notify_proxies(async_poll_event_t *poll, async_poll_event triggered_events, zend_object *exception)
363+
static zend_always_inline void
364+
async_poll_notify_proxies(async_poll_event_t *poll, async_poll_event triggered_events, zend_object *exception)
363365
{
364366
/* Process each proxy that matches triggered events */
365367
for (uint32_t i = 0; i < poll->proxies_count; i++) {
@@ -381,6 +383,7 @@ static zend_always_inline void async_poll_notify_proxies(async_poll_event_t *pol
381383
}
382384
}
383385
}
386+
384387
/* }}} */
385388

386389
/* {{{ async_poll_add_proxy */
@@ -394,11 +397,12 @@ static zend_always_inline void async_poll_add_proxy(async_poll_event_t *poll, ze
394397
if (poll->proxies_count == poll->proxies_capacity) {
395398
poll->proxies_capacity *= 2;
396399
poll->proxies = (zend_async_poll_proxy_t **) perealloc(
397-
poll->proxies, poll->proxies_capacity * sizeof(zend_async_poll_proxy_t *), 0);
400+
poll->proxies, poll->proxies_capacity * sizeof(zend_async_poll_proxy_t *), 0);
398401
}
399402

400403
poll->proxies[poll->proxies_count++] = proxy;
401404
}
405+
402406
/* }}} */
403407

404408
/* {{{ async_poll_remove_proxy */
@@ -412,6 +416,7 @@ static zend_always_inline void async_poll_remove_proxy(async_poll_event_t *poll,
412416
}
413417
}
414418
}
419+
415420
/* }}} */
416421

417422
/* {{{ async_poll_aggregate_events */
@@ -430,6 +435,7 @@ static zend_always_inline async_poll_event async_poll_aggregate_events(async_pol
430435

431436
return aggregated;
432437
}
438+
433439
/* }}} */
434440

435441
/* {{{ libuv_poll_proxy_start */
@@ -438,7 +444,7 @@ static void libuv_poll_proxy_start(zend_async_event_t *event)
438444
EVENT_START_PROLOGUE(event);
439445

440446
zend_async_poll_proxy_t *proxy = (zend_async_poll_proxy_t *) event;
441-
async_poll_event_t *poll = (async_poll_event_t *)proxy->poll_event;
447+
async_poll_event_t *poll = (async_poll_event_t *) proxy->poll_event;
442448

443449
/* Add proxy to the array */
444450
async_poll_add_proxy(poll, proxy);
@@ -459,6 +465,7 @@ static void libuv_poll_proxy_start(zend_async_event_t *event)
459465
ZEND_ASYNC_INCREASE_EVENT_COUNT;
460466
event->loop_ref_count = 1;
461467
}
468+
462469
/* }}} */
463470

464471
/* {{{ libuv_poll_proxy_stop */
@@ -467,7 +474,7 @@ static void libuv_poll_proxy_stop(zend_async_event_t *event)
467474
EVENT_STOP_PROLOGUE(event);
468475

469476
zend_async_poll_proxy_t *proxy = (zend_async_poll_proxy_t *) event;
470-
async_poll_event_t *poll = (async_poll_event_t *)proxy->poll_event;
477+
async_poll_event_t *poll = (async_poll_event_t *) proxy->poll_event;
471478

472479
/* Remove proxy from the array */
473480
async_poll_remove_proxy(poll, proxy);
@@ -490,6 +497,7 @@ static void libuv_poll_proxy_stop(zend_async_event_t *event)
490497
event->loop_ref_count = 0;
491498
ZEND_ASYNC_DECREASE_EVENT_COUNT;
492499
}
500+
493501
/* }}} */
494502

495503
/* {{{ libuv_poll_proxy_dispose */
@@ -501,7 +509,7 @@ static void libuv_poll_proxy_dispose(zend_async_event_t *event)
501509
}
502510

503511
zend_async_poll_proxy_t *proxy = (zend_async_poll_proxy_t *) event;
504-
async_poll_event_t *poll = (async_poll_event_t *)proxy->poll_event;
512+
async_poll_event_t *poll = (async_poll_event_t *) proxy->poll_event;
505513

506514
if (event->loop_ref_count > 0) {
507515
event->loop_ref_count = 1;
@@ -515,6 +523,7 @@ static void libuv_poll_proxy_dispose(zend_async_event_t *event)
515523

516524
pefree(proxy, 0);
517525
}
526+
518527
/* }}} */
519528

520529
/* {{{ libuv_new_poll_event */
@@ -578,12 +587,13 @@ zend_async_poll_event_t *libuv_new_socket_event(zend_socket_t socket, async_poll
578587
/* }}} */
579588

580589
/* {{{ libuv_new_poll_proxy_event */
581-
zend_async_poll_proxy_t *libuv_new_poll_proxy_event(zend_async_poll_event_t *poll_event, async_poll_event events, size_t extra_size)
590+
zend_async_poll_proxy_t *
591+
libuv_new_poll_proxy_event(zend_async_poll_event_t *poll_event, async_poll_event events, size_t extra_size)
582592
{
583593
START_REACTOR_OR_RETURN_NULL;
584594

585-
zend_async_poll_proxy_t *proxy =
586-
pecalloc(1, extra_size != 0 ? sizeof(zend_async_poll_proxy_t) + extra_size : sizeof(zend_async_poll_proxy_t), 0);
595+
zend_async_poll_proxy_t *proxy = pecalloc(
596+
1, extra_size != 0 ? sizeof(zend_async_poll_proxy_t) + extra_size : sizeof(zend_async_poll_proxy_t), 0);
587597

588598
/* Set up proxy */
589599
proxy->poll_event = poll_event;

php_async_api.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
#define PHP_ASYNC_API_H
1818

1919
#ifdef PHP_WIN32
20-
# ifdef ASYNC_EXPORTS
21-
# define PHP_ASYNC_API __declspec(dllexport)
22-
# else
23-
# define PHP_ASYNC_API __declspec(dllimport)
24-
# endif
20+
#ifdef ASYNC_EXPORTS
21+
#define PHP_ASYNC_API __declspec(dllexport)
2522
#else
26-
# if defined(__GNUC__) && __GNUC__ >= 4
27-
# define PHP_ASYNC_API __attribute__ ((visibility("default")))
28-
# else
29-
# define PHP_ASYNC_API
30-
# endif
23+
#define PHP_ASYNC_API __declspec(dllimport)
24+
#endif
25+
#else
26+
#if defined(__GNUC__) && __GNUC__ >= 4
27+
#define PHP_ASYNC_API __attribute__((visibility("default")))
28+
#else
29+
#define PHP_ASYNC_API
30+
#endif
3131
#endif
3232

3333
#endif // PHP_ASYNC_API_H

0 commit comments

Comments
 (0)