@@ -37,6 +37,7 @@ struct scheduler_task_t
37
37
struct scheduler_t
38
38
{
39
39
queue<scheduler_task_t *> *list;
40
+ bool started;
40
41
zend_object std;
41
42
};
42
43
@@ -61,7 +62,6 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_scheduler_set, 0, 0, 1)
61
62
ZEND_ARG_ARRAY_INFO(0 , settings, 0 )
62
63
ZEND_END_ARG_INFO()
63
64
64
- static PHP_METHOD(swoole_coroutine_scheduler, __construct);
65
65
static PHP_METHOD(swoole_coroutine_scheduler, add);
66
66
static PHP_METHOD (swoole_coroutine_scheduler, parallel);
67
67
static PHP_METHOD (swoole_coroutine_scheduler, start);
@@ -101,7 +101,6 @@ static void scheduler_free_object(zend_object *object)
101
101
102
102
static const zend_function_entry swoole_coroutine_scheduler_methods[] =
103
103
{
104
- PHP_ME (swoole_coroutine_scheduler, __construct, arginfo_swoole_void, ZEND_ACC_PUBLIC)
105
104
PHP_ME (swoole_coroutine_scheduler, add, arginfo_swoole_coroutine_scheduler_add, ZEND_ACC_PUBLIC)
106
105
PHP_ME (swoole_coroutine_scheduler, parallel, arginfo_swoole_coroutine_scheduler_parallel, ZEND_ACC_PUBLIC)
107
106
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)
203
202
s->list ->push (task);
204
203
}
205
204
206
- static PHP_METHOD (swoole_coroutine_scheduler, __construct )
205
+ static PHP_METHOD (swoole_coroutine_scheduler, add )
207
206
{
208
- if (SwooleG.main_reactor )
207
+ scheduler_t *s = scheduler_get_object (Z_OBJ_P (getThis ()));
208
+ if (s->started )
209
209
{
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 ()));
211
211
RETURN_FALSE;
212
212
}
213
- php_swoole_reactor_init ();
214
- }
215
213
216
- static PHP_METHOD (swoole_coroutine_scheduler, add)
217
- {
218
214
scheduler_task_t *task = (scheduler_task_t *) ecalloc (1 , sizeof (scheduler_task_t ));
219
215
220
216
ZEND_PARSE_PARAMETERS_START (1 , -1 )
@@ -223,11 +219,18 @@ static PHP_METHOD(swoole_coroutine_scheduler, add)
223
219
ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
224
220
225
221
task->count = 1 ;
226
- scheduler_add_task (scheduler_get_object ( Z_OBJ_P ( getThis ())) , task);
222
+ scheduler_add_task (s , task);
227
223
}
228
224
229
225
static PHP_METHOD (swoole_coroutine_scheduler, parallel)
230
226
{
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
+
231
234
scheduler_task_t *task = (scheduler_task_t *) ecalloc (1 , sizeof (scheduler_task_t ));
232
235
zend_long count;
233
236
@@ -238,12 +241,25 @@ static PHP_METHOD(swoole_coroutine_scheduler, parallel)
238
241
ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
239
242
240
243
task->count = count;
241
- scheduler_add_task (scheduler_get_object ( Z_OBJ_P ( getThis ())) , task);
244
+ scheduler_add_task (s , task);
242
245
}
243
246
244
247
static PHP_METHOD (swoole_coroutine_scheduler, start)
245
248
{
246
249
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 ;
247
263
while (!s->list ->empty ())
248
264
{
249
265
scheduler_task_t *task = s->list ->front ();
@@ -256,8 +272,8 @@ static PHP_METHOD(swoole_coroutine_scheduler, start)
256
272
sw_zend_fci_params_discard (&task->fci );
257
273
efree (task);
258
274
}
259
-
260
275
php_swoole_event_wait ();
261
276
delete s->list ;
262
277
s->list = nullptr ;
278
+ s->started = false ;
263
279
}
0 commit comments