Skip to content

Commit

Permalink
LazyLock usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Dec 17, 2024
1 parent 8138904 commit bc40a2b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = ["ssrlive", "cssivision <[email protected]>"]
description = "Convert http proxy or socks5 proxy to socks5 proxy."
readme = "README.md"
edition = "2021"
rust-version = "1.70"
rust-version = "1.80"

[lib]
crate-type = ["staticlib", "cdylib", "lib"]
Expand Down
10 changes: 5 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::Config;
use std::{net::SocketAddr, os::raw::c_int, sync::Arc};
use std::{net::SocketAddr, os::raw::c_int, sync::LazyLock, sync::Mutex};

static mut TUN_QUIT: Option<Arc<tokio::sync::mpsc::Sender<()>>> = None;
static TUN_QUIT: LazyLock<Mutex<Option<tokio::sync::mpsc::Sender<()>>>> = LazyLock::new(|| Mutex::new(None));

pub(crate) fn api_internal_run<F>(config: Config, callback: Option<F>) -> c_int
where
F: FnOnce(SocketAddr) + Send + Sync + 'static,
{
if unsafe { TUN_QUIT.is_some() } {
if TUN_QUIT.lock().unwrap().is_some() {
log::error!("socks-hub already started");
return -1;
}
Expand All @@ -17,7 +17,7 @@ where

let (tx, quit) = tokio::sync::mpsc::channel::<()>(1);

unsafe { TUN_QUIT = Some(Arc::new(tx)) };
TUN_QUIT.lock().unwrap().replace(tx);

crate::main_entry(&config, quit, callback).await?;
Ok::<_, crate::BoxError>(())
Expand All @@ -39,7 +39,7 @@ where
}

pub(crate) fn api_internal_stop() -> c_int {
let res = match unsafe { TUN_QUIT.take() } {
let res = match TUN_QUIT.lock().unwrap().take() {
None => {
log::error!("socks-hub not started");
-1
Expand Down

0 comments on commit bc40a2b

Please sign in to comment.