Skip to content

Commit c87f212

Browse files
committed
poll: use PollBackend enum for backends
1 parent d027c6c commit c87f212

File tree

7 files changed

+114
-79
lines changed

7 files changed

+114
-79
lines changed

ext/standard/poll.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616

1717
#include "php.h"
18+
#include "zend_enum.h"
19+
#include "zend_exceptions.h"
1820
#include "php_network.h"
1921
#include "php_poll.h"
2022
#include "poll_arginfo.h"
21-
#include "zend_exceptions.h"
2223

2324
/* Class entries */
25+
static zend_class_entry *php_poll_backend_class_entry;
2426
static zend_class_entry *php_poll_context_class_entry;
2527
static zend_class_entry *php_poll_watcher_class_entry;
2628
static zend_class_entry *php_poll_handle_class_entry;
@@ -453,19 +455,19 @@ PHP_METHOD(PollWatcher, remove)
453455

454456
PHP_METHOD(PollContext, __construct)
455457
{
456-
zend_long backend_long = PHP_POLL_BACKEND_AUTO;
457-
zend_string *backend_str = NULL;
458+
zval *backend_obj;
458459

459460
ZEND_PARSE_PARAMETERS_START(0, 1)
460461
Z_PARAM_OPTIONAL
461-
Z_PARAM_STR_OR_LONG(backend_str, backend_long)
462+
Z_PARAM_OBJECT_OF_CLASS(backend_obj, php_poll_backend_class_entry)
462463
ZEND_PARSE_PARAMETERS_END();
463464

464465
php_poll_context_object *intern = PHP_POLL_CONTEXT_OBJ_FROM_ZV(getThis());
465466

466-
if (backend_str == NULL) {
467-
intern->ctx = php_poll_create((php_poll_backend_type) backend_long, 0);
467+
if (backend_obj == NULL) {
468+
intern->ctx = php_poll_create(PHP_POLL_BACKEND_AUTO, 0);
468469
} else {
470+
const zend_string *backend_str = Z_STR_P(zend_enum_fetch_case_value(Z_OBJ_P(backend_obj)));
469471
intern->ctx = php_poll_create_by_name(ZSTR_VAL(backend_str), 0);
470472
}
471473

@@ -599,13 +601,29 @@ PHP_METHOD(PollContext, wait)
599601
efree(events);
600602
}
601603

602-
PHP_METHOD(PollContext, getBackendName)
604+
PHP_METHOD(PollContext, getBackend)
603605
{
604606
ZEND_PARSE_PARAMETERS_NONE();
605607

606608
php_poll_context_object *intern = PHP_POLL_CONTEXT_OBJ_FROM_ZV(getThis());
607609
const char *backend_name = php_poll_backend_name(intern->ctx);
608-
RETURN_STRING(backend_name);
610+
const char *entryname = NULL;
611+
612+
if (!strcmp(backend_name, "epoll")) {
613+
entryname = "Epoll";
614+
} else if (!strcmp(backend_name, "kqueu")) {
615+
entryname = "Kqueue";
616+
} else if (!strcmp(backend_name, "wsapoll")) {
617+
entryname = "WSAPoll";
618+
} else if (!strcmp(backend_name, "eventport")) {
619+
entryname = "EventPorts";
620+
} else if (!strcmp(backend_name, "poll")) {
621+
entryname = "Poll";
622+
} else {
623+
entryname = "Auto"; /* This should never happen */
624+
}
625+
626+
RETURN_OBJ_COPY(zend_enum_get_case_cstr(php_poll_backend_class_entry, entryname));
609627
}
610628

611629
/* Initialize the stream poll classes - add to PHP_MINIT_FUNCTION */
@@ -614,6 +632,9 @@ PHP_MINIT_FUNCTION(poll)
614632
/* Register symbols */
615633
register_poll_symbols(module_number);
616634

635+
/* Register backend */
636+
php_poll_backend_class_entry = register_class_PollBackend();
637+
617638
/* Register base PollHandle class */
618639
php_poll_handle_class_entry = register_class_PollHandle();
619640
php_poll_handle_class_entry->create_object = php_poll_handle_create_object;

ext/standard/poll.stub.php

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,15 @@
3838
*/
3939
const POLL_EVENT_ET = UNKNOWN;
4040

41-
/**
42-
* @var int
43-
* @cvalue PHP_POLL_BACKEND_AUTO
44-
*/
45-
const POLL_BACKEND_AUTO = UNKNOWN;
46-
/**
47-
* @var int
48-
* @cvalue PHP_POLL_BACKEND_POLL
49-
*/
50-
const POLL_BACKEND_POLL = UNKNOWN;
51-
/**
52-
* @var int
53-
* @cvalue PHP_POLL_BACKEND_EPOLL
54-
*/
55-
const POLL_BACKEND_EPOLL = UNKNOWN;
56-
/**
57-
* @var int
58-
* @cvalue PHP_POLL_BACKEND_KQUEUE
59-
*/
60-
const POLL_BACKEND_KQUEUE = UNKNOWN;
61-
/**
62-
* @var int
63-
* @cvalue PHP_POLL_BACKEND_EVENTPORT
64-
*/
65-
const POLL_BACKEND_EVENTPORT = UNKNOWN;
66-
/**
67-
* @var int
68-
* @cvalue PHP_POLL_BACKEND_WSAPOLL
69-
*/
70-
const POLL_BACKEND_WSAPOLL = UNKNOWN;
71-
41+
enum PollBackend : string
42+
{
43+
case Auto = "auto";
44+
case Poll = "poll";
45+
case Epoll = "epoll";
46+
case Kqueue = "kqueue";
47+
case EventPorts = "eventport";
48+
case WSAPoll = "wsapoll";
49+
}
7250

7351
abstract class PollHandle
7452
{
@@ -113,13 +91,13 @@ public function remove(): void {}
11391

11492
final class PollContext
11593
{
116-
public function __construct(int|string $backend = POLL_BACKEND_AUTO) {}
94+
public function __construct(PollBackend $backend = PollBackend::Auto) {}
11795

11896
public function add(PollHandle $handle, int $events, mixed $data = null): PollWatcher {}
11997

12098
public function wait(int $timeout = -1, int $maxEvents = -1): array {}
12199

122-
public function getBackendName(): string {}
100+
public function getBackend(): PollBackend {}
123101
}
124102

125103
class PollException extends Exception

ext/standard/poll_arginfo.h

Lines changed: 42 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/tests/poll/poll.inc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
function pt_new_stream_poll(): PollContext {
55
$backend = getenv('POLL_TEST_BACKEND');
6-
return new PollContext($backend === false ? POLL_BACKEND_AUTO : $backend);
6+
return new PollContext($backend === false ? PollBackend::Auto : PollBackend::from($backend));
77
}
88

99
function pt_stream_poll_add($poll_ctx, $stream, $events, $data = null): PollWatcher {
@@ -12,9 +12,9 @@ function pt_stream_poll_add($poll_ctx, $stream, $events, $data = null): PollWatc
1212

1313
function pt_skip_for_backend($backend, $msg): void {
1414
$backends_to_skip = is_array($backend) ? $backend : array($backend);
15-
$current_backend = pt_new_stream_poll()->getBackendName();
16-
if (in_array($current_backend, $backends_to_skip)) {
17-
die("skip backend $current_backend $msg\n");
15+
$current_backend = pt_new_stream_poll()->getBackend();
16+
if (in_array($current_backend->value, $backends_to_skip)) {
17+
die("skip backend $current_backend->value $msg\n");
1818
}
1919
}
2020

@@ -119,12 +119,12 @@ function pt_expect_events($watchers, $expected, ?PollContext $poll_ctx = null):
119119
$event_count = count($watchers);
120120
$expected_count = count($expected);
121121

122-
// Get current backend name for backend-specific expectations
123-
$backend_name = $poll_ctx ? $poll_ctx->getBackendName() : 'unknown';
122+
// Get the current backend for backend-specific expectations
123+
$backend = $poll_ctx ? $poll_ctx->getBackend() : 'unknown';
124124

125125
if ($event_count !== $expected_count) {
126126
echo "Event count mismatch: got $event_count, expected $expected_count\n";
127-
pt_print_mismatched_events($watchers, $expected, [], $backend_name);
127+
pt_print_mismatched_events($watchers, $expected, [], $backend->value);
128128
return;
129129
}
130130

@@ -149,7 +149,7 @@ function pt_expect_events($watchers, $expected, ?PollContext $poll_ctx = null):
149149
$resolved_event = $exp_event;
150150

151151
if (isset($exp_event['events']) && is_array($exp_event['events'])) {
152-
$resolved_event['events'] = pt_resolve_backend_specific_value($exp_event['events'], $backend_name);
152+
$resolved_event['events'] = pt_resolve_backend_specific_value($exp_event['events'], $backend->value);
153153
}
154154

155155
$resolved_expected[] = $resolved_event;
@@ -195,7 +195,7 @@ function pt_expect_events($watchers, $expected, ?PollContext $poll_ctx = null):
195195
echo "Events matched - count: $event_count\n";
196196
} else {
197197
echo "Events did not match:\n";
198-
pt_print_mismatched_events($watchers, $expected, $matched, $backend_name);
198+
pt_print_mismatched_events($watchers, $expected, $matched, $backend->value);
199199
}
200200
}
201201

ext/standard/tests/poll/poll_stream_backend_name_unix.phpt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ if (substr(PHP_OS, 0, 3) == 'WIN') {
1111
require_once __DIR__ . '/poll.inc';
1212
// this just prints default poll ctx
1313
$poll_ctx = new PollContext();
14-
var_dump($poll_ctx->getBackendName());
15-
// poll is always available on Unix systems
16-
$poll_ctx = new PollContext(POLL_BACKEND_POLL);
17-
var_dump($poll_ctx->getBackendName());
18-
// test with string
19-
$poll_ctx = new PollContext('poll');
20-
var_dump($poll_ctx->getBackendName());
14+
var_dump($poll_ctx->getBackend());
15+
// test with poll that is always available on Unix systems
16+
$poll_ctx = new PollContext(PollBackend::Poll);
17+
$backend = $poll_ctx->getBackend();
18+
var_dump($backend->name);
19+
var_dump($backend->value);
2120

2221
?>
2322
--EXPECTF--
24-
string(%d) %s
25-
string(4) "poll"
23+
enum(PollBackend::%s)
24+
string(4) "Poll"
2625
string(4) "poll"

ext/standard/tests/poll/poll_stream_backend_name_windows.phpt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ if (substr(PHP_OS, 0, 3) != 'WIN') {
99
--FILE--
1010
<?php
1111
require_once __DIR__ . '/poll.inc';
12-
// wsapoll is always available on Windows
13-
$poll_ctx = new PollContext(POLL_BACKEND_WSAPOLL);
14-
var_dump($poll_ctx->getBackendName());
15-
// test with string
16-
$poll_ctx = new PollContext('wsapoll');
17-
var_dump($poll_ctx->getBackendName());
18-
12+
// this just prints default poll ctx
13+
$poll_ctx = new PollContext();
14+
var_dump($poll_ctx->getBackend());
15+
// test with WSAPoll
16+
$poll_ctx = new PollContext(PollBackend::WSAPoll);
17+
$backend = $poll_ctx->getBackend();
18+
var_dump($backend->name);
19+
var_dump($backend->value);
1920
?>
20-
--EXPECT--
21-
string(7) "wsapoll"
21+
--EXPECTF--
22+
enum(PollBackend::%s)
23+
string(7) "WSAPoll"
2224
string(7) "wsapoll"

main/poll/poll_core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ PHPAPI php_poll_ctx *php_poll_create(php_poll_backend_type preferred_backend, ui
134134
/* Create new poll context */
135135
PHPAPI php_poll_ctx *php_poll_create_by_name(const char *preferred_backend, uint32_t flags)
136136
{
137+
if (!strcmp(preferred_backend, "auto")) {
138+
return php_poll_create(PHP_POLL_BACKEND_AUTO, flags);
139+
}
140+
137141
php_poll_ctx *ctx = php_poll_create_context(flags);
138142
if (ctx == NULL) {
139143
return NULL;

0 commit comments

Comments
 (0)