Skip to content

Commit

Permalink
fix "fix fix shmem API on Windows"
Browse files Browse the repository at this point in the history
  • Loading branch information
usamoi committed Nov 2, 2024
1 parent d43e310 commit 825cdf8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
5 changes: 4 additions & 1 deletion pgrx/src/atomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ where
}

pub fn get(&self) -> &T {
unsafe { (*self.inner.get()).as_ref().expect("PgAtomic was not initialized") }
unsafe {
let shared = self.inner.get().read().as_ref().expect("PgAtomic was not initialized");
shared
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions pgrx/src/lwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ impl<T> PgLwLock<T> {
/// Obtain a shared lock (which comes with `&T` access)
pub fn share(&self) -> PgLwLockShareGuard<T> {
unsafe {
let shared = *self.inner.get().as_ref().expect("PgLwLock was not initialized");
let shared = self.inner.get().read().as_ref().expect("PgLwLock was not initialized");
pg_sys::LWLockAcquire((*shared).lock_ptr, pg_sys::LWLockMode::LW_SHARED);
PgLwLockShareGuard { data: &(*shared).data, lock: (*shared).lock_ptr }
PgLwLockShareGuard { data: &*(*shared).data.get(), lock: (*shared).lock_ptr }
}
}

/// Obtain an exclusive lock (which comes with `&mut T` access)
pub fn exclusive(&self) -> PgLwLockExclusiveGuard<T> {
unsafe {
let shared = *self.inner.get().as_ref().expect("PgLwLock was not initialized");
let shared = self.inner.get().read().as_ref().expect("PgLwLock was not initialized");
pg_sys::LWLockAcquire((*shared).lock_ptr, pg_sys::LWLockMode::LW_EXCLUSIVE);
PgLwLockExclusiveGuard { data: &mut (*shared).data, lock: (*shared).lock_ptr }
PgLwLockExclusiveGuard { data: &mut *(*shared).data.get(), lock: (*shared).lock_ptr }
}
}

Expand All @@ -77,7 +77,7 @@ impl<T> PgLwLock<T> {

#[repr(C)]
pub struct PgLwLockShared<T> {
pub data: T,
pub data: UnsafeCell<T>,
pub lock_ptr: *mut pg_sys::LWLock,
}

Expand Down
25 changes: 11 additions & 14 deletions pgrx/src/shmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![deny(unsafe_op_in_unsafe_fn)]
use crate::lwlock::*;
use crate::{pg_sys, PgAtomic};
use std::cell::UnsafeCell;
use std::hash::Hash;

/// Custom types that want to participate in shared memory must implement this marker trait
Expand Down Expand Up @@ -176,27 +177,26 @@ impl PgSharedMem {
/// Must be run from the shared memory init hook, use for types which are guarded by a `LWLock`
/// SAFETY: Must only be called from inside the Postgres shared memory init hook
pub unsafe fn shmem_init_locked<T: Default + PGRXSharedMemory>(lock: &PgLwLock<T>) {
let mut found = false;
unsafe {
let shm_name = lock.name();
let addin_shmem_init_lock: *mut pg_sys::LWLock =
&mut (*pg_sys::MainLWLockArray.add(21)).lock;
let addin_shmem_init_lock = &raw mut (*pg_sys::MainLWLockArray.add(21)).lock;
pg_sys::LWLockAcquire(addin_shmem_init_lock, pg_sys::LWLockMode::LW_EXCLUSIVE);

let mut found = false;
let fv_shmem = pg_sys::ShmemInitStruct(
shm_name.as_ptr(),
std::mem::size_of::<PgLwLockShared<T>>(),
&mut found,
) as *mut PgLwLockShared<T>;

)
.cast::<PgLwLockShared<T>>();
if !found {
fv_shmem.write(PgLwLockShared {
data: T::default(),
data: UnsafeCell::new(T::default()),
lock_ptr: &raw mut (*pg_sys::GetNamedLWLockTranche(shm_name.as_ptr())).lock,
});
}
lock.attach(fv_shmem);

lock.attach(fv_shmem);
pg_sys::LWLockRelease(addin_shmem_init_lock);
}
}
Expand All @@ -206,21 +206,18 @@ impl PgSharedMem {
pub unsafe fn shmem_init_atomic<T: atomic_traits::Atomic + Default>(atomic: &PgAtomic<T>) {
unsafe {
let shm_name = atomic.name();

let addin_shmem_init_lock: *mut pg_sys::LWLock =
&mut (*pg_sys::MainLWLockArray.add(21)).lock;
let addin_shmem_init_lock = &raw mut (*pg_sys::MainLWLockArray.add(21)).lock;
pg_sys::LWLockAcquire(addin_shmem_init_lock, pg_sys::LWLockMode::LW_EXCLUSIVE);

let mut found = false;
pg_sys::LWLockAcquire(addin_shmem_init_lock, pg_sys::LWLockMode::LW_EXCLUSIVE);
let fv_shmem =
pg_sys::ShmemInitStruct(shm_name.as_ptr(), std::mem::size_of::<T>(), &mut found)
as *mut T;

.cast::<T>();
if !found {
fv_shmem.write(T::default());
}
atomic.attach(fv_shmem);

atomic.attach(fv_shmem);
pg_sys::LWLockRelease(addin_shmem_init_lock);
}
}
Expand Down

0 comments on commit 825cdf8

Please sign in to comment.