+
+#if defined(DEVELOPMENT) || defined(DEBUG)
+#if CONFIG_WAITQ_DEBUG
+static uint64_t g_wqset_num = 0;
+struct g_wqset {
+ queue_chain_t link;
+ struct waitq_set *wqset;
+};
+
+static queue_head_t g_wqset_list;
+static struct waitq_set *g_waitq_set = NULL;
+
+static inline struct waitq_set *sysctl_get_wqset(int idx)
+{
+ struct g_wqset *gwqs;
+
+ if (!g_wqset_num)
+ queue_init(&g_wqset_list);
+
+ /* don't bother with locks: this is test-only code! */
+ qe_foreach_element(gwqs, &g_wqset_list, link) {
+ if ((int)(wqset_id(gwqs->wqset) & 0xffffffff) == idx)
+ return gwqs->wqset;
+ }
+
+ /* allocate a new one */
+ ++g_wqset_num;
+ gwqs = (struct g_wqset *)kalloc(sizeof(*gwqs));
+ assert(gwqs != NULL);
+
+ gwqs->wqset = waitq_set_alloc(SYNC_POLICY_FIFO|SYNC_POLICY_PREPOST, NULL);
+ enqueue_tail(&g_wqset_list, &gwqs->link);
+ printf("[WQ]: created new waitq set 0x%llx\n", wqset_id(gwqs->wqset));
+
+ return gwqs->wqset;
+}
+
+#define MAX_GLOBAL_TEST_QUEUES 64
+static int g_wq_init = 0;
+static struct waitq g_wq[MAX_GLOBAL_TEST_QUEUES];
+
+static inline struct waitq *global_test_waitq(int idx)
+{
+ if (idx < 0)
+ return NULL;
+
+ if (!g_wq_init) {
+ g_wq_init = 1;
+ for (int i = 0; i < MAX_GLOBAL_TEST_QUEUES; i++)
+ waitq_init(&g_wq[i], SYNC_POLICY_FIFO);
+ }
+
+ return &g_wq[idx % MAX_GLOBAL_TEST_QUEUES];
+}
+
+static int sysctl_waitq_wakeup_one SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ int error;
+ int index;
+ struct waitq *waitq;
+ kern_return_t kr;
+ int64_t event64 = 0;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+ if (event64 < 0) {
+ index = (int)((-event64) & 0xffffffff);
+ waitq = wqset_waitq(sysctl_get_wqset(index));
+ index = -index;
+ } else {
+ index = (int)event64;
+ waitq = global_test_waitq(index);
+ }
+
+ event64 = 0;
+
+ printf("[WQ]: Waking one thread on waitq [%d] event:0x%llx\n",
+ index, event64);
+ kr = waitq_wakeup64_one(waitq, (event64_t)event64, THREAD_AWAKENED,
+ WAITQ_ALL_PRIORITIES);
+ printf("[WQ]: \tkr=%d\n", kr);
+
+ return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_wakeup_one, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_waitq_wakeup_one, "Q", "wakeup one thread waiting on given event");
+
+
+static int sysctl_waitq_wakeup_all SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ int error;
+ int index;
+ struct waitq *waitq;
+ kern_return_t kr;
+ int64_t event64 = 0;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+ if (event64 < 0) {
+ index = (int)((-event64) & 0xffffffff);
+ waitq = wqset_waitq(sysctl_get_wqset(index));
+ index = -index;
+ } else {
+ index = (int)event64;
+ waitq = global_test_waitq(index);
+ }
+
+ event64 = 0;
+
+ printf("[WQ]: Waking all threads on waitq [%d] event:0x%llx\n",
+ index, event64);
+ kr = waitq_wakeup64_all(waitq, (event64_t)event64,
+ THREAD_AWAKENED, WAITQ_ALL_PRIORITIES);
+ printf("[WQ]: \tkr=%d\n", kr);
+
+ return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_wakeup_all, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_waitq_wakeup_all, "Q", "wakeup all threads waiting on given event");
+
+
+static int sysctl_waitq_wait SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ int error;
+ int index;
+ struct waitq *waitq;
+ kern_return_t kr;
+ int64_t event64 = 0;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+ if (event64 < 0) {
+ index = (int)((-event64) & 0xffffffff);
+ waitq = wqset_waitq(sysctl_get_wqset(index));
+ index = -index;
+ } else {
+ index = (int)event64;
+ waitq = global_test_waitq(index);
+ }
+
+ event64 = 0;
+
+ printf("[WQ]: Current thread waiting on waitq [%d] event:0x%llx\n",
+ index, event64);
+ kr = waitq_assert_wait64(waitq, (event64_t)event64, THREAD_INTERRUPTIBLE, 0);
+ if (kr == THREAD_WAITING)
+ thread_block(THREAD_CONTINUE_NULL);
+ printf("[WQ]: \tWoke Up: kr=%d\n", kr);
+
+ return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_wait, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_waitq_wait, "Q", "start waiting on given event");
+
+
+static int sysctl_wqset_select SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ int error;
+ struct waitq_set *wqset;
+ uint64_t event64 = 0;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ goto out;
+
+ wqset = sysctl_get_wqset((int)(event64 & 0xffffffff));
+ g_waitq_set = wqset;
+
+ event64 = wqset_id(wqset);
+ printf("[WQ]: selected wqset 0x%llx\n", event64);
+
+out:
+ if (g_waitq_set)
+ event64 = wqset_id(g_waitq_set);
+ else
+ event64 = (uint64_t)(-1);
+
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+}
+SYSCTL_PROC(_kern, OID_AUTO, wqset_select, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_wqset_select, "Q", "select/create a global waitq set");
+
+
+static int sysctl_waitq_link SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ int error;
+ int index;
+ struct waitq *waitq;
+ struct waitq_set *wqset;
+ kern_return_t kr;
+ uint64_t reserved_link = 0;
+ int64_t event64 = 0;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+ if (!g_waitq_set)
+ g_waitq_set = sysctl_get_wqset(1);
+ wqset = g_waitq_set;
+
+ if (event64 < 0) {
+ struct waitq_set *tmp;
+ index = (int)((-event64) & 0xffffffff);
+ tmp = sysctl_get_wqset(index);
+ if (tmp == wqset)
+ goto out;
+ waitq = wqset_waitq(tmp);
+ index = -index;
+ } else {
+ index = (int)event64;
+ waitq = global_test_waitq(index);
+ }
+
+ printf("[WQ]: linking waitq [%d] to global wqset (0x%llx)\n",
+ index, wqset_id(wqset));
+ reserved_link = waitq_link_reserve(waitq);
+ kr = waitq_link(waitq, wqset, WAITQ_SHOULD_LOCK, &reserved_link);
+ waitq_link_release(reserved_link);
+
+ printf("[WQ]: \tkr=%d\n", kr);
+
+out:
+ return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_link, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_waitq_link, "Q", "link global waitq to test waitq set");
+
+
+static int sysctl_waitq_unlink SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ int error;
+ int index;
+ struct waitq *waitq;
+ struct waitq_set *wqset;
+ kern_return_t kr;
+ uint64_t event64 = 0;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+ if (!g_waitq_set)
+ g_waitq_set = sysctl_get_wqset(1);
+ wqset = g_waitq_set;
+
+ index = (int)event64;
+ waitq = global_test_waitq(index);
+
+ printf("[WQ]: unlinking waitq [%d] from global wqset (0x%llx)\n",
+ index, wqset_id(wqset));
+
+ kr = waitq_unlink(waitq, wqset);
+ printf("[WQ]: \tkr=%d\n", kr);
+
+ return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_unlink, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_waitq_unlink, "Q", "unlink global waitq from test waitq set");
+
+
+static int sysctl_waitq_clear_prepost SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ struct waitq *waitq;
+ uint64_t event64 = 0;
+ int error, index;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+ index = (int)event64;
+ waitq = global_test_waitq(index);
+
+ printf("[WQ]: clearing prepost on waitq [%d]\n", index);
+ waitq_clear_prepost(waitq);
+
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_clear_prepost, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_waitq_clear_prepost, "Q", "clear prepost on given waitq");
+
+
+static int sysctl_wqset_unlink_all SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ int error;
+ struct waitq_set *wqset;
+ kern_return_t kr;
+ uint64_t event64 = 0;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+ if (!g_waitq_set)
+ g_waitq_set = sysctl_get_wqset(1);
+ wqset = g_waitq_set;
+
+ printf("[WQ]: unlinking all queues from global wqset (0x%llx)\n",
+ wqset_id(wqset));
+
+ kr = waitq_set_unlink_all(wqset);
+ printf("[WQ]: \tkr=%d\n", kr);
+
+ return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, wqset_unlink_all, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_wqset_unlink_all, "Q", "unlink all queues from test waitq set");
+
+
+static int sysctl_wqset_clear_preposts SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+ struct waitq_set *wqset = NULL;
+ uint64_t event64 = 0;
+ int error, index;
+
+ error = SYSCTL_IN(req, &event64, sizeof(event64));
+ if (error)
+ return error;
+
+ if (!req->newptr)
+ goto out;
+
+ index = (int)((event64) & 0xffffffff);
+ wqset = sysctl_get_wqset(index);
+ assert(wqset != NULL);
+
+ printf("[WQ]: clearing preposts on wqset 0x%llx\n", wqset_id(wqset));
+ waitq_set_clear_preposts(wqset);
+
+out:
+ if (wqset)
+ event64 = wqset_id(wqset);
+ else
+ event64 = (uint64_t)(-1);
+
+ return SYSCTL_OUT(req, &event64, sizeof(event64));
+}
+SYSCTL_PROC(_kern, OID_AUTO, wqset_clear_preposts, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_wqset_clear_preposts, "Q", "clear preposts on given waitq set");
+
+#endif /* CONFIG_WAITQ_DEBUG */
+#endif /* defined(DEVELOPMENT) || defined(DEBUG) */