@@ -86,7 +86,7 @@ static void php_swoole_server_onManagerStart(Server *serv);
86
86
static void php_swoole_server_onManagerStop (Server *serv);
87
87
88
88
static int php_swoole_server_task_finish (Server *serv, zval *zdata, EventData *current_task);
89
- static TaskId php_swoole_server_task_pack (EventData *task, zval *data );
89
+ static TaskId php_swoole_server_task_pack (zval *data, EventData *task );
90
90
static bool php_swoole_server_task_unpack (zval *zresult, EventData *task_result);
91
91
static int php_swoole_server_dispatch_func (Server *serv, Connection *conn, SendData *data);
92
92
static zval *php_swoole_server_add_port (ServerObject *server_object, ListenPort *port);
@@ -655,7 +655,7 @@ int php_swoole_create_dir(const char *path, size_t length) {
655
655
return php_stream_mkdir (path, 0777 , PHP_STREAM_MKDIR_RECURSIVE | REPORT_ERRORS, nullptr ) ? 0 : -1 ;
656
656
}
657
657
658
- static TaskId php_swoole_server_task_pack (EventData *task, zval *zdata ) {
658
+ static TaskId php_swoole_server_task_pack (zval *zdata, EventData *task ) {
659
659
smart_str serialized_data = {};
660
660
php_serialize_data_t var_hash;
661
661
@@ -2973,7 +2973,8 @@ static PHP_METHOD(swoole_server, stats) {
2973
2973
2974
2974
if (serv->task_worker_num > 0 ) {
2975
2975
add_assoc_long_ex (return_value, ZEND_STRL (" task_idle_worker_num" ), serv->get_idle_task_worker_num ());
2976
- add_assoc_long_ex (return_value, ZEND_STRL (" tasking_num" ), serv->get_task_count ());
2976
+ add_assoc_long_ex (return_value, ZEND_STRL (" tasking_num" ), serv->get_tasking_num ());
2977
+ add_assoc_long_ex (return_value, ZEND_STRL (" task_count" ), serv->gs ->task_count );
2977
2978
}
2978
2979
2979
2980
add_assoc_long_ex (return_value, ZEND_STRL (" coroutine_num" ), Coroutine::count ());
@@ -3056,9 +3057,6 @@ static PHP_METHOD(swoole_server, taskwait) {
3056
3057
RETURN_FALSE;
3057
3058
}
3058
3059
3059
- EventData buf;
3060
- memset (&buf.info , 0 , sizeof (buf.info ));
3061
-
3062
3060
zval *zdata;
3063
3061
double timeout = SW_TASKWAIT_TIMEOUT;
3064
3062
zend_long dst_worker_id = -1 ;
@@ -3074,11 +3072,11 @@ static PHP_METHOD(swoole_server, taskwait) {
3074
3072
RETURN_FALSE;
3075
3073
}
3076
3074
3077
- if (php_swoole_server_task_pack (&buf, zdata) < 0 ) {
3075
+ EventData buf;
3076
+ if (php_swoole_server_task_pack (zdata, &buf) < 0 ) {
3078
3077
RETURN_FALSE;
3079
3078
}
3080
3079
3081
- int _dst_worker_id = (int ) dst_worker_id;
3082
3080
TaskId task_id = serv->get_task_id (&buf);
3083
3081
3084
3082
// coroutine
@@ -3091,9 +3089,7 @@ static PHP_METHOD(swoole_server, taskwait) {
3091
3089
task_co.count = 1 ;
3092
3090
task_co.result = return_value;
3093
3091
3094
- sw_atomic_fetch_add (&serv->gs ->tasking_num , 1 );
3095
- if (serv->gs ->task_workers .dispatch (&buf, &_dst_worker_id) < 0 ) {
3096
- sw_atomic_fetch_sub (&serv->gs ->tasking_num , 1 );
3092
+ if (!serv->task (&buf, (int *) &dst_worker_id)) {
3097
3093
RETURN_FALSE;
3098
3094
}
3099
3095
@@ -3104,49 +3100,19 @@ static PHP_METHOD(swoole_server, taskwait) {
3104
3100
if (!retval) {
3105
3101
RETURN_FALSE;
3106
3102
}
3107
- return ;
3108
- }
3109
-
3110
- uint64_t notify;
3111
- EventData *task_result = &(serv->task_result [swoole_get_process_id ()]);
3112
- sw_memset_zero (task_result, sizeof (*task_result));
3113
- Pipe *pipe = serv->task_notify_pipes .at (swoole_get_process_id ()).get ();
3114
- network::Socket *task_notify_socket = pipe ->get_socket (false );
3115
-
3116
- // clear history task
3117
- while (task_notify_socket->wait_event (0 , SW_EVENT_READ) == SW_OK) {
3118
- if (task_notify_socket->read (¬ify, sizeof (notify)) <= 0 ) {
3119
- break ;
3103
+ } else {
3104
+ auto retval = serv->task_sync (&buf, (int *) &dst_worker_id, timeout);
3105
+ if (!retval) {
3106
+ RETURN_FALSE;
3120
3107
}
3121
- }
3122
-
3123
- sw_atomic_fetch_add (&serv->gs ->tasking_num , 1 );
3124
-
3125
- if (serv->gs ->task_workers .dispatch_blocking (&buf, &_dst_worker_id) == SW_OK) {
3126
- while (1 ) {
3127
- if (task_notify_socket->wait_event ((int ) (timeout * 1000 ), SW_EVENT_READ) != SW_OK) {
3128
- break ;
3129
- }
3130
- if (pipe ->read (¬ify, sizeof (notify)) > 0 ) {
3131
- if (serv->get_task_id (task_result) != task_id) {
3132
- continue ;
3133
- }
3134
- zval zresult;
3135
- if (!php_swoole_server_task_unpack (&zresult, task_result)) {
3136
- RETURN_FALSE;
3137
- } else {
3138
- RETURN_ZVAL (&zresult, 0 , 0 );
3139
- }
3140
- break ;
3141
- } else {
3142
- php_swoole_sys_error (E_WARNING, " taskwait failed" );
3143
- break ;
3144
- }
3108
+ zval zresult;
3109
+ auto task_result = serv->get_task_result ();
3110
+ if (!php_swoole_server_task_unpack (&zresult, task_result)) {
3111
+ RETURN_FALSE;
3112
+ } else {
3113
+ RETURN_ZVAL (&zresult, 0 , 0 );
3145
3114
}
3146
- } else {
3147
- sw_atomic_fetch_sub (&serv->gs ->tasking_num , 1 );
3148
3115
}
3149
- RETURN_FALSE;
3150
3116
}
3151
3117
3152
3118
static PHP_METHOD (swoole_server, taskWaitMulti) {
@@ -3164,9 +3130,6 @@ static PHP_METHOD(swoole_server, taskWaitMulti) {
3164
3130
RETURN_FALSE;
3165
3131
}
3166
3132
3167
- EventData buf;
3168
- memset (&buf.info , 0 , sizeof (buf.info ));
3169
-
3170
3133
zval *ztasks;
3171
3134
double timeout = SW_TASKWAIT_TIMEOUT;
3172
3135
@@ -3187,10 +3150,10 @@ static PHP_METHOD(swoole_server, taskWaitMulti) {
3187
3150
RETURN_FALSE;
3188
3151
}
3189
3152
3190
- int list_of_id[SW_MAX_CONCURRENT_TASK] = {};
3153
+ TaskId list_of_id[SW_MAX_CONCURRENT_TASK] = {};
3191
3154
3192
3155
uint64_t notify;
3193
- EventData *task_result = &( serv->task_result [ swoole_get_process_id ()] );
3156
+ EventData *task_result = serv->get_task_result ( );
3194
3157
sw_memset_zero (task_result, sizeof (*task_result));
3195
3158
Pipe *pipe = serv->task_notify_pipes .at (swoole_get_process_id ()).get ();
3196
3159
Worker *worker = serv->get_worker (swoole_get_process_id ());
@@ -3219,7 +3182,9 @@ static PHP_METHOD(swoole_server, taskWaitMulti) {
3219
3182
3220
3183
zval *ztask;
3221
3184
SW_HASHTABLE_FOREACH_START (Z_ARRVAL_P (ztasks), ztask)
3222
- TaskId task_id = php_swoole_server_task_pack (&buf, ztask);
3185
+
3186
+ EventData buf;
3187
+ TaskId task_id = php_swoole_server_task_pack (ztask, &buf);
3223
3188
if (task_id < 0 ) {
3224
3189
php_swoole_fatal_error (E_WARNING, " task pack failed" );
3225
3190
goto _fail;
@@ -3280,7 +3245,7 @@ static PHP_METHOD(swoole_server, taskWaitMulti) {
3280
3245
}
3281
3246
(void ) add_index_zval (return_value, j, &zresult);
3282
3247
_next:
3283
- content->offset += sizeof (DataHead) + result->info . len ;
3248
+ content->offset += result->size () ;
3284
3249
} while (content->offset < 0 || (size_t ) content->offset < content->length );
3285
3250
// delete tmp file
3286
3251
unlink (file_path.c_str ());
@@ -3313,9 +3278,6 @@ static PHP_METHOD(swoole_server, taskCo) {
3313
3278
int i = 0 ;
3314
3279
uint32_t n_task = php_swoole_array_length (ztasks);
3315
3280
3316
- EventData buf;
3317
- memset (&buf.info , 0 , sizeof (buf.info ));
3318
-
3319
3281
if (n_task >= SW_MAX_CONCURRENT_TASK) {
3320
3282
php_swoole_fatal_error (E_WARNING, " too many concurrent tasks" );
3321
3283
RETURN_FALSE;
@@ -3325,7 +3287,7 @@ static PHP_METHOD(swoole_server, taskCo) {
3325
3287
RETURN_FALSE;
3326
3288
}
3327
3289
3328
- int *list = (int *) ecalloc (n_task, sizeof (int ));
3290
+ TaskId *list = (TaskId *) ecalloc (n_task, sizeof (TaskId ));
3329
3291
if (list == nullptr ) {
3330
3292
RETURN_FALSE;
3331
3293
}
@@ -3337,20 +3299,19 @@ static PHP_METHOD(swoole_server, taskCo) {
3337
3299
3338
3300
zval *ztask;
3339
3301
SW_HASHTABLE_FOREACH_START (Z_ARRVAL_P (ztasks), ztask) {
3340
- task_id = php_swoole_server_task_pack (&buf, ztask);
3302
+ EventData buf;
3303
+ task_id = php_swoole_server_task_pack (ztask, &buf);
3341
3304
if (task_id < 0 ) {
3342
3305
php_swoole_fatal_error (E_WARNING, " failed to pack task" );
3343
3306
goto _fail;
3344
3307
}
3345
3308
buf.info .ext_flags |= (SW_TASK_NONBLOCK | SW_TASK_COROUTINE);
3346
3309
dst_worker_id = -1 ;
3347
- sw_atomic_fetch_add (&serv->gs ->tasking_num , 1 );
3348
- if (serv->gs ->task_workers .dispatch (&buf, &dst_worker_id) < 0 ) {
3310
+ if (!serv->task (&buf, &dst_worker_id)) {
3349
3311
task_id = -1 ;
3350
3312
_fail:
3351
3313
add_index_bool (return_value, i, 0 );
3352
3314
n_task--;
3353
- sw_atomic_fetch_sub (&serv->gs ->tasking_num , 1 );
3354
3315
} else {
3355
3316
server_object->property ->task_coroutine_map [task_id] = &task_co;
3356
3317
}
@@ -3406,14 +3367,11 @@ static PHP_METHOD(swoole_server, task) {
3406
3367
}
3407
3368
3408
3369
EventData buf;
3409
- memset (&buf.info , 0 , sizeof (buf.info ));
3410
-
3411
- if (php_swoole_server_task_pack (&buf, zdata) < 0 ) {
3370
+ TaskId task_id = php_swoole_server_task_pack (zdata, &buf);
3371
+ if (task_id < 0 ) {
3412
3372
RETURN_FALSE;
3413
3373
}
3414
3374
3415
- TaskId task_id = serv->get_task_id (&buf);
3416
-
3417
3375
if (!serv->is_worker ()) {
3418
3376
buf.info .ext_flags |= SW_TASK_NOREPLY;
3419
3377
} else if (fci.size ) {
@@ -3424,15 +3382,11 @@ static PHP_METHOD(swoole_server, task) {
3424
3382
3425
3383
buf.info .ext_flags |= SW_TASK_NONBLOCK;
3426
3384
3427
- int _dst_worker_id = (int ) dst_worker_id;
3428
- sw_atomic_fetch_add (&serv->gs ->tasking_num , 1 );
3429
-
3430
- if (serv->gs ->task_workers .dispatch (&buf, &_dst_worker_id) >= 0 ) {
3385
+ if (serv->task (&buf, (int *) &dst_worker_id)) {
3431
3386
RETURN_LONG (task_id);
3387
+ } else {
3388
+ RETURN_FALSE;
3432
3389
}
3433
-
3434
- sw_atomic_fetch_sub (&serv->gs ->tasking_num , 1 );
3435
- RETURN_FALSE;
3436
3390
}
3437
3391
3438
3392
static PHP_METHOD (swoole_server, command) {
@@ -3523,17 +3477,11 @@ static PHP_METHOD(swoole_server, sendMessage) {
3523
3477
}
3524
3478
3525
3479
EventData buf;
3526
- memset (&buf.info , 0 , sizeof (buf.info ));
3527
-
3528
- if (php_swoole_server_task_pack (&buf, zmessage) < 0 ) {
3480
+ if (php_swoole_server_task_pack (zmessage, &buf) < 0 ) {
3529
3481
RETURN_FALSE;
3530
3482
}
3531
3483
3532
- buf.info .type = SW_SERVER_EVENT_PIPE_MESSAGE;
3533
-
3534
- Worker *to_worker = serv->get_worker (worker_id);
3535
- SW_CHECK_RETURN (serv->send_to_worker_from_worker (
3536
- to_worker, &buf, sizeof (buf.info ) + buf.info .len , SW_PIPE_MASTER | SW_PIPE_NONBLOCK));
3484
+ RETURN_BOOL (serv->send_pipe_message (worker_id, &buf));
3537
3485
}
3538
3486
3539
3487
static PHP_METHOD (swoole_server, finish) {
@@ -3576,21 +3524,19 @@ static PHP_METHOD(swoole_server_task, finish) {
3576
3524
}
3577
3525
3578
3526
static PHP_METHOD (swoole_server_task, pack) {
3579
- EventData buf;
3580
- memset (&buf.info , 0 , sizeof (buf.info ));
3581
-
3582
3527
zval *zdata;
3583
3528
3584
3529
ZEND_PARSE_PARAMETERS_START (1 , 1 )
3585
3530
Z_PARAM_ZVAL (zdata)
3586
3531
ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
3587
3532
3588
- if (php_swoole_server_task_pack (&buf, zdata) < 0 ) {
3533
+ EventData buf;
3534
+ if (php_swoole_server_task_pack (zdata, &buf) < 0 ) {
3589
3535
RETURN_FALSE;
3590
3536
}
3591
3537
buf.info .ext_flags |= (SW_TASK_NONBLOCK | SW_TASK_NOREPLY);
3592
3538
3593
- RETURN_STRINGL ((char *) &buf, sizeof ( buf.info ) + buf. info . len );
3539
+ RETURN_STRINGL ((char *) &buf, buf.size () );
3594
3540
}
3595
3541
3596
3542
static PHP_METHOD (swoole_server_task, unpack) {
0 commit comments