26#define WAIT_ALL UINT64_MAX
37#define WAIT_BLOCK(waitQueue, condition) \
39 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
40 uint64_t result = 0; \
41 while (!(condition) && result == 0) \
43 wait_queue_t* temp = waitQueue; \
44 if (wait_block_setup(&temp, 1, CLOCKS_NEVER) == ERR) \
49 result = wait_block_commit(); \
64#define WAIT_BLOCK_TIMEOUT(waitQueue, condition, timeout) \
66 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
67 uint64_t result = 0; \
68 clock_t uptime = timer_uptime(); \
69 clock_t deadline = (timeout) == CLOCKS_NEVER ? CLOCKS_NEVER : (timeout) + uptime; \
70 while (!(condition) && result == 0) \
72 if (deadline <= uptime) \
78 clock_t remaining = deadline == CLOCKS_NEVER ? CLOCKS_NEVER : (deadline > uptime ? deadline - uptime : 0); \
79 wait_queue_t* temp = waitQueue; \
80 if (wait_block_setup(&temp, 1, remaining) == ERR) \
85 result = wait_block_commit(); \
86 uptime = timer_uptime(); \
101#define WAIT_BLOCK_LOCK(waitQueue, lock, condition) \
103 assert(!(rflags_read() & RFLAGS_INTERRUPT_ENABLE)); \
104 uint64_t result = 0; \
105 while (!(condition) && result == 0) \
107 wait_queue_t* temp = waitQueue; \
108 if (wait_block_setup(&temp, 1, CLOCKS_NEVER) == ERR) \
113 lock_release(lock); \
114 result = wait_block_commit(); \
115 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
116 lock_acquire(lock); \
132#define WAIT_BLOCK_LOCK_TIMEOUT(waitQueue, lock, condition, timeout) \
134 uint64_t result = 0; \
135 clock_t uptime = timer_uptime(); \
136 clock_t deadline = (timeout) == CLOCKS_NEVER ? CLOCKS_NEVER : (timeout) + uptime; \
137 while (!(condition) && result == ERR) \
139 if (deadline <= uptime) \
145 clock_t remaining = deadline == CLOCKS_NEVER ? CLOCKS_NEVER : (deadline > uptime ? deadline - uptime : 0); \
146 wait_queue_t* temp = waitQueue; \
147 if (wait_block_setup(&temp, 1, remaining) == ERR) \
152 lock_release(lock); \
153 result = wait_block_commit(); \
154 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
155 lock_acquire(lock); \
156 uptime = timer_uptime(); \
165typedef struct wait_queue
178typedef struct wait_entry
220#define WAIT_QUEUE_CREATE(name) {.lock = LOCK_CREATE, .entries = LIST_CREATE(name.entries)}
uint64_t wait_block_commit(void)
Block the currently running thread.
bool wait_block_finalize(interrupt_frame_t *frame, cpu_t *self, thread_t *thread, clock_t uptime)
Finalize blocking of a thread.
uint64_t wait_block_setup(wait_queue_t **waitQueues, uint64_t amount, clock_t timeout)
Setup blocking but dont block yet.
void wait_block_cancel(void)
Cancel blocking.
uint64_t wait_unblock(wait_queue_t *waitQueue, uint64_t amount, errno_t err)
Unblock threads waiting on a wait queue.
void wait_queue_init(wait_queue_t *waitQueue)
Initialize wait queue.
void wait_cpu_ctx_init(wait_cpu_ctx_t *wait, cpu_t *self)
Initialize per-CPU wait context.
void wait_thread_ctx_init(wait_thread_ctx_t *wait)
Initialize per-thread wait context.
void wait_queue_deinit(wait_queue_t *waitQueue)
Deinitialize wait queue.
void wait_unblock_thread(thread_t *thread, errno_t err)
Unblock a specific thread.
clock_t uptime(void)
System call for retreving the time since boot.
__UINT64_TYPE__ clock_t
A nanosecond time.
A entry in a doubly linked list.
A simple ticket lock implementation.
Thread of execution structure.
list_t blockedThreads
List of blocked threads, sorted by deadline.
cpu_t * cpu
The CPU this context belongs to.
wait_queue_t * waitQueue
The wait queue the thread is waiting on.
thread_t * thread
The thread that is waiting.
list_entry_t queueEntry
Used in wait_queue_t->entries.
list_entry_t threadEntry
Used in wait_thread_ctx_t->entries.
list_t entries
List of wait entries for threads waiting on this queue.
wait_cpu_ctx_t * cpu
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.
list_t entries
List of wait entries, one for each wait queue the thread is waiting on.