/* The worker cycle */ static void *worker_thread_cycle(void *_async) { /* setup signal and thread's local-storage async variable. */ struct Async *async = _async; char sig_buf; /* pause for signal for as long as we're active. */ while (async->run && (read(async->pipe.in, &sig_buf, 1) >= 0)) { perform_tasks(async); sched_yield(); } perform_tasks(async); return 0; }
/** * \brief Waits for an Async object to finish up (joins all the threads * in the thread pool) and DESTROYS the Async object (frees its * memory and resources). * * This function will wait forever or until a signal is received and all * the tasks in the queue have been processed. */ static void async_wait(async_p async) { if (!async) return; /* wake threads (just in case) by sending `async->count` * number of wakeups */ if (async->pipe.out) write(async->pipe.out, async, async->count); /* join threads */ for (int i = 0; i < async->count; i++) { join_thread(async->threads[i]); } /* perform any pending tasks */ perform_tasks(async); /* release queue memory and resources */ async_destroy(async); }