| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
|---|---|
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <mos/allocator.hpp> |
| 6 | #include <mos/lib/structures/list.hpp> |
| 7 | #include <mos/lib/sync/spinlock.hpp> |
| 8 | #include <mos/mos_global.h> |
| 9 | |
| 10 | /** |
| 11 | * @brief The entry in the waiters list of a process, or a thread |
| 12 | */ |
| 13 | struct waitable_list_entry_t : mos::NamedType<"WaitlistEntry"> |
| 14 | { |
| 15 | as_linked_list; |
| 16 | tid_t waiter; |
| 17 | }; |
| 18 | |
| 19 | struct waitlist_t : mos::NamedType<"Waitlist"> |
| 20 | { |
| 21 | bool closed = false; // if true, then the process is closed and should not be waited on |
| 22 | spinlock_t lock = SPINLOCK_INIT; // protects the waiters list |
| 23 | list_head list; // list of threads waiting |
| 24 | waitlist_t(); |
| 25 | }; |
| 26 | |
| 27 | void waitlist_init(waitlist_t *list); |
| 28 | __nodiscard bool waitlist_append(waitlist_t *list); |
| 29 | size_t waitlist_wake(waitlist_t *list, size_t max_wakeups); |
| 30 | void waitlist_close(waitlist_t *list); |
| 31 | void waitlist_remove_me(waitlist_t *waitlist); |
| 32 | |
| 33 | #define waitlist_wake_one(list) waitlist_wake(list, 1) |
| 34 | #define waitlist_wake_all(list) waitlist_wake(list, SIZE_MAX) |
| 35 |