| 1 | /* @title: APCs */ |
| 2 | #pragma once |
| 3 | #include <irq/irq.h> |
| 4 | #include <sch/sched.h> |
| 5 | #include <smp/core.h> |
| 6 | #include <stdatomic.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | #include <structures/list.h> |
| 11 | #include <thread/apc_types.h> |
| 12 | #include <thread/thread.h> |
| 13 | #include <types/refcount.h> |
| 14 | |
| 15 | enum apc_state : uint8_t { |
| 16 | APC_STATE_IDLE = 0, |
| 17 | APC_STATE_QUEUED, |
| 18 | APC_STATE_EXECUTING, |
| 19 | }; |
| 20 | |
| 21 | struct apc; |
| 22 | typedef void (*apc_destroy_t)(struct apc *apc); |
| 23 | |
| 24 | struct apc { |
| 25 | apc_func_t func; |
| 26 | void *ctx; |
| 27 | struct thread *owner; |
| 28 | struct apc *next; |
| 29 | refcount_t refcount; |
| 30 | _Atomic enum apc_state state; |
| 31 | apc_destroy_t destroy; |
| 32 | }; |
| 33 | |
| 34 | struct event_apc { |
| 35 | struct apc apc; |
| 36 | struct apc_event_desc *desc; |
| 37 | size_t execute_times; |
| 38 | }; |
| 39 | |
| 40 | struct apc_event_desc { |
| 41 | const char *name; /* TODO: add fields to this structure */ |
| 42 | }; |
| 43 | |
| 44 | #define APC_EVENT_EXTERN(n) extern struct apc_event_desc __apc_event_##n |
| 45 | |
| 46 | #define APC_EVENT_CREATE(n, strname) \ |
| 47 | struct apc_event_desc __apc_event_##n = {.name = strname} |
| 48 | #define APC_EVENT(n) &(__apc_event_##n) |
| 49 | |
| 50 | struct apc *apc_create(void); |
| 51 | struct event_apc *apc_event_apc_create(void); |
| 52 | void apc_init(struct apc *a, apc_func_t fn, void *arg1, apc_destroy_t destroy); |
| 53 | void apc_event_apc_init(struct event_apc *a, apc_func_t fn, void *arg1, |
| 54 | apc_destroy_t destroy); |
| 55 | bool apc_get(struct apc *a); |
| 56 | void apc_put(struct apc *a); |
| 57 | void apc_destroy_free(struct apc *a); |
| 58 | void apc_event_signal(struct apc_event_desc *desc); |
| 59 | |
| 60 | /* The caller must hold a reference to both t and a. */ |
| 61 | bool apc_enqueue(struct thread *t, struct apc *a, enum apc_type type); |
| 62 | bool apc_enqueue_event_apc(struct event_apc *a, struct apc_event_desc *d); |
| 63 | /* The caller must hold a reference to both t and a. */ |
| 64 | bool apc_cancel(struct thread *t, struct apc *a); |
| 65 | |
| 66 | void apc_check_and_deliver(struct thread *t); |
| 67 | |
| 68 | void apc_enable_special(); |
| 69 | void apc_disable_special(); |
| 70 | |
| 71 | void apc_enable_kernel(); |
| 72 | void apc_disable_kernel(); |
| 73 | |
| 74 | void apc_rundown_thread(struct thread *t); |
| 75 | |
| 76 | static inline const char *apc_event_str(struct apc_event_desc *evt) { |
| 77 | return evt->name; |
| 78 | } |
| 79 | |
| 80 | static inline bool apc_enqueue_on_curr(struct apc *a, enum apc_type type) { |
| 81 | return apc_enqueue(t: thread_get_current(), a, type); |
| 82 | } |
| 83 | |
| 84 | static inline void apc_queue_init(struct apc_queue *q) { |
| 85 | q->head = q->tail = NULL; |
| 86 | } |
| 87 | |