forked from birdwyx/phpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpgo_context.h
426 lines (377 loc) · 14.2 KB
/
phpgo_context.h
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#pragma once
#include <libgo/scheduler.h>
#include "freeable.h"
#include "task_local_storage.h"
#if PHP_MAJOR_VERSION < 7
#include "php_main.h"
#endif
extern "C" {
#include "ext/standard/basic_functions.h"
}
using namespace co;
#ifdef ZTS
#define TSRMLS_FIELD TSRMLS_D
#else
#define TSRMLS_FIELD
#endif
#ifdef ZTS
/*save_to_ctx->tsrm_ls = tsrm_ls*/
#define PHPGO_SAVE_TSRMLS(save_to_ctx) save_to_ctx->TSRMLS_C = TSRMLS_C
/*void*** tsrm_ls = load_from_ctx->trsm_ls*/
#define PHPGO_LOAD_TSRMLS(load_from_ctx) TSRMLS_C = load_from_ctx->TSRMLS_C
#else
#define PHPGO_SAVE_TSRMLS(save_to_ctx)
#define PHPGO_LOAD_TSRMLS(load_from_ctx)
#endif
#define NUM_TRACK_VARS 6
#define GET_HTTP_GLOBAL(name, http_globals, offset) \
do{ \
void* data = nullptr; \
phpgo_zend_hash_find(&EG(symbol_table), name, sizeof(name), (void**)&data); \
zval* pz_arr = PHP5_VS_7(*(zval**)data, (zval*)data); \
\
PHP5_AND_BELOW( \
if((http_globals)[offset]) zval_ptr_dtor( &(http_globals)[offset] ); \
if(pz_arr) { \
(http_globals)[offset] = pz_arr; \
Z_ADDREF_P(pz_arr); \
}else{ \
(http_globals)[offset] = nullptr; \
} \
) \
PHP7_AND_ABOVE( \
zval_ptr_dtor( &(http_globals)[offset] ); \
if(pz_arr) { \
ZVAL_COPY_VALUE( &(http_globals)[offset], pz_arr); \
Z_ADDREF_P(pz_arr); \
}else{ \
ZVAL_NULL( &(http_globals)[offset] ); \
} \
) \
}while(0)
#define GET_HTTP_REQUEST_GLOBAL(http_request_global) \
do{ \
void* data = nullptr; \
phpgo_zend_hash_find(&EG(symbol_table), "_REQUEST", sizeof("_REQUEST"), (void**)&data); \
zval* pz_arr = PHP5_VS_7(*(zval**)data, (zval*)data); \
\
PHP5_AND_BELOW( \
if(http_request_global) zval_ptr_dtor( &(http_request_global) ); \
if(pz_arr){ \
(http_request_global) = pz_arr; \
Z_ADDREF_P(pz_arr); \
}else{ \
(http_request_global) = nullptr; \
} \
) \
PHP7_AND_ABOVE( \
zval_ptr_dtor( &(http_request_global) ); \
if(pz_arr){ \
ZVAL_COPY_VALUE( &(http_request_global), pz_arr ); \
if( Z_TYPE_P(pz_arr) != IS_NULL ) \
Z_ADDREF_P(pz_arr); \
}else{ \
ZVAL_NULL( &(http_request_global) ); \
} \
) \
}while(0)
#define SET_HTTP_GLOBAL(name, http_globals, offset) \
do{ \
PHP5_AND_BELOW( \
if( (http_globals)[offset] ) {\
phpgo_zend_hash_update( \
&EG(symbol_table), name, sizeof(name), \
&(http_globals)[offset], sizeof(zval *), NULL \
); \
Z_ADDREF_P( (http_globals)[offset] ); \
} \
) \
PHP7_AND_ABOVE(\
zval* tmp = &(http_globals)[offset]; \
phpgo_zend_hash_update( \
&EG(symbol_table), name, sizeof(name), \
&tmp, sizeof(zval *), NULL \
); \
Z_ADDREF_P( &(http_globals)[offset] ); \
) \
}while(0)
#define SET_HTTP_REQUEST_GLOBAL(http_request_global) \
do{ \
PHP5_AND_BELOW( \
if( http_request_global ) {\
phpgo_zend_hash_update( \
&EG(symbol_table), "_REQUEST", sizeof("_REQUEST"),\
&(http_request_global), sizeof(zval *), NULL \
); \
Z_ADDREF_P( http_request_global ); \
} \
) \
PHP7_AND_ABOVE(\
zval* tmp = &(http_request_global); \
phpgo_zend_hash_update( \
&EG(symbol_table), "_REQUEST", sizeof("_REQUEST"), \
&tmp, sizeof(zval *), NULL \
); \
if( Z_TYPE_P(&(http_request_global)) != IS_NULL ) \
Z_ADDREF_P( &(http_request_global) ); \
) \
}while(0)
#define SET_HTTP_GLOBALS(http_globals, http_request_global) \
do{ \
SET_HTTP_GLOBAL("_GET", http_globals, TRACK_VARS_GET); \
SET_HTTP_GLOBAL("_POST", http_globals, TRACK_VARS_POST); \
SET_HTTP_GLOBAL("_COOKIE", http_globals, TRACK_VARS_COOKIE); \
SET_HTTP_GLOBAL("_SERVER", http_globals, TRACK_VARS_SERVER); \
SET_HTTP_GLOBAL("_ENV", http_globals, TRACK_VARS_ENV); \
SET_HTTP_GLOBAL("_FILES", http_globals, TRACK_VARS_FILES); \
SET_HTTP_REQUEST_GLOBAL( http_request_global ); /*get $_REQUEST*/ \
}while(0)
#define GET_HTTP_GLOBALS(http_globals, http_request_global) \
do{ \
GET_HTTP_GLOBAL("_GET", http_globals, TRACK_VARS_GET); \
GET_HTTP_GLOBAL("_POST", http_globals, TRACK_VARS_POST); \
GET_HTTP_GLOBAL("_COOKIE", http_globals, TRACK_VARS_COOKIE); \
GET_HTTP_GLOBAL("_SERVER", http_globals, TRACK_VARS_SERVER); \
GET_HTTP_GLOBAL("_ENV", http_globals, TRACK_VARS_ENV); \
GET_HTTP_GLOBAL("_FILES", http_globals, TRACK_VARS_FILES); \
GET_HTTP_REQUEST_GLOBAL( http_request_global ); /*get $_REQUEST*/ \
}while(0)
#define DELREF_HTTP_GLOBALS(http_globals, http_request_global) \
do{ \
for(int i=0; i<NUM_TRACK_VARS; i++){ \
PHP5_AND_BELOW( \
if((http_globals)[i]) )\
zval_ptr_dtor( &(http_globals)[i] ); \
} \
PHP5_AND_BELOW( \
if(http_request_global) ) \
zval_ptr_dtor( &(http_request_global) ); \
}while(0)
#define REPLACE_PG_HTTP_GLOBALS_WITH(__http_globals) \
do{ \
for(int i=0; i<NUM_TRACK_VARS; i++){ \
zval_ptr_dtor( &PG(http_globals)[i] ); \
} \
memcpy( PG(http_globals), (__http_globals), sizeof(PG(http_globals)) ); \
for(int i=0; i<NUM_TRACK_VARS; i++){ \
PHP5_VS_7( \
Z_ADDREF_P( (__http_globals)[i] ), \
Z_ADDREF_P( &(__http_globals)[i] ) \
); \
} \
}while(0)
#define MAKE_HTTP_GLOBALS(http_globals, http_request_global) \
do{ \
for(int i=0; i<NUM_TRACK_VARS; i++){ \
PHP5_AND_BELOW( \
MAKE_STD_ZVAL( (http_globals)[i] ); \
array_init( (http_globals)[i] ); \
); \
PHP7_AND_ABOVE( array_init( &(http_globals)[i] ) ); \
} \
PHP5_AND_BELOW( \
MAKE_STD_ZVAL( http_request_global ); \
array_init( http_request_global ); \
); \
PHP7_AND_ABOVE( array_init( &(http_request_global) ) ); \
}while(0)
/*null-out our concerned globals to avoid potential problem*/
#if PHP_MAJOR_VERSION < 7
#define PHPGO_INITIALIZE_RUNNING_ENVIRONMENT() \
{ \
EG(current_execute_data ) = NULL; \
EG(bailout ) = NULL; \
EG(argument_stack ) = NULL; \
EG(scope ) = NULL; \
EG(This ) = NULL; \
EG(called_scope ) = NULL; \
EG(active_symbol_table ) = NULL; \
EG(return_value_ptr_ptr ) = NULL; \
EG(active_op_array ) = NULL; \
EG(opline_ptr ) = NULL; \
INIT_ZVAL(EG(error_zval)); \
EG(error_zval_ptr ) = NULL; \
EG(user_error_handler ) = NULL; \
BG(user_shutdown_function_names) = NULL; \
}
#else
#define PHPGO_INITIALIZE_RUNNING_ENVIRONMENT() \
{ \
EG(current_execute_data ) = NULL; \
EG(bailout ) = NULL; \
EG(vm_stack ) = NULL; \
EG(vm_stack_top ) = NULL; \
EG(vm_stack_end ) = NULL; \
BG(user_shutdown_function_names) = NULL; \
}
#endif
struct PhpgoBaseContext{
uint64_t guard__[8];
uint64_t task_id;
bool http_globals_cleanup_required;
struct _zend_execute_data* EG_current_execute_data;
#if PHP_MAJOR_VERSION < 7
/*go routine running environment*/
zend_vm_stack EG_argument_stack;
zend_class_entry* EG_scope;
zval* EG_This;
zend_class_entry* EG_called_scope;
HashTable* EG_active_symbol_table;
zval** EG_return_value_ptr_ptr;
zend_op_array* EG_active_op_array;
zend_op** EG_opline_ptr;
zval EG_error_zval;
zval* EG_error_zval_ptr;
zval* EG_user_error_handler;
zval* PG_http_globals[NUM_TRACK_VARS];
zval* http_request_global;
TSRMLS_FIELD; /*ZTS: void ***tsrm_ls;*/
#else
/* php7 */
zend_vm_stack EG_vm_stack;
zval* EG_vm_stack_top;
zval* EG_vm_stack_end;
zval PG_http_globals[NUM_TRACK_VARS];
zval http_request_global;
#endif
JMP_BUF* EG_bailout;
HashTable* BG_user_shutdown_function_names;
uint64_t __guard[8];
/**/
PhpgoBaseContext(){
bzero(this, sizeof(*this));
memset(guard__, 0xcc, sizeof(guard__));
memset(__guard, 0xcc, sizeof(__guard));
PHP7_AND_ABOVE(
for(int i=0; i< NUM_TRACK_VARS; i++)
ZVAL_NULL( &PG_http_globals[i] );
ZVAL_NULL( &http_request_global );
);
}
protected:
inline void SwapOut(bool include_http_globals){
#if PHP_MAJOR_VERSION < 7
TSRMLS_FIELD;
PHPGO_LOAD_TSRMLS(this);
#endif
if(include_http_globals){
GET_HTTP_GLOBALS(this->PG_http_globals, this->http_request_global);
http_globals_cleanup_required = true;
}
/* save the current EG */
this->EG_current_execute_data = EG(current_execute_data );
#if PHP_MAJOR_VERSION < 7
this->EG_argument_stack = EG(argument_stack );
this->EG_scope = EG(scope );
this->EG_This = EG(This );
this->EG_called_scope = EG(called_scope );
this->EG_active_symbol_table = EG(active_symbol_table );
this->EG_return_value_ptr_ptr = EG(return_value_ptr_ptr );
this->EG_active_op_array = EG(active_op_array );
this->EG_opline_ptr = EG(opline_ptr );
this->EG_error_zval = EG(error_zval );
this->EG_error_zval_ptr = EG(error_zval_ptr );
this->EG_user_error_handler = EG(user_error_handler );
#else
this->EG_vm_stack = EG(vm_stack );
this->EG_vm_stack_top = EG(vm_stack_top );
this->EG_vm_stack_end = EG(vm_stack_end );
#endif
this->EG_bailout = EG(bailout );
this->BG_user_shutdown_function_names = BG(user_shutdown_function_names);
}
inline void SwapIn(bool include_http_globals){
#if PHP_MAJOR_VERSION < 7
TSRMLS_FIELD;
PHPGO_LOAD_TSRMLS(this);
#endif
/* load EG from the task specific context*/
EG(current_execute_data ) = this->EG_current_execute_data;
#if PHP_MAJOR_VERSION < 7
EG(argument_stack ) = this->EG_argument_stack ;
EG(scope ) = this->EG_scope ;
EG(This ) = this->EG_This ;
EG(called_scope ) = this->EG_called_scope ;
EG(active_symbol_table ) = this->EG_active_symbol_table ;
EG(return_value_ptr_ptr ) = this->EG_return_value_ptr_ptr;
EG(active_op_array ) = this->EG_active_op_array ;
EG(opline_ptr ) = this->EG_opline_ptr ;
EG(error_zval ) = this->EG_error_zval ;
EG(error_zval_ptr ) = this->EG_error_zval_ptr ;
EG(user_error_handler ) = this->EG_user_error_handler ;
#else
EG(vm_stack ) = this->EG_vm_stack ;
EG(vm_stack_top ) = this->EG_vm_stack_top ;
EG(vm_stack_end ) = this->EG_vm_stack_end ;
#endif
EG(bailout ) = this->EG_bailout ;
BG(user_shutdown_function_names) = this->BG_user_shutdown_function_names;
if(include_http_globals){
REPLACE_PG_HTTP_GLOBALS_WITH(this->PG_http_globals);
SET_HTTP_GLOBALS(this->PG_http_globals, this->http_request_global);
}
}
public:
inline void Cleanup(){
if (this->http_globals_cleanup_required) {
DELREF_HTTP_GLOBALS(this->PG_http_globals, this->http_request_global);
}
}
};
struct PhpgoContext : public PhpgoBaseContext, public FreeableImpl{
public:
uint64_t go_routine_options;
bool go_routine_finished;
public:
PhpgoContext(uint64_t options TSRMLS_DC){
#if PHP_MAJOR_VERSION < 7
TSRMLS_SET_CTX(this->TSRMLS_C);
#else
#endif
this->go_routine_options = options;
this->go_routine_finished = false;
this->task_id = g_Scheduler.GetCurrentTaskID();
}
inline void SwapOut(){
PhpgoBaseContext::SwapOut(this->go_routine_options & GoRoutineOptions::gro_isolate_http_globals);
// the coroutine ( not the scheduler ) is finished
// free the task local storage (including this context itself)
if(this->go_routine_finished) {
this->Cleanup();
}
}
inline void SwapIn(){
PhpgoBaseContext::SwapIn(this->go_routine_options & GoRoutineOptions::gro_isolate_http_globals);
}
inline void SetFinished(bool finished){
this->go_routine_finished = finished;
}
inline void Cleanup(){
PhpgoBaseContext::Cleanup();
// should be last sentence as this will free the context itself
TaskLocalStorage::FreeSpecifics(this->task_id);
}
};
// the Scheduler Context is essentially the same as the Task's context,
// but since Scheduler Context is thread local and thread locals cannot
// have virtual members, we have to remove the FreeableImpl from the
// Scheduler Context...
struct PhpgoSchedulerContext : public PhpgoBaseContext{
public:
PhpgoSchedulerContext(){
this->task_id = 0;
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH(); // void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL)
TSRMLS_SET_CTX(this->TSRMLS_C); // this->tsrm_ls = (void ***) tsrm_ls
#endif
}
inline void SwapOut(bool include_http_globals){
PhpgoBaseContext::SwapOut(include_http_globals);
}
inline void SwapIn(bool include_http_globals){
PhpgoBaseContext::SwapIn(include_http_globals);
}
};
// the scheduler may be executed in multiple thread:
// use thread local variable to store the scheduler EG's
extern thread_local PhpgoSchedulerContext scheduler_ctx;