Skip to content

Instantly share code, notes, and snippets.

@liuliu
Created August 16, 2018 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuliu/a77b5649088ef6c919d499fc251c1dd9 to your computer and use it in GitHub Desktop.
Save liuliu/a77b5649088ef6c919d499fc251c1dd9 to your computer and use it in GitHub Desktop.
static void deltask(schd_t* const schd, task_t* const t)
{
if (t->prev)
t->prev->next = t->next;
else
schd->head = t->next;
if (t->next)
t->next->prev = t->prev;
else
schd->tail = t->prev;
}
static void* schdmain(void* userdata)
{
schd_t* const schd = (schd_t*)userdata;
for (;;) {
pthread_mutex_lock(&schd->mutex);
// No one is waiting, and no more tasks. exit.
if (schd->head == 0 && schd->count.suspend == 0)
{
pthread_mutex_unlock(&schd->mutex);
break;
}
if (schd->head == 0)
{
pthread_cond_wait(&schd->cv, &schd->mutex);
pthread_mutex_unlock(&schd->mutex);
continue;
}
task_t* const t = schd->head;
deltask(schd, t);
pthread_mutex_unlock(&schd->mutex);
swapcontext(&schd->caller, &t->context);
t->context = schd->callee;
if (t->done)
taskfree(t);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment