Skip to content

Commit

Permalink
rwlock-ify dispatcher list
Browse files Browse the repository at this point in the history
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Oct 5, 2020
1 parent cfd8236 commit 6da22e5
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions tracing-core/src/callsite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use crate::stdlib::{
fmt,
hash::{Hash, Hasher},
ptr,
sync::{
atomic::{AtomicPtr, Ordering},
Mutex, MutexGuard,
},
sync::atomic::{AtomicPtr, Ordering},
};
use crate::{
dispatcher::{self, Dispatch},
Expand Down Expand Up @@ -71,18 +68,19 @@ pub use self::inner::{rebuild_interest_cache, register};
#[cfg(feature = "std")]
mod inner {
use super::*;
use std::sync::RwLock;
use std::vec::Vec;
type Dispatchers = Vec<dispatcher::Registrar>;

struct Registry {
callsites: Callsites,
dispatchers: Mutex<Dispatchers>,
dispatchers: RwLock<Dispatchers>,
}

lazy_static! {
static ref REGISTRY: Registry = Registry {
callsites: LinkedList::new(),
dispatchers: Mutex::new(Vec::new()),
dispatchers: RwLock::new(Vec::new()),
};
}

Expand All @@ -106,7 +104,7 @@ mod inner {
/// [`Interest::sometimes()`]: super::subscriber::Interest::sometimes
/// [`Subscriber`]: super::subscriber::Subscriber
pub fn rebuild_interest_cache() {
let mut dispatchers = REGISTRY.dispatchers.lock().unwrap();
let mut dispatchers = REGISTRY.dispatchers.write().unwrap();
let callsites = &REGISTRY.callsites;
rebuild_interest(callsites, &mut dispatchers);
}
Expand All @@ -116,13 +114,13 @@ mod inner {
/// This should be called once per callsite after the callsite has been
/// constructed.
pub fn register(registration: &'static Registration) {
let mut dispatchers = REGISTRY.dispatchers.lock().unwrap();
let mut dispatchers = REGISTRY.dispatchers.read().unwrap();
rebuild_callsite_interest(&mut dispatchers, registration.callsite);
REGISTRY.callsites.push(registration);
}

pub(crate) fn register_dispatch(dispatch: &Dispatch) {
let mut dispatchers = REGISTRY.dispatchers.lock().unwrap();
let mut dispatchers = REGISTRY.dispatchers.write().unwrap();
let callsites = &REGISTRY.callsites;

dispatchers.push(dispatch.registrar());
Expand All @@ -131,7 +129,7 @@ mod inner {
}

fn rebuild_callsite_interest(
dispatchers: &mut MutexGuard<'_, Vec<dispatcher::Registrar>>,
dispatchers: &[dispatcher::Registrar],
callsite: &'static dyn Callsite,
) {
let meta = callsite.metadata();
Expand All @@ -156,10 +154,7 @@ mod inner {
callsite.set_interest(interest)
}

fn rebuild_interest(
callsites: &Callsites,
dispatchers: &mut MutexGuard<'_, Vec<dispatcher::Registrar>>,
) {
fn rebuild_interest(callsites: &Callsites, dispatchers: &mut Vec<dispatcher::Registrar>) {
let mut max_level = LevelFilter::OFF;
dispatchers.retain(|registrar| {
if let Some(dispatch) = registrar.upgrade() {
Expand Down

0 comments on commit 6da22e5

Please sign in to comment.