blob: 61b06e68df8d53f0e9dc3e03f672d49e240e1c9f [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
Benjamin Walsh8d7c2742017-02-11 10:50:27 -05002 * Copyright (c) 2016-2017 Wind River Systems, Inc.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7#include <kernel.h>
Benjamin Walshf6ca7de2016-11-08 10:36:50 -05008#include <kernel_structs.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -04009#include <atomic.h>
Benjamin Walshb4b108d2016-10-13 10:31:48 -040010#include <ksched.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040011#include <wait_q.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050012#include <misc/util.h>
Andrew Boie76c04a22017-09-27 14:45:10 -070013#include <syscall_handler.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040014
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050015/* the only struct _kernel instance */
16struct _kernel _kernel = {0};
17
Benjamin Walsh456c6da2016-09-02 18:55:39 -040018/* set the bit corresponding to prio in ready q bitmap */
Benjamin Walshb12a8e02016-12-14 15:24:12 -050019#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020static void _set_ready_q_prio_bit(int prio)
21{
22 int bmap_index = _get_ready_q_prio_bmap_index(prio);
Kumar Galacc334c72017-04-21 10:55:34 -050023 u32_t *bmap = &_ready_q.prio_bmap[bmap_index];
Benjamin Walsh456c6da2016-09-02 18:55:39 -040024
25 *bmap |= _get_ready_q_prio_bit(prio);
26}
Benjamin Walshb12a8e02016-12-14 15:24:12 -050027#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -040028
29/* clear the bit corresponding to prio in ready q bitmap */
Benjamin Walshb12a8e02016-12-14 15:24:12 -050030#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031static void _clear_ready_q_prio_bit(int prio)
32{
33 int bmap_index = _get_ready_q_prio_bmap_index(prio);
Kumar Galacc334c72017-04-21 10:55:34 -050034 u32_t *bmap = &_ready_q.prio_bmap[bmap_index];
Benjamin Walsh456c6da2016-09-02 18:55:39 -040035
36 *bmap &= ~_get_ready_q_prio_bit(prio);
37}
Benjamin Walshb12a8e02016-12-14 15:24:12 -050038#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -040039
Benjamin Walsh096d8e92016-12-16 16:45:05 -050040#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -040041/*
Benjamin Walsh88b36912016-12-02 10:37:27 -050042 * Find the next thread to run when there is no thread in the cache and update
43 * the cache.
44 */
45static struct k_thread *_get_ready_q_head(void)
46{
47 int prio = _get_highest_ready_prio();
48 int q_index = _get_ready_q_q_index(prio);
49 sys_dlist_t *list = &_ready_q.q[q_index];
50
51 __ASSERT(!sys_dlist_is_empty(list),
52 "no thread to run (prio: %d, queue index: %u)!\n",
53 prio, q_index);
54
55 struct k_thread *thread =
56 (struct k_thread *)sys_dlist_peek_head_not_empty(list);
57
58 return thread;
59}
Benjamin Walsh096d8e92016-12-16 16:45:05 -050060#endif
Benjamin Walsh88b36912016-12-02 10:37:27 -050061
62/*
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063 * Add thread to the ready queue, in the slot for its priority; the thread
64 * must not be on a wait queue.
Benjamin Walsh35497d62016-09-30 13:44:58 -040065 *
66 * This function, along with _move_thread_to_end_of_prio_q(), are the _only_
67 * places where a thread is put on the ready queue.
68 *
69 * Interrupts must be locked when calling this function.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070 */
Benjamin Walsh35497d62016-09-30 13:44:58 -040071
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040072void _add_thread_to_ready_q(struct k_thread *thread)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073{
Benjamin Walshb12a8e02016-12-14 15:24:12 -050074#ifdef CONFIG_MULTITHREADING
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050075 int q_index = _get_ready_q_q_index(thread->base.prio);
76 sys_dlist_t *q = &_ready_q.q[q_index];
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050078 _set_ready_q_prio_bit(thread->base.prio);
79 sys_dlist_append(q, &thread->base.k_q_node);
Benjamin Walsh35497d62016-09-30 13:44:58 -040080
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050081 struct k_thread **cache = &_ready_q.cache;
Benjamin Walsh35497d62016-09-30 13:44:58 -040082
Benjamin Walsh88b36912016-12-02 10:37:27 -050083 *cache = _is_t1_higher_prio_than_t2(thread, *cache) ? thread : *cache;
Benjamin Walshb12a8e02016-12-14 15:24:12 -050084#else
85 sys_dlist_append(&_ready_q.q[0], &thread->base.k_q_node);
86 _ready_q.prio_bmap[0] = 1;
87 _ready_q.cache = thread;
88#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -040089}
90
Benjamin Walsh35497d62016-09-30 13:44:58 -040091/*
92 * This function, along with _move_thread_to_end_of_prio_q(), are the _only_
93 * places where a thread is taken off the ready queue.
94 *
95 * Interrupts must be locked when calling this function.
96 */
97
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040098void _remove_thread_from_ready_q(struct k_thread *thread)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099{
Benjamin Walshb12a8e02016-12-14 15:24:12 -0500100#ifdef CONFIG_MULTITHREADING
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500101 int q_index = _get_ready_q_q_index(thread->base.prio);
102 sys_dlist_t *q = &_ready_q.q[q_index];
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400103
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500104 sys_dlist_remove(&thread->base.k_q_node);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105 if (sys_dlist_is_empty(q)) {
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500106 _clear_ready_q_prio_bit(thread->base.prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107 }
Benjamin Walsh35497d62016-09-30 13:44:58 -0400108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109 struct k_thread **cache = &_ready_q.cache;
Benjamin Walsh35497d62016-09-30 13:44:58 -0400110
Benjamin Walsh88b36912016-12-02 10:37:27 -0500111 *cache = *cache == thread ? _get_ready_q_head() : *cache;
Benjamin Walshb12a8e02016-12-14 15:24:12 -0500112#else
113 _ready_q.prio_bmap[0] = 0;
114 _ready_q.cache = NULL;
115 sys_dlist_remove(&thread->base.k_q_node);
116#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117}
118
119/* reschedule threads if the scheduler is not locked */
120/* not callable from ISR */
121/* must be called with interrupts locked */
122void _reschedule_threads(int key)
123{
Benjamin Walsh8e4a5342016-12-14 14:34:29 -0500124#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125 K_DEBUG("rescheduling threads\n");
126
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127 if (_must_switch_threads()) {
128 K_DEBUG("context-switching out %p\n", _current);
129 _Swap(key);
130 } else {
131 irq_unlock(key);
132 }
Benjamin Walsh8e4a5342016-12-14 14:34:29 -0500133#else
134 irq_unlock(key);
135#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400136}
137
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500138void k_sched_lock(void)
139{
Benjamin Walsh8e4a5342016-12-14 14:34:29 -0500140#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walshe6a69ca2016-12-21 14:54:04 -0500141 __ASSERT(_current->base.sched_locked != 1, "");
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500142 __ASSERT(!_is_in_isr(), "");
143
Benjamin Walshe6a69ca2016-12-21 14:54:04 -0500144 --_current->base.sched_locked;
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500145
Benjamin Walsh8d7c2742017-02-11 10:50:27 -0500146 /* Probably not needed since we're in a real function,
147 * but it doesn't hurt.
148 */
149 compiler_barrier();
150
Kumar Gala34a57db2017-04-19 10:39:57 -0500151 K_DEBUG("scheduler locked (%p:%d)\n",
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500152 _current, _current->base.sched_locked);
Benjamin Walsh8e4a5342016-12-14 14:34:29 -0500153#endif
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500154}
155
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400156void k_sched_unlock(void)
157{
Benjamin Walsh8e4a5342016-12-14 14:34:29 -0500158#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walshe6a69ca2016-12-21 14:54:04 -0500159 __ASSERT(_current->base.sched_locked != 0, "");
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400160 __ASSERT(!_is_in_isr(), "");
161
162 int key = irq_lock();
163
Benjamin Walsh8d7c2742017-02-11 10:50:27 -0500164 /* compiler_barrier() not needed, comes from irq_lock() */
165
Benjamin Walshe6a69ca2016-12-21 14:54:04 -0500166 ++_current->base.sched_locked;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400167
Kumar Gala34a57db2017-04-19 10:39:57 -0500168 K_DEBUG("scheduler unlocked (%p:%d)\n",
Benjamin Walsha4e033f2016-11-18 16:08:24 -0500169 _current, _current->base.sched_locked);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400170
171 _reschedule_threads(key);
Benjamin Walsh8e4a5342016-12-14 14:34:29 -0500172#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400173}
174
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400175/* convert milliseconds to ticks */
176
Benjamin Walsh62092182016-12-20 14:39:08 -0500177#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500178s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400179{
Kumar Galacc334c72017-04-21 10:55:34 -0500180 s64_t ms_ticks_per_sec = (s64_t)ms * sys_clock_ticks_per_sec;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400181
Kumar Galacc334c72017-04-21 10:55:34 -0500182 return (s32_t)ceiling_fraction(ms_ticks_per_sec, MSEC_PER_SEC);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400183}
Benjamin Walsh62092182016-12-20 14:39:08 -0500184#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400185
186/* pend the specified thread: it must *not* be in the ready queue */
187/* must be called with interrupts locked */
Kumar Galacc334c72017-04-21 10:55:34 -0500188void _pend_thread(struct k_thread *thread, _wait_q_t *wait_q, s32_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400189{
Benjamin Walshb12a8e02016-12-14 15:24:12 -0500190#ifdef CONFIG_MULTITHREADING
Benjamin Walshb8c21602016-12-23 19:34:41 -0500191 sys_dlist_t *wait_q_list = (sys_dlist_t *)wait_q;
Luiz Augusto von Dentz87aa6212017-08-22 15:27:31 +0300192 struct k_thread *pending;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400193
Luiz Augusto von Dentz87aa6212017-08-22 15:27:31 +0300194 SYS_DLIST_FOR_EACH_CONTAINER(wait_q_list, pending, base.k_q_node) {
Benjamin Walshb8c21602016-12-23 19:34:41 -0500195 if (_is_t1_higher_prio_than_t2(thread, pending)) {
Luiz Augusto von Dentz87aa6212017-08-22 15:27:31 +0300196 sys_dlist_insert_before(wait_q_list,
197 &pending->base.k_q_node,
Benjamin Walshb8c21602016-12-23 19:34:41 -0500198 &thread->base.k_q_node);
199 goto inserted;
200 }
201 }
202
203 sys_dlist_append(wait_q_list, &thread->base.k_q_node);
204
205inserted:
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400206 _mark_thread_as_pending(thread);
207
208 if (timeout != K_FOREVER) {
Kumar Galacc334c72017-04-21 10:55:34 -0500209 s32_t ticks = _TICK_ALIGN + _ms_to_ticks(timeout);
Benjamin Walsha36e0cf2016-11-23 22:15:44 -0500210
211 _add_thread_timeout(thread, wait_q, ticks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400212 }
Benjamin Walshb12a8e02016-12-14 15:24:12 -0500213#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400214}
215
216/* pend the current thread */
217/* must be called with interrupts locked */
Kumar Galacc334c72017-04-21 10:55:34 -0500218void _pend_current_thread(_wait_q_t *wait_q, s32_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400219{
220 _remove_thread_from_ready_q(_current);
221 _pend_thread(_current, wait_q, timeout);
222}
223
Maciek Borzecki81bdee32017-05-18 12:23:55 +0200224#if defined(CONFIG_PREEMPT_ENABLED) && defined(CONFIG_KERNEL_DEBUG)
225/* debug aid */
226static void _dump_ready_q(void)
227{
228 K_DEBUG("bitmaps: ");
229 for (int bitmap = 0; bitmap < K_NUM_PRIO_BITMAPS; bitmap++) {
230 K_DEBUG("%x", _ready_q.prio_bmap[bitmap]);
231 }
232 K_DEBUG("\n");
233 for (int prio = 0; prio < K_NUM_PRIORITIES; prio++) {
234 K_DEBUG("prio: %d, head: %p\n",
235 prio - _NUM_COOP_PRIO,
236 sys_dlist_peek_head(&_ready_q.q[prio]));
237 }
238}
239#endif /* CONFIG_PREEMPT_ENABLED && CONFIG_KERNEL_DEBUG */
240
Benjamin Walsh35497d62016-09-30 13:44:58 -0400241/*
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400242 * Check if there is a thread of higher prio than the current one. Should only
243 * be called if we already know that the current thread is preemptible.
244 */
245int __must_switch_threads(void)
246{
Benjamin Walsh8e4a5342016-12-14 14:34:29 -0500247#ifdef CONFIG_PREEMPT_ENABLED
Kumar Gala34a57db2017-04-19 10:39:57 -0500248 K_DEBUG("current prio: %d, highest prio: %d\n",
Benjamin Walsha4e033f2016-11-18 16:08:24 -0500249 _current->base.prio, _get_highest_ready_prio());
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400250
Maciek Borzecki81bdee32017-05-18 12:23:55 +0200251#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400252 _dump_ready_q();
Maciek Borzecki81bdee32017-05-18 12:23:55 +0200253#endif /* CONFIG_KERNEL_DEBUG */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400254
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500255 return _is_prio_higher(_get_highest_ready_prio(), _current->base.prio);
Benjamin Walsh8e4a5342016-12-14 14:34:29 -0500256#else
257 return 0;
258#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400259}
260
Andrew Boie76c04a22017-09-27 14:45:10 -0700261int _impl_k_thread_priority_get(k_tid_t thread)
Allan Stephens399d0ad2016-10-07 13:41:34 -0500262{
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500263 return thread->base.prio;
Allan Stephens399d0ad2016-10-07 13:41:34 -0500264}
265
Andrew Boie76c04a22017-09-27 14:45:10 -0700266#ifdef CONFIG_USERSPACE
Andrew Boie225e4c02017-10-12 09:54:26 -0700267_SYSCALL_HANDLER1_SIMPLE(k_thread_priority_get, K_OBJ_THREAD,
268 struct k_thread *);
Andrew Boie76c04a22017-09-27 14:45:10 -0700269#endif
270
Andrew Boie468190a2017-09-29 14:00:48 -0700271void _impl_k_thread_priority_set(k_tid_t tid, int prio)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400272{
Benjamin Walsh3cc2ba92016-11-08 15:44:05 -0500273 /*
274 * Use NULL, since we cannot know what the entry point is (we do not
275 * keep track of it) and idle cannot change its priority.
276 */
277 _ASSERT_VALID_PRIO(prio, NULL);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400278 __ASSERT(!_is_in_isr(), "");
279
Benjamin Walsh37511232016-10-13 08:10:07 -0400280 struct k_thread *thread = (struct k_thread *)tid;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400281 int key = irq_lock();
282
283 _thread_priority_set(thread, prio);
284 _reschedule_threads(key);
285}
286
Andrew Boie468190a2017-09-29 14:00:48 -0700287#ifdef CONFIG_USERSPACE
Leandro Pereira6f99bdb2017-10-13 14:00:22 -0700288_SYSCALL_HANDLER(k_thread_priority_set, thread_p, prio)
Andrew Boie468190a2017-09-29 14:00:48 -0700289{
Andrew Boie5008fed2017-10-08 10:11:24 -0700290 struct k_thread *thread = (struct k_thread *)thread_p;
291
Andrew Boie225e4c02017-10-12 09:54:26 -0700292 _SYSCALL_OBJ(thread, K_OBJ_THREAD);
293 _SYSCALL_VERIFY_MSG(_VALID_PRIO(prio, NULL),
Andrew Boie37ff5a92017-10-10 12:30:23 -0700294 "invalid thread priority %d", (int)prio);
Punit Varace60d042017-11-13 12:26:43 +0530295 _SYSCALL_VERIFY_MSG((s8_t)prio >= thread->base.prio,
Andrew Boie5008fed2017-10-08 10:11:24 -0700296 "thread priority may only be downgraded (%d < %d)",
297 prio, thread->base.prio);
298
Andrew Boie468190a2017-09-29 14:00:48 -0700299 _impl_k_thread_priority_set((k_tid_t)thread, prio);
300 return 0;
301}
302#endif
303
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400304/*
Benjamin Walsh35497d62016-09-30 13:44:58 -0400305 * Interrupts must be locked when calling this function.
306 *
307 * This function, along with _add_thread_to_ready_q() and
308 * _remove_thread_from_ready_q(), are the _only_ places where a thread is
309 * taken off or put on the ready queue.
310 */
311void _move_thread_to_end_of_prio_q(struct k_thread *thread)
312{
Benjamin Walshb12a8e02016-12-14 15:24:12 -0500313#ifdef CONFIG_MULTITHREADING
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500314 int q_index = _get_ready_q_q_index(thread->base.prio);
315 sys_dlist_t *q = &_ready_q.q[q_index];
Benjamin Walsh35497d62016-09-30 13:44:58 -0400316
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500317 if (sys_dlist_is_tail(q, &thread->base.k_q_node)) {
Benjamin Walsh35497d62016-09-30 13:44:58 -0400318 return;
319 }
320
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500321 sys_dlist_remove(&thread->base.k_q_node);
322 sys_dlist_append(q, &thread->base.k_q_node);
Benjamin Walsh35497d62016-09-30 13:44:58 -0400323
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500324 struct k_thread **cache = &_ready_q.cache;
Benjamin Walsh35497d62016-09-30 13:44:58 -0400325
Benjamin Walsh88b36912016-12-02 10:37:27 -0500326 *cache = *cache == thread ? _get_ready_q_head() : *cache;
Benjamin Walshb12a8e02016-12-14 15:24:12 -0500327#endif
Benjamin Walsh35497d62016-09-30 13:44:58 -0400328}
329
Andrew Boie468190a2017-09-29 14:00:48 -0700330void _impl_k_yield(void)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400331{
332 __ASSERT(!_is_in_isr(), "");
333
334 int key = irq_lock();
335
Benjamin Walsh35497d62016-09-30 13:44:58 -0400336 _move_thread_to_end_of_prio_q(_current);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400337
338 if (_current == _get_next_ready_thread()) {
339 irq_unlock(key);
Andrew Boie5dcb2792017-05-11 13:29:15 -0700340#ifdef CONFIG_STACK_SENTINEL
341 _check_stack_sentinel();
342#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400343 } else {
344 _Swap(key);
345 }
346}
347
Andrew Boie468190a2017-09-29 14:00:48 -0700348#ifdef CONFIG_USERSPACE
Andrew Boie225e4c02017-10-12 09:54:26 -0700349_SYSCALL_HANDLER0_SIMPLE_VOID(k_yield);
Andrew Boie468190a2017-09-29 14:00:48 -0700350#endif
351
Andrew Boie76c04a22017-09-27 14:45:10 -0700352void _impl_k_sleep(s32_t duration)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400353{
Benjamin Walshb12a8e02016-12-14 15:24:12 -0500354#ifdef CONFIG_MULTITHREADING
Carles Cufi9849df82016-12-02 15:31:08 +0100355 /* volatile to guarantee that irq_lock() is executed after ticks is
356 * populated
357 */
Kumar Galacc334c72017-04-21 10:55:34 -0500358 volatile s32_t ticks;
Carles Cufi9849df82016-12-02 15:31:08 +0100359 unsigned int key;
360
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400361 __ASSERT(!_is_in_isr(), "");
Benjamin Walsh688973e2016-10-05 16:03:31 -0400362 __ASSERT(duration != K_FOREVER, "");
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400363
Kumar Gala34a57db2017-04-19 10:39:57 -0500364 K_DEBUG("thread %p for %d ns\n", _current, duration);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400365
Benjamin Walsh5596f782016-12-09 19:57:17 -0500366 /* wait of 0 ms is treated as a 'yield' */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400367 if (duration == 0) {
368 k_yield();
369 return;
370 }
371
Carles Cufi9849df82016-12-02 15:31:08 +0100372 ticks = _TICK_ALIGN + _ms_to_ticks(duration);
373 key = irq_lock();
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400374
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400375 _remove_thread_from_ready_q(_current);
Benjamin Walsha36e0cf2016-11-23 22:15:44 -0500376 _add_thread_timeout(_current, NULL, ticks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400377
378 _Swap(key);
Benjamin Walshb12a8e02016-12-14 15:24:12 -0500379#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400380}
381
Andrew Boie76c04a22017-09-27 14:45:10 -0700382#ifdef CONFIG_USERSPACE
Leandro Pereira6f99bdb2017-10-13 14:00:22 -0700383_SYSCALL_HANDLER(k_sleep, duration)
Andrew Boie76c04a22017-09-27 14:45:10 -0700384{
Andrew Boie225e4c02017-10-12 09:54:26 -0700385 /* FIXME there were some discussions recently on whether we should
386 * relax this, thread would be unscheduled until k_wakeup issued
387 */
388 _SYSCALL_VERIFY_MSG(duration != K_FOREVER,
Andrew Boie37ff5a92017-10-10 12:30:23 -0700389 "sleeping forever not allowed");
Andrew Boie225e4c02017-10-12 09:54:26 -0700390 _impl_k_sleep(duration);
Andrew Boie76c04a22017-09-27 14:45:10 -0700391
392 return 0;
393}
394#endif
395
Andrew Boie468190a2017-09-29 14:00:48 -0700396void _impl_k_wakeup(k_tid_t thread)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400397{
398 int key = irq_lock();
399
400 /* verify first if thread is not waiting on an object */
Benjamin Walshce9f7822016-10-06 16:25:39 -0400401 if (_is_thread_pending(thread)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400402 irq_unlock(key);
403 return;
404 }
405
Benjamin Walshd211a522016-12-06 11:44:01 -0500406 if (_abort_thread_timeout(thread) == _INACTIVE) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400407 irq_unlock(key);
408 return;
409 }
410
411 _ready_thread(thread);
412
413 if (_is_in_isr()) {
414 irq_unlock(key);
415 } else {
416 _reschedule_threads(key);
417 }
418}
419
Andrew Boie468190a2017-09-29 14:00:48 -0700420#ifdef CONFIG_USERSPACE
Andrew Boie225e4c02017-10-12 09:54:26 -0700421_SYSCALL_HANDLER1_SIMPLE_VOID(k_wakeup, K_OBJ_THREAD, k_tid_t);
Andrew Boie468190a2017-09-29 14:00:48 -0700422#endif
423
Andrew Boie76c04a22017-09-27 14:45:10 -0700424k_tid_t _impl_k_current_get(void)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400425{
426 return _current;
427}
428
Andrew Boie76c04a22017-09-27 14:45:10 -0700429#ifdef CONFIG_USERSPACE
Andrew Boie225e4c02017-10-12 09:54:26 -0700430_SYSCALL_HANDLER0_SIMPLE(k_current_get);
Andrew Boie76c04a22017-09-27 14:45:10 -0700431#endif
432
Peter Mitsis68d1f4b2016-09-12 11:35:26 -0400433#ifdef CONFIG_TIMESLICING
Kumar Galacc334c72017-04-21 10:55:34 -0500434extern s32_t _time_slice_duration; /* Measured in ms */
435extern s32_t _time_slice_elapsed; /* Measured in ms */
Peter Mitsis68d1f4b2016-09-12 11:35:26 -0400436extern int _time_slice_prio_ceiling;
437
Kumar Galacc334c72017-04-21 10:55:34 -0500438void k_sched_time_slice_set(s32_t duration_in_ms, int prio)
Peter Mitsis68d1f4b2016-09-12 11:35:26 -0400439{
440 __ASSERT(duration_in_ms >= 0, "");
441 __ASSERT((prio >= 0) && (prio < CONFIG_NUM_PREEMPT_PRIORITIES), "");
442
443 _time_slice_duration = duration_in_ms;
444 _time_slice_elapsed = 0;
445 _time_slice_prio_ceiling = prio;
446}
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800447
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800448int _is_thread_time_slicing(struct k_thread *thread)
449{
450 /*
451 * Time slicing is done on the thread if following conditions are met
452 *
453 * Time slice duration should be set > 0
454 * Should not be the idle thread
455 * Priority should be higher than time slice priority ceiling
456 * There should be multiple threads active with same priority
457 */
458
459 if (!(_time_slice_duration > 0) || (_is_idle_thread_ptr(thread))
460 || _is_prio_higher(thread->base.prio, _time_slice_prio_ceiling)) {
461 return 0;
462 }
463
464 int q_index = _get_ready_q_q_index(thread->base.prio);
465 sys_dlist_t *q = &_ready_q.q[q_index];
466
467 return sys_dlist_has_multiple_nodes(q);
468}
469
470/* Must be called with interrupts locked */
471/* Should be called only immediately before a thread switch */
472void _update_time_slice_before_swap(void)
473{
Andrew Boie3989de72017-05-30 12:51:39 -0700474#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800475 if (!_is_thread_time_slicing(_get_next_ready_thread())) {
476 return;
477 }
478
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800479 u32_t remaining = _get_remaining_program_time();
480
481 if (!remaining || (_time_slice_duration < remaining)) {
482 _set_time(_time_slice_duration);
Youvedeep Singhf807d4d2017-07-18 16:13:16 +0530483 } else {
484 /* Account previous elapsed time and reprogram
485 * timer with remaining time
486 */
487 _set_time(remaining);
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800488 }
Youvedeep Singhf807d4d2017-07-18 16:13:16 +0530489
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800490#endif
Andrew Boie3989de72017-05-30 12:51:39 -0700491 /* Restart time slice count at new thread switch */
492 _time_slice_elapsed = 0;
493}
Peter Mitsis68d1f4b2016-09-12 11:35:26 -0400494#endif /* CONFIG_TIMESLICING */
Benjamin Walsh445830d2016-11-10 15:54:27 -0500495
Andrew Boie468190a2017-09-29 14:00:48 -0700496int _impl_k_is_preempt_thread(void)
Benjamin Walsh445830d2016-11-10 15:54:27 -0500497{
498 return !_is_in_isr() && _is_preempt(_current);
499}
Andrew Boie468190a2017-09-29 14:00:48 -0700500
501#ifdef CONFIG_USERSPACE
Andrew Boie225e4c02017-10-12 09:54:26 -0700502_SYSCALL_HANDLER0_SIMPLE(k_is_preempt_thread);
Andrew Boie468190a2017-09-29 14:00:48 -0700503#endif