Skip to content

Commit d9694c4

Browse files
committed
Task process can't use coro
1 parent 6be65bb commit d9694c4

File tree

1 file changed

+59
-34
lines changed

1 file changed

+59
-34
lines changed

swoole_server.c

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,13 +1284,64 @@ static void php_swoole_onShutdown(swServer *serv)
12841284
SwooleG.lock.unlock(&SwooleG.lock);
12851285
}
12861286

1287+
#ifdef SW_COROUTINE
1288+
static void php_swoole_onWorkerStart_coroutine(zval *zserv, zval *zworker_id)
1289+
{
1290+
zval *retval = NULL;
1291+
zval *args[2];
1292+
args[0] = zserv;
1293+
args[1] = zworker_id;
1294+
1295+
zend_fcall_info_cache *cache = php_sw_server_caches[SW_SERVER_CB_onWorkerStart];
1296+
int ret = coro_create(cache, args, 2, &retval, NULL, NULL);
1297+
if (ret != 0)
1298+
{
1299+
sw_zval_ptr_dtor(&zworker_id);
1300+
if (ret == CORO_LIMIT)
1301+
{
1302+
swWarn("Failed to handle onWorkerStart. Coroutine limited.");
1303+
}
1304+
return;
1305+
}
1306+
1307+
if (EG(exception))
1308+
{
1309+
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1310+
}
1311+
if (retval)
1312+
{
1313+
sw_zval_ptr_dtor(&retval);
1314+
}
1315+
}
1316+
#endif
1317+
1318+
static void php_swoole_onWorkerStart_callback(zval *zserv, zval *zworker_id)
1319+
{
1320+
zval *retval = NULL;
1321+
zval **args[2];
1322+
args[0] = &zserv;
1323+
args[1] = &zworker_id;
1324+
1325+
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onWorkerStart], &retval,
1326+
2, args, 0, NULL TSRMLS_CC) == FAILURE)
1327+
{
1328+
swoole_php_fatal_error(E_WARNING, "onWorkerStart handler error.");
1329+
}
1330+
1331+
if (EG(exception))
1332+
{
1333+
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1334+
}
1335+
if (retval)
1336+
{
1337+
sw_zval_ptr_dtor(&retval);
1338+
}
1339+
}
1340+
12871341
static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
12881342
{
12891343
zval *zserv = (zval *) serv->ptr2;
12901344
zval *zworker_id;
1291-
zval *retval = NULL;
1292-
1293-
SWOOLE_GET_TSRMLS;
12941345

12951346
SW_MAKE_STD_ZVAL(zworker_id);
12961347
ZVAL_LONG(zworker_id, worker_id);
@@ -1327,48 +1378,22 @@ static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
13271378
*/
13281379
zend_update_property_long(swoole_server_class_entry_ptr, zserv, ZEND_STRL("worker_pid"), getpid() TSRMLS_CC);
13291380

1330-
sw_zval_ptr_dtor(&zworker_id);
1331-
13321381
/**
13331382
* Have not set the event callback
13341383
*/
13351384
if (php_sw_server_callbacks[SW_SERVER_CB_onWorkerStart] == NULL)
13361385
{
13371386
return;
13381387
}
1339-
#ifndef SW_COROUTINE
1340-
zval **args[2];
1341-
args[0] = &zserv;
1342-
args[1] = &zworker_id;
1343-
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onWorkerStart], &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
1344-
{
1345-
swoole_php_fatal_error(E_WARNING, "onWorkerStart handler error.");
1346-
}
1347-
#else
1348-
zval *args[2];
1349-
args[0] = zserv;
1350-
args[1] = zworker_id;
1351-
1352-
zend_fcall_info_cache *cache = php_sw_server_caches[SW_SERVER_CB_onWorkerStart];
1353-
int ret = coro_create(cache, args, 2, &retval, NULL, NULL);
1354-
if (ret != 0)
1388+
#ifdef SW_COROUTINE
1389+
if (worker_id < serv->worker_num)
13551390
{
1356-
sw_zval_ptr_dtor(&zworker_id);
1357-
if (ret == CORO_LIMIT)
1358-
{
1359-
swWarn("Failed to handle onWorkerStart. Coroutine limited.");
1360-
}
1361-
return;
1391+
php_swoole_onWorkerStart_coroutine(zserv, zworker_id);
13621392
}
1393+
else
13631394
#endif
1364-
1365-
if (EG(exception))
13661395
{
1367-
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1368-
}
1369-
if (retval)
1370-
{
1371-
sw_zval_ptr_dtor(&retval);
1396+
php_swoole_onWorkerStart_callback(zserv, zworker_id);
13721397
}
13731398
}
13741399

0 commit comments

Comments
 (0)