Runs `f` on this thread's buffer. Only `take` contends for the lock.
(&self, f: impl FnOnce(&mut ThreadBuf))
| 386 | |
| 387 | /// Runs `f` on this thread's buffer. Only `take` contends for the lock. |
| 388 | fn with_buf(&self, f: impl FnOnce(&mut ThreadBuf)) { |
| 389 | // `try_with`: spans may still close while thread-locals are being destroyed. |
| 390 | let _ = THREAD_BUF.try_with(|slot| { |
| 391 | let mut slot = slot.borrow_mut(); |
| 392 | // The `Weak` keeps the allocation, so the address can't be reused by another layer. |
| 393 | if !matches!(&*slot, Some((state, _)) if state.as_ptr() == Arc::as_ptr(&self.0)) { |
| 394 | let buf = Arc::new(Mutex::new(self.new_thread_buf())); |
| 395 | self.0.threads.lock().unwrap().push(buf.clone()); |
| 396 | *slot = Some((Arc::downgrade(&self.0), buf)); |
| 397 | } |
| 398 | let (_, buf) = slot.as_ref().unwrap(); |
| 399 | f(&mut buf.lock().unwrap()); |
| 400 | }); |
| 401 | } |
| 402 | |
| 403 | fn new_thread_buf(&self) -> ThreadBuf { |
| 404 | let tid = match POOL_INDEX.with(Cell::get) { |
no test coverage detected