1
0

completion_mp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-04-26 Shell lockless rt_completion for MP system
  9. */
  10. #define DBG_TAG "drivers.ipc"
  11. #define DBG_LVL DBG_INFO
  12. #include <rtdbg.h>
  13. #include <rtthread.h>
  14. #include <rthw.h>
  15. #include <rtdevice.h>
  16. #define RT_COMPLETED 1
  17. #define RT_UNCOMPLETED 0
  18. #define RT_WAKING (-1)
  19. #define RT_OCCUPIED (-2)
  20. #define RT_COMPLETION_NEW_STAT(thread, flag) (((flag) & 1) | (((rt_base_t)thread) & ~1))
  21. /**
  22. * The C11 atomic can be ~5% and even faster in testing on the arm64 platform
  23. * compared to rt_atomic. So the C11 way is always preferred.
  24. */
  25. #ifdef RT_USING_STDC_ATOMIC
  26. #include <stdatomic.h>
  27. #define IPC_STORE(dst, val, morder) atomic_store_explicit(dst, val, morder)
  28. #define IPC_LOAD(dst, morder) atomic_load_explicit(dst, morder)
  29. #define IPC_BARRIER(morder) atomic_thread_fence(morder)
  30. #define IPC_CAS(dst, exp, desired, succ, fail) \
  31. atomic_compare_exchange_strong_explicit(dst, exp, desired, succ, fail)
  32. #else /* !RT_USING_STDC_ATOMIC */
  33. #include <rtatomic.h>
  34. #define IPC_STORE(dst, val, morder) rt_atomic_store(dst, val)
  35. #define IPC_LOAD(dst, morder) rt_atomic_load(dst)
  36. #define IPC_BARRIER(morder)
  37. #define IPC_CAS(dst, exp, desired, succ, fail) \
  38. rt_atomic_compare_exchange_strong(dst, exp, desired)
  39. #endif /* RT_USING_STDC_ATOMIC */
  40. static rt_err_t _comp_susp_thread(struct rt_completion *completion,
  41. rt_thread_t thread, rt_int32_t timeout,
  42. int suspend_flag);
  43. /**
  44. * @brief This function will initialize a completion object.
  45. *
  46. * @param completion is a pointer to a completion object.
  47. */
  48. void rt_completion_init(struct rt_completion *completion)
  49. {
  50. RT_ASSERT(completion != RT_NULL);
  51. IPC_STORE(&completion->susp_thread_n_flag, RT_UNCOMPLETED,
  52. memory_order_relaxed);
  53. }
  54. RTM_EXPORT(rt_completion_init);
  55. /**
  56. * @brief This is same as rt_completion_wait_flags(), except that this API is NOT
  57. * ISR-safe (you can NOT call completion_done() on isr routine).
  58. *
  59. * @param completion is a pointer to a completion object.
  60. * @param timeout is a timeout period (unit: OS ticks). If the completion is unavailable, the thread will wait for
  61. * the completion done up to the amount of time specified by the argument.
  62. * NOTE: Generally, we use the macro RT_WAITING_FOREVER to set this parameter, which means that when the
  63. * completion is unavailable, the thread will be waitting forever.
  64. * @param suspend_flag suspend flags. See rt_thread_suspend_with_flag()
  65. *
  66. * @return Return the operation status. ONLY when the return value is RT_EOK, the operation is successful.
  67. * If the return value is any other values, it means that the completion wait failed.
  68. *
  69. * @warning This function can ONLY be called in the thread context. It MUST NOT be called in interrupt context.
  70. */
  71. rt_err_t rt_completion_wait_flags_noisr(struct rt_completion *completion,
  72. rt_int32_t timeout, int suspend_flag)
  73. {
  74. rt_err_t result = -RT_ERROR;
  75. rt_thread_t thread;
  76. rt_bool_t exchange_succ;
  77. rt_base_t expected_value;
  78. RT_ASSERT(completion != RT_NULL);
  79. /* current context checking */
  80. RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
  81. thread = rt_thread_self();
  82. do
  83. {
  84. /* try to consume one completion */
  85. expected_value = RT_COMPLETED;
  86. exchange_succ =
  87. IPC_CAS(&completion->susp_thread_n_flag, &expected_value,
  88. RT_UNCOMPLETED, memory_order_acquire, memory_order_relaxed);
  89. if (exchange_succ)
  90. {
  91. /* consume succeed, now return EOK */
  92. result = RT_EOK;
  93. break;
  94. }
  95. else if (expected_value == RT_WAKING)
  96. {
  97. /* previous wake is not done yet, yield thread & try again */
  98. rt_thread_yield();
  99. }
  100. else
  101. {
  102. /**
  103. * API rules say: only one thread can suspend on complete.
  104. * So we assert if debug.
  105. */
  106. RT_ASSERT(expected_value == RT_UNCOMPLETED);
  107. if (timeout != 0)
  108. {
  109. /**
  110. * try to occupy completion, noted that we are assuming that
  111. * `expected_value == RT_UNCOMPLETED`
  112. */
  113. exchange_succ = IPC_CAS(
  114. &completion->susp_thread_n_flag, &expected_value,
  115. RT_OCCUPIED, memory_order_relaxed, memory_order_relaxed);
  116. if (exchange_succ)
  117. {
  118. /* complete waiting business and return result */
  119. result = _comp_susp_thread(completion, thread, timeout,
  120. suspend_flag);
  121. RT_ASSERT(rt_atomic_load(&completion->susp_thread_n_flag) !=
  122. RT_OCCUPIED);
  123. break;
  124. }
  125. else
  126. {
  127. /* try again */
  128. }
  129. }
  130. else
  131. {
  132. result = -RT_ETIMEOUT;
  133. break;
  134. }
  135. }
  136. } while (1);
  137. return result;
  138. }
  139. /**
  140. * @brief This function will wait for a completion, if the completion is unavailable, the thread shall wait for
  141. * the completion up to a specified time.
  142. *
  143. * @param completion is a pointer to a completion object.
  144. * @param timeout is a timeout period (unit: OS ticks). If the completion is unavailable, the thread will wait for
  145. * the completion done up to the amount of time specified by the argument.
  146. * NOTE: Generally, we use the macro RT_WAITING_FOREVER to set this parameter, which means that when the
  147. * completion is unavailable, the thread will be waitting forever.
  148. * @param suspend_flag suspend flags. See rt_thread_suspend_with_flag()
  149. *
  150. * @return Return the operation status. ONLY when the return value is RT_EOK, the operation is successful.
  151. * If the return value is any other values, it means that the completion wait failed.
  152. *
  153. * @warning This function can ONLY be called in the thread context. It MUST NOT be called in interrupt context.
  154. */
  155. rt_err_t rt_completion_wait_flags(struct rt_completion *completion,
  156. rt_int32_t timeout, int suspend_flag)
  157. {
  158. rt_err_t error;
  159. rt_ubase_t level;
  160. level = rt_hw_local_irq_disable();
  161. error = rt_completion_wait_flags_noisr(completion, timeout, suspend_flag);
  162. rt_hw_local_irq_enable(level);
  163. return error;
  164. }
  165. static rt_base_t _wait_until_update(struct rt_completion *completion, rt_base_t expected)
  166. {
  167. rt_base_t current_value;
  168. /* spinning for update */
  169. do
  170. {
  171. rt_hw_isb();
  172. current_value =
  173. IPC_LOAD(&completion->susp_thread_n_flag, memory_order_relaxed);
  174. } while (current_value == expected);
  175. return current_value;
  176. }
  177. /**
  178. * Try to suspend thread and update completion
  179. */
  180. static rt_err_t _comp_susp_thread(struct rt_completion *completion,
  181. rt_thread_t thread, rt_int32_t timeout,
  182. int suspend_flag)
  183. {
  184. rt_err_t error = -RT_ERROR;
  185. rt_base_t clevel;
  186. rt_base_t comp_waiting;
  187. /* suspend thread */
  188. clevel = rt_enter_critical();
  189. /* reset thread error number */
  190. thread->error = RT_EOK;
  191. error = rt_thread_suspend_with_flag(thread, suspend_flag);
  192. if (error)
  193. {
  194. rt_exit_critical_safe(clevel);
  195. RT_ASSERT(rt_atomic_load(&completion->susp_thread_n_flag) ==
  196. RT_OCCUPIED);
  197. IPC_STORE(&completion->susp_thread_n_flag, RT_UNCOMPLETED,
  198. memory_order_relaxed);
  199. }
  200. else
  201. {
  202. /* set to waiting */
  203. comp_waiting = RT_COMPLETION_NEW_STAT(thread, RT_UNCOMPLETED);
  204. RT_ASSERT(rt_atomic_load(&completion->susp_thread_n_flag) ==
  205. RT_OCCUPIED);
  206. IPC_STORE(&completion->susp_thread_n_flag, comp_waiting,
  207. memory_order_relaxed);
  208. /* current context checking */
  209. RT_DEBUG_NOT_IN_INTERRUPT;
  210. /* start timer */
  211. if (timeout > 0)
  212. {
  213. /* reset the timeout of thread timer and start it */
  214. rt_timer_control(&(thread->thread_timer),
  215. RT_TIMER_CTRL_SET_TIME,
  216. &timeout);
  217. rt_timer_start(&(thread->thread_timer));
  218. }
  219. /* do schedule */
  220. rt_schedule();
  221. rt_exit_critical_safe(clevel);
  222. /* thread is woken up */
  223. error = thread->error;
  224. error = error > 0 ? -error : error;
  225. /* clean completed flag & remove susp_thread on the case of waking by timeout */
  226. if (!error)
  227. {
  228. /* completion done successfully */
  229. RT_ASSERT(rt_atomic_load(&completion->susp_thread_n_flag) !=
  230. comp_waiting);
  231. /* the necessary barrier is done during thread sched */
  232. }
  233. else
  234. {
  235. /* try to cancel waiting if woken up expectedly or timeout */
  236. if (!IPC_CAS(&completion->susp_thread_n_flag, &comp_waiting,
  237. RT_UNCOMPLETED, memory_order_relaxed,
  238. memory_order_relaxed))
  239. {
  240. /* cancel failed, producer had woken us in the past, fix error */
  241. if (comp_waiting == RT_WAKING)
  242. {
  243. _wait_until_update(completion, RT_WAKING);
  244. }
  245. IPC_BARRIER(memory_order_acquire);
  246. error = RT_EOK;
  247. }
  248. }
  249. }
  250. return error;
  251. }
  252. /**
  253. * @brief This function indicates a completion has done and wakeup the thread
  254. * and update its errno. No update is applied if it's a negative value.
  255. *
  256. * @param completion is a pointer to a completion object.
  257. * @param thread_errno is the errno set to waking thread.
  258. * @return RT_EOK if wakeup succeed.
  259. * RT_EEMPTY if wakeup failure and the completion is set to completed.
  260. * RT_EBUSY if the completion is still in completed state
  261. */
  262. rt_err_t rt_completion_wakeup_by_errno(struct rt_completion *completion,
  263. rt_err_t thread_errno)
  264. {
  265. rt_err_t error = -RT_ERROR;
  266. rt_thread_t suspend_thread;
  267. rt_bool_t exchange_succ;
  268. rt_base_t expected_value;
  269. RT_ASSERT(completion != RT_NULL);
  270. do
  271. {
  272. /* try to transform from uncompleted to completed */
  273. expected_value = RT_UNCOMPLETED;
  274. exchange_succ =
  275. IPC_CAS(&completion->susp_thread_n_flag, &expected_value,
  276. RT_COMPLETED, memory_order_release, memory_order_relaxed);
  277. if (exchange_succ)
  278. {
  279. error = -RT_EEMPTY;
  280. break;
  281. }
  282. else
  283. {
  284. if (expected_value == RT_COMPLETED)
  285. {
  286. /* completion still in completed state */
  287. error = -RT_EBUSY;
  288. break;
  289. }
  290. else if (expected_value == RT_OCCUPIED ||
  291. expected_value == RT_WAKING)
  292. {
  293. continue;
  294. }
  295. else
  296. {
  297. /* try to resume the thread and set uncompleted */
  298. exchange_succ = IPC_CAS(
  299. &completion->susp_thread_n_flag, &expected_value,
  300. RT_WAKING, memory_order_relaxed, memory_order_relaxed);
  301. if (exchange_succ)
  302. {
  303. #define GET_THREAD(val) ((rt_thread_t)((val) & ~1))
  304. suspend_thread = GET_THREAD(expected_value);
  305. if (thread_errno >= 0)
  306. {
  307. suspend_thread->error = thread_errno;
  308. }
  309. /* safe to assume publication done even on resume failure */
  310. RT_ASSERT(rt_atomic_load(&completion->susp_thread_n_flag) ==
  311. RT_WAKING);
  312. IPC_STORE(&completion->susp_thread_n_flag, RT_UNCOMPLETED,
  313. memory_order_release);
  314. rt_thread_resume(suspend_thread);
  315. error = RT_EOK;
  316. break;
  317. }
  318. else
  319. {
  320. /* failed in racing to resume thread, try again */
  321. }
  322. }
  323. }
  324. } while (1);
  325. return error;
  326. }