ladivic
Loading...
Searching...
No Matches
ldvc_ipc.hpp
Go to the documentation of this file.
1/*
2 * This file is part of the ladivic library.
3 * Copyright (c) 2024 Nathanne Isip
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24/**
25 * @file ldvc_ipc.hpp
26 * @brief Provides utilities for Inter-Process Communication (IPC) using shared memory.
27 *
28 * This header file contains a set of functions for managing shared memory segments
29 * to facilitate inter-process communication (IPC). These functions enable the creation,
30 * attachment, detachment, and destruction of shared memory segments, ensuring thread safety
31 * during these operations.
32 *
33 * @author Nathanne Isip
34 *
35 */
36#ifndef LDVC_IPC_HPP
37#define LDVC_IPC_HPP
38
39#include <sys/ipc.h>
40#include <sys/shm.h>
41#include <ldvc_type.hpp>
42
43/**
44 *
45 * @brief Creates a new IPC shared memory segment.
46 *
47 * This function creates a new IPC shared memory segment with the specified
48 * size and a unique key derived from the provided path.
49 *
50 * @tparam T The type of data to be stored in the shared memory segment.
51 *
52 * @param mtx A mutex used to ensure thread safety during the creation process.
53 * @param path The path used to generate the unique key for the shared memory segment.
54 *
55 * @return The shared memory identifier (shmid) on success, or -1 on failure.
56 *
57 */
58template<typename T>
59i32 ldvc_create_ipc(std::mutex& mtx, string path)
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}
69
70/**
71 *
72 * @brief Attaches to an existing IPC shared memory segment.
73 *
74 * This function attaches to an existing IPC shared memory segment
75 * specified by its identifier (shmid).
76 *
77 * @tparam T The type of data stored in the shared memory segment.
78 *
79 * @param shmid The identifier of the shared memory segment to attach to.
80 * @param mtx A mutex used to ensure thread safety during the attachment process.
81 *
82 * @return A pointer to the shared memory region on success, or nullptr on failure.
83 *
84 */
85template<typename T>
86T* ldvc_attach_ipc(i32 shmid, std::mutex& mtx)
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}
96
97/**
98 *
99 * @brief Detaches from an IPC shared memory segment.
100 *
101 * This function detaches from an IPC shared memory segment
102 * given a pointer to the shared memory region.
103 *
104 * @tparam T The type of data stored in the shared memory segment.
105 *
106 * @param data A pointer to the shared memory region.
107 * @param mtx A mutex used to ensure thread safety during the detachment process.
108 *
109 * @return 0 on success, or -1 on failure.
110 *
111 */
112template<typename T>
113u8 ldvc_detach_ipc(T* data, std::mutex& mtx)
114{
115 std::lock_guard<std::mutex> lock(mtx);
116
117 if(shmdt(data) == -1)
118 return -1;
119 return 0;
120}
121
122/**
123 *
124 * @brief Destroys an IPC shared memory segment.
125 *
126 * This function destroys an IPC shared memory segment specified by
127 * its identifier (shmid).
128 *
129 * @tparam T The type of data stored in the shared memory segment.
130 *
131 * @param shmid The identifier of the shared memory segment to destroy.
132 * @param mtx A mutex used to ensure thread safety during the destruction process.
133 *
134 * @return True if the destruction was successful, false otherwise.
135 *
136 */
137template<typename T>
138bool ldvc_destroy_ipc(i32 shmid, std::mutex& mtx)
139{
140 std::lock_guard<std::mutex> lock(mtx);
141 return shmctl(shmid, IPC_RMID, nullptr) == -1;
142}
143
144#endif
i32 ldvc_create_ipc(std::mutex &mtx, string path)
Creates a new IPC shared memory segment.
Definition ldvc_ipc.hpp:59
u8 ldvc_detach_ipc(T *data, std::mutex &mtx)
Detaches from an IPC shared memory segment.
Definition ldvc_ipc.hpp:113
bool ldvc_destroy_ipc(i32 shmid, std::mutex &mtx)
Destroys an IPC shared memory segment.
Definition ldvc_ipc.hpp:138
T * ldvc_attach_ipc(i32 shmid, std::mutex &mtx)
Attaches to an existing IPC shared memory segment.
Definition ldvc_ipc.hpp:86
Defines common types used throughout the library.
unsigned char u8
Unsigned integer types.
Definition ldvc_type.hpp:54
void * any
Alias for void pointer.
Definition ldvc_type.hpp:66