50 panic(
NULL,
"Wait queue with pending threads freed");
105 if (waitQueues ==
NULL || amount == 0)
116 for (
uint64_t i = 0; i < amount; i++)
121 for (
uint64_t i = 0; i < amount; i++)
137 for (
uint64_t j = 0; j < amount; j++)
148 entry->
queue = waitQueues[i];
159 for (
uint64_t i = 0; i < amount; i++)
169 panic(
NULL,
"Thread in invalid state in wait_block_prepare() state=%d", expected);
218 panic(
NULL,
"Invalid thread state %d in wait_block_commit()", state);
300 const uint64_t threadsPerBatch = 64;
307 uint64_t toUnblock = amount < threadsPerBatch ? amount : threadsPerBatch;
316 if (collected == toUnblock)
328 threads[collected] = thread;
340 for (
uint64_t i = 0; i < collected; i++)
352 return amountUnblocked;
#define assert(expression)
void ipi_invoke(void)
Invoke a IPI interrupt on the current CPU.
static cpu_t * cpu_get_unsafe(void)
Gets the current CPU structure without disabling interrupts.
static cpu_t * cpu_get(void)
Gets the current CPU structure.
static void cpu_put(void)
Releases the current CPU structure.
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
bool thread_is_note_pending(thread_t *thread)
Check if a thread has a note pending.
thread_state_t
Thread state enum.
@ THREAD_UNBLOCKING
Has started unblocking, used to prevent the same thread being unblocked multiple times.
@ THREAD_PRE_BLOCK
Has started the process of blocking but has not yet been given to a owner cpu.
@ THREAD_BLOCKED
Is blocking and waiting in one or multiple wait queues.
@ THREAD_ACTIVE
Is either running or ready to run.
uint64_t wait_block_prepare(wait_queue_t **waitQueues, uint64_t amount, clock_t timeout)
Prepare to block the currently running thread.
uint64_t wait_unblock(wait_queue_t *queue, uint64_t amount, errno_t err)
Unblock threads waiting on a wait queue.
uint64_t wait_block_commit(void)
Block the currently running thread.
void wait_init(wait_t *wait)
Initialize an instance of the waiting subsystem.
bool wait_block_finalize(interrupt_frame_t *frame, cpu_t *self, thread_t *thread, clock_t uptime)
Finalize blocking of a thread.
void wait_block_cancel(void)
Cancels blocking of the currently running thread.
void wait_queue_deinit(wait_queue_t *queue)
Deinitialize wait queue.
void wait_check_timeouts(interrupt_frame_t *frame, cpu_t *self)
Check for timeouts and unblock threads as needed.
void wait_queue_init(wait_queue_t *queue)
Initialize wait queue.
void wait_client_init(wait_client_t *client)
Initialize a threads wait client.
void wait_unblock_thread(thread_t *thread, errno_t err)
Unblock a specific thread.
void sched_submit(thread_t *thread)
Submits a thread to the scheduler.
static void lock_init(lock_t *lock)
Initializes a lock.
#define LOCK_SCOPE(lock)
Acquires a lock for the reminder of the current scope.
static void lock_release(lock_t *lock)
Releases a lock.
static void lock_acquire(lock_t *lock)
Acquires a lock, blocking until it is available.
clock_t sys_time_uptime(void)
Time since boot.
void timer_set(clock_t uptime, clock_t deadline)
Schedule a one-shot timer interrupt on the current CPU.
#define EINVAL
Invalid argument.
#define EINTR
Interrupted system call.
#define ETIMEDOUT
Connection timed out.
#define ENOMEM
Out of memory.
#define errno
Error number variable.
#define LIST_FOR_EACH(elem, list, member)
Iterates over a list.
static list_entry_t * list_first(list_t *list)
Gets the first entry in the list without removing it.
static list_entry_t * list_pop_first(list_t *list)
Pops the first entry from the list.
static list_entry_t * list_last(list_t *list)
Gets the last entry in the list without removing it.
static void list_push_back(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
static void list_prepend(list_t *list, list_entry_t *head, list_entry_t *entry)
Prepends an entry to the list.
#define LIST_FOR_EACH_SAFE(elem, temp, list, member)
Safely iterates over a list, allowing for element removal during iteration.
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
static bool list_is_empty(list_t *list)
Checks if a list is empty.
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
static void list_init(list_t *list)
Initializes a list.
clock_t uptime(void)
System call for retreving the time since boot.
#define NULL
Pointer error value.
#define ERR
Integer error value.
#define CONTAINER_OF(ptr, type, member)
Container of macro.
#define CONTAINER_OF_SAFE(ptr, type, member)
Safe container of macro.
#define CLOCKS_DEADLINE(timeout, uptime)
Safely calculate deadline from timeout.
__UINT64_TYPE__ clock_t
A nanosecond time.
#define RFLAGS_INTERRUPT_ENABLE
static uint64_t rflags_read()
#define atomic_store(object, desired)
#define atomic_compare_exchange_strong(object, expected, desired)
#define atomic_exchange(object, desired)
#define atomic_load(object)
_PUBLIC void * malloc(size_t size)
_PUBLIC void free(void *ptr)
thread_t *volatile runThread
The currently running thread on this CPU.
Thread of execution structure.
Represents a thread in the waiting subsystem.
list_t entries
List of wait entries, one for each wait queue the thread is waiting on.
wait_t * owner
The wait cpu context of the cpu the thread is blocked on.
clock_t deadline
Deadline for timeout, CLOCKS_NEVER for no timeout.
errno_t err
Error number set when unblocking the thread, EOK for no error.
Represents a thread waiting on a wait queue.
thread_t * thread
The thread that is waiting.
wait_queue_t * queue
The wait queue the thread is waiting on.
list_entry_t queueEntry
Used in wait_queue_t->entries.
list_entry_t threadEntry
Used in wait_client_t->entries.
The primitive that threads block on.
list_t entries
List of wait entries for threads waiting on this queue.
Represents one instance of the waiting subsystem for a CPU.
list_t blockedThreads
List of blocked threads, sorted by deadline.
static void wait_remove_wait_entries(thread_t *thread, errno_t err)