+#define KNOTE_LOCK_CTX(n) \
+ struct knote_lock_ctx n
+#endif
+
+
+__options_decl(kq_state_t, uint16_t, {
+ KQ_SEL = 0x0001, /* select was recorded for kq */
+ KQ_SLEEP = 0x0002, /* thread is waiting for events */
+ KQ_PROCWAIT = 0x0004, /* thread waiting for processing */
+ KQ_KEV32 = 0x0008, /* kq is used with 32-bit events */
+ KQ_KEV64 = 0x0010, /* kq is used with 64-bit events */
+ KQ_KEV_QOS = 0x0020, /* kq events carry QoS info */
+ KQ_WORKQ = 0x0040, /* KQ is bound to process workq */
+ KQ_WORKLOOP = 0x0080, /* KQ is part of a workloop */
+ KQ_PROCESSING = 0x0100, /* KQ is being processed */
+ KQ_DRAIN = 0x0200, /* kq is draining */
+ KQ_WAKEUP = 0x0400, /* kq awakened while processing */
+ KQ_DYNAMIC = 0x0800, /* kqueue is dynamically managed */
+ KQ_R2K_ARMED = 0x1000, /* ast notification armed */
+ KQ_HAS_TURNSTILE = 0x2000, /* this kqueue has a turnstile */
+});
+
+/*
+ * kqueue - common core definition of a kqueue
+ *
+ * No real structures are allocated of this type. They are
+ * either kqfile objects or kqworkq objects - each of which is
+ * derived from this definition.
+ */
+struct kqueue {
+ struct {
+ struct waitq_set kq_wqs; /* private waitq set */
+ lck_spin_t kq_lock; /* kqueue lock */
+ kq_state_t kq_state; /* state of the kq */
+ union {
+ uint16_t kq_waitq_hook;/* prepost hook (kqwl/kqwq) */
+ uint16_t kq_level; /* nesting level of the kq */
+ };
+ uint32_t kq_count; /* number of queued events */
+ struct proc *kq_p; /* process containing kqueue */
+ struct knote_locks kq_knlocks; /* list of knote locks held */
+ }; /* make sure struct padding is put before kq_queue */
+ struct kqtailq kq_queue[0]; /* variable array of queues */
+};
+
+/*
+ * kqfile - definition of a typical kqueue opened as a file descriptor
+ * via the kqueue() system call.
+ *
+ * Adds selinfo support to the base kqueue definition, as these
+ * fds can be fed into select().
+ */
+struct kqfile {
+ struct kqueue kqf_kqueue; /* common kqueue core */
+ struct kqtailq kqf_queue; /* queue of woken up knotes */
+ struct kqtailq kqf_suppressed; /* suppression queue */
+ struct selinfo kqf_sel; /* parent select/kqueue info */
+#define kqf_wqs kqf_kqueue.kq_wqs
+#define kqf_lock kqf_kqueue.kq_lock
+#define kqf_state kqf_kqueue.kq_state
+#define kqf_level kqf_kqueue.kq_level
+#define kqf_count kqf_kqueue.kq_count
+#define kqf_p kqf_kqueue.kq_p
+};
+
+#define QOS_INDEX_KQFILE 0 /* number of qos levels in a file kq */
+
+/*
+ * WorkQ kqueues need to request threads to service the triggered
+ * knotes in the queue. These threads are brought up on a
+ * effective-requested-QoS basis. Knotes are segregated based on
+ * that value - calculated by computing max(event-QoS, kevent-QoS).
+ * Only one servicing thread is requested at a time for all the
+ * knotes at a given effective-requested-QoS.
+ */
+
+#if !defined(KQWQ_QOS_MANAGER)
+#define KQWQ_QOS_MANAGER (THREAD_QOS_LAST)
+#endif
+
+#if !defined(KQWQ_NBUCKETS)
+#define KQWQ_NBUCKETS (KQWQ_QOS_MANAGER + 1)
+#endif
+
+/*
+ * kqworkq - definition of a private kqueue used to coordinate event
+ * handling for pthread work queues.
+ *
+ * These have per-qos processing queues and state to coordinate with
+ * the pthread kext to ask for threads at corresponding pthread priority
+ * values.
+ */
+struct kqworkq {
+ struct kqueue kqwq_kqueue;
+ struct kqtailq kqwq_queue[KQWQ_NBUCKETS]; /* array of queues */
+ struct kqtailq kqwq_suppressed[KQWQ_NBUCKETS]; /* Per-QoS suppression queues */
+ workq_threadreq_s kqwq_request[KQWQ_NBUCKETS]; /* per-QoS request states */
+};
+
+#define kqwq_wqs kqwq_kqueue.kq_wqs
+#define kqwq_lock kqwq_kqueue.kq_lock
+#define kqwq_state kqwq_kqueue.kq_state
+#define kqwq_waitq_hook kqwq_kqueue.kq_waitq_hook
+#define kqwq_count kqwq_kqueue.kq_count
+#define kqwq_p kqwq_kqueue.kq_p
+
+/*
+ * WorkLoop kqueues need to request a thread to service the triggered
+ * knotes in the queue. The thread is brought up on a
+ * effective-requested-QoS basis. Knotes are segregated based on
+ * that value. Once a request is made, it cannot be undone. If
+ * events with higher QoS arrive after, they are stored in their
+ * own queues and an override applied to the original request based
+ * on the delta between the two QoS values.
+ */
+
+/*
+ * "Stay-active" knotes are held in a separate bucket that indicates
+ * special handling required. They are kept separate because the
+ * wakeups issued to them don't have context to tell us where to go
+ * to find and process them. All processing of them happens at the
+ * highest QoS. Unlike WorkQ kqueues, there is no special singular
+ * "manager thread" for a process. We simply request a servicing
+ * thread at the higest known QoS when these are woken (or override
+ * an existing request to that).
+ */
+#define KQWL_BUCKET_STAYACTIVE (THREAD_QOS_LAST)
+
+#if !defined(KQWL_NBUCKETS)
+#define KQWL_NBUCKETS (KQWL_BUCKET_STAYACTIVE + 1)