@@ -1284,13 +1284,64 @@ static void php_swoole_onShutdown(swServer *serv)
1284
1284
SwooleG .lock .unlock (& SwooleG .lock );
1285
1285
}
1286
1286
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
+
1287
1341
static void php_swoole_onWorkerStart (swServer * serv , int worker_id )
1288
1342
{
1289
1343
zval * zserv = (zval * ) serv -> ptr2 ;
1290
1344
zval * zworker_id ;
1291
- zval * retval = NULL ;
1292
-
1293
- SWOOLE_GET_TSRMLS ;
1294
1345
1295
1346
SW_MAKE_STD_ZVAL (zworker_id );
1296
1347
ZVAL_LONG (zworker_id , worker_id );
@@ -1327,48 +1378,22 @@ static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
1327
1378
*/
1328
1379
zend_update_property_long (swoole_server_class_entry_ptr , zserv , ZEND_STRL ("worker_pid" ), getpid () TSRMLS_CC );
1329
1380
1330
- sw_zval_ptr_dtor (& zworker_id );
1331
-
1332
1381
/**
1333
1382
* Have not set the event callback
1334
1383
*/
1335
1384
if (php_sw_server_callbacks [SW_SERVER_CB_onWorkerStart ] == NULL )
1336
1385
{
1337
1386
return ;
1338
1387
}
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 )
1355
1390
{
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 );
1362
1392
}
1393
+ else
1363
1394
#endif
1364
-
1365
- if (EG (exception ))
1366
1395
{
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 );
1372
1397
}
1373
1398
}
1374
1399
0 commit comments