ladivic
Loading...
Searching...
No Matches
ldvc_ipc.hpp File Reference

Provides utilities for Inter-Process Communication (IPC) using shared memory. More...

#include <sys/ipc.h>
#include <sys/shm.h>
#include <ldvc_type.hpp>
Include dependency graph for ldvc_ipc.hpp:

Go to the source code of this file.

Functions

template<typename T >
i32 ldvc_create_ipc (std::mutex &mtx, string path)
 Creates a new IPC shared memory segment.
 
template<typename T >
Tldvc_attach_ipc (i32 shmid, std::mutex &mtx)
 Attaches to an existing IPC shared memory segment.
 
template<typename T >
u8 ldvc_detach_ipc (T *data, std::mutex &mtx)
 Detaches from an IPC shared memory segment.
 
template<typename T >
bool ldvc_destroy_ipc (i32 shmid, std::mutex &mtx)
 Destroys an IPC shared memory segment.
 

Detailed Description

Provides utilities for Inter-Process Communication (IPC) using shared memory.

This header file contains a set of functions for managing shared memory segments to facilitate inter-process communication (IPC). These functions enable the creation, attachment, detachment, and destruction of shared memory segments, ensuring thread safety during these operations.

Author
Nathanne Isip

Definition in file ldvc_ipc.hpp.

Function Documentation

◆ ldvc_attach_ipc()

template<typename T >
T * ldvc_attach_ipc ( i32 shmid,
std::mutex & mtx )

Attaches to an existing IPC shared memory segment.

This function attaches to an existing IPC shared memory segment specified by its identifier (shmid).

Template Parameters
TThe type of data stored in the shared memory segment.
Parameters
shmidThe identifier of the shared memory segment to attach to.
mtxA mutex used to ensure thread safety during the attachment process.
Returns
A pointer to the shared memory region on success, or nullptr on failure.

Definition at line 86 of file ldvc_ipc.hpp.

87{
88 std::lock_guard<std::mutex> lock(mtx);
89
90 any ptr = shmat(shmid, nullptr, 0);
91 if(ptr == (any) -1)
92 return nullptr;
93
94 return static_cast<T*>(ptr);
95}
T * ldvc_malloc(usize size)
Allocates memory for an array of elements.
Definition ldvc_mem.hpp:61
void * any
Alias for void pointer.
Definition ldvc_type.hpp:66

◆ ldvc_create_ipc()

template<typename T >
i32 ldvc_create_ipc ( std::mutex & mtx,
string path )

Creates a new IPC shared memory segment.

This function creates a new IPC shared memory segment with the specified size and a unique key derived from the provided path.

Template Parameters
TThe type of data to be stored in the shared memory segment.
Parameters
mtxA mutex used to ensure thread safety during the creation process.
pathThe path used to generate the unique key for the shared memory segment.
Returns
The shared memory identifier (shmid) on success, or -1 on failure.

Definition at line 59 of file ldvc_ipc.hpp.

60{
61 std::lock_guard<std::mutex> lock(mtx);
62
63 key_t key = ftok(path.c_str(), 'A');
64 if(key == -1)
65 return -1;
66
67 return shmget(key, sizeof(T), 0666 | IPC_CREAT);
68}

◆ ldvc_destroy_ipc()

template<typename T >
bool ldvc_destroy_ipc ( i32 shmid,
std::mutex & mtx )

Destroys an IPC shared memory segment.

This function destroys an IPC shared memory segment specified by its identifier (shmid).

Template Parameters
TThe type of data stored in the shared memory segment.
Parameters
shmidThe identifier of the shared memory segment to destroy.
mtxA mutex used to ensure thread safety during the destruction process.
Returns
True if the destruction was successful, false otherwise.

Definition at line 138 of file ldvc_ipc.hpp.

139{
140 std::lock_guard<std::mutex> lock(mtx);
141 return shmctl(shmid, IPC_RMID, nullptr) == -1;
142}

◆ ldvc_detach_ipc()

template<typename T >
u8 ldvc_detach_ipc ( T * data,
std::mutex & mtx )

Detaches from an IPC shared memory segment.

This function detaches from an IPC shared memory segment given a pointer to the shared memory region.

Template Parameters
TThe type of data stored in the shared memory segment.
Parameters
dataA pointer to the shared memory region.
mtxA mutex used to ensure thread safety during the detachment process.
Returns
0 on success, or -1 on failure.

Definition at line 113 of file ldvc_ipc.hpp.

114{
115 std::lock_guard<std::mutex> lock(mtx);
116
117 if(shmdt(data) == -1)
118 return -1;
119 return 0;
120}