-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventloop_cb.c
More file actions
161 lines (130 loc) · 3.57 KB
/
eventloop_cb.c
File metadata and controls
161 lines (130 loc) · 3.57 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
/*
* 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;
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);
}
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;
eventloop_timer_heap_push(&EVENTLOOP_G(timer_heap), cb);
break;
case EVENTLOOP_CB_REPEAT:
cb->repeat.expiry = eventloop_now() + cb->repeat.interval;
eventloop_timer_heap_push(&EVENTLOOP_G(timer_heap), cb);
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)--;
}
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;
}