* Try to suspend thread and update completion */
| 204 | * Try to suspend thread and update completion |
| 205 | */ |
| 206 | static rt_err_t _comp_susp_thread(struct rt_completion *completion, |
| 207 | rt_thread_t thread, rt_int32_t timeout, |
| 208 | int suspend_flag) |
| 209 | { |
| 210 | rt_err_t error = -RT_ERROR; |
| 211 | rt_base_t clevel; |
| 212 | rt_base_t comp_waiting; |
| 213 | |
| 214 | /* suspend thread */ |
| 215 | clevel = rt_enter_critical(); |
| 216 | |
| 217 | /* reset thread error number */ |
| 218 | thread->error = RT_EOK; |
| 219 | |
| 220 | error = rt_thread_suspend_with_flag(thread, suspend_flag); |
| 221 | |
| 222 | if (error) |
| 223 | { |
| 224 | rt_exit_critical_safe(clevel); |
| 225 | RT_ASSERT(rt_atomic_load(&completion->susp_thread_n_flag) == |
| 226 | RT_OCCUPIED); |
| 227 | IPC_STORE(&completion->susp_thread_n_flag, RT_UNCOMPLETED, |
| 228 | memory_order_relaxed); |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | /* set to waiting */ |
| 233 | comp_waiting = RT_COMPLETION_NEW_STAT(thread, RT_UNCOMPLETED); |
| 234 | RT_ASSERT(rt_atomic_load(&completion->susp_thread_n_flag) == |
| 235 | RT_OCCUPIED); |
| 236 | IPC_STORE(&completion->susp_thread_n_flag, comp_waiting, |
| 237 | memory_order_relaxed); |
| 238 | |
| 239 | /* current context checking */ |
| 240 | RT_DEBUG_NOT_IN_INTERRUPT; |
| 241 | |
| 242 | /* start timer */ |
| 243 | if (timeout > 0) |
| 244 | { |
| 245 | rt_tick_t timeout_tick = timeout; |
| 246 | /* reset the timeout of thread timer and start it */ |
| 247 | rt_timer_control(&(thread->thread_timer), |
| 248 | RT_TIMER_CTRL_SET_TIME, |
| 249 | &timeout_tick); |
| 250 | rt_timer_start(&(thread->thread_timer)); |
| 251 | } |
| 252 | |
| 253 | /* do schedule */ |
| 254 | rt_schedule(); |
| 255 | |
| 256 | rt_exit_critical_safe(clevel); |
| 257 | |
| 258 | /* thread is woken up */ |
| 259 | error = thread->error; |
| 260 | error = error > 0 ? -error : error; |
| 261 | |
| 262 | /* clean completed flag & remove susp_thread on the case of waking by timeout */ |
| 263 | if (!error) |
no test coverage detected