40#define WAIT_ALL UINT64_MAX
48#define WAIT_BLOCK(queue, condition) \
50 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
51 uint64_t result = 0; \
52 while (!(condition) && result == 0) \
54 wait_queue_t* temp = queue; \
55 if (wait_block_prepare(&temp, 1, CLOCKS_NEVER) == ERR) \
60 result = wait_block_commit(); \
72#define WAIT_BLOCK_TIMEOUT(queue, condition, timeout) \
74 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
75 uint64_t result = 0; \
76 clock_t uptime = sys_time_uptime(); \
77 clock_t deadline = CLOCKS_DEADLINE(timeout, uptime); \
78 while (!(condition) && result == 0) \
80 if (deadline <= uptime) \
86 clock_t remaining = CLOCKS_REMAINING(deadline, uptime); \
87 wait_queue_t* temp = queue; \
88 if (wait_block_prepare(&temp, 1, remaining) == ERR) \
93 result = wait_block_commit(); \
94 uptime = sys_time_uptime(); \
106#define WAIT_BLOCK_LOCK(queue, lock, condition) \
108 assert(!(rflags_read() & RFLAGS_INTERRUPT_ENABLE)); \
109 uint64_t result = 0; \
110 while (!(condition) && result == 0) \
112 wait_queue_t* temp = queue; \
113 if (wait_block_prepare(&temp, 1, CLOCKS_NEVER) == ERR) \
118 lock_release(lock); \
119 result = wait_block_commit(); \
120 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
121 lock_acquire(lock); \
133#define WAIT_BLOCK_LOCK_TIMEOUT(queue, lock, condition, timeout) \
135 uint64_t result = 0; \
136 clock_t uptime = sys_time_uptime(); \
137 clock_t deadline = CLOCKS_DEADLINE(timeout, uptime); \
138 while (!(condition) && result == ERR) \
140 if (deadline <= uptime) \
146 clock_t remaining = CLOCKS_REMAINING(deadline, uptime); \
147 wait_queue_t* temp = queue; \
148 if (wait_block_prepare(&temp, 1, remaining) == ERR) \
153 lock_release(lock); \
154 result = wait_block_commit(); \
155 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
156 lock_acquire(lock); \
157 uptime = sys_time_uptime(); \
169typedef struct wait_entry
181typedef struct wait_queue
194typedef struct wait_client
219#define WAIT_QUEUE_CREATE(name) {.lock = LOCK_CREATE(), .entries = LIST_CREATE(name.entries)}
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.
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.
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.