Skip to content

Commit

Permalink
add comment to the scheduler's main
Browse files Browse the repository at this point in the history
  • Loading branch information
NianJi committed Apr 1, 2019
1 parent f6c689a commit 755dc55
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cocore/coroutine.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,30 @@ void coroutine_memory_free(void *ptr, size_t size) {
return schedule;
}

// The main entry of the coroutine's scheduler
// The scheduler is just a special coroutine, so we can use yield.
void coroutine_scheduler_main(coroutine_t *scheduler_co) {

coroutine_scheduler_t *scheduler = scheduler_co->scheduler;
for (;;) {

// pop a coroutine from queue.head.
// Pop a coroutine from the scheduler's queue.
coroutine_t *co = scheduler_queue_pop(scheduler);
if (co == NULL) {
// jump out. scheduler will enter idle.
// Yield the scheduler, give back cpu to origin thread.
coroutine_yield(scheduler_co);

// When some coroutine add to the scheduler's queue,
// the scheduler will resume again,
// then will resume here, continue the loop.
continue;
}
// set scheduler's current running coroutine.
// Set scheduler's current running coroutine.
scheduler->running_coroutine = co;
// resume the coroutine
// Resume the coroutine
coroutine_resume_im(co);

// scheduler's current running coroutine.
// Set scheduler's current running coroutine to nil.
scheduler->running_coroutine = nil;

// if coroutine finished, free coroutine.
Expand Down

0 comments on commit 755dc55

Please sign in to comment.