Skip to content

Commit b19768c

Browse files
committed
add php_swoole_check_task_param.
1 parent f3a83d3 commit b19768c

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed

swoole_server.c

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ int php_swoole_get_send_data(zval *zdata, char **str TSRMLS_DC)
133133
return length;
134134
}
135135

136+
static sw_inline int php_swoole_check_task_param(int dst_worker_id TSRMLS_DC)
137+
{
138+
if (SwooleG.task_worker_num < 1)
139+
{
140+
swoole_php_fatal_error(E_WARNING, "Task method cannot use, Please set task_worker_num.");
141+
return SW_ERR;
142+
}
143+
144+
if (dst_worker_id >= SwooleG.task_worker_num)
145+
{
146+
swoole_php_fatal_error(E_WARNING, "worker_id must be less than serv->task_worker_num.");
147+
return SW_ERR;
148+
}
149+
150+
if (!swIsWorker())
151+
{
152+
swoole_php_fatal_error(E_WARNING, "The method can only be used in the worker process.");
153+
return SW_ERR;
154+
}
155+
156+
return SW_OK;
157+
}
158+
136159
static zval* php_swoole_get_task_result(swEventData *task_result TSRMLS_DC)
137160
{
138161
zval *task_notify_data, *task_notify_unserialized_data;
@@ -2504,52 +2527,31 @@ PHP_FUNCTION(swoole_server_taskwait)
25042527
php_serialize_data_t var_hash;
25052528

25062529
double timeout = SW_TASKWAIT_TIMEOUT;
2507-
long worker_id = -1;
2530+
long dst_worker_id = -1;
25082531

25092532
if (SwooleGS->start == 0)
25102533
{
25112534
php_error_docref(NULL TSRMLS_CC, E_WARNING, "server is not running.");
25122535
RETURN_FALSE;
25132536
}
25142537

2515-
if (swIsMaster())
2516-
{
2517-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use task in master process.");
2518-
RETURN_FALSE;
2519-
}
2520-
25212538
if (zobject == NULL)
25222539
{
2523-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz|dl", &zobject, swoole_server_class_entry_ptr, &data, &timeout, &worker_id) == FAILURE)
2540+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz|dl", &zobject, swoole_server_class_entry_ptr, &data, &timeout, &dst_worker_id) == FAILURE)
25242541
{
25252542
return;
25262543
}
25272544
}
25282545
else
25292546
{
2530-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|dl", &data, &timeout, &worker_id) == FAILURE)
2547+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|dl", &data, &timeout, &dst_worker_id) == FAILURE)
25312548
{
25322549
return;
25332550
}
25342551
}
25352552

2536-
swServer *serv = swoole_get_object(zobject);
2537-
2538-
if (SwooleG.task_worker_num < 1)
2539-
{
2540-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use task. Please set task_worker_num.");
2541-
RETURN_FALSE;
2542-
}
2543-
2544-
if (worker_id >= SwooleG.task_worker_num)
2545-
{
2546-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "worker_id must be less than serv->task_worker_num");
2547-
RETURN_FALSE;
2548-
}
2549-
2550-
if (SwooleWG.id >= serv->worker_num)
2553+
if (php_swoole_check_task_param(dst_worker_id TSRMLS_CC) < 0)
25512554
{
2552-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot dispatch task in task worker.");
25532555
RETURN_FALSE;
25542556
}
25552557

@@ -2612,11 +2614,11 @@ PHP_FUNCTION(swoole_server_taskwait)
26122614
//clear history task
26132615
while (read(efd, &notify, sizeof(notify)) > 0);
26142616

2615-
if (swProcessPool_dispatch_blocking(&SwooleGS->task_workers, &buf, (int*) &worker_id) >= 0)
2617+
if (swProcessPool_dispatch_blocking(&SwooleGS->task_workers, &buf, (int*) &dst_worker_id) >= 0)
26162618
{
26172619
task_notify_pipe->timeout = timeout;
26182620
int ret = task_notify_pipe->read(task_notify_pipe, &notify, sizeof(notify));
2619-
swWorker *worker = swProcessPool_get_worker(&SwooleGS->task_workers, worker_id);
2621+
swWorker *worker = swProcessPool_get_worker(&SwooleGS->task_workers, dst_worker_id);
26202622
sw_atomic_fetch_sub(&worker->tasking_num, 1);
26212623

26222624
if (ret > 0)
@@ -2641,7 +2643,7 @@ PHP_FUNCTION(swoole_server_task)
26412643
smart_str serialized_data = {0};
26422644
php_serialize_data_t var_hash;
26432645

2644-
long worker_id = -1;
2646+
long dst_worker_id = -1;
26452647

26462648
if (SwooleGS->start == 0)
26472649
{
@@ -2651,34 +2653,21 @@ PHP_FUNCTION(swoole_server_task)
26512653

26522654
if (zobject == NULL)
26532655
{
2654-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz|l", &zobject, swoole_server_class_entry_ptr, &data, &worker_id) == FAILURE)
2656+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz|l", &zobject, swoole_server_class_entry_ptr, &data, &dst_worker_id) == FAILURE)
26552657
{
26562658
return;
26572659
}
26582660
}
26592661
else
26602662
{
2661-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &data, &worker_id) == FAILURE)
2663+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &data, &dst_worker_id) == FAILURE)
26622664
{
26632665
return;
26642666
}
26652667
}
26662668

2667-
if (SwooleG.task_worker_num < 1)
2668-
{
2669-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Task method cannot use, Please set task_worker_num.");
2670-
RETURN_FALSE;
2671-
}
2672-
2673-
if (worker_id >= SwooleG.task_worker_num)
2674-
{
2675-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "worker_id must be less than serv->task_worker_num.");
2676-
RETURN_FALSE;
2677-
}
2678-
2679-
if (!swIsWorker())
2669+
if (php_swoole_check_task_param(dst_worker_id TSRMLS_CC) < 0)
26802670
{
2681-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The method can only be used in the worker process.");
26822671
RETURN_FALSE;
26832672
}
26842673

@@ -2736,7 +2725,7 @@ PHP_FUNCTION(swoole_server_task)
27362725

27372726
smart_str_free(&serialized_data);
27382727

2739-
if (swProcessPool_dispatch(&SwooleGS->task_workers, &buf, (int*) &worker_id) >= 0)
2728+
if (swProcessPool_dispatch(&SwooleGS->task_workers, &buf, (int*) &dst_worker_id) >= 0)
27402729
{
27412730
sw_atomic_fetch_add(&SwooleStats->tasking_num, 1);
27422731
RETURN_LONG(buf.info.fd);

0 commit comments

Comments
 (0)