Skip to content

Commit e49d3bc

Browse files
committed
Scheduler reuse
1 parent c83ad17 commit e49d3bc

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

swoole_coroutine_scheduler.cc

+28-12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct scheduler_task_t
3737
struct scheduler_t
3838
{
3939
queue<scheduler_task_t*> *list;
40+
bool started;
4041
zend_object std;
4142
};
4243

@@ -61,7 +62,6 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_scheduler_set, 0, 0, 1)
6162
ZEND_ARG_ARRAY_INFO(0, settings, 0)
6263
ZEND_END_ARG_INFO()
6364

64-
static PHP_METHOD(swoole_coroutine_scheduler, __construct);
6565
static PHP_METHOD(swoole_coroutine_scheduler, add);
6666
static PHP_METHOD(swoole_coroutine_scheduler, parallel);
6767
static PHP_METHOD(swoole_coroutine_scheduler, start);
@@ -101,7 +101,6 @@ static void scheduler_free_object(zend_object *object)
101101

102102
static const zend_function_entry swoole_coroutine_scheduler_methods[] =
103103
{
104-
PHP_ME(swoole_coroutine_scheduler, __construct, arginfo_swoole_void, ZEND_ACC_PUBLIC)
105104
PHP_ME(swoole_coroutine_scheduler, add, arginfo_swoole_coroutine_scheduler_add, ZEND_ACC_PUBLIC)
106105
PHP_ME(swoole_coroutine_scheduler, parallel, arginfo_swoole_coroutine_scheduler_parallel, ZEND_ACC_PUBLIC)
107106
PHP_ME(swoole_coroutine_scheduler, set, arginfo_swoole_coroutine_scheduler_set, ZEND_ACC_PUBLIC)
@@ -203,18 +202,15 @@ static void scheduler_add_task(scheduler_t *s, scheduler_task_t *task)
203202
s->list->push(task);
204203
}
205204

206-
static PHP_METHOD(swoole_coroutine_scheduler, __construct)
205+
static PHP_METHOD(swoole_coroutine_scheduler, add)
207206
{
208-
if (SwooleG.main_reactor)
207+
scheduler_t *s = scheduler_get_object(Z_OBJ_P(getThis()));
208+
if (s->started)
209209
{
210-
zend_throw_exception_ex(swoole_exception_ce, -2, "eventLoop has already been created. unable to create %s", SW_Z_OBJCE_NAME_VAL_P(getThis()));
210+
php_swoole_fatal_error(E_WARNING, "scheduler is running, unable to execute %s->add", SW_Z_OBJCE_NAME_VAL_P(getThis()));
211211
RETURN_FALSE;
212212
}
213-
php_swoole_reactor_init();
214-
}
215213

216-
static PHP_METHOD(swoole_coroutine_scheduler, add)
217-
{
218214
scheduler_task_t *task = (scheduler_task_t *) ecalloc(1, sizeof(scheduler_task_t));
219215

220216
ZEND_PARSE_PARAMETERS_START(1, -1)
@@ -223,11 +219,18 @@ static PHP_METHOD(swoole_coroutine_scheduler, add)
223219
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
224220

225221
task->count = 1;
226-
scheduler_add_task(scheduler_get_object(Z_OBJ_P(getThis())), task);
222+
scheduler_add_task(s, task);
227223
}
228224

229225
static PHP_METHOD(swoole_coroutine_scheduler, parallel)
230226
{
227+
scheduler_t *s = scheduler_get_object(Z_OBJ_P(getThis()));
228+
if (s->started)
229+
{
230+
php_swoole_fatal_error(E_WARNING, "scheduler is running, unable to execute %s->parallel", SW_Z_OBJCE_NAME_VAL_P(getThis()));
231+
RETURN_FALSE;
232+
}
233+
231234
scheduler_task_t *task = (scheduler_task_t *) ecalloc(1, sizeof(scheduler_task_t));
232235
zend_long count;
233236

@@ -238,12 +241,25 @@ static PHP_METHOD(swoole_coroutine_scheduler, parallel)
238241
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
239242

240243
task->count = count;
241-
scheduler_add_task(scheduler_get_object(Z_OBJ_P(getThis())), task);
244+
scheduler_add_task(s, task);
242245
}
243246

244247
static PHP_METHOD(swoole_coroutine_scheduler, start)
245248
{
246249
scheduler_t *s = scheduler_get_object(Z_OBJ_P(getThis()));
250+
251+
if (SwooleG.main_reactor)
252+
{
253+
zend_throw_exception_ex(swoole_exception_ce, -2, "eventLoop has already been created. unable to create %s", SW_Z_OBJCE_NAME_VAL_P(getThis()));
254+
RETURN_FALSE;
255+
}
256+
if (s->started)
257+
{
258+
php_swoole_fatal_error(E_WARNING, "scheduler is running, unable to execute %s->start", SW_Z_OBJCE_NAME_VAL_P(getThis()));
259+
RETURN_FALSE;
260+
}
261+
php_swoole_reactor_init();
262+
s->started = true;
247263
while (!s->list->empty())
248264
{
249265
scheduler_task_t *task = s->list->front();
@@ -256,8 +272,8 @@ static PHP_METHOD(swoole_coroutine_scheduler, start)
256272
sw_zend_fci_params_discard(&task->fci);
257273
efree(task);
258274
}
259-
260275
php_swoole_event_wait();
261276
delete s->list;
262277
s->list = nullptr;
278+
s->started = false;
263279
}

0 commit comments

Comments
 (0)