MOS Source Code
Loading...
Searching...
No Matches
wait.c
Go to the documentation of this file.
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#include "
mos/mm/slab.h
"
4
#include "
mos/mm/slab_autoinit.h
"
5
#include "
mos/tasks/schedule.h
"
6
7
#include <
mos/lib/structures/list.h
>
8
#include <
mos/lib/sync/spinlock.h
>
9
#include <
mos/platform/platform.h
>
10
#include <
mos/syslog/printk.h
>
11
#include <
mos/tasks/task_types.h
>
12
#include <
mos/tasks/thread.h
>
13
#include <
mos/tasks/wait.h
>
14
#include <
mos_stdlib.h
>
15
#include <
mos_string.h
>
16
17
slab_t
*
waitlist_slab
=
NULL
;
18
SLAB_AUTOINIT
(
"waitlist"
,
waitlist_slab
,
waitlist_t
);
19
20
static
slab_t
*
waitlist_listentry_slab
=
NULL
;
21
SLAB_AUTOINIT
(
"waitlist_entry"
,
waitlist_listentry_slab
,
waitable_list_entry_t
);
22
23
void
waitlist_init
(
waitlist_t
*list)
24
{
25
memzero
(list,
sizeof
(
waitlist_t
));
26
linked_list_init
(&list->
list
);
27
}
28
29
bool
waitlist_append
(
waitlist_t
*list)
30
{
31
spinlock_acquire
(&list->
lock
);
32
if
(list->
closed
)
33
{
34
spinlock_release
(&list->
lock
);
35
return
false
;
36
}
37
38
waitable_list_entry_t
*entry = kmalloc(
waitlist_listentry_slab
);
39
entry->
waiter
=
current_thread
->tid;
40
list_node_append
(&list->
list
,
list_node
(entry));
41
spinlock_release
(&list->
lock
);
42
return
true
;
43
}
44
45
size_t
waitlist_wake
(
waitlist_t
*list,
size_t
max_wakeups)
46
{
47
spinlock_acquire
(&list->
lock
);
48
49
if
(
list_is_empty
(&list->
list
))
50
{
51
spinlock_release
(&list->
lock
);
52
return
0;
53
}
54
55
size_t
wakeups = 0;
56
while
(wakeups < max_wakeups && !
list_is_empty
(&list->
list
))
57
{
58
list_node_t *node =
list_node_pop
(&list->
list
);
59
waitable_list_entry_t
*entry =
list_entry
(node,
waitable_list_entry_t
);
60
61
thread_t
*thread =
thread_get
(entry->
waiter
);
62
if
(thread)
// if the thread is still there
63
{
64
if
(thread->
state
==
THREAD_STATE_BLOCKED
)
65
scheduler_wake_thread
(thread);
66
}
67
kfree(entry);
68
wakeups++;
69
}
70
71
spinlock_release
(&list->
lock
);
72
73
return
wakeups;
74
}
75
76
void
waitlist_close
(
waitlist_t
*list)
77
{
78
spinlock_acquire
(&list->
lock
);
79
if
(list->
closed
)
80
pr_warn
(
"waitlist already closed"
);
81
82
list->
closed
=
true
;
83
spinlock_release
(&list->
lock
);
84
}
85
86
void
waitlist_remove_me
(
waitlist_t
*waitlist)
87
{
88
spinlock_acquire
(&waitlist->
lock
);
89
90
list_foreach
(
waitable_list_entry_t
, entry, waitlist->
list
)
91
{
92
if
(entry->waiter ==
current_thread
->tid)
93
{
94
list_remove
(entry);
95
kfree(entry);
96
break
;
97
}
98
}
99
100
spinlock_release
(&waitlist->
lock
);
101
}
linked_list_init
MOSAPI void linked_list_init(list_node_t *head_node)
Initialise a circular double linked list.
Definition
list.c:15
list_node_pop
MOSAPI list_node_t * list_node_pop(list_node_t *head)
Definition
list.c:56
list_node_append
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition
list.c:68
list_foreach
#define list_foreach(t, v, h)
Iterate over a list.
Definition
list.h:83
list_entry
#define list_entry(node, type)
Get the container struct of a list node.
Definition
list.h:46
list_is_empty
MOSAPI bool list_is_empty(const list_node_t *head)
Definition
list.c:21
list_remove
#define list_remove(element)
Definition
list.h:74
list.h
mos_stdlib.h
mos_string.h
NULL
#define NULL
Definition
pb_syshdr.h:46
platform.h
current_thread
#define current_thread
Definition
platform.h:30
THREAD_STATE_BLOCKED
@ THREAD_STATE_BLOCKED
thread is blocked by a wait condition
Definition
platform.h:65
printk.h
pr_warn
#define pr_warn(fmt,...)
Definition
printk.h:38
memzero
#define memzero(ptr, size)
Definition
rpc_client.c:40
schedule.h
scheduler_wake_thread
void scheduler_wake_thread(thread_t *thread)
Wake a thread.
Definition
schedule.c:91
slab.h
slab_autoinit.h
SLAB_AUTOINIT
#define SLAB_AUTOINIT(name, var, type)
Definition
slab_autoinit.h:14
spinlock.h
spinlock_acquire
#define spinlock_acquire(lock)
Definition
spinlock.h:61
spinlock_release
#define spinlock_release(lock)
Definition
spinlock.h:62
list_node
A node in a linked list.
Definition
list.h:27
slab_t
Definition
slab.h:45
thread_t
Definition
task_types.h:75
thread_t::state
thread_state_t state
thread state
Definition
task_types.h:83
waitable_list_entry_t
The entry in the waiters list of a process, or a thread.
Definition
wait.h:15
waitable_list_entry_t::waiter
tid_t waiter
Definition
wait.h:17
waitlist_t
Definition
wait.h:21
waitlist_t::closed
bool closed
Definition
wait.h:22
waitlist_t::lock
spinlock_t lock
Definition
wait.h:23
waitlist_t::list
list_head list
Definition
wait.h:24
task_types.h
thread.h
thread_get
thread_t * thread_get(tid_t id)
Definition
thread.c:166
waitlist_init
void waitlist_init(waitlist_t *list)
Definition
wait.c:23
waitlist_wake
size_t waitlist_wake(waitlist_t *list, size_t max_wakeups)
Definition
wait.c:45
waitlist_slab
slab_t * waitlist_slab
Definition
wait.c:17
waitlist_append
bool waitlist_append(waitlist_t *list)
Definition
wait.c:29
waitlist_remove_me
void waitlist_remove_me(waitlist_t *waitlist)
Definition
wait.c:86
waitlist_close
void waitlist_close(waitlist_t *list)
Definition
wait.c:76
waitlist_listentry_slab
static slab_t * waitlist_listentry_slab
Definition
wait.c:20
wait.h
kernel
tasks
wait.c
Generated on Sun Sep 1 2024 18:22:52 for MOS Source Code by
1.12.0