MOS Source Code
Loading...
Searching...
No Matches
naive.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
7
8#include <mos/allocator.hpp>
9#include <mos_stdlib.hpp>
10
17
18struct naive_sched_node_t : mos::NamedType<"NaiveSched.Node">
19{
22};
23
24static void naive_sched_init(scheduler_t *instance)
25{
26 naive_sched_t *scheduler = container_of(instance, naive_sched_t, base);
27 spinlock_init(&scheduler->lock);
28 linked_list_init(&scheduler->threads);
29 pr_dinfo2(naive_sched, "naive scheduler initialized");
30}
31
33{
34 naive_sched_t *scheduler = container_of(instance, naive_sched_t, base);
35
36 spinlock_acquire(&scheduler->lock);
37 if (list_is_empty(&scheduler->threads))
38 {
39 spinlock_release(&scheduler->lock);
40 pr_dinfo(naive_sched, "no threads to run");
41 return NULL;
42 }
43
45 list_remove(node);
46 spinlock_release(&scheduler->lock);
47
48 Thread *thread = node->thread;
49 delete node;
50
51 MOS_ASSERT_X(thread != current_thread, "current thread queued in scheduler");
53
54 pr_dinfo2(naive_sched, "naive scheduler selected thread %pt", thread);
55 return thread;
56}
57
58static void naive_sched_add_thread(scheduler_t *instance, Thread *thread)
59{
60 naive_sched_t *scheduler = container_of(instance, naive_sched_t, base);
61
62 pr_dinfo(naive_sched, "adding thread %pt to scheduler", thread);
63
66 node->thread = thread;
67
68 spinlock_acquire(&scheduler->lock);
69 list_node_append(&scheduler->threads, list_node(node));
70 spinlock_release(&scheduler->lock);
71}
72
73static void naive_sched_remove_thread(scheduler_t *instance, Thread *thread)
74{
75 pr_dinfo2(naive_sched, "naive scheduler removed thread %pt", thread);
76
77 naive_sched_t *scheduler = container_of(instance, naive_sched_t, base);
78 spinlock_acquire(&scheduler->lock);
79 list_foreach(naive_sched_node_t, node, scheduler->threads)
80 {
81 if (node->thread == thread)
82 {
83 list_remove(node);
84 delete node;
85 break;
86 }
87 }
88 spinlock_release(&scheduler->lock);
89}
90
92 .init = naive_sched_init,
93 .select_next = naive_sched_select_next,
94 .add_thread = naive_sched_add_thread,
95 .remove_thread = naive_sched_remove_thread,
96};
97
99 .base = { .ops = &naive_sched_ops },
100};
101
#define MOS_ASSERT_X(cond, msg,...)
Definition assert.hpp:15
MOSAPI void linked_list_init(list_node_t *head_node)
Initialise a circular double linked list.
Definition list.cpp:15
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition list.cpp:68
#define list_foreach(t, v, h)
Iterate over a list.
Definition list.hpp:89
#define list_node(element)
Get the ‘list_node’ of a list element. This is exactly the reverse of ‘list_entry’ above.
Definition list.hpp:74
#define list_entry(node, type)
Get the container struct of a list node.
Definition list.hpp:52
list_node_t list_head
A linked list head.
Definition list.hpp:23
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.cpp:21
#define list_remove(element)
Definition list.hpp:80
static const scheduler_ops_t naive_sched_ops
Definition naive.cpp:91
static Thread * naive_sched_select_next(scheduler_t *instance)
Definition naive.cpp:32
static naive_sched_t naive_sched
Definition naive.cpp:98
static void naive_sched_remove_thread(scheduler_t *instance, Thread *thread)
Definition naive.cpp:73
static void naive_sched_add_thread(scheduler_t *instance, Thread *thread)
Definition naive.cpp:58
static void naive_sched_init(scheduler_t *instance)
Definition naive.cpp:24
T * create(Args &&...args)
Definition allocator.hpp:10
#define NULL
Definition pb_syshdr.h:46
#define current_thread
Definition platform.hpp:32
#define pr_dinfo(feat, fmt,...)
Definition printk.hpp:28
#define pr_dinfo2(feat, fmt,...)
Definition printk.hpp:27
#define MOS_SCHEDULER(_name, _i)
Definition scheduler.hpp:34
#define spinlock_init(lock)
Definition spinlock.hpp:24
#define spinlock_acquire(lock)
Definition spinlock.hpp:64
#define spinlock_release(lock)
Definition spinlock.hpp:65
spinlock_t state_lock
protects the thread state
Node in the naive scheduler's list of threads.
Definition naive.cpp:19
Thread * thread
Definition naive.cpp:21
scheduler_t base
Definition naive.cpp:13
spinlock_t lock
Definition naive.cpp:15
list_head threads
list of runnable threads
Definition naive.cpp:14
#define container_of(ptr, type, member)
Definition types.hpp:31