1 #include "HalideRuntime.h"
2 
3 extern "C" {
4 
halide_default_do_task(void * user_context,halide_task_t f,int idx,uint8_t * closure)5 WEAK int halide_default_do_task(void *user_context, halide_task_t f, int idx,
6                                 uint8_t *closure) {
7     return f(user_context, idx, closure);
8 }
9 
halide_default_do_loop_task(void * user_context,halide_loop_task_t f,int min,int extent,uint8_t * closure,void * task_parent)10 WEAK int halide_default_do_loop_task(void *user_context, halide_loop_task_t f,
11                                      int min, int extent, uint8_t *closure,
12                                      void *task_parent) {
13     return f(user_context, min, extent, closure, task_parent);
14 }
15 
halide_default_do_par_for(void * user_context,halide_task_t f,int min,int size,uint8_t * closure)16 WEAK int halide_default_do_par_for(void *user_context, halide_task_t f,
17                                    int min, int size, uint8_t *closure) {
18     for (int x = min; x < min + size; x++) {
19         int result = halide_do_task(user_context, f, x, closure);
20         if (result) {
21             return result;
22         }
23     }
24     return 0;
25 }
26 
halide_default_do_parallel_tasks(void * user_context,int num_tasks,struct halide_parallel_task_t * tasks,void * task_parent)27 WEAK int halide_default_do_parallel_tasks(void *user_context, int num_tasks,
28                                           struct halide_parallel_task_t *tasks,
29                                           void *task_parent) {
30     halide_error(NULL, "halide_default_do_parallel_tasks not implemented on this platform.");
31     return -1;
32 }
33 
halide_default_semaphore_init(halide_semaphore_t * s,int n)34 WEAK int halide_default_semaphore_init(halide_semaphore_t *s, int n) {
35     halide_error(NULL, "halide_default_semaphore_init not implemented on this platform.");
36     return 0;
37 }
38 
halide_default_semaphore_release(halide_semaphore_t * s,int n)39 WEAK int halide_default_semaphore_release(halide_semaphore_t *s, int n) {
40     halide_error(NULL, "halide_default_semaphore_release not implemented on this platform.");
41     return 0;
42 }
43 
halide_default_semaphore_try_acquire(halide_semaphore_t * s,int n)44 WEAK bool halide_default_semaphore_try_acquire(halide_semaphore_t *s, int n) {
45     halide_error(NULL, "halide_default_semaphore_try_acquire not implemented on this platform.");
46     return false;
47 }
48 
49 }  // extern "C"
50 
51 namespace Halide {
52 namespace Runtime {
53 namespace Internal {
54 
55 WEAK halide_do_task_t custom_do_task = halide_default_do_task;
56 WEAK halide_do_loop_task_t custom_do_loop_task = halide_default_do_loop_task;
57 WEAK halide_do_par_for_t custom_do_par_for = halide_default_do_par_for;
58 WEAK halide_do_parallel_tasks_t custom_do_parallel_tasks = halide_default_do_parallel_tasks;
59 WEAK halide_semaphore_init_t custom_semaphore_init = halide_default_semaphore_init;
60 WEAK halide_semaphore_try_acquire_t custom_semaphore_try_acquire = halide_default_semaphore_try_acquire;
61 WEAK halide_semaphore_release_t custom_semaphore_release = halide_default_semaphore_release;
62 
63 }  // namespace Internal
64 }  // namespace Runtime
65 }  // namespace Halide
66 
67 extern "C" {
68 
halide_spawn_thread(void (* f)(void *),void * closure)69 WEAK halide_thread *halide_spawn_thread(void (*f)(void *), void *closure) {
70     // We can't fake spawning a thread. Emit an error.
71     halide_error(NULL, "halide_spawn_thread not implemented on this platform.");
72     return NULL;
73 }
74 
halide_join_thread(halide_thread * thread_arg)75 WEAK void halide_join_thread(halide_thread *thread_arg) {
76     halide_error(NULL, "halide_join_thread not implemented on this platform.");
77 }
78 
79 // Don't need to do anything with mutexes since we are in a fake thread pool.
halide_mutex_lock(halide_mutex * mutex)80 WEAK void halide_mutex_lock(halide_mutex *mutex) {
81 }
82 
halide_mutex_unlock(halide_mutex * mutex)83 WEAK void halide_mutex_unlock(halide_mutex *mutex) {
84 }
85 
86 // Fake mutex array. We still define a pointer to halide_mutex since empty struct leads
87 // to compile error (empty struct has size 0 in C, size 1 in C++).
88 struct halide_mutex_array {
89     halide_mutex *mutex;
90 };
91 
halide_mutex_array_create(int sz)92 WEAK halide_mutex_array *halide_mutex_array_create(int sz) {
93     return NULL;
94 }
95 
halide_mutex_array_destroy(void * user_context,void * array)96 WEAK void halide_mutex_array_destroy(void *user_context, void *array) {
97 }
98 
halide_mutex_array_lock(halide_mutex_array * array,int entry)99 WEAK int halide_mutex_array_lock(halide_mutex_array *array, int entry) {
100     return 0;
101 }
102 
halide_mutex_array_unlock(halide_mutex_array * array,int entry)103 WEAK int halide_mutex_array_unlock(halide_mutex_array *array, int entry) {
104     return 0;
105 }
106 
halide_shutdown_thread_pool()107 WEAK void halide_shutdown_thread_pool() {
108 }
109 
halide_set_num_threads(int n)110 WEAK int halide_set_num_threads(int n) {
111     if (n != 1) {
112         halide_error(NULL, "halide_set_num_threads: only supports a value of 1 on this platform.");
113     }
114     return 1;
115 }
116 
halide_set_custom_do_task(halide_do_task_t f)117 WEAK halide_do_task_t halide_set_custom_do_task(halide_do_task_t f) {
118     halide_do_task_t result = custom_do_task;
119     custom_do_task = f;
120     return result;
121 }
122 
halide_set_custom_do_par_for(halide_do_par_for_t f)123 WEAK halide_do_par_for_t halide_set_custom_do_par_for(halide_do_par_for_t f) {
124     halide_do_par_for_t result = custom_do_par_for;
125     custom_do_par_for = f;
126     return result;
127 }
128 
halide_do_task(void * user_context,halide_task_t f,int idx,uint8_t * closure)129 WEAK int halide_do_task(void *user_context, halide_task_t f, int idx,
130                         uint8_t *closure) {
131     return (*custom_do_task)(user_context, f, idx, closure);
132 }
133 
halide_do_par_for(void * user_context,halide_task_t f,int min,int size,uint8_t * closure)134 WEAK int halide_do_par_for(void *user_context, halide_task_t f,
135                            int min, int size, uint8_t *closure) {
136     return (*custom_do_par_for)(user_context, f, min, size, closure);
137 }
138 
halide_do_loop_task(void * user_context,halide_loop_task_t f,int min,int size,uint8_t * closure,void * task_parent)139 WEAK int halide_do_loop_task(void *user_context, halide_loop_task_t f,
140                              int min, int size, uint8_t *closure, void *task_parent) {
141     return custom_do_loop_task(user_context, f, min, size, closure, task_parent);
142 }
143 
halide_do_parallel_tasks(void * user_context,int num_tasks,struct halide_parallel_task_t * tasks,void * task_parent)144 WEAK int halide_do_parallel_tasks(void *user_context, int num_tasks,
145                                   struct halide_parallel_task_t *tasks,
146                                   void *task_parent) {
147     return custom_do_parallel_tasks(user_context, num_tasks, tasks, task_parent);
148 }
149 
halide_semaphore_init(struct halide_semaphore_t * sema,int count)150 WEAK int halide_semaphore_init(struct halide_semaphore_t *sema, int count) {
151     return custom_semaphore_init(sema, count);
152 }
153 
halide_semaphore_release(struct halide_semaphore_t * sema,int count)154 WEAK int halide_semaphore_release(struct halide_semaphore_t *sema, int count) {
155     return custom_semaphore_release(sema, count);
156 }
157 
halide_semaphore_try_acquire(struct halide_semaphore_t * sema,int count)158 WEAK bool halide_semaphore_try_acquire(struct halide_semaphore_t *sema, int count) {
159     return custom_semaphore_try_acquire(sema, count);
160 }
161 
162 }  // extern "C"
163