63 panic(
NULL,
"Wait queue with pending threads freed");
113 if (waitQueues ==
NULL || amount == 0)
124 for (
uint64_t i = 0; i < amount; i++)
129 for (
uint64_t i = 0; i < amount; i++)
145 for (
uint64_t j = 0; j < amount; j++)
156 entry->
queue = waitQueues[i];
167 for (
uint64_t i = 0; i < amount; i++)
177 panic(
NULL,
"Thread in invalid state in wait_block_prepare() state=%d", expected);
227 panic(
NULL,
"Invalid thread state %d in wait_block_commit()", state);
311 const uint64_t threadsPerBatch = 64;
318 uint64_t toUnblock = amount < threadsPerBatch ? amount : threadsPerBatch;
327 if (collected == toUnblock)
339 threads[collected] = thread;
351 for (
uint64_t i = 0; i < collected; i++)
363 return amountUnblocked;
#define assert(expression)
static void cli_pop(void)
Decrements the CLI depth, re-enabling interrupts if depth reaches zero and interrupts were enabled pr...
static void cli_push(void)
Increments the CLI depth, disabling interrupts if depth was zero.
void ipi_invoke(void)
Invoke a IPI interrupt on the current CPU.
#define SELF_PTR(ptr)
Macro to get a pointer to a percpu variable on the current CPU.
#define PERCPU_DEFINE_CTOR(type, name)
Macro to define a percpu variable with a constructor.
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
void cache_free(void *obj)
Free an object back to its cache.
#define CACHE_LINE
Cache line size in bytes.
void * cache_alloc(cache_t *cache)
Allocate an object from the cache.
#define CACHE_CREATE(_cache, _name, _size, _alignment, _ctor, _dtor)
Macro to create a cache initializer.
clock_t clock_uptime(void)
Retrieve the time in nanoseconds since boot.
bool thread_is_note_pending(thread_t *thread)
Check if a thread has a note pending.
static thread_t * thread_current_unsafe(void)
Retrieves the currently running thread without disabling interrupts.
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_block_cancel(void)
Cancels blocking of the currently running thread.
void wait_queue_deinit(wait_queue_t *queue)
Deinitialize wait queue.
void wait_queue_init(wait_queue_t *queue)
Initialize wait queue.
void wait_client_init(wait_client_t *client)
Initialize a threads wait client.
bool wait_block_finalize(interrupt_frame_t *frame, thread_t *thread, clock_t uptime)
Finalize blocking of a thread.
void wait_check_timeouts(interrupt_frame_t *frame)
Check for timeouts and unblock threads as needed.
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.
void timer_set(clock_t now, 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 UNUSED(x)
Mark a variable as unused.
#define LIST_FOR_EACH(elem, list, member)
Iterates over a list.
static void list_remove(list_entry_t *entry)
Removes a list entry from its current 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_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_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 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 list_entry_t * list_pop_front(list_t *list)
Pops the first entry from the list.
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(void)
#define atomic_store(object, desired)
#define atomic_compare_exchange_strong(object, expected, desired)
#define atomic_exchange(object, desired)
#define atomic_load(object)
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)
static cache_t waitEntryCache