-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpool.h
More file actions
129 lines (104 loc) · 4.83 KB
/
pool.h
File metadata and controls
129 lines (104 loc) · 4.83 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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Edmond |
+----------------------------------------------------------------------+
*/
#ifndef ASYNC_POOL_H
#define ASYNC_POOL_H
#include "php_async_api.h"
#include <Zend/zend_async_API.h>
#include "internal/circular_buffer.h"
///////////////////////////////////////////////////////////////////////////////
// Internal pool implementation
// Note: Base types (zend_async_pool_t, zend_async_circuit_state_t,
// handler function types, strategy struct) are in zend_async_API.h
///////////////////////////////////////////////////////////////////////////////
/* Pool waiter - coroutine waiting for resource */
typedef struct
{
zend_coroutine_event_callback_t callback;
} zend_async_pool_waiter_t;
/**
* Extended pool structure with implementation details.
* Contains zend_async_pool_t as first member for ABI compatibility.
*/
typedef struct _async_pool_s
{
/* ABI structure (must be first) */
zend_async_pool_t base;
/* Storage */
circular_buffer_t idle; /* idle resources (zval) */
uint32_t active_count; /* resources currently in use */
/* Waiters queue - like waiting_receivers in channel */
zend_async_callbacks_vector_t waiters;
/* Healthcheck timer */
zend_async_timer_event_t *healthcheck_timer;
zend_async_event_callback_t healthcheck_callback;
uint32_t healthcheck_interval_ms;
} async_pool_t;
/* Helper macros */
#define ASYNC_POOL_TOTAL(pool) (circular_buffer_count(&(pool)->idle) + (pool)->active_count)
#define ZEND_ASYNC_POOL_IS_CLOSED(pool) ZEND_ASYNC_EVENT_IS_CLOSED(&(pool)->base.event)
///////////////////////////////////////////////////////////////////////////////
// C API functions
///////////////////////////////////////////////////////////////////////////////
/* Create a new pool with PHP callables */
async_pool_t *zend_async_pool_create(zend_fcall_t *factory,
zend_fcall_t *destructor,
zend_fcall_t *healthcheck,
zend_fcall_t *before_acquire,
zend_fcall_t *before_release,
uint32_t min_size,
uint32_t max_size,
uint32_t healthcheck_interval_ms);
/* Create a new pool with internal C handlers */
async_pool_t *zend_async_pool_create_internal(zend_async_pool_factory_fn factory,
zend_async_pool_destructor_fn destructor,
zend_async_pool_healthcheck_fn healthcheck,
zend_async_pool_before_acquire_fn before_acquire,
zend_async_pool_before_release_fn before_release,
uint32_t min_size,
uint32_t max_size,
uint32_t healthcheck_interval_ms);
/* Acquire resource (blocking) */
bool zend_async_pool_acquire(async_pool_t *pool, zval *result, zend_long timeout_ms);
/* Try to acquire resource (non-blocking) */
bool zend_async_pool_try_acquire(async_pool_t *pool, zval *result);
/* Release resource back to pool */
void zend_async_pool_release(async_pool_t *pool, zval *resource);
/* Close pool (wake waiters with exception) */
void zend_async_pool_close(async_pool_t *pool);
/* Destroy pool (free memory) */
void zend_async_pool_destroy(async_pool_t *pool);
/* Statistics */
uint32_t zend_async_pool_count(async_pool_t *pool);
uint32_t zend_async_pool_idle_count(async_pool_t *pool);
uint32_t zend_async_pool_active_count(async_pool_t *pool);
///////////////////////////////////////////////////////////////////////////////
// PHP object wrapper
///////////////////////////////////////////////////////////////////////////////
typedef struct _async_pool_obj_s
{
async_pool_t *pool; /* pointer to internal pool */
zend_object std; /* PHP object (must be last) */
} async_pool_obj_t;
/* Class entries */
extern zend_class_entry *async_ce_pool;
extern zend_class_entry *async_ce_pool_exception;
/* Convert macros */
#define ASYNC_POOL_OBJ_FROM_OBJ(obj) ((async_pool_obj_t *) ((char *) (obj) - XtOffsetOf(async_pool_obj_t, std)))
/* Create PHP object wrapper for existing pool */
zend_object *async_pool_create_object_for_pool(async_pool_t *pool);
/* Registration function */
void async_register_pool_ce(void);
#endif /* ASYNC_POOL_H */