Skip to content

Commit

Permalink
Replace std::sync locks with parking_lot locks
Browse files Browse the repository at this point in the history
  • Loading branch information
marcins committed May 22, 2024
1 parent db3ca09 commit 61c1651
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/node-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ serde_json = "1.0.116"
toml = "0.8.12"
anyhow = "1.0.82"
mockall = "0.12.1"
parking_lot = "0.12"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
sentry = { version = "0.32.2", optional = true, default-features = false, features = ["backtrace", "contexts", "panic", "reqwest", "debug-images", "anyhow"]}
Expand Down
8 changes: 4 additions & 4 deletions crates/node-bindings/src/init_sentry/sentry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use parking_lot::Mutex;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;

use napi::Error;
Expand All @@ -26,7 +26,7 @@ fn init_sentry() -> Result<(), Status> {

log::info!("Initialising Sentry in rust...");

if SENTRY_GUARD.lock().unwrap().is_some() {
if SENTRY_GUARD.lock().is_some() {
return Err(Error::from_reason(
"Sentry guard already set, should only initialise Sentry once.",
));
Expand Down Expand Up @@ -63,7 +63,7 @@ fn init_sentry() -> Result<(), Status> {

let guard = init((sentry_dsn, sentry_client_options));

SENTRY_GUARD.lock().unwrap().replace(guard);
SENTRY_GUARD.lock().replace(guard);

sentry::configure_scope(|scope| {
scope.set_user(Some(sentry::User {
Expand All @@ -81,7 +81,7 @@ fn init_sentry() -> Result<(), Status> {

#[napi]
fn close_sentry() {
if let Some(guard) = SENTRY_GUARD.lock().unwrap().take() {
if let Some(guard) = SENTRY_GUARD.lock().take() {
guard.close(Some(TIMEOUT));
}
}
1 change: 1 addition & 0 deletions packages/transformers/js/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ pathdiff = "0.2.0"
path-slash = "0.1.4"
indexmap = "1.9.2"
parcel-macros = { path = "../../../../crates/macros" }
parking_lot = "0.12"
8 changes: 5 additions & 3 deletions packages/transformers/js/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,21 @@ macro_rules! id {
}

#[derive(Debug, Clone, Default)]
pub struct ErrorBuffer(std::sync::Arc<std::sync::Mutex<Vec<swc_core::common::errors::Diagnostic>>>);
pub struct ErrorBuffer(
std::sync::Arc<parking_lot::Mutex<Vec<swc_core::common::errors::Diagnostic>>>,
);

impl Emitter for ErrorBuffer {
fn emit(&mut self, db: &DiagnosticBuilder) {
self.0.lock().unwrap().push((**db).clone());
self.0.lock().push((**db).clone());
}
}

pub fn error_buffer_to_diagnostics(
error_buffer: &ErrorBuffer,
source_map: &SourceMap,
) -> Vec<Diagnostic> {
let s = error_buffer.0.lock().unwrap().clone();
let s = error_buffer.0.lock().clone();
s.iter()
.map(|diagnostic| {
let message = diagnostic.message();
Expand Down
1 change: 1 addition & 0 deletions packages/utils/node-resolver-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ elsa = "1.7.0"
once_cell = "1.17.0"
glob-match = "0.2.1"
dashmap = "5.4.0"
parking_lot = "0.12"

[dev-dependencies]
assert_fs = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/node-resolver-rs/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::borrow::Cow;
use std::ops::Deref;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Mutex;

use dashmap::DashMap;
use elsa::sync::FrozenMap;
use parcel_filesystem::FileSystem;
use parking_lot::Mutex;
use typed_arena::Arena;

use crate::package_json::PackageJson;
Expand Down Expand Up @@ -193,7 +193,7 @@ fn read<F: FileSystem>(
arena: &Mutex<Arena<Box<str>>>,
path: &Path,
) -> std::io::Result<&'static mut str> {
let arena = arena.lock().unwrap();
let arena = arena.lock();
let data = arena.alloc(fs.read_to_string(path)?.into_boxed_str());
// The data lives as long as the arena. In public methods, we only vend temporary references.
Ok(unsafe { &mut *(&mut **data as *mut str) })
Expand Down

0 comments on commit 61c1651

Please sign in to comment.