+
+ /*
+ * Wake up other thread that are waiting for this thread to finish
+ * detaching
+ */
+ d->bd_flags &= ~BPF_DETACHING;
+ d->bd_flags |= BPF_DETACHED;
+ /*
+ * Note that We've kept the reference because we may have dropped
+ * the lock when turning off promiscuous mode
+ */
+ bpf_release_d(d);
+
+done:
+ /*
+ * When closing makes sure no other thread refer to the bpf_d
+ */
+ if (bpf_debug != 0)
+ printf("%s: %llx done\n",
+ __func__, (uint64_t)VM_KERNEL_ADDRPERM(d));
+ /*
+ * Let the caller know the bpf_d is closed
+ */
+ if ((d->bd_flags & BPF_CLOSING))
+ return (1);
+ else
+ return (0);
+}
+
+
+/*
+ * Start asynchronous timer, if necessary.
+ * Must be called with bpf_mlock held.
+ */
+static void
+bpf_start_timer(struct bpf_d *d)
+{
+ uint64_t deadline;
+ struct timeval tv;
+
+ if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
+ tv.tv_sec = d->bd_rtout / hz;
+ tv.tv_usec = (d->bd_rtout % hz) * tick;
+
+ clock_interval_to_deadline(
+ (uint64_t)tv.tv_sec * USEC_PER_SEC + tv.tv_usec,
+ NSEC_PER_USEC, &deadline);
+ /*
+ * The state is BPF_IDLE, so the timer hasn't
+ * been started yet, and hasn't gone off yet;
+ * there is no thread call scheduled, so this
+ * won't change the schedule.
+ *
+ * XXX - what if, by the time it gets entered,
+ * the deadline has already passed?
+ */
+ thread_call_enter_delayed(d->bd_thread_call, deadline);
+ d->bd_state = BPF_WAITING;
+ }
+}
+
+/*
+ * Cancel asynchronous timer.
+ * Must be called with bpf_mlock held.
+ */
+static boolean_t
+bpf_stop_timer(struct bpf_d *d)
+{
+ /*
+ * If the timer has already gone off, this does nothing.
+ * Our caller is expected to set d->bd_state to BPF_IDLE,
+ * with the bpf_mlock, after we are called. bpf_timed_out()
+ * also grabs bpf_mlock, so, if the timer has gone off and
+ * bpf_timed_out() hasn't finished, it's waiting for the
+ * lock; when this thread releases the lock, it will
+ * find the state is BPF_IDLE, and just release the
+ * lock and return.
+ */
+ return (thread_call_cancel(d->bd_thread_call));
+}
+
+void
+bpf_acquire_d(struct bpf_d *d)
+{
+ void *lr_saved = __builtin_return_address(0);
+
+ lck_mtx_assert(bpf_mlock, LCK_MTX_ASSERT_OWNED);
+
+ d->bd_refcnt += 1;
+
+ d->bd_ref_lr[d->bd_next_ref_lr] = lr_saved;
+ d->bd_next_ref_lr = (d->bd_next_ref_lr + 1) % BPF_REF_HIST;