+
+int
+tcp_lock(so, refcount, lr)
+ 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 inpcb *)so->so_pcb)->inpcb_mtx);
+ }
+ else {
+ panic("tcp_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("tcp_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] = (u_int32_t *)lr_saved;
+ so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX;
+ return (0);
+}
+
+int
+tcp_unlock(so, refcount, lr)
+ 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;
+
+#ifdef MORE_TCPLOCK_DEBUG
+ printf("tcp_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
+ so, so->so_pcb, ((struct inpcb *)so->so_pcb)->inpcb_mtx, so->so_usecount, lr_saved);
+#endif
+ if (refcount)
+ so->so_usecount--;
+
+ if (so->so_usecount < 0)
+ panic("tcp_unlock: so=%x usecount=%x\n", so, so->so_usecount);
+ if (so->so_pcb == NULL)
+ panic("tcp_unlock: so=%x NO PCB usecount=%x lr=%x\n", so, so->so_usecount, lr_saved);
+ else {
+ lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
+ so->unlock_lr[so->next_unlock_lr] = (u_int *)lr_saved;
+ so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
+ lck_mtx_unlock(((struct inpcb *)so->so_pcb)->inpcb_mtx);
+ }
+ return (0);
+}
+
+lck_mtx_t *
+tcp_getlock(so, locktype)
+ struct socket *so;
+ int locktype;
+{
+ struct inpcb *inp = sotoinpcb(so);
+
+ if (so->so_pcb) {
+ if (so->so_usecount < 0)
+ panic("tcp_getlock: so=%x usecount=%x\n", so, so->so_usecount);
+ return(inp->inpcb_mtx);
+ }
+ else {
+ panic("tcp_getlock: so=%x NULL so_pcb\n", so);
+ return (so->so_proto->pr_domain->dom_mtx);
+ }
+}