PatchworkOS
Loading...
Searching...
No Matches
wait.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/cpu/regs.h>
4#include <kernel/sync/lock.h>
5
6#include <errno.h>
7#include <sys/list.h>
8#include <sys/proc.h>
9
10typedef struct thread thread_t;
11typedef struct cpu cpu_t;
12
26#define WAIT_ALL UINT64_MAX
27
37#define WAIT_BLOCK(waitQueue, condition) \
38 ({ \
39 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
40 uint64_t result = 0; \
41 while (!(condition) && result == 0) \
42 { \
43 wait_queue_t* temp = waitQueue; \
44 if (wait_block_setup(&temp, 1, CLOCKS_NEVER) == ERR) \
45 { \
46 result = ERR; \
47 break; \
48 } \
49 result = wait_block_commit(); \
50 } \
51 result; \
52 })
53
64#define WAIT_BLOCK_TIMEOUT(waitQueue, condition, timeout) \
65 ({ \
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) \
71 { \
72 if (deadline <= uptime) \
73 { \
74 errno = ETIMEDOUT; \
75 result = ERR; \
76 break; \
77 } \
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) \
81 { \
82 result = ERR; \
83 break; \
84 } \
85 result = wait_block_commit(); \
86 uptime = timer_uptime(); \
87 } \
88 result; \
89 })
90
101#define WAIT_BLOCK_LOCK(waitQueue, lock, condition) \
102 ({ \
103 assert(!(rflags_read() & RFLAGS_INTERRUPT_ENABLE)); \
104 uint64_t result = 0; \
105 while (!(condition) && result == 0) \
106 { \
107 wait_queue_t* temp = waitQueue; \
108 if (wait_block_setup(&temp, 1, CLOCKS_NEVER) == ERR) \
109 { \
110 result = ERR; \
111 break; \
112 } \
113 lock_release(lock); \
114 result = wait_block_commit(); \
115 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
116 lock_acquire(lock); \
117 } \
118 result; \
119 })
120
132#define WAIT_BLOCK_LOCK_TIMEOUT(waitQueue, lock, condition, timeout) \
133 ({ \
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) \
138 { \
139 if (deadline <= uptime) \
140 { \
141 errno = ETIMEDOUT; \
142 result = ERR; \
143 break; \
144 } \
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) \
148 { \
149 result = ERR; \
150 break; \
151 } \
152 lock_release(lock); \
153 result = wait_block_commit(); \
154 assert(rflags_read() & RFLAGS_INTERRUPT_ENABLE); \
155 lock_acquire(lock); \
156 uptime = timer_uptime(); \
157 } \
158 result; \
159 })
160
165typedef struct wait_queue
166{
170
185
198
213
220#define WAIT_QUEUE_CREATE(name) {.lock = LOCK_CREATE, .entries = LIST_CREATE(name.entries)}
221
227void wait_queue_init(wait_queue_t* waitQueue);
228
234void wait_queue_deinit(wait_queue_t* waitQueue);
235
242
251void wait_cpu_ctx_init(wait_cpu_ctx_t* wait, cpu_t* self);
252
271
282void wait_unblock_thread(thread_t* thread, errno_t err);
283
292uint64_t wait_unblock(wait_queue_t* waitQueue, uint64_t amount, errno_t err);
293
306uint64_t wait_block_setup(wait_queue_t** waitQueues, uint64_t amount, clock_t timeout);
307
315void wait_block_cancel(void);
316
331
int errno_t
Definition errno_t.h:4
uint64_t wait_block_commit(void)
Block the currently running thread.
Definition wait.c:321
bool wait_block_finalize(interrupt_frame_t *frame, cpu_t *self, thread_t *thread, clock_t uptime)
Finalize blocking of a thread.
Definition wait.c:103
uint64_t wait_block_setup(wait_queue_t **waitQueues, uint64_t amount, clock_t timeout)
Setup blocking but dont block yet.
Definition wait.c:227
void wait_block_cancel(void)
Cancel blocking.
Definition wait.c:300
uint64_t wait_unblock(wait_queue_t *waitQueue, uint64_t amount, errno_t err)
Unblock threads waiting on a wait queue.
Definition wait.c:168
void wait_queue_init(wait_queue_t *waitQueue)
Initialize wait queue.
Definition wait.c:71
void wait_cpu_ctx_init(wait_cpu_ctx_t *wait, cpu_t *self)
Initialize per-CPU wait context.
Definition wait.c:95
void wait_thread_ctx_init(wait_thread_ctx_t *wait)
Initialize per-thread wait context.
Definition wait.c:87
void wait_queue_deinit(wait_queue_t *waitQueue)
Deinitialize wait queue.
Definition wait.c:77
void wait_unblock_thread(thread_t *thread, errno_t err)
Unblock a specific thread.
Definition wait.c:156
clock_t uptime(void)
System call for retreving the time since boot.
Definition uptime.c:6
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
CPU structure.
Definition cpu.h:42
Trap Frame Structure.
Definition interrupt.h:42
A entry in a doubly linked list.
Definition list.h:38
A doubly linked list.
Definition list.h:51
A simple ticket lock implementation.
Definition lock.h:43
Thread of execution structure.
Definition thread.h:55
Per-CPU wait context.
Definition wait.h:193
lock_t lock
Definition wait.h:196
list_t blockedThreads
List of blocked threads, sorted by deadline.
Definition wait.h:194
cpu_t * cpu
The CPU this context belongs to.
Definition wait.h:195
Per-thread wait entry.
Definition wait.h:179
wait_queue_t * waitQueue
The wait queue the thread is waiting on.
Definition wait.h:183
thread_t * thread
The thread that is waiting.
Definition wait.h:182
list_entry_t queueEntry
Used in wait_queue_t->entries.
Definition wait.h:180
list_entry_t threadEntry
Used in wait_thread_ctx_t->entries.
Definition wait.h:181
Wait queue structure.
Definition wait.h:166
lock_t lock
Definition wait.h:167
list_t entries
List of wait entries for threads waiting on this queue.
Definition wait.h:168
Per-thread wait context.
Definition wait.h:207
wait_cpu_ctx_t * cpu
The wait cpu context of the cpu the thread is blocked on.
Definition wait.h:211
clock_t deadline
Deadline for timeout, CLOCKS_NEVER for no timeout.
Definition wait.h:210
errno_t err
Error number set when unblocking the thread, EOK for no error.
Definition wait.h:209
list_t entries
List of wait entries, one for each wait queue the thread is waiting on.
Definition wait.h:208