-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheventloop_cb.c
More file actions
190 lines (155 loc) · 4.16 KB
/
Copy patheventloop_cb.c
File metadata and controls
190 lines (155 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2026 Aleksandr Cherednikov
* Licensed under the MIT License. See LICENSE for details.
*/
#include "php_eventloop.h"
#include "ext/standard/php_string.h"
static zend_string *eventloop_generate_id(void)
{
char buf[24];
int len = snprintf(buf, sizeof(buf), "eL%" PRIx64, EVENTLOOP_G(next_id)++);
return zend_string_init(buf, len, 0);
}
eventloop_callback *eventloop_cb_create(eventloop_cb_type type, zval *closure)
{
eventloop_callback *cb;
cb = ecalloc(1, sizeof(eventloop_callback));
cb->id = eventloop_generate_id();
cb->type = type;
cb->flags = EVENTLOOP_CB_FLAG_ENABLED | EVENTLOOP_CB_FLAG_REFERENCED;
cb->refcount = 1; /* Held by EVENTLOOP_G(callbacks). */
ZVAL_COPY(&cb->closure, closure);
cb->heap_index = UINT32_MAX;
zend_hash_add_ptr(&EVENTLOOP_G(callbacks), cb->id, cb);
return cb;
}
void eventloop_cb_free(eventloop_callback *cb)
{
ZEND_ASSERT(cb != NULL);
if (cb->type == EVENTLOOP_CB_READABLE || cb->type == EVENTLOOP_CB_WRITABLE) {
zval_ptr_dtor(&cb->io.stream);
}
zval_ptr_dtor(&cb->closure);
zend_string_release(cb->id);
efree(cb);
}
void eventloop_cb_addref(eventloop_callback *cb)
{
ZEND_ASSERT(cb != NULL);
ZEND_ASSERT(cb->refcount > 0);
cb->refcount++;
}
void eventloop_cb_release(eventloop_callback *cb)
{
ZEND_ASSERT(cb != NULL);
ZEND_ASSERT(cb->refcount > 0);
cb->refcount--;
if (cb->refcount == 0) {
eventloop_cb_free(cb);
}
}
eventloop_callback *eventloop_cb_find(const zend_string *id)
{
return zend_hash_find_ptr(&EVENTLOOP_G(callbacks), (zend_string *)id);
}
void eventloop_cb_enable(eventloop_callback *cb)
{
ZEND_ASSERT(cb != NULL);
if (cb->flags & EVENTLOOP_CB_FLAG_CANCELLED) {
return;
}
if (UNEXPECTED(cb->flags & EVENTLOOP_CB_FLAG_ENABLED)) {
return;
}
cb->flags |= EVENTLOOP_CB_FLAG_ENABLED;
switch (cb->type) {
case EVENTLOOP_CB_READABLE:
case EVENTLOOP_CB_WRITABLE:
if (EXPECTED(EVENTLOOP_G(driver))) {
EVENTLOOP_G(driver)->add(cb);
}
break;
case EVENTLOOP_CB_DELAY:
cb->delay.expiry = eventloop_now() + cb->delay.delay;
if (!eventloop_timer_heap_push(&EVENTLOOP_G(timer_heap), cb)) {
cb->flags &= ~EVENTLOOP_CB_FLAG_ENABLED;
}
break;
case EVENTLOOP_CB_REPEAT:
cb->repeat.expiry = eventloop_now() + cb->repeat.interval;
if (!eventloop_timer_heap_push(&EVENTLOOP_G(timer_heap), cb)) {
cb->flags &= ~EVENTLOOP_CB_FLAG_ENABLED;
}
break;
default:
break;
}
}
void eventloop_cb_disable(eventloop_callback *cb)
{
ZEND_ASSERT(cb != NULL);
if (UNEXPECTED(!(cb->flags & EVENTLOOP_CB_FLAG_ENABLED))) {
return;
}
cb->flags &= ~EVENTLOOP_CB_FLAG_ENABLED;
switch (cb->type) {
case EVENTLOOP_CB_READABLE:
case EVENTLOOP_CB_WRITABLE:
if (EXPECTED(EVENTLOOP_G(driver))) {
EVENTLOOP_G(driver)->remove(cb);
}
break;
case EVENTLOOP_CB_DELAY:
case EVENTLOOP_CB_REPEAT:
if (cb->heap_index != UINT32_MAX) {
eventloop_timer_heap_remove(&EVENTLOOP_G(timer_heap), cb);
}
break;
default:
break;
}
}
void eventloop_cb_cancel(eventloop_callback *cb)
{
ZEND_ASSERT(cb != NULL);
if (UNEXPECTED(cb->flags & EVENTLOOP_CB_FLAG_CANCELLED)) {
return;
}
if (cb->type == EVENTLOOP_CB_READABLE || cb->type == EVENTLOOP_CB_WRITABLE) {
EVENTLOOP_G(io_watcher_count)--;
}
#ifndef PHP_WIN32
if (cb->type == EVENTLOOP_CB_SIGNAL) {
eventloop_signal_callback_cancelled(cb);
}
#endif
eventloop_cb_disable(cb);
cb->flags |= EVENTLOOP_CB_FLAG_CANCELLED;
cb->flags &= ~EVENTLOOP_CB_FLAG_ENABLED;
zend_hash_del(&EVENTLOOP_G(callbacks), cb->id);
}
bool eventloop_has_referenced_callbacks(void)
{
eventloop_callback *cb;
ZEND_HASH_FOREACH_PTR(&EVENTLOOP_G(callbacks), cb) {
if ((cb->flags & EVENTLOOP_CB_FLAG_REFERENCED) &&
(cb->flags & EVENTLOOP_CB_FLAG_ENABLED) &&
!(cb->flags & EVENTLOOP_CB_FLAG_CANCELLED)) {
return true;
}
} ZEND_HASH_FOREACH_END();
return false;
}
uint32_t eventloop_referenced_count(void)
{
uint32_t count = 0;
eventloop_callback *cb;
ZEND_HASH_FOREACH_PTR(&EVENTLOOP_G(callbacks), cb) {
if ((cb->flags & EVENTLOOP_CB_FLAG_REFERENCED) &&
(cb->flags & EVENTLOOP_CB_FLAG_ENABLED) &&
!(cb->flags & EVENTLOOP_CB_FLAG_CANCELLED)) {
count++;
}
} ZEND_HASH_FOREACH_END();
return count;
}