Skip to content

Commit

Permalink
Add thread_safe feature
Browse files Browse the repository at this point in the history
  • Loading branch information
aueam committed Jul 30, 2024
1 parent 4a95402 commit db23d1b
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 126 deletions.
6 changes: 6 additions & 0 deletions oi-pkg-checker-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ log = "0.4.20"
serde_json = "1"
bincode = "1.3.3"
serde = { version = "1", features = ["derive"] }

[profile.release]
opt-level = 3

[features]
thread_safe = []
112 changes: 112 additions & 0 deletions oi-pkg-checker-core/src/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,115 @@ pub mod depend_types;
pub mod dependency_type;
pub mod package;
pub mod rev_depend_type;

#[macro_export]
#[cfg(not(feature = "thread_safe"))]
macro_rules! shared_type {
($t:ty) => {
std::rc::Rc<std::cell::RefCell<$t>>
};
}

#[macro_export]
#[cfg(not(feature = "thread_safe"))]
macro_rules! weak_type {
($t:ty) => {
std::rc::Weak<std::cell::RefCell<$t>>
};
}

#[macro_export]
#[cfg(not(feature = "thread_safe"))]
macro_rules! new {
($t:expr) => {
std::rc::Rc::new(std::cell::RefCell::new($t))
};
}

#[macro_export]
#[cfg(not(feature = "thread_safe"))]
macro_rules! clone {
($t:expr) => {
std::rc::Rc::clone($t)
};
}

#[macro_export]
#[cfg(not(feature = "thread_safe"))]
macro_rules! downgrade {
($t:expr) => {
std::rc::Rc::downgrade($t)
};
}

#[macro_export]
#[cfg(not(feature = "thread_safe"))]
macro_rules! get {
($shared:expr) => {
$shared.borrow()
};
}

#[macro_export]
#[cfg(not(feature = "thread_safe"))]
macro_rules! get_mut {
($shared:expr) => {
$shared.borrow_mut()
};
}

#[macro_export]
#[cfg(feature = "thread_safe")]
macro_rules! shared_type {
($t:ty) => {
std::sync::Arc<std::sync::Mutex<$t>>
};
}

#[macro_export]
#[cfg(feature = "thread_safe")]
macro_rules! weak_type {
($t:ty) => {
std::sync::Weak<std::sync::Mutex<$t>>
};
}

#[macro_export]
#[cfg(feature = "thread_safe")]
macro_rules! new {
($t:expr) => {
std::sync::Arc::new(std::sync::Mutex::new($t))
};
}

#[macro_export]
#[cfg(feature = "thread_safe")]
macro_rules! clone {
($t:expr) => {
std::sync::Arc::clone($t)
};
}

#[macro_export]
#[cfg(feature = "thread_safe")]
macro_rules! downgrade {
($t:expr) => {
std::sync::Arc::downgrade($t)
};
}

#[macro_export]
#[cfg(feature = "thread_safe")]
macro_rules! get {
($shared:expr) => {
$shared.lock().unwrap()
};
}

#[macro_export]
#[cfg(feature = "thread_safe")]
macro_rules! get_mut {
($shared:expr) => {
get!($shared)
};
}
Loading

0 comments on commit db23d1b

Please sign in to comment.