Skip to content

Commit

Permalink
Relax memory orderings
Browse files Browse the repository at this point in the history
For the BSEC we need to use Acquire and Release to ensure the
manipulations of the unsafe bsec library are not order outside of the
the area holding the BSEC_IN_USE lock. However, no AcqRel is required
because we need to establish no memory access get ordered after the
write of the lock.

For the FakeClock there is no critical section, purely incrementing the
counter in an atomic way is sufficient. Thus, relaxed ordering is fine
here.
  • Loading branch information
jgosmann committed Nov 9, 2021
1 parent f2f7ae0 commit b52987d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.1] - 2021-11-09

### Fixed

* Memory orderings for aquireing and releasing BSEC have been slightly relaxed.
* Memory orderings for the `FakeClock` (only used in tests) have been relaxed.


## [0.3.0] - 2021-05-15

### Added
Expand Down Expand Up @@ -40,8 +48,9 @@ Try to get documentation to build on docs.rs.
Initial release.


[Unreleased]: https://github.com/jgosmann/bsec/compare/v0.3.0...HEAD
[0.3.0]: https://github.com/jgosmann/bsec/compare/v0.3.0...v0.3.0
[Unreleased]: https://github.com/jgosmann/bsec/compare/v0.3.1...HEAD
[0.3.0]: https://github.com/jgosmann/bsec/compare/v0.3.0...v0.3.1
[0.3.0]: https://github.com/jgosmann/bsec/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/jgosmann/bsec/compare/v0.1.2...v0.2.0
[0.1.2]: https://github.com/jgosmann/bsec/compare/v0.1.1...v0.1.2
[0.1.1]: https://github.com/jgosmann/bsec/compare/v0.1.0...v0.1.1
Expand Down
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[package]
name = "bsec"
version = "0.3.0"
authors = ["Jan Gosmann <[email protected]>"]
categories = ["embedded"]
description = "Rust API to the Bosch BSEC library."
edition = "2018"
keywords = ["bindings", "bsec"]
license = "MIT OR Apache-2.0"
description = "Rust API to the Bosch BSEC library."
repository = "https://github.com/jgosmann/bsec"
name = "bsec"
readme = "README.md"
keywords = ["bindings", "bsec"]
categories = ["embedded"]
repository = "https://github.com/jgosmann/bsec"
version = "0.3.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bme680 = { version = "0.5.1", optional = true }
embedded-hal = { version = "0.2.5", optional = true }
bme680 = {version = "0.5.1", optional = true}
embedded-hal = {version = "0.2.5", optional = true}
libalgobsec-sys = "0.1.0"
nb = "1.0.0"

Expand Down
10 changes: 7 additions & 3 deletions src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ pub mod tests {
/// This module is only available if the **test-support** feature is enabled.
#[cfg(any(test, feature = "test-support"))]
pub mod test_support {
use std::{sync::atomic::{AtomicI64, Ordering}, time::Duration};
use std::{
sync::atomic::{AtomicI64, Ordering},
time::Duration,
};

use super::*;

Expand Down Expand Up @@ -114,14 +117,15 @@ pub mod test_support {
impl Clock for FakeClock {
/// Returns the current timestamp and advances it by one.
fn timestamp_ns(&self) -> i64 {
self.timestamp_ns.fetch_add(1, Ordering::AcqRel)
self.timestamp_ns.fetch_add(1, Ordering::Relaxed)
}
}

impl FakeClock {
/// Advance the clock's internal time by `duration`.
pub fn advance_by(&self, duration: Duration) {
self.timestamp_ns.fetch_add(duration.as_nanos() as i64, Ordering::AcqRel);
self.timestamp_ns
.fetch_add(duration.as_nanos() as i64, Ordering::Relaxed);
}
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<S: BmeSensor, C: Clock, B: Borrow<C>> Bsec<S, C, B> {
/// * `clock`: [`Clock`] implementation to obtain timestamps.
pub fn init(bme: S, clock: B) -> Result<Self, Error<S::Error>> {
if BSEC_IN_USE
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_ok()
{
unsafe {
Expand Down Expand Up @@ -479,7 +479,7 @@ impl<S: BmeSensor, C: Clock, B: Borrow<C>> Bsec<S, C, B> {

impl<S: BmeSensor, C: Clock, B: Borrow<C>> Drop for Bsec<S, C, B> {
fn drop(&mut self) {
BSEC_IN_USE.store(false, Ordering::SeqCst);
BSEC_IN_USE.store(false, Ordering::Release);
}
}

Expand Down

0 comments on commit b52987d

Please sign in to comment.