Skip to content

Commit 6b4a13b

Browse files
committed
Variety of minor changes from feedback
1 parent fa3ee76 commit 6b4a13b

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

Zend/zend_fibers.c

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
+----------------------------------------------------------------------+
1818
*/
1919

20-
#include "php.h"
2120
#include "zend.h"
2221
#include "zend_API.h"
22+
#include "zend_ini.h"
2323
#include "zend_vm.h"
2424
#include "zend_interfaces.h"
2525
#include "zend_exceptions.h"
@@ -29,7 +29,13 @@
2929
#include "zend_fibers_arginfo.h"
3030

3131
#ifdef HAVE_VALGRIND
32-
#include "valgrind/valgrind.h"
32+
#include <valgrind/valgrind.h>
33+
#endif
34+
35+
#ifndef PHP_WIN32
36+
#include <unistd.h>
37+
#include <sys/mman.h>
38+
#include <limits.h>
3339
#endif
3440

3541
ZEND_API zend_class_entry *zend_ce_fiber;
@@ -52,6 +58,8 @@ typedef struct _transfer_t {
5258
extern fcontext_t make_fcontext(void *sp, size_t size, void (*fn)(transfer_t));
5359
extern transfer_t jump_fcontext(fcontext_t to, void *vp);
5460

61+
#define ZEND_FIBER_DEFAULT_PAGE_SIZE 4096
62+
5563
#define ZEND_FIBER_BACKUP_EG(stack, stack_page_size, execute_data, error_reporting, trace_num) do { \
5664
stack = EG(vm_stack); \
5765
stack->top = EG(vm_stack_top); \
@@ -86,7 +94,7 @@ ZEND_API void zend_observer_fiber_switch_register(zend_observer_fiber_switch_han
8694
}
8795
/* }}} */
8896

89-
zend_always_inline static void zend_observer_fiber_switch_notify(zend_fiber *from, zend_fiber *to) /* {{{ */
97+
static zend_always_inline void zend_observer_fiber_switch_notify(zend_fiber *from, zend_fiber *to) /* {{{ */
9098
{
9199
zend_llist_element *element;
92100
zend_observer_fiber_switch_handler callback;
@@ -98,7 +106,22 @@ zend_always_inline static void zend_observer_fiber_switch_notify(zend_fiber *fro
98106
}
99107
/* }}} */
100108

101-
static zend_bool zend_fiber_stack_allocate(zend_fiber_stack *stack, size_t size) /* {{{ */
109+
size_t zend_fiber_page_size()
110+
{
111+
#if _POSIX_MAPPED_FILES
112+
static size_t page_size;
113+
114+
if (!page_size) {
115+
page_size = sysconf(_SC_PAGESIZE);
116+
}
117+
118+
return page_size;
119+
#else
120+
return ZEND_FIBER_DEFAULT_PAGE_SIZE;
121+
#endif
122+
}
123+
124+
static bool zend_fiber_stack_allocate(zend_fiber_stack *stack, size_t size) /* {{{ */
102125
{
103126
void *pointer;
104127
const size_t page_size = zend_fiber_page_size();
@@ -112,28 +135,28 @@ static zend_bool zend_fiber_stack_allocate(zend_fiber_stack *stack, size_t size)
112135
pointer = VirtualAlloc(0, msize, MEM_COMMIT, PAGE_READWRITE);
113136

114137
if (!pointer) {
115-
return 0;
138+
return false;
116139
}
117140

118141
# if ZEND_FIBER_GUARD_PAGES
119142
DWORD protect;
120143

121144
if (!VirtualProtect(pointer, ZEND_FIBER_GUARD_PAGES * page_size, PAGE_READWRITE | PAGE_GUARD, &protect)) {
122145
VirtualFree(pointer, 0, MEM_RELEASE);
123-
return 0;
146+
return false;
124147
}
125148
# endif
126149
#else
127150
pointer = mmap(NULL, msize, PROT_READ | PROT_WRITE, ZEND_FIBER_STACK_FLAGS, -1, 0);
128151

129152
if (pointer == MAP_FAILED) {
130-
return 0;
153+
return false;
131154
}
132155

133156
# if ZEND_FIBER_GUARD_PAGES
134157
if (mprotect(pointer, ZEND_FIBER_GUARD_PAGES * page_size, PROT_NONE) < 0) {
135158
munmap(pointer, msize);
136-
return 0;
159+
return false;
137160
}
138161
# endif
139162
#endif
@@ -145,7 +168,7 @@ static zend_bool zend_fiber_stack_allocate(zend_fiber_stack *stack, size_t size)
145168
stack->valgrind = VALGRIND_STACK_REGISTER(base, base + msize - ZEND_FIBER_GUARD_PAGES * page_size);
146169
#endif
147170

148-
return 1;
171+
return true;
149172
}
150173
/* }}} */
151174

@@ -195,10 +218,10 @@ static ZEND_NORETURN void zend_fiber_trampoline(transfer_t transfer) /* {{{ */
195218
}
196219
/* }}} */
197220

198-
ZEND_API zend_bool zend_fiber_init_context(zend_fiber_context *context, zend_fiber_coroutine coroutine, size_t stack_size) /* {{{ */
221+
ZEND_API bool zend_fiber_init_context(zend_fiber_context *context, zend_fiber_coroutine coroutine, size_t stack_size) /* {{{ */
199222
{
200223
if (UNEXPECTED(!zend_fiber_stack_allocate(&context->stack, stack_size))) {
201-
return 0;
224+
return false;
202225
}
203226

204227
// Stack grows down, calculate the top of the stack. make_fcontext then shifts pointer to lower 16-byte boundary.
@@ -208,22 +231,18 @@ ZEND_API zend_bool zend_fiber_init_context(zend_fiber_context *context, zend_fib
208231

209232
if (UNEXPECTED(!context->self)) {
210233
zend_fiber_stack_free(&context->stack);
211-
return 0;
234+
return false;
212235
}
213236

214237
context->function = coroutine;
215238
context->caller = NULL;
216239

217-
return 1;
240+
return true;
218241
}
219242
/* }}} */
220243

221244
ZEND_API void zend_fiber_destroy_context(zend_fiber_context *context) /* {{{ */
222245
{
223-
if (!context) {
224-
return;
225-
}
226-
227246
zend_fiber_stack_free(&context->stack);
228247
}
229248
/* }}} */
@@ -352,7 +371,7 @@ static void ZEND_STACK_ALIGNED zend_fiber_execute(zend_fiber_context *context) /
352371

353372
fiber->execute_data = (zend_execute_data *) stack->top;
354373

355-
ZEND_SECURE_ZERO(fiber->execute_data, sizeof(zend_execute_data));
374+
memset(fiber->execute_data, 0, sizeof(zend_execute_data));
356375

357376
EG(current_execute_data) = fiber->execute_data;
358377
EG(jit_trace_num) = 0;
@@ -392,15 +411,13 @@ static zend_object *zend_fiber_object_create(zend_class_entry *ce) /* {{{ */
392411
zend_fiber *fiber;
393412

394413
fiber = emalloc(sizeof(zend_fiber));
395-
ZEND_SECURE_ZERO(fiber, sizeof(zend_fiber));
414+
memset(fiber, 0, sizeof(zend_fiber));
396415

397416
fiber->id = EG(next_fiber_id)++;
398417

399418
zend_object_std_init(&fiber->std, ce);
400419
fiber->std.handlers = &zend_fiber_handlers;
401420

402-
ZVAL_UNDEF(&fiber->value);
403-
404421
zend_hash_index_add_ptr(&EG(fibers), fiber->id, fiber);
405422

406423
return &fiber->std;
@@ -458,7 +475,7 @@ ZEND_METHOD(Fiber, __construct)
458475
zend_fiber *fiber = (zend_fiber *) Z_OBJ_P(getThis());
459476

460477
ZEND_PARSE_PARAMETERS_START(1, 1)
461-
Z_PARAM_FUNC_EX(fiber->fci, fiber->fci_cache, 0, 0)
478+
Z_PARAM_FUNC(fiber->fci, fiber->fci_cache)
462479
ZEND_PARSE_PARAMETERS_END();
463480

464481
// Keep a reference to closures or callable objects until the fiber is started.
@@ -470,14 +487,22 @@ ZEND_METHOD(Fiber, __construct)
470487
ZEND_METHOD(Fiber, start)
471488
{
472489
zend_fiber *fiber = (zend_fiber *) Z_OBJ_P(getThis());
490+
zval *params;
491+
uint32_t param_count;
492+
zend_array *named_params;
493+
494+
ZEND_PARSE_PARAMETERS_START(0, -1)
495+
Z_PARAM_VARIADIC_WITH_NAMED(params, param_count, named_params);
496+
ZEND_PARSE_PARAMETERS_END();
473497

474498
if (fiber->status != ZEND_FIBER_STATUS_INIT) {
475499
zend_throw_error(zend_ce_fiber_error, "Cannot start a fiber that has already been started");
476500
return;
477501
}
478502

479-
fiber->fci.params = ZEND_CALL_ARG(execute_data, 1);
480-
fiber->fci.param_count = ZEND_CALL_NUM_ARGS(execute_data);
503+
fiber->fci.params = params;
504+
fiber->fci.param_count = param_count;
505+
fiber->fci.named_params = named_params;
481506

482507
if (!zend_fiber_init_context(&fiber->context, zend_fiber_execute, EG(fiber_stack_size))) {
483508
zend_throw_error(NULL, "Could not create fiber context");
@@ -505,6 +530,11 @@ ZEND_METHOD(Fiber, suspend)
505530
zend_fiber *fiber = EG(current_fiber);
506531
zval *error, *value = NULL;
507532

533+
ZEND_PARSE_PARAMETERS_START(0, 1)
534+
Z_PARAM_OPTIONAL
535+
Z_PARAM_ZVAL(value);
536+
ZEND_PARSE_PARAMETERS_END();
537+
508538
if (UNEXPECTED(!fiber)) {
509539
zend_throw_error(zend_ce_fiber_error, "Cannot suspend outside of a fiber");
510540
return;
@@ -519,11 +549,6 @@ ZEND_METHOD(Fiber, suspend)
519549
return;
520550
}
521551

522-
ZEND_PARSE_PARAMETERS_START(0, 1)
523-
Z_PARAM_OPTIONAL
524-
Z_PARAM_ZVAL(value);
525-
ZEND_PARSE_PARAMETERS_END();
526-
527552
if (value) {
528553
ZVAL_COPY(&fiber->value, value);
529554
} else {

Zend/zend_fibers.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,6 @@ typedef struct _zend_fiber_context {
5252
zend_fiber_stack stack;
5353
} zend_fiber_context;
5454

55-
#if _POSIX_MAPPED_FILES
56-
# include <sys/mman.h>
57-
# include <limits.h>
58-
59-
static zend_always_inline size_t zend_fiber_page_size()
60-
{
61-
static size_t page_size;
62-
63-
if (!page_size) {
64-
page_size = sysconf(_SC_PAGESIZE);
65-
}
66-
67-
return page_size;
68-
}
69-
#else
70-
# define zend_fiber_page_size() 4096
71-
#endif
72-
7355
#define ZEND_FIBER_GUARD_PAGES 1
7456

7557
#define ZEND_FIBER_DEFAULT_PAGE_COUNT (((sizeof(void *)) < 8) ? 512 : 2048)
@@ -132,6 +114,8 @@ ZEND_COLD ZEND_NORETURN void zend_error_suspend_fiber(
132114
ZEND_API void zend_fiber_switch_context(zend_fiber_context *to);
133115
ZEND_API void zend_fiber_suspend_context(zend_fiber_context *current);
134116

117+
size_t zend_fiber_page_size();
118+
135119
#define ZEND_FIBER_VM_STACK_SIZE (1024 * sizeof(zval))
136120

137121
END_EXTERN_C()

0 commit comments

Comments
 (0)