timer.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 1997-2016 Wind River Systems, Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <kernel.h>
  7. #include <init.h>
  8. #include <ksched.h>
  9. #include <wait_q.h>
  10. #include <syscall_handler.h>
  11. #include <stdbool.h>
  12. #include <spinlock.h>
  13. static struct k_spinlock lock;
  14. /**
  15. * @brief Handle expiration of a kernel timer object.
  16. *
  17. * @param t Timeout used by the timer.
  18. *
  19. * @return N/A
  20. */
  21. void z_timer_expiration_handler(struct _timeout *t)
  22. {
  23. struct k_timer *timer = CONTAINER_OF(t, struct k_timer, timeout);
  24. struct k_thread *thread;
  25. k_spinlock_key_t key = k_spin_lock(&lock);
  26. /*
  27. * if the timer is periodic, start it again; don't add _TICK_ALIGN
  28. * since we're already aligned to a tick boundary
  29. */
  30. if (!K_TIMEOUT_EQ(timer->period, K_NO_WAIT) &&
  31. !K_TIMEOUT_EQ(timer->period, K_FOREVER)) {
  32. z_add_timeout(&timer->timeout, z_timer_expiration_handler,
  33. timer->period);
  34. }
  35. /* update timer's status */
  36. timer->status += 1U;
  37. /* invoke timer expiry function */
  38. if (timer->expiry_fn != NULL) {
  39. timer->expiry_fn(timer);
  40. }
  41. if (!IS_ENABLED(CONFIG_MULTITHREADING)) {
  42. k_spin_unlock(&lock, key);
  43. return;
  44. }
  45. thread = z_waitq_head(&timer->wait_q);
  46. if (thread == NULL) {
  47. k_spin_unlock(&lock, key);
  48. return;
  49. }
  50. z_unpend_thread_no_timeout(thread);
  51. arch_thread_return_value_set(thread, 0);
  52. k_spin_unlock(&lock, key);
  53. z_ready_thread(thread);
  54. }
  55. void k_timer_init(struct k_timer *timer,
  56. k_timer_expiry_t expiry_fn,
  57. k_timer_stop_t stop_fn)
  58. {
  59. timer->expiry_fn = expiry_fn;
  60. timer->stop_fn = stop_fn;
  61. timer->status = 0U;
  62. if (IS_ENABLED(CONFIG_MULTITHREADING)) {
  63. z_waitq_init(&timer->wait_q);
  64. }
  65. z_init_timeout(&timer->timeout);
  66. SYS_PORT_TRACING_OBJ_INIT(k_timer, timer);
  67. timer->user_data = NULL;
  68. z_object_init(timer);
  69. }
  70. void z_impl_k_timer_start(struct k_timer *timer, k_timeout_t duration,
  71. k_timeout_t period)
  72. {
  73. SYS_PORT_TRACING_OBJ_FUNC(k_timer, start, timer);
  74. /* Acquire spinlock to ensure safety during concurrent calls to
  75. * k_timer_start for scheduling or rescheduling. This is necessary
  76. * since k_timer_start can be preempted, especially for the same
  77. * timer instance.
  78. */
  79. k_spinlock_key_t key = k_spin_lock(&lock);
  80. if (K_TIMEOUT_EQ(duration, K_FOREVER)) {
  81. k_spin_unlock(&lock, key);
  82. return;
  83. }
  84. /* z_add_timeout() always adds one to the incoming tick count
  85. * to round up to the next tick (by convention it waits for
  86. * "at least as long as the specified timeout"), but the
  87. * period interval is always guaranteed to be reset from
  88. * within the timer ISR, so no round up is desired. Subtract
  89. * one.
  90. *
  91. * Note that the duration (!) value gets the same treatment
  92. * for backwards compatibility. This is unfortunate
  93. * (i.e. k_timer_start() doesn't treat its initial sleep
  94. * argument the same way k_sleep() does), but historical. The
  95. * timer_api test relies on this behavior.
  96. */
  97. if (!K_TIMEOUT_EQ(period, K_FOREVER) && period.ticks != 0 &&
  98. Z_TICK_ABS(period.ticks) < 0) {
  99. period.ticks = MAX(period.ticks - 1, 1);
  100. }
  101. if (Z_TICK_ABS(duration.ticks) < 0) {
  102. duration.ticks = MAX(duration.ticks - 1, 0);
  103. }
  104. (void)z_abort_timeout(&timer->timeout);
  105. timer->period = period;
  106. timer->status = 0U;
  107. z_add_timeout(&timer->timeout, z_timer_expiration_handler,
  108. duration);
  109. k_spin_unlock(&lock, key);
  110. }
  111. #ifdef CONFIG_USERSPACE
  112. static inline void z_vrfy_k_timer_start(struct k_timer *timer,
  113. k_timeout_t duration,
  114. k_timeout_t period)
  115. {
  116. Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
  117. z_impl_k_timer_start(timer, duration, period);
  118. }
  119. #include <syscalls/k_timer_start_mrsh.c>
  120. #endif
  121. void z_impl_k_timer_stop(struct k_timer *timer)
  122. {
  123. SYS_PORT_TRACING_OBJ_FUNC(k_timer, stop, timer);
  124. int inactive = z_abort_timeout(&timer->timeout) != 0;
  125. if (inactive) {
  126. return;
  127. }
  128. if (timer->stop_fn != NULL) {
  129. timer->stop_fn(timer);
  130. }
  131. if (IS_ENABLED(CONFIG_MULTITHREADING)) {
  132. struct k_thread *pending_thread = z_unpend1_no_timeout(&timer->wait_q);
  133. if (pending_thread != NULL) {
  134. z_ready_thread(pending_thread);
  135. z_reschedule_unlocked();
  136. }
  137. }
  138. }
  139. #ifdef CONFIG_USERSPACE
  140. static inline void z_vrfy_k_timer_stop(struct k_timer *timer)
  141. {
  142. Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
  143. z_impl_k_timer_stop(timer);
  144. }
  145. #include <syscalls/k_timer_stop_mrsh.c>
  146. #endif
  147. uint32_t z_impl_k_timer_status_get(struct k_timer *timer)
  148. {
  149. k_spinlock_key_t key = k_spin_lock(&lock);
  150. uint32_t result = timer->status;
  151. timer->status = 0U;
  152. k_spin_unlock(&lock, key);
  153. return result;
  154. }
  155. #ifdef CONFIG_USERSPACE
  156. static inline uint32_t z_vrfy_k_timer_status_get(struct k_timer *timer)
  157. {
  158. Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
  159. return z_impl_k_timer_status_get(timer);
  160. }
  161. #include <syscalls/k_timer_status_get_mrsh.c>
  162. #endif
  163. uint32_t z_impl_k_timer_status_sync(struct k_timer *timer)
  164. {
  165. __ASSERT(!arch_is_in_isr(), "");
  166. SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_timer, status_sync, timer);
  167. if (!IS_ENABLED(CONFIG_MULTITHREADING)) {
  168. uint32_t result;
  169. do {
  170. k_spinlock_key_t key = k_spin_lock(&lock);
  171. if (!z_is_inactive_timeout(&timer->timeout)) {
  172. result = *(volatile uint32_t *)&timer->status;
  173. timer->status = 0U;
  174. k_spin_unlock(&lock, key);
  175. if (result > 0) {
  176. break;
  177. }
  178. } else {
  179. result = timer->status;
  180. k_spin_unlock(&lock, key);
  181. break;
  182. }
  183. } while (true);
  184. return result;
  185. }
  186. k_spinlock_key_t key = k_spin_lock(&lock);
  187. uint32_t result = timer->status;
  188. if (result == 0U) {
  189. if (!z_is_inactive_timeout(&timer->timeout)) {
  190. SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_timer, status_sync, timer, K_FOREVER);
  191. /* wait for timer to expire or stop */
  192. (void)z_pend_curr(&lock, key, &timer->wait_q, K_FOREVER);
  193. /* get updated timer status */
  194. key = k_spin_lock(&lock);
  195. result = timer->status;
  196. } else {
  197. /* timer is already stopped */
  198. }
  199. } else {
  200. /* timer has already expired at least once */
  201. }
  202. timer->status = 0U;
  203. k_spin_unlock(&lock, key);
  204. /**
  205. * @note New tracing hook
  206. */
  207. SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_timer, status_sync, timer, result);
  208. return result;
  209. }
  210. #ifdef CONFIG_USERSPACE
  211. static inline uint32_t z_vrfy_k_timer_status_sync(struct k_timer *timer)
  212. {
  213. Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
  214. return z_impl_k_timer_status_sync(timer);
  215. }
  216. #include <syscalls/k_timer_status_sync_mrsh.c>
  217. static inline k_ticks_t z_vrfy_k_timer_remaining_ticks(
  218. const struct k_timer *timer)
  219. {
  220. Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
  221. return z_impl_k_timer_remaining_ticks(timer);
  222. }
  223. #include <syscalls/k_timer_remaining_ticks_mrsh.c>
  224. static inline k_ticks_t z_vrfy_k_timer_expires_ticks(
  225. const struct k_timer *timer)
  226. {
  227. Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
  228. return z_impl_k_timer_expires_ticks(timer);
  229. }
  230. #include <syscalls/k_timer_expires_ticks_mrsh.c>
  231. static inline void *z_vrfy_k_timer_user_data_get(const struct k_timer *timer)
  232. {
  233. Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
  234. return z_impl_k_timer_user_data_get(timer);
  235. }
  236. #include <syscalls/k_timer_user_data_get_mrsh.c>
  237. static inline void z_vrfy_k_timer_user_data_set(struct k_timer *timer,
  238. void *user_data)
  239. {
  240. Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
  241. z_impl_k_timer_user_data_set(timer, user_data);
  242. }
  243. #include <syscalls/k_timer_user_data_set_mrsh.c>
  244. #endif