Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing memory orderings for MAX_LOG_LEVEL_FILTER #453

Closed
nico-abram opened this issue May 3, 2021 · 1 comment · Fixed by #463
Closed

Confusing memory orderings for MAX_LOG_LEVEL_FILTER #453

nico-abram opened this issue May 3, 2021 · 1 comment · Fixed by #463

Comments

@nico-abram
Copy link

Skimming the code for lib.rs, I see the log level is a global atomic:

static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(0);

I'm opening this issue because, as I understand it, the memory orderings used for the store/loads to this variable in this file make no sense:

SeqCst is used for the store:

pub fn set_max_level(level: LevelFilter) {
    MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::SeqCst)
}

Relaxed is used for the load:

pub fn max_level() -> LevelFilter {
    // Since `LevelFilter` is `repr(usize)`,
    // this transmute is sound if and only if `MAX_LOG_LEVEL_FILTER`
    // is set to a usize that is a valid discriminant for `LevelFilter`.
    // Since `MAX_LOG_LEVEL_FILTER` is private, the only time it's set
    // is by `set_max_level` above, i.e. by casting a `LevelFilter` to `usize`.
    // So any usize stored in `MAX_LOG_LEVEL_FILTER` is a valid discriminant.
    unsafe { mem::transmute(MAX_LOG_LEVEL_FILTER.load(Ordering::Relaxed)) }
}

AIUI, this means the SeqCst store is not synchronizing with any SeqCst loads, so it is useless. Shouldn't the store also be relaxed (To match what I understand are the current semantics being used), or the load be made SeqCst (To change from the current Relaxed semantics to SeqCst)?

I found this PR which favors using SeqCst everywhere except where it's perf-critical, is the (confusing, and seemingly AFAIK useless since it's not synchronizing with any SeqCst load) SeqCst store intended?

The current load/store pairs seem confusing to me because it's not clear if the semantics desired are Relaxed (In which case the SeqCst store should not be there), or SeqCst (In which case there is currently a bug because of the Relaxed load)

@sfackler
Copy link
Member

sfackler commented May 3, 2021

I think it makes sense to change the store to relaxed.

EFanZh pushed a commit to EFanZh/log that referenced this issue Jul 23, 2023
* Run `$CARGO -vV` in `detect-targets` if env `CARGO` present
* Improve crate doc for `detect-targets`
* Use env var `CARGO` in `install_from_source` if present
* Test use of `CARGO` env in `tests.sh`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants