1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use crate::lwlock::*;
use crate::{pg_sys, PgAtomic};
use uuid::Uuid;
pub unsafe trait PGXSharedMemory {}
#[macro_export]
macro_rules! pg_shmem_init {
($thing:expr) => {
$thing.pg_init();
unsafe {
static mut PREV_SHMEM_STARTUP_HOOK: Option<unsafe extern "C" fn()> = None;
PREV_SHMEM_STARTUP_HOOK = pg_sys::shmem_startup_hook;
pg_sys::shmem_startup_hook = Some(shmem_hook);
#[pg_guard]
extern "C" fn shmem_hook() {
unsafe {
if let Some(i) = PREV_SHMEM_STARTUP_HOOK {
i();
}
}
$thing.shmem_init();
}
}
};
}
pub trait PgSharedMemoryInitialization {
fn pg_init(&'static self);
fn shmem_init(&'static self);
}
impl<T> PgSharedMemoryInitialization for PgLwLock<T>
where
T: Default + PGXSharedMemory + 'static,
{
fn pg_init(&'static self) {
PgSharedMem::pg_init_locked(self);
}
fn shmem_init(&'static self) {
PgSharedMem::shmem_init_locked(self);
}
}
impl<T> PgSharedMemoryInitialization for PgAtomic<T>
where
T: atomic_traits::Atomic + Default,
{
fn pg_init(&'static self) {
PgSharedMem::pg_init_atomic(self);
}
fn shmem_init(&'static self) {
PgSharedMem::shmem_init_atomic(self);
}
}
pub struct PgSharedMem {}
impl PgSharedMem {
pub fn pg_init_locked<T: Default + PGXSharedMemory>(lock: &PgLwLock<T>) {
unsafe {
let lock = std::ffi::CString::new(lock.get_name()).expect("CString::new failed");
pg_sys::RequestAddinShmemSpace(std::mem::size_of::<T>());
pg_sys::RequestNamedLWLockTranche(lock.as_ptr(), 1);
}
}
pub fn pg_init_atomic<T: atomic_traits::Atomic + Default>(_atomic: &PgAtomic<T>) {
unsafe {
pg_sys::RequestAddinShmemSpace(std::mem::size_of::<T>());
}
}
pub fn shmem_init_locked<T: Default + PGXSharedMemory>(lock: &PgLwLock<T>) {
let mut found = false;
unsafe {
let shm_name = std::ffi::CString::new(lock.get_name()).expect("CString::new failed");
let addin_shmem_init_lock: *mut pg_sys::LWLock =
&mut (*pg_sys::MainLWLockArray.add(21)).lock;
pg_sys::LWLockAcquire(addin_shmem_init_lock, pg_sys::LWLockMode_LW_EXCLUSIVE);
let fv_shmem =
pg_sys::ShmemInitStruct(shm_name.into_raw(), std::mem::size_of::<T>(), &mut found)
as *mut T;
std::ptr::write(fv_shmem, <T>::default());
lock.attach(fv_shmem);
pg_sys::LWLockRelease(addin_shmem_init_lock);
}
}
pub fn shmem_init_atomic<T: atomic_traits::Atomic + Default>(atomic: &PgAtomic<T>) {
unsafe {
let shm_name =
std::ffi::CString::new(Uuid::new_v4().to_string()).expect("CString::new() failed");
let addin_shmem_init_lock: *mut pg_sys::LWLock =
&mut (*pg_sys::MainLWLockArray.add(21)).lock;
let mut found = false;
pg_sys::LWLockAcquire(addin_shmem_init_lock, pg_sys::LWLockMode_LW_EXCLUSIVE);
let fv_shmem =
pg_sys::ShmemInitStruct(shm_name.into_raw(), std::mem::size_of::<T>(), &mut found)
as *mut T;
atomic.attach(fv_shmem);
let atomic = T::default();
std::ptr::copy(&atomic, fv_shmem, 1);
pg_sys::LWLockRelease(addin_shmem_init_lock);
}
}
}
unsafe impl PGXSharedMemory for bool {}
unsafe impl PGXSharedMemory for char {}
unsafe impl PGXSharedMemory for str {}
unsafe impl PGXSharedMemory for () {}
unsafe impl PGXSharedMemory for i8 {}
unsafe impl PGXSharedMemory for i16 {}
unsafe impl PGXSharedMemory for i32 {}
unsafe impl PGXSharedMemory for i64 {}
unsafe impl PGXSharedMemory for i128 {}
unsafe impl PGXSharedMemory for u8 {}
unsafe impl PGXSharedMemory for u16 {}
unsafe impl PGXSharedMemory for u32 {}
unsafe impl PGXSharedMemory for u64 {}
unsafe impl PGXSharedMemory for u128 {}
unsafe impl PGXSharedMemory for usize {}
unsafe impl PGXSharedMemory for isize {}
unsafe impl PGXSharedMemory for f32 {}
unsafe impl PGXSharedMemory for f64 {}
unsafe impl<T> PGXSharedMemory for [T] where T: PGXSharedMemory + Default {}
unsafe impl<A, B> PGXSharedMemory for (A, B)
where
A: PGXSharedMemory + Default,
B: PGXSharedMemory + Default,
{
}
unsafe impl<A, B, C> PGXSharedMemory for (A, B, C)
where
A: PGXSharedMemory + Default,
B: PGXSharedMemory + Default,
C: PGXSharedMemory + Default,
{
}
unsafe impl<A, B, C, D> PGXSharedMemory for (A, B, C, D)
where
A: PGXSharedMemory + Default,
B: PGXSharedMemory + Default,
C: PGXSharedMemory + Default,
D: PGXSharedMemory + Default,
{
}
unsafe impl<A, B, C, D, E> PGXSharedMemory for (A, B, C, D, E)
where
A: PGXSharedMemory + Default,
B: PGXSharedMemory + Default,
C: PGXSharedMemory + Default,
D: PGXSharedMemory + Default,
E: PGXSharedMemory + Default,
{
}
unsafe impl<N: Default + PGXSharedMemory, T: heapless::ArrayLength<N>> PGXSharedMemory
for heapless::Vec<N, T>
{
}
unsafe impl<
K: Eq + hash32::Hash,
V: Default,
N: heapless::ArrayLength<heapless::Bucket<K, V>>
+ heapless::ArrayLength<Option<heapless::Pos>>,
S,
> PGXSharedMemory for heapless::IndexMap<K, V, N, S>
{
}