Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #50 from furuholm/noop_locking
Browse files Browse the repository at this point in the history
Noop locking
  • Loading branch information
Michael-F-Bryan committed Dec 28, 2019
2 parents f7947d7 + ad210c4 commit efc99af
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 86 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ matrix:

include:
- rust: stable
- rust: 1.34.0
# static_assertions requires rustc 1.37.0
- rust: 1.37.0
- rust: nightly

- rust: nightly
Expand Down
79 changes: 6 additions & 73 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions libsignal-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ libsignal-protocol-sys = { path = "../libsignal-protocol-sys/" }
failure = "0.1.5"
failure_derive = "0.1.5"
rand = "0.6.5"
parking_lot = "0.8.0"
lock_api = "0.2.0"
log = "0.4.6"
static_assertions = "1.1.0"

# -- Optional Crates -- #
openssl = { version = "0.10", optional = true }
Expand Down
22 changes: 12 additions & 10 deletions libsignal-protocol/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ use std::{
rc::Rc,
sync::Mutex,
time::SystemTime,
marker::{Send, Sync},
};

use failure::Error;
use lock_api::RawMutex as _;
use log::Level;
use parking_lot::RawMutex;

#[cfg(feature = "crypto-native")]
use crate::crypto::DefaultCrypto;
Expand Down Expand Up @@ -333,7 +332,6 @@ impl ContextInner {
let mut global_context: *mut sys::signal_context = ptr::null_mut();
let crypto = CryptoProvider::new(crypto);
let mut state = Pin::new(Box::new(State {
mux: RawMutex::INIT,
log_func: Mutex::new(Box::new(default_log_func)),
}));

Expand Down Expand Up @@ -425,14 +423,19 @@ fn translate_log_level(raw: c_int) -> Level {
}
}

unsafe extern "C" fn lock_function(user_data: *mut c_void) {
let state = &*(user_data as *const State);
state.mux.lock();
// Assert that Context does not implement [`Send`] and [`Sync`] to guarantee
// that each [`Context`] instance is only used within a single thread.
//
// See https://github.com/Michael-F-Bryan/libsignal-protocol-rs/issues/49
// for details.
static_assertions::assert_not_impl_all!(Context: Send, Sync);

unsafe extern "C" fn lock_function(_user_data: *mut c_void) {
// Locking is not required as [`Context`] cannot be shared between
// threads as long as it does not implement [`Sync`] and [`Send`].
}

unsafe extern "C" fn unlock_function(user_data: *mut c_void) {
let state = &*(user_data as *const State);
state.mux.unlock();
unsafe extern "C" fn unlock_function(_user_data: *mut c_void) {
}

/// The "user state" we pass to `libsignal-protocol-c` as part of the global
Expand All @@ -444,7 +447,6 @@ unsafe extern "C" fn unlock_function(user_data: *mut c_void) {
/// `libsignal-protocol-c` library, so any mutation **must** be done using the
/// appropriate synchronisation mechanisms (i.e. `RefCell` or atomics).
struct State {
mux: RawMutex,
log_func: Mutex<Box<dyn Fn(Level, &str) + RefUnwindSafe>>,
}

Expand Down

0 comments on commit efc99af

Please sign in to comment.