Skip to content

Commit 4f75383

Browse files
committed
poll: add raw events support to allow lower overhead on kqueue
1 parent ab9997f commit 4f75383

File tree

6 files changed

+38
-19
lines changed

6 files changed

+38
-19
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3362,7 +3362,7 @@ function soundex(string $string): string {}
33623362

33633363
/* streamsfuncs.c */
33643364

3365-
function stream_poll_create(int|string $backend = STREAM_POLL_BACKEND_AUTO): StreamPollContext {}
3365+
function stream_poll_create(int|string $backend = STREAM_POLL_BACKEND_AUTO, bool $raw_events = false): StreamPollContext {}
33663366

33673367
/** @param resource $stream */
33683368
function stream_poll_add(StreamPollContext $poll_ctx, $stream, int $events, mixed $data = null): void {}

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/stream_poll.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,20 @@ PHP_FUNCTION(stream_poll_create)
9999
{
100100
zend_long backend_long = PHP_POLL_BACKEND_AUTO;
101101
zend_string *backend_str = NULL;
102+
bool raw_events = false;
102103
php_poll_ctx *poll_ctx;
103104

104105
ZEND_PARSE_PARAMETERS_START(0, 1)
105106
Z_PARAM_OPTIONAL
106107
Z_PARAM_STR_OR_LONG(backend_str, backend_long)
108+
Z_PARAM_BOOL(raw_events)
107109
ZEND_PARSE_PARAMETERS_END();
108110

111+
uint32_t flags = raw_events ? PHP_POLL_FLAG_RAW_EVENTS : 0;
109112
if (backend_str == NULL) {
110-
poll_ctx = php_poll_create((php_poll_backend_type) backend_long, false);
113+
poll_ctx = php_poll_create((php_poll_backend_type) backend_long, flags);
111114
} else {
112-
poll_ctx = php_poll_create_by_name(ZSTR_VAL(backend_str), false);
115+
poll_ctx = php_poll_create_by_name(ZSTR_VAL(backend_str), flags);
113116
}
114117
if (!poll_ctx) {
115118
zend_throw_exception(

main/php_poll.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#define PHP_POLL_ONESHOT 0x20
2929
#define PHP_POLL_ET 0x40 /* Edge-triggered */
3030

31+
/* Poll flags */
32+
#define PHP_POLL_FLAG_PERSISTENT 0x01
33+
#define PHP_POLL_FLAG_RAW_EVENTS 0x02
34+
3135
/* Poll backend types */
3236
typedef enum {
3337
PHP_POLL_BACKEND_AUTO = -1,
@@ -70,8 +74,8 @@ typedef struct php_poll_backend_ops php_poll_backend_ops;
7074
typedef struct php_poll_event php_poll_event;
7175

7276
/* Public API */
73-
PHPAPI php_poll_ctx *php_poll_create(php_poll_backend_type preferred_backend, bool persistent);
74-
PHPAPI php_poll_ctx *php_poll_create_by_name(const char *preferred_backend, bool persistent);
77+
PHPAPI php_poll_ctx *php_poll_create(php_poll_backend_type preferred_backend, uint32_t flags);
78+
PHPAPI php_poll_ctx *php_poll_create_by_name(const char *preferred_backend, uint32_t flags);
7579

7680
PHPAPI zend_result php_poll_set_max_events_hint(php_poll_ctx *ctx, int max_events);
7781
PHPAPI zend_result php_poll_init(php_poll_ctx *ctx);

main/poll/php_poll_internal.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ typedef struct php_poll_backend_ops {
6262
struct php_poll_ctx {
6363
const php_poll_backend_ops *backend_ops;
6464
php_poll_backend_type backend_type;
65-
66-
bool initialized;
67-
bool persistent;
65+
php_poll_error last_error;
6866

6967
/* Optional capacity hint for backends */
7068
int max_events_hint;
7169

72-
/* Last error */
73-
php_poll_error last_error;
70+
/* Flags */
71+
uint32_t initialized : 1;
72+
uint32_t persistent : 1;
73+
uint32_t raw_events : 1;
7474

7575
/* Backend-specific data */
7676
void *backend_data;

main/poll/poll_core.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,31 @@ static const php_poll_backend_ops *php_poll_get_backend_ops_by_name(const char *
9999
return NULL;
100100
}
101101

102-
/* Create new poll context */
103-
PHPAPI php_poll_ctx *php_poll_create(php_poll_backend_type preferred_backend, bool persistent)
102+
static php_poll_ctx *php_poll_create_context(uint32_t flags)
104103
{
104+
bool persistent = flags & PHP_POLL_FLAG_PERSISTENT;
105105
php_poll_ctx *ctx = php_poll_calloc(1, sizeof(php_poll_ctx), persistent);
106106
if (!ctx) {
107107
return NULL;
108108
}
109109
ctx->persistent = persistent;
110+
ctx->raw_events = flags & PHP_POLL_FLAG_RAW_EVENTS;
111+
112+
return ctx;
113+
}
114+
115+
/* Create new poll context */
116+
PHPAPI php_poll_ctx *php_poll_create(php_poll_backend_type preferred_backend, uint32_t flags)
117+
{
118+
php_poll_ctx *ctx = php_poll_create_context(flags);
119+
if (ctx == NULL) {
120+
return NULL;
121+
}
110122

111123
/* Get backend operations */
112124
ctx->backend_ops = php_poll_get_backend_ops(preferred_backend);
113125
if (!ctx->backend_ops) {
114-
pefree(ctx, persistent);
126+
pefree(ctx, ctx->persistent);
115127
return NULL;
116128
}
117129
ctx->backend_type = preferred_backend;
@@ -120,18 +132,17 @@ PHPAPI php_poll_ctx *php_poll_create(php_poll_backend_type preferred_backend, bo
120132
}
121133

122134
/* Create new poll context */
123-
PHPAPI php_poll_ctx *php_poll_create_by_name(const char *preferred_backend, bool persistent)
135+
PHPAPI php_poll_ctx *php_poll_create_by_name(const char *preferred_backend, uint32_t flags)
124136
{
125-
php_poll_ctx *ctx = php_poll_calloc(1, sizeof(php_poll_ctx), persistent);
126-
if (!ctx) {
137+
php_poll_ctx *ctx = php_poll_create_context(flags);
138+
if (ctx == NULL) {
127139
return NULL;
128140
}
129-
ctx->persistent = persistent;
130141

131142
/* Get backend operations */
132143
ctx->backend_ops = php_poll_get_backend_ops_by_name(preferred_backend);
133144
if (!ctx->backend_ops) {
134-
pefree(ctx, persistent);
145+
pefree(ctx, ctx->persistent);
135146
return NULL;
136147
}
137148
ctx->backend_type = ctx->backend_ops->type;

0 commit comments

Comments
 (0)