MOS Source Code
Loading...
Searching...
No Matches
wait.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4
10#include <mos/tasks/thread.hpp>
11#include <mos/tasks/wait.hpp>
12#include <mos_stdlib.hpp>
13#include <mos_string.hpp>
14
16{
17 memzero(list, sizeof(waitlist_t));
18 linked_list_init(&list->list);
19}
20
22{
23 spinlock_acquire(&list->lock);
24 if (list->closed)
25 {
26 spinlock_release(&list->lock);
27 return false;
28 }
29
31 entry->waiter = current_thread->tid;
32 list_node_append(&list->list, list_node(entry));
33 spinlock_release(&list->lock);
34 return true;
35}
36
37size_t waitlist_wake(waitlist_t *list, size_t max_wakeups)
38{
39 spinlock_acquire(&list->lock);
40
41 if (list_is_empty(&list->list))
42 {
43 spinlock_release(&list->lock);
44 return 0;
45 }
46
47 size_t wakeups = 0;
48 while (wakeups < max_wakeups && !list_is_empty(&list->list))
49 {
50 list_node_t *node = list_node_pop(&list->list);
52
53 Thread *thread = thread_get(entry->waiter);
54 if (thread) // if the thread is still there
55 {
56 if (thread->state == THREAD_STATE_BLOCKED)
58 }
59 delete entry;
60 wakeups++;
61 }
62
63 spinlock_release(&list->lock);
64
65 return wakeups;
66}
67
69{
70 spinlock_acquire(&list->lock);
71 if (list->closed)
72 pr_warn("waitlist already closed");
73
74 list->closed = true;
75 spinlock_release(&list->lock);
76}
77
79{
80 spinlock_acquire(&waitlist->lock);
81
82 list_foreach(waitable_list_entry_t, entry, waitlist->list)
83 {
84 if (entry->waiter == current_thread->tid)
85 {
86 list_remove(entry);
87 delete entry;
88 break;
89 }
90 }
91
92 spinlock_release(&waitlist->lock);
93}
94
MOSAPI void linked_list_init(list_node_t *head_node)
Initialise a circular double linked list.
Definition list.cpp:15
MOSAPI list_node_t * list_node_pop(list_node_t *head)
Definition list.cpp:56
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
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.cpp:21
#define list_remove(element)
Definition list.hpp:80
T * create(Args &&...args)
Definition allocator.hpp:10
#define current_thread
Definition platform.hpp:32
@ THREAD_STATE_BLOCKED
thread is blocked by a wait condition
Definition platform.hpp:70
#define pr_warn(fmt,...)
Definition printk.hpp:38
#define memzero(ptr, size)
void scheduler_wake_thread(Thread *thread)
Wake a thread.
Definition schedule.cpp:91
#define spinlock_acquire(lock)
Definition spinlock.hpp:64
#define spinlock_release(lock)
Definition spinlock.hpp:65
thread_state_t state
thread state
The entry in the waiters list of a process, or a thread.
Definition wait.hpp:14
tid_t waiter
Definition wait.hpp:16
bool closed
Definition wait.hpp:21
waitlist_t()
Definition wait.cpp:95
spinlock_t lock
Definition wait.hpp:22
list_head list
Definition wait.hpp:23
Thread * thread_get(tid_t id)
Definition thread.cpp:174
void waitlist_init(waitlist_t *list)
Definition wait.cpp:15
size_t waitlist_wake(waitlist_t *list, size_t max_wakeups)
Definition wait.cpp:37
bool waitlist_append(waitlist_t *list)
Definition wait.cpp:21
void waitlist_remove_me(waitlist_t *waitlist)
Definition wait.cpp:78
void waitlist_close(waitlist_t *list)
Definition wait.cpp:68
void waitlist_init(waitlist_t *list)
Definition wait.cpp:15