Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = ["/.github", "/.crates"]

[dependencies]
bitflags = "2"
parking_lot = "0.12"
parking_lot = { version = "0.12", features = ["arc_lock"] }
cfg-if = "1.0"
once_cell = "1.17"
anyhow = { version = "1", optional = true }
Expand Down
129 changes: 110 additions & 19 deletions src/zend/globals.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Types related to the PHP executor, sapi and process globals.

use parking_lot::{ArcRwLockReadGuard, ArcRwLockWriteGuard, RawRwLock, RwLock};
use std::collections::HashMap;
use std::ffi::CStr;
use std::ops::{Deref, DerefMut};
use std::slice;
use std::str;

use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::sync::{Arc, LazyLock};

use crate::boxed::ZBox;
use crate::exception::PhpResult;
Expand Down Expand Up @@ -51,7 +51,15 @@ impl ExecutorGlobals {
// return an invalid pointer.
let globals = unsafe { ext_php_rs_executor_globals().as_ref() }
.expect("Static executor globals were invalid");
let guard = GLOBALS_LOCK.read();

cfg_if::cfg_if! {
if #[cfg(php_zts)] {
let guard = lock::GLOBALS_LOCK.with(|l| l.read_arc());
} else {
let guard = lock::GLOBALS_LOCK.read_arc();
}
}

GlobalReadGuard { globals, guard }
}

Expand All @@ -67,7 +75,15 @@ impl ExecutorGlobals {
// return an invalid pointer.
let globals = unsafe { ext_php_rs_executor_globals().as_mut() }
.expect("Static executor globals were invalid");
let guard = GLOBALS_LOCK.write();

cfg_if::cfg_if! {
if #[cfg(php_zts)] {
let guard = lock::GLOBALS_LOCK.with(|l| l.write_arc());
} else {
let guard = lock::GLOBALS_LOCK.write_arc();
}
}

GlobalWriteGuard { globals, guard }
}

Expand Down Expand Up @@ -198,7 +214,7 @@ impl SapiModule {
// return an invalid pointer.
let globals = unsafe { ext_php_rs_sapi_module().as_ref() }
.expect("Static executor globals were invalid");
let guard = SAPI_MODULE_LOCK.read();
let guard = SAPI_MODULE_LOCK.read_arc();
GlobalReadGuard { globals, guard }
}

Expand All @@ -214,7 +230,7 @@ impl SapiModule {
// return an invalid pointer.
let globals = unsafe { ext_php_rs_sapi_module().as_mut() }
.expect("Static executor globals were invalid");
let guard = SAPI_MODULE_LOCK.write();
let guard = SAPI_MODULE_LOCK.write_arc();
GlobalWriteGuard { globals, guard }
}
}
Expand All @@ -234,7 +250,15 @@ impl ProcessGlobals {
// SAFETY: PHP executor globals are statically declared therefore should never
// return an invalid pointer.
let globals = unsafe { &*ext_php_rs_process_globals() };
let guard = PROCESS_GLOBALS_LOCK.read();

cfg_if::cfg_if! {
if #[cfg(php_zts)] {
let guard = lock::PROCESS_GLOBALS_LOCK.with(|l| l.read_arc());
} else {
let guard = lock::PROCESS_GLOBALS_LOCK.read_arc();
}
}

GlobalReadGuard { globals, guard }
}

Expand All @@ -249,7 +273,15 @@ impl ProcessGlobals {
// SAFETY: PHP executor globals are statically declared therefore should never
// return an invalid pointer.
let globals = unsafe { &mut *ext_php_rs_process_globals() };
let guard = PROCESS_GLOBALS_LOCK.write();

cfg_if::cfg_if! {
if #[cfg(php_zts)] {
let guard = lock::PROCESS_GLOBALS_LOCK.with(|l| l.write_arc());
} else {
let guard = lock::PROCESS_GLOBALS_LOCK.write_arc();
}
}

GlobalWriteGuard { globals, guard }
}

Expand Down Expand Up @@ -357,7 +389,15 @@ impl SapiGlobals {
// SAFETY: PHP executor globals are statically declared therefore should never
// return an invalid pointer.
let globals = unsafe { &*ext_php_rs_sapi_globals() };
let guard = SAPI_GLOBALS_LOCK.read();

cfg_if::cfg_if! {
if #[cfg(php_zts)] {
let guard = lock::SAPI_GLOBALS_LOCK.with(|l| l.read_arc());
} else {
let guard = lock::SAPI_GLOBALS_LOCK.read_arc();
}
}

GlobalReadGuard { globals, guard }
}

Expand All @@ -372,7 +412,15 @@ impl SapiGlobals {
// SAFETY: PHP executor globals are statically declared therefore should never
// return an invalid pointer.
let globals = unsafe { &mut *ext_php_rs_sapi_globals() };
let guard = SAPI_GLOBALS_LOCK.write();

cfg_if::cfg_if! {
if #[cfg(php_zts)] {
let guard = lock::SAPI_GLOBALS_LOCK.with(|l| l.write_arc());
} else {
let guard = lock::SAPI_GLOBALS_LOCK.write_arc();
}
}

GlobalWriteGuard { globals, guard }
}

Expand Down Expand Up @@ -576,7 +624,15 @@ impl FileGlobals {
// return an invalid pointer.
let globals = unsafe { ext_php_rs_file_globals().as_ref() }
.expect("Static file globals were invalid");
let guard = FILE_GLOBALS_LOCK.read();

cfg_if::cfg_if! {
if #[cfg(php_zts)] {
let guard = lock::FILE_GLOBALS_LOCK.with(|l| l.read_arc());
} else {
let guard = lock::FILE_GLOBALS_LOCK.read_arc();
}
}

GlobalReadGuard { globals, guard }
}

Expand All @@ -591,7 +647,15 @@ impl FileGlobals {
// SAFETY: PHP executor globals are statically declared therefore should never
// return an invalid pointer.
let globals = unsafe { &mut *ext_php_rs_file_globals() };
let guard = SAPI_GLOBALS_LOCK.write();

cfg_if::cfg_if! {
if #[cfg(php_zts)] {
let guard = lock::FILE_GLOBALS_LOCK.with(|l| l.write_arc());
} else {
let guard = lock::FILE_GLOBALS_LOCK.write_arc();
}
}

GlobalWriteGuard { globals, guard }
}

Expand All @@ -605,23 +669,50 @@ impl FileGlobals {
///
/// PHP provides no indication if the executor globals are being accessed so
/// this is only effective on the Rust side.
static GLOBALS_LOCK: RwLock<()> = const_rwlock(());
static PROCESS_GLOBALS_LOCK: RwLock<()> = const_rwlock(());
static SAPI_GLOBALS_LOCK: RwLock<()> = const_rwlock(());
static FILE_GLOBALS_LOCK: RwLock<()> = const_rwlock(());
#[cfg(not(php_zts))]
pub(crate) mod lock {
use parking_lot::RwLock;
use std::sync::{Arc, LazyLock};

pub(crate) static GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
LazyLock::new(|| Arc::new(RwLock::new(())));
pub(crate) static PROCESS_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
LazyLock::new(|| Arc::new(RwLock::new(())));
pub(crate) static SAPI_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
LazyLock::new(|| Arc::new(RwLock::new(())));
pub(crate) static FILE_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
LazyLock::new(|| Arc::new(RwLock::new(())));
}

/// Executor globals rwlock.
///
/// PHP provides no indication if the executor globals are being accessed so
/// this is only effective on the Rust side.
#[cfg(php_zts)]
pub(crate) mod lock {
use parking_lot::{const_rwlock, RwLock};
use std::sync::Arc;

thread_local! {
pub(crate) static GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new(const_rwlock(()));
pub(crate) static PROCESS_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
pub(crate) static SAPI_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
pub(crate) static FILE_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
}
}

/// SAPI globals rwlock.
///
/// PHP provides no indication if the executor globals are being accessed so
/// this is only effective on the Rust side.
static SAPI_MODULE_LOCK: RwLock<()> = const_rwlock(());
static SAPI_MODULE_LOCK: LazyLock<Arc<RwLock<()>>> = LazyLock::new(|| Arc::new(RwLock::new(())));

/// Wrapper guard that contains a reference to a given type `T`. Dropping a
/// guard releases the lock on the relevant rwlock.
pub struct GlobalReadGuard<T: 'static> {
globals: &'static T,
#[allow(dead_code)]
guard: RwLockReadGuard<'static, ()>,
guard: ArcRwLockReadGuard<RawRwLock, ()>,
}

impl<T> Deref for GlobalReadGuard<T> {
Expand All @@ -637,7 +728,7 @@ impl<T> Deref for GlobalReadGuard<T> {
pub struct GlobalWriteGuard<T: 'static> {
globals: &'static mut T,
#[allow(dead_code)]
guard: RwLockWriteGuard<'static, ()>,
guard: ArcRwLockWriteGuard<RawRwLock, ()>,
}

impl<T> Deref for GlobalWriteGuard<T> {
Expand Down