1 /* 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright 2026 The FreeBSD Foundation 5 * 6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 7 * under sponsorship from the FreeBSD Foundation. 8 */ 9 10 #ifndef __DEV_NTSYNCVAR_H__ 11 #define __DEV_NTSYNCVAR_H__ 12 13 #include <sys/lock.h> 14 #include <sys/mutex.h> 15 #include <sys/time.h> 16 #include <sys/queue.h> 17 #include <dev/ntsync/ntsync.h> 18 19 enum ntsync_obj_type { 20 NTSYNC_OBJ_SEM, 21 NTSYNC_OBJ_MUTEX, 22 NTSYNC_OBJ_EVENT, 23 }; 24 25 struct ntsync_wait_state; 26 27 struct ntsync_obj_waiter { 28 struct ntsync_wait_state *state; 29 TAILQ_ENTRY(ntsync_obj_waiter) link; 30 }; 31 32 struct ntsync_obj { 33 enum ntsync_obj_type type; 34 struct ntsync_priv *owner; 35 TAILQ_HEAD(, ntsync_obj_waiter) waiters; 36 /* any */ 37 bool (*is_signaled)(struct ntsync_obj *, 38 struct ntsync_wait_state *state, int index); 39 void (*consume)(struct ntsync_obj *, struct ntsync_wait_state *, 40 int index); 41 /* all */ 42 bool (*prepare)(struct ntsync_obj *, struct ntsync_wait_state *state, 43 int index, bool *stop); 44 void (*commit)(struct ntsync_obj *, struct ntsync_wait_state *state, 45 int index); 46 void (*post_commit)(struct ntsync_obj *, 47 struct ntsync_wait_state *state, int index); 48 }; 49 50 struct ntsync_obj_sem { 51 struct ntsync_obj obj; 52 struct ntsync_sem_args a; 53 struct ntsync_sem_args a1; 54 }; 55 #define OBJ_TO_SEM(obj) __containerof(obj, struct ntsync_obj_sem, obj) 56 57 struct ntsync_obj_mutex { 58 struct ntsync_obj obj; 59 struct ntsync_mutex_args a; 60 struct ntsync_mutex_args a1; 61 bool abandoned; 62 }; 63 #define OBJ_TO_MUTEX(obj) __containerof(obj, struct ntsync_obj_mutex, obj) 64 65 struct ntsync_obj_event { 66 struct ntsync_obj obj; 67 struct ntsync_event_args a; 68 struct ntsync_event_args a1; 69 bool pulse; 70 }; 71 #define OBJ_TO_EVENT(obj) __containerof(obj, struct ntsync_obj_event, obj) 72 73 struct ntsync_wait_state { 74 struct ntsync_wait_args *nwa; 75 struct ntsync_priv *owner; 76 struct ntsync_obj_waiter waiters[NTSYNC_MAX_WAIT_COUNT + 1]; 77 int fds[NTSYNC_MAX_WAIT_COUNT]; 78 struct file *fps[NTSYNC_MAX_WAIT_COUNT]; 79 struct file *fp_alert; 80 int obj_count; 81 struct ntsync_obj *objs[NTSYNC_MAX_WAIT_COUNT + 1]; 82 struct ntsync_obj_event *alert_event; 83 sbintime_t sb; 84 int error; 85 int index; 86 bool any; 87 bool all; 88 bool ready; 89 }; 90 91 struct ntsync_priv { 92 struct mtx lock; 93 unsigned objs_cnt; 94 bool closed; 95 }; 96 97 #define NTSYNC_PRIV_LOCK(priv) mtx_lock(&priv->lock) 98 #define NTSYNC_PRIV_UNLOCK(priv) mtx_unlock(&priv->lock) 99 #define NTSYNC_PRIV_ASSERT(priv) mtx_assert(&priv->lock, MA_OWNED) 100 101 extern struct cdevsw ntsync_cdevsw; 102 103 struct file; 104 struct thread; 105 int ntsync_sem_release(struct thread *td, struct file *fp, uint32_t *val); 106 int ntsync_sem_read(struct thread *td, struct file *fp, 107 struct ntsync_sem_args *a); 108 int ntsync_mutex_unlock(struct thread *td, struct file *fp, 109 struct ntsync_mutex_args *a); 110 int ntsync_mutex_kill(struct thread *td, struct file *fp, uint32_t val); 111 int ntsync_mutex_read(struct thread *td, struct file *fp, 112 struct ntsync_mutex_args *a, bool *doco); 113 int ntsync_event_set(struct thread *td, struct file *fp, uint32_t *val); 114 int ntsync_event_reset(struct thread *td, struct file *fp, uint32_t *val); 115 int ntsync_event_pulse(struct thread *td, struct file *fp, uint32_t *val); 116 int ntsync_event_read(struct thread *td, struct file *fp, 117 struct ntsync_event_args *a); 118 119 #endif 120