bh_thread.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _BH_THREAD_H
  6. #define _BH_THREAD_H
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include "bh_config.h"
  11. #include "bh_platform.h"
  12. #define BH_MAX_THREAD 32
  13. #define BH_MAX_TLS_NUM 2
  14. #define BHT_ERROR (-1)
  15. #define BHT_TIMED_OUT (1)
  16. #define BHT_OK (0)
  17. #define BHT_NO_WAIT 0x00000000
  18. #define BHT_WAIT_FOREVER 0xFFFFFFFF
  19. /**
  20. * vm_thread_sys_init
  21. * initiation function for beihai thread system. Invoked at the beginning of beihai intiation.
  22. *
  23. * @return 0 if succuess.
  24. */
  25. int _vm_thread_sys_init(void);
  26. #ifdef _INSTRUMENT_TEST_ENABLED
  27. int vm_thread_sys_init_instr(const char*func_name);
  28. #define vm_thread_sys_init(void) vm_thread_sys_init_instr(__FUNCTION__)
  29. #else
  30. #define vm_thread_sys_init _vm_thread_sys_init
  31. #endif
  32. void vm_thread_sys_destroy(void);
  33. /**
  34. * This function creates a thread
  35. *
  36. * @param p_tid [OUTPUT] the pointer of tid
  37. * @param start main routine of the thread
  38. * @param arg argument passed to main routine
  39. * @param stack_size bytes of stack size
  40. *
  41. * @return 0 if success.
  42. */
  43. int _vm_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
  44. unsigned int stack_size);
  45. #ifdef _INSTRUMENT_TEST_ENABLED
  46. int vm_thread_create_instr(korp_tid *p_tid, thread_start_routine_t start, void *arg, unsigned int stack_size, const char*func_name);
  47. #define vm_thread_create(p_tid, start, arg, stack_size) vm_thread_create_instr(p_tid, start, arg, stack_size, __FUNCTION__)
  48. #else
  49. #define vm_thread_create _vm_thread_create
  50. #endif
  51. /**
  52. * This function creates a thread
  53. *
  54. * @param p_tid [OUTPUT] the pointer of tid
  55. * @param start main routine of the thread
  56. * @param arg argument passed to main routine
  57. * @param stack_size bytes of stack size
  58. * @param prio the priority
  59. *
  60. * @return 0 if success.
  61. */
  62. int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
  63. void *arg, unsigned int stack_size, int prio);
  64. #ifdef _INSTRUMENT_TEST_ENABLED
  65. int vm_thread_create_with_prio_instr(korp_tid *p_tid, thread_start_routine_t start, void *arg, unsigned int stack_size, int prio, const char*func_name);
  66. #define vm_thread_create_with_prio(p_tid, start, arg, stack_size) vm_thread_create_instr(p_tid, start, arg, stack_size, prio, __FUNCTION__)
  67. #else
  68. #define vm_thread_create_with_prio _vm_thread_create_with_prio
  69. #endif
  70. /**
  71. * This function never returns.
  72. *
  73. * @param code not used
  74. */
  75. void vm_thread_exit(void *code);
  76. /**
  77. * This function gets current thread id
  78. *
  79. * @return current thread id
  80. */
  81. korp_tid _vm_self_thread(void);
  82. #ifdef _INSTRUMENT_TEST_ENABLED
  83. korp_tid vm_self_thread_instr(const char*func_name);
  84. #define vm_self_thread(void) vm_self_thread_instr(__FUNCTION__)
  85. #else
  86. #define vm_self_thread _vm_self_thread
  87. #endif
  88. /**
  89. * This function saves a pointer in thread local storage. One thread can only save one pointer.
  90. *
  91. * @param idx tls array index
  92. * @param ptr pointer need save as TLS
  93. *
  94. * @return 0 if success
  95. */
  96. int _vm_tls_put(unsigned idx, void *ptr);
  97. #ifdef _INSTRUMENT_TEST_ENABLED
  98. int vm_tls_put_instr(unsigned idx, void *ptr, const char*func_name);
  99. #define vm_tls_put(idx, ptr) vm_tls_put_instr(idx, ptr, __FUNCTION__)
  100. #else
  101. #define vm_tls_put _vm_tls_put
  102. #endif
  103. /**
  104. * This function gets a pointer saved in TLS.
  105. *
  106. * @param idx tls array index
  107. *
  108. * @return the pointer saved in TLS.
  109. */
  110. void *_vm_tls_get(unsigned idx);
  111. #ifdef _INSTRUMENT_TEST_ENABLED
  112. void *vm_tls_get_instr(unsigned idx, const char*func_name);
  113. #define vm_tls_get(idx) vm_tls_get_instr(idx, __FUNCTION__)
  114. #else
  115. #define vm_tls_get _vm_tls_get
  116. #endif
  117. #define vm_thread_testcancel(void)
  118. /**
  119. * This function creates a non-recursive mutex
  120. *
  121. * @param mutex [OUTPUT] pointer to mutex initialized.
  122. *
  123. * @return 0 if success
  124. */
  125. int _vm_mutex_init(korp_mutex *mutex);
  126. #ifdef INSTRUMENT_TEST_ENABLED
  127. int vm_mutex_init_instr(korp_mutex *mutex, const char*func_name);
  128. #define vm_mutex_init(mutex) vm_mutex_init_instr(mutex, __FUNCTION__)
  129. #else
  130. #define vm_mutex_init _vm_mutex_init
  131. #endif
  132. /**
  133. * This function creates a recursive mutex
  134. *
  135. * @param mutex [OUTPUT] pointer to mutex initialized.
  136. *
  137. * @return 0 if success
  138. */
  139. int _vm_recursive_mutex_init(korp_mutex *mutex);
  140. #ifdef INSTRUMENT_TEST_ENABLED
  141. int vm_recursive_mutex_init_instr(korp_mutex *mutex, const char*func_name);
  142. #define vm_recursive_mutex_init(mutex) vm_recursive_mutex_init_instr(mutex, __FUNCTION__)
  143. #else
  144. #define vm_recursive_mutex_init _vm_recursive_mutex_init
  145. #endif
  146. /**
  147. * This function destroys a mutex
  148. *
  149. * @param mutex pointer to mutex need destroy
  150. *
  151. * @return 0 if success
  152. */
  153. int _vm_mutex_destroy(korp_mutex *mutex);
  154. #ifdef _INSTRUMENT_TEST_ENABLED
  155. int vm_mutex_destroy_instr(korp_mutex *mutex, const char*func_name);
  156. #define vm_mutex_destroy(mutex) vm_mutex_destroy_instr(mutex, __FUNCTION__)
  157. #else
  158. #define vm_mutex_destroy _vm_mutex_destroy
  159. #endif
  160. /**
  161. * This function locks the mutex
  162. *
  163. * @param mutex pointer to mutex need lock
  164. *
  165. * @return Void
  166. */
  167. void vm_mutex_lock(korp_mutex *mutex);
  168. /**
  169. * This function locks the mutex without waiting
  170. *
  171. * @param mutex pointer to mutex need lock
  172. *
  173. * @return 0 if success
  174. */
  175. int vm_mutex_trylock(korp_mutex *mutex);
  176. /**
  177. * This function unlocks the mutex
  178. *
  179. * @param mutex pointer to mutex need unlock
  180. *
  181. * @return Void
  182. */
  183. void vm_mutex_unlock(korp_mutex *mutex);
  184. /**
  185. * This function creates a semaphone
  186. *
  187. * @param sem [OUTPUT] pointer to semaphone
  188. * @param c counter of semaphone
  189. *
  190. * @return 0 if success
  191. */
  192. int _vm_sem_init(korp_sem *sem, unsigned int c);
  193. #ifdef _INSTRUMENT_TEST_ENABLED
  194. int vm_sem_init_instr(korp_sem *sem, unsigned int c, const char*func_name);
  195. #define vm_sem_init(sem, c) vm_sem_init_instr(sem, c, __FUNCTION__)
  196. #else
  197. #define vm_sem_init _vm_sem_init
  198. #endif
  199. /**
  200. * This function destroys a semaphone
  201. *
  202. * @param sem pointer to semaphone need destroy
  203. *
  204. * @return 0 if success
  205. */
  206. int _vm_sem_destroy(korp_sem *sem);
  207. #ifdef _INSTRUMENT_TEST_ENABLED
  208. int vm_sem_destroy_instr(korp_sem *sem, const char*func_name);
  209. #define vm_sem_destroy(sem) vm_sem_destroy_instr(sem, __FUNCTION__)
  210. #else
  211. #define vm_sem_destroy _vm_sem_destroy
  212. #endif
  213. /**
  214. * This function performs wait operation on semaphone
  215. *
  216. * @param sem pointer to semaphone need perform wait operation
  217. *
  218. * @return 0 if success
  219. */
  220. int _vm_sem_wait(korp_sem *sem);
  221. #ifdef _INSTRUMENT_TEST_ENABLED
  222. int vm_sem_wait_instr(korp_sem *sem, const char*func_name);
  223. #define vm_sem_wait(sem) vm_sem_wait_instr(sem, __FUNCTION__)
  224. #else
  225. #define vm_sem_wait _vm_sem_wait
  226. #endif
  227. /**
  228. * This function performs wait operation on semaphone with a timeout
  229. *
  230. * @param sem pointer to semaphone need perform wait operation
  231. * @param mills wait milliseconds to return
  232. *
  233. * @return 0 if success
  234. * @return BH_TIMEOUT if time out
  235. */
  236. int _vm_sem_reltimedwait(korp_sem *sem, int mills);
  237. #ifdef _INSTRUMENT_TEST_ENABLED
  238. int vm_sem_reltimedwait_instr(korp_sem *sem, int mills, const char*func_name);
  239. #define vm_sem_reltimedwait(sem, mills) vm_sem_reltimedwait_instr(sem, mills, __FUNCTION__)
  240. #else
  241. #define vm_sem_reltimedwait _vm_sem_reltimedwait
  242. #endif
  243. /**
  244. * This function performs post operation on semaphone
  245. *
  246. * @param sem pointer to semaphone need perform post operation
  247. *
  248. * @return 0 if success
  249. */
  250. int _vm_sem_post(korp_sem *sem);
  251. #ifdef _INSTRUMENT_TEST_ENABLED
  252. int vm_sem_post_instr(korp_sem *sem, const char*func_name);
  253. #define vm_sem_post(sem) vm_sem_post_instr(sem, __FUNCTION__)
  254. #else
  255. #define vm_sem_post _vm_sem_post
  256. #endif
  257. /**
  258. * This function creates a condition variable
  259. *
  260. * @param cond [OUTPUT] pointer to condition variable
  261. *
  262. * @return 0 if success
  263. */
  264. int _vm_cond_init(korp_cond *cond);
  265. #ifdef INSTRUMENT_TEST_ENABLED
  266. int vm_cond_init_instr(korp_cond *cond, const char*func_name);
  267. #define vm_cond_init(cond) vm_cond_init_instr(cond, __FUNCTION__)
  268. #else
  269. #define vm_cond_init _vm_cond_init
  270. #endif
  271. /**
  272. * This function destroys condition variable
  273. *
  274. * @param cond pointer to condition variable
  275. *
  276. * @return 0 if success
  277. */
  278. int _vm_cond_destroy(korp_cond *cond);
  279. #ifdef _INSTRUMENT_TEST_ENABLED
  280. int vm_cond_destroy_instr(korp_cond *cond, const char*func_name);
  281. #define vm_cond_destroy(cond) vm_cond_destroy_instr(cond, __FUNCTION__)
  282. #else
  283. #define vm_cond_destroy _vm_cond_destroy
  284. #endif
  285. /**
  286. * This function will block on a condition varible.
  287. *
  288. * @param cond pointer to condition variable
  289. * @param mutex pointer to mutex to protect the condition variable
  290. *
  291. * @return 0 if success
  292. */
  293. int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex);
  294. #ifdef _INSTRUMENT_TEST_ENABLED
  295. int vm_cond_wait_instr(korp_cond *cond, korp_mutex *mutex, const char*func_name);
  296. #define vm_cond_wait(cond, mutex) vm_cond_wait_instr(cond, mutex, __FUNCTION__)
  297. #else
  298. #define vm_cond_wait _vm_cond_wait
  299. #endif
  300. /**
  301. * This function will block on a condition varible or return if time specified passes.
  302. *
  303. * @param cond pointer to condition variable
  304. * @param mutex pointer to mutex to protect the condition variable
  305. * @param mills milliseconds to wait
  306. *
  307. * @return 0 if success
  308. */
  309. int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills);
  310. #ifdef _INSTRUMENT_TEST_ENABLED
  311. int vm_cond_reltimedwait_instr(korp_cond *cond, korp_mutex *mutex, int mills, const char*func_name);
  312. #define vm_cond_reltimedwait(cond, mutex, mills) vm_cond_reltimedwait_instr(cond, mutex, mills, __FUNCTION__)
  313. #else
  314. #define vm_cond_reltimedwait _vm_cond_reltimedwait
  315. #endif
  316. /**
  317. * This function signals the condition variable
  318. *
  319. * @param cond condition variable
  320. *
  321. * @return 0 if success
  322. */
  323. int _vm_cond_signal(korp_cond *cond);
  324. #ifdef _INSTRUMENT_TEST_ENABLED
  325. int vm_cond_signal_instr(korp_cond *cond, const char*func_name);
  326. #define vm_cond_signal(cond) vm_cond_signal_instr(cond, __FUNCTION__)
  327. #else
  328. #define vm_cond_signal _vm_cond_signal
  329. #endif
  330. int _vm_cond_broadcast(korp_cond *cond);
  331. #ifdef _INSTRUMENT_TEST_ENABLED
  332. int vm_cond_broadcast_instr(korp_cond *cond, const char*func_name);
  333. #define vm_cond_broadcast(cond) vm_cond_broadcast_instr(cond, __FUNCTION__)
  334. #else
  335. #define vm_cond_broadcast _vm_cond_broadcast
  336. #endif
  337. int _vm_thread_cancel(korp_tid thread);
  338. #ifdef _INSTRUMENT_TEST_ENABLED
  339. int vm_thread_cancel_instr(korp_tid thread, const char*func_name);
  340. #define vm_thread_cancel(thread) vm_thread_cancel_instr(thread, __FUNCTION__)
  341. #else
  342. #define vm_thread_cancel _vm_thread_cancel
  343. #endif
  344. int _vm_thread_join(korp_tid thread, void **value_ptr, int mills);
  345. #ifdef _INSTRUMENT_TEST_ENABLED
  346. int vm_thread_join_instr(korp_tid thread, void **value_ptr, int mills, const char*func_name);
  347. #define vm_thread_join(thread, value_ptr, mills) vm_thread_join_instr(thread, value_ptr, mills, __FUNCTION__)
  348. #else
  349. #define vm_thread_join _vm_thread_join
  350. #endif
  351. int _vm_thread_detach(korp_tid thread);
  352. #ifdef _INSTRUMENT_TEST_ENABLED
  353. int vm_thread_detach_instr(korp_tid thread, const char*func_name);
  354. #define vm_thread_detach(thread) vm_thread_detach_instr(thread, __FUNCTION__)
  355. #else
  356. #define vm_thread_detach _vm_thread_detach
  357. #endif
  358. #ifdef __cplusplus
  359. }
  360. #endif
  361. #endif /* #ifndef _BH_THREAD_H */