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"
6
10#include <mos/syslog/printk.h>
12#include <mos/tasks/thread.h>
13#include <mos/tasks/wait.h>
14#include <mos_stdlib.h>
15#include <mos_string.h>
16
19
22
24{
25 memzero(list, sizeof(waitlist_t));
26 linked_list_init(&list->list);
27}
28
30{
31 spinlock_acquire(&list->lock);
32 if (list->closed)
33 {
34 spinlock_release(&list->lock);
35 return false;
36 }
37
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
45size_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);
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)
66 }
67 kfree(entry);
68 wakeups++;
69 }
70
71 spinlock_release(&list->lock);
72
73 return wakeups;
74}
75
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
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}
MOSAPI void linked_list_init(list_node_t *head_node)
Initialise a circular double linked list.
Definition list.c:15
MOSAPI list_node_t * list_node_pop(list_node_t *head)
Definition list.c:56
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_entry(node, type)
Get the container struct of a list node.
Definition list.h:46
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.c:21
#define list_remove(element)
Definition list.h:74
#define NULL
Definition pb_syshdr.h:46
#define current_thread
Definition platform.h:30
@ THREAD_STATE_BLOCKED
thread is blocked by a wait condition
Definition platform.h:65
#define pr_warn(fmt,...)
Definition printk.h:38
#define memzero(ptr, size)
Definition rpc_client.c:40
void scheduler_wake_thread(thread_t *thread)
Wake a thread.
Definition schedule.c:91
#define SLAB_AUTOINIT(name, var, type)
#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
Definition slab.h:45
thread_state_t state
thread state
Definition task_types.h:83
The entry in the waiters list of a process, or a thread.
Definition wait.h:15
tid_t waiter
Definition wait.h:17
bool closed
Definition wait.h:22
spinlock_t lock
Definition wait.h:23
list_head list
Definition wait.h:24
thread_t * thread_get(tid_t id)
Definition thread.c:166
void waitlist_init(waitlist_t *list)
Definition wait.c:23
size_t waitlist_wake(waitlist_t *list, size_t max_wakeups)
Definition wait.c:45
slab_t * waitlist_slab
Definition wait.c:17
bool waitlist_append(waitlist_t *list)
Definition wait.c:29
void waitlist_remove_me(waitlist_t *waitlist)
Definition wait.c:86
void waitlist_close(waitlist_t *list)
Definition wait.c:76
static slab_t * waitlist_listentry_slab
Definition wait.c:20