5
#include "normxcorr_hw_msg.h"
11
# define hw_run_entry(entry) entry
12
#else /* HW_SINGLE_MODE */
13
# define hw_run_entry(entry) runs[entry]
14
#endif /* HW_SINGLE_MODE */
16
static void *hw_thread_function(HWThread ctx) {
29
hw_sched_lock(sched, job_cond);
31
hw_sched_lock(sched, compl_cond);
32
ctx->status = HW_THREAD_STATUS_IDLE;
33
hw_sched_broadcast(sched, compl);
34
hw_sched_unlock(sched, compl_cond);
36
while (sched->status) {
37
hw_sched_wait(sched, job);
38
if (!sched->status) break;
40
hw_sched_unlock(sched, job_cond);
42
run = hw_run_entry(sched->entry);
43
chunk_id = hw_sched_get_chunk(sched, ctx->thread_id);
45
/* Should be after get_chunk, since we can check if it's first time */
46
ctx->status = HW_THREAD_STATUS_RUNNING;
47
while (chunk_id >= 0) {
48
err = run(ctx, hwctx, chunk_id, sched->ctx);
50
reportError("Error exectuing code, %i\n", err);
53
chunk_id = hw_sched_get_chunk(sched, ctx->thread_id);
56
hw_sched_lock(sched, job_cond);
58
hw_sched_lock(sched, compl_cond);
59
ctx->status = HW_THREAD_STATUS_DONE;
60
hw_sched_broadcast(sched, compl);
61
hw_sched_unlock(sched, compl_cond);
64
hw_sched_unlock(sched, job_cond);
69
HWThread hw_thread_create(HWSched sched, int thread_id, void *hwctx, HWRunFunction *run_func, HWFreeFunction free_func) {
74
ctx = (HWThread)malloc(sizeof(HWThreadS));
77
memset(ctx, 0, sizeof(HWThreadS));
82
ctx->free = free_func;
83
ctx->thread_id = thread_id;
84
ctx->status = HW_THREAD_STATUS_INIT;
86
ctx->thread = g_thread_create((GThreadFunc)hw_thread_function, ctx, 1, &err);
88
reportError("Error spawn thread, errno: %i\n", err->message);
91
hw_thread_destroy(ctx);
98
void hw_thread_destroy(HWThread ctx) {
100
g_thread_join(ctx->thread);
108
ctx->free(ctx->hwctx);