ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provsql_shmem.c
Go to the documentation of this file.
1/**
2 * @file provsql_shmem.c
3 * @brief Shared-memory segment lifecycle and LWLock management.
4 *
5 * Implements the functions declared in @c provsql_shmem.h:
6 * - @c provsql_shmem_startup(): allocates (or attaches to) the
7 * @c provsqlSharedState segment, initialises the @c LWLock, and
8 * calls @c pipe(2) to create the two inter-process pipes. Chains
9 * to @c prev_shmem_startup if set.
10 * - @c provsql_memsize(): returns the size of the shared segment.
11 * - @c provsql_shmem_request(): requests shared memory on PG ≥ 15.
12 * - @c provsql_shmem_lock_exclusive() / @c provsql_shmem_lock_shared() /
13 * @c provsql_shmem_unlock(): thin wrappers around
14 * @c LWLockAcquire() / @c LWLockRelease().
15 */
16#include <unistd.h>
17
18#include "postgres.h"
19#include "storage/shmem.h"
20
21#include "provsql_shmem.h"
22#include "provsql_mmap.h"
23
24shmem_startup_hook_type prev_shmem_startup = NULL;
25#if (PG_VERSION_NUM >= 150000)
26shmem_request_hook_type prev_shmem_request = NULL;
27#endif
28
30
32{
33 bool found;
34 int pipes_b_to_m[2];
35 int pipes_m_to_b[2];
36
39
40 // Reset in case of restart
42
43 LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
44
45 provsql_shared_state = ShmemInitStruct(
46 "provsql",
48 &found);
49
50 if(!found) {
51 provsql_shared_state->lock =&(GetNamedLWLockTranche("provsql"))->lock;
52 }
53
54 LWLockRelease(AddinShmemInitLock);
55
56 // Already initialized
57 if(found)
58 return;
59
60 if(pipe(pipes_b_to_m) || pipe(pipes_m_to_b))
61 provsql_error("Cannot create pipe to communicate with MMap worker");
62
63 provsql_shared_state->pipebmr=pipes_b_to_m[0];
64 provsql_shared_state->pipebmw=pipes_b_to_m[1];
65 provsql_shared_state->pipembr=pipes_m_to_b[0];
66 provsql_shared_state->pipembw=pipes_m_to_b[1];
67}
68
70{
71 return MAXALIGN(sizeof(provsqlSharedState));
72}
73
75{
76#if (PG_VERSION_NUM >= 150000)
77 if (prev_shmem_request)
78 prev_shmem_request();
79#endif
80
81 RequestAddinShmemSpace(provsql_memsize());
82
83 RequestNamedLWLockTranche("provsql", 1);
84}
85
87{
88 LWLockAcquire(provsql_shared_state->lock, LW_EXCLUSIVE);
89}
90
92{
93 LWLockAcquire(provsql_shared_state->lock, LW_SHARED);
94}
95
97{
98 LWLockRelease(provsql_shared_state->lock);
99}
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Background worker and IPC primitives for mmap-backed circuit storage.
void provsql_shmem_unlock(void)
Release the ProvSQL LWLock.
void provsql_shmem_lock_exclusive(void)
Acquire the ProvSQL LWLock in exclusive mode.
provsqlSharedState * provsql_shared_state
Pointer to the ProvSQL shared-memory segment (set in provsql_shmem_startup).
void provsql_shmem_request(void)
Request shared memory from PostgreSQL (PG ≥ 15).
shmem_startup_hook_type prev_shmem_startup
Saved pointer to the previous shmem_startup_hook, for chaining.
void provsql_shmem_startup(void)
Initialise the ProvSQL shared-memory segment.
Size provsql_memsize(void)
Return the number of bytes required for the shared-memory segment.
void provsql_shmem_lock_shared(void)
Acquire the ProvSQL LWLock in shared mode.
Shared-memory segment and inter-process pipe management.
Shared state stored in the PostgreSQL shared-memory segment.
long pipebmr
Background-to-main pipe: read end (worker reads)
LWLock * lock
Mutual-exclusion lock for pipe writes.
long pipembr
Main-to-background pipe: read end (worker reads)
long pipebmw
Background-to-main pipe: write end (worker writes)
long pipembw
Main-to-background pipe: write end (backend writes)