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