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
3
#include "
mos/tasks/schedule.hpp
"
4
5
#include <
mos/lib/structures/list.hpp
>
6
#include <
mos/lib/sync/spinlock.hpp
>
7
#include <
mos/platform/platform.hpp
>
8
#include <
mos/syslog/printk.hpp
>
9
#include <
mos/tasks/task_types.hpp
>
10
#include <
mos/tasks/thread.hpp
>
11
#include <
mos/tasks/wait.hpp
>
12
#include <
mos_stdlib.hpp
>
13
#include <
mos_string.hpp
>
14
15
void
waitlist_init
(
waitlist_t
*list)
16
{
17
memzero
(list,
sizeof
(
waitlist_t
));
18
linked_list_init
(&list->
list
);
19
}
20
21
bool
waitlist_append
(
waitlist_t
*list)
22
{
23
spinlock_acquire
(&list->
lock
);
24
if
(list->
closed
)
25
{
26
spinlock_release
(&list->
lock
);
27
return
false
;
28
}
29
30
waitable_list_entry_t
*entry =
mos::create<waitable_list_entry_t>
();
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
37
size_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
);
51
waitable_list_entry_t
*entry =
list_entry
(node,
waitable_list_entry_t
);
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
)
57
scheduler_wake_thread
(thread);
58
}
59
delete
entry;
60
wakeups++;
61
}
62
63
spinlock_release
(&list->
lock
);
64
65
return
wakeups;
66
}
67
68
void
waitlist_close
(
waitlist_t
*list)
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
78
void
waitlist_remove_me
(
waitlist_t
*waitlist)
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
95
waitlist_t::waitlist_t
()
96
{
97
waitlist_init
(
this
);
98
}
linked_list_init
MOSAPI void linked_list_init(list_node_t *head_node)
Initialise a circular double linked list.
Definition
list.cpp:15
list_node_pop
MOSAPI list_node_t * list_node_pop(list_node_t *head)
Definition
list.cpp:56
list_node_append
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition
list.cpp:68
list_foreach
#define list_foreach(t, v, h)
Iterate over a list.
Definition
list.hpp:89
list_node
#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
list_entry
#define list_entry(node, type)
Get the container struct of a list node.
Definition
list.hpp:52
list_is_empty
MOSAPI bool list_is_empty(const list_node_t *head)
Definition
list.cpp:21
list_remove
#define list_remove(element)
Definition
list.hpp:80
mos_stdlib.hpp
mos_string.hpp
mos::create
T * create(Args &&...args)
Definition
allocator.hpp:10
platform.hpp
current_thread
#define current_thread
Definition
platform.hpp:32
THREAD_STATE_BLOCKED
@ THREAD_STATE_BLOCKED
thread is blocked by a wait condition
Definition
platform.hpp:70
printk.hpp
pr_warn
#define pr_warn(fmt,...)
Definition
printk.hpp:38
list.hpp
memzero
#define memzero(ptr, size)
Definition
rpc_server.cpp:48
schedule.hpp
scheduler_wake_thread
void scheduler_wake_thread(Thread *thread)
Wake a thread.
Definition
schedule.cpp:91
spinlock.hpp
spinlock_acquire
#define spinlock_acquire(lock)
Definition
spinlock.hpp:64
spinlock_release
#define spinlock_release(lock)
Definition
spinlock.hpp:65
Thread
Definition
task_types.hpp:94
Thread::state
thread_state_t state
thread state
Definition
task_types.hpp:102
waitable_list_entry_t
The entry in the waiters list of a process, or a thread.
Definition
wait.hpp:14
waitable_list_entry_t::waiter
tid_t waiter
Definition
wait.hpp:16
waitlist_t
Definition
wait.hpp:20
waitlist_t::closed
bool closed
Definition
wait.hpp:21
waitlist_t::waitlist_t
waitlist_t()
Definition
wait.cpp:95
waitlist_t::lock
spinlock_t lock
Definition
wait.hpp:22
waitlist_t::list
list_head list
Definition
wait.hpp:23
task_types.hpp
thread.hpp
thread_get
Thread * thread_get(tid_t id)
Definition
thread.cpp:174
waitlist_init
void waitlist_init(waitlist_t *list)
Definition
wait.cpp:15
waitlist_wake
size_t waitlist_wake(waitlist_t *list, size_t max_wakeups)
Definition
wait.cpp:37
waitlist_append
bool waitlist_append(waitlist_t *list)
Definition
wait.cpp:21
waitlist_remove_me
void waitlist_remove_me(waitlist_t *waitlist)
Definition
wait.cpp:78
waitlist_close
void waitlist_close(waitlist_t *list)
Definition
wait.cpp:68
wait.hpp
waitlist_init
void waitlist_init(waitlist_t *list)
Definition
wait.cpp:15
kernel
tasks
wait.cpp
Generated on Tue Feb 18 2025 16:41:40 for MOS Source Code by
1.13.2