9
static void *hw_thread_function(HWThread ctx) {
22
hw_sched_lock(sched, job_cond);
24
hw_sched_lock(sched, compl_cond);
25
ctx->status = HW_THREAD_STATUS_IDLE;
26
hw_sched_broadcast(sched, compl);
27
hw_sched_unlock(sched, compl_cond);
29
while (sched->status) {
30
hw_sched_wait(sched, job);
31
if (!sched->status) break;
33
hw_sched_unlock(sched, job_cond);
35
run = runs[sched->entry];
36
chunk_id = hw_sched_get_chunk(sched, ctx->thread_id);
38
/* Should be after get_chunk, since we can check if it's first time */
39
ctx->status = HW_THREAD_STATUS_RUNNING;
40
while (chunk_id >= 0) {
41
err = run(ctx, hwctx, chunk_id, sched->ctx);
43
fprintf(stderr, "Error exectuing code, %i\n", err);
46
chunk_id = hw_sched_get_chunk(sched, ctx->thread_id);
49
hw_sched_lock(sched, job_cond);
51
hw_sched_lock(sched, compl_cond);
52
ctx->status = HW_THREAD_STATUS_DONE;
53
hw_sched_broadcast(sched, compl);
54
hw_sched_unlock(sched, compl_cond);
57
hw_sched_unlock(sched, job_cond);
62
HWThread hw_thread_create(HWSched sched, int thread_id, void *hwctx, HWRunFunction *run_func, HWFreeFunction free_func) {
67
ctx = (HWThread)malloc(sizeof(HWThreadS));
70
memset(ctx, 0, sizeof(HWThreadS));
75
ctx->free = free_func;
76
ctx->thread_id = thread_id;
77
ctx->status = HW_THREAD_STATUS_INIT;
79
err = pthread_create(&ctx->thread, NULL, (void*(*)(void*))hw_thread_function, ctx);
81
fprintf(stderr, "Error spawn thread, errno: %i\n", errno);
82
hw_thread_destroy(ctx);
89
void hw_thread_destroy(HWThread ctx) {
91
pthread_join(ctx->thread, NULL);
99
ctx->free(ctx->hwctx);