bh_thread.h 11 KB

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