+static int
+ctl_lock(struct socket *so, int refcount, int lr)
+ {
+ int lr_saved;
+ if (lr == 0)
+ lr_saved = (unsigned int) __builtin_return_address(0);
+ else lr_saved = lr;
+
+ if (so->so_pcb) {
+ lck_mtx_lock(((struct ctl_cb *)so->so_pcb)->mtx);
+ } else {
+ panic("ctl_lock: so=%x NO PCB! lr=%x\n", so, lr_saved);
+ lck_mtx_lock(so->so_proto->pr_domain->dom_mtx);
+ }
+
+ if (so->so_usecount < 0)
+ panic("ctl_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
+ so, so->so_pcb, lr_saved, so->so_usecount);
+
+ if (refcount)
+ so->so_usecount++;
+
+ so->lock_lr[so->next_lock_lr] = (void *)lr_saved;
+ so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX;
+ return (0);
+}
+
+static int
+ctl_unlock(struct socket *so, int refcount, int lr)
+{
+ int lr_saved;
+ lck_mtx_t * mutex_held;
+
+ if (lr == 0)
+ lr_saved = (unsigned int) __builtin_return_address(0);
+ else lr_saved = lr;
+
+#ifdef MORE_KCTLLOCK_DEBUG
+ printf("ctl_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
+ so, so->so_pcb, ((struct ctl_cb *)so->so_pcb)->mtx, so->so_usecount, lr_saved);
+#endif
+ if (refcount)
+ so->so_usecount--;
+
+ if (so->so_usecount < 0)
+ panic("ctl_unlock: so=%x usecount=%x\n", so, so->so_usecount);
+ if (so->so_pcb == NULL) {
+ panic("ctl_unlock: so=%x NO PCB usecount=%x lr=%x\n", so, so->so_usecount, lr_saved);
+ mutex_held = so->so_proto->pr_domain->dom_mtx;
+ } else {
+ mutex_held = ((struct ctl_cb *)so->so_pcb)->mtx;
+ }
+ lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
+ so->unlock_lr[so->next_unlock_lr] = (void *)lr_saved;
+ so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
+ lck_mtx_unlock(mutex_held);
+
+ if (so->so_usecount == 0)
+ ctl_sofreelastref(so);
+
+ return (0);
+}
+
+static lck_mtx_t *
+ctl_getlock(struct socket *so, __unused int locktype)
+{
+ struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb;
+
+ if (so->so_pcb) {
+ if (so->so_usecount < 0)
+ panic("ctl_getlock: so=%x usecount=%x\n", so, so->so_usecount);
+ return(kcb->mtx);
+ } else {
+ panic("ctl_getlock: so=%x NULL so_pcb\n", so);
+ return (so->so_proto->pr_domain->dom_mtx);
+ }
+}