MLIB
Loading...
Searching...
No Matches
shmem.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7
8#pragma once
9
10#if __has_include("defs.h")
11#include "defs.h"
12#endif
13
14#include "safe_winsock.h"
15#include <stdexcept>
16
17namespace mlib {
18
21{
22public:
23 // constructors/destructor
24 shmem_base ();
25 shmem_base (const std::string& name, size_t size);
26 virtual ~shmem_base ();
27
28 // open/close
29 virtual bool open (const std::string& name, size_t size);
30 virtual bool close ();
31
32 bool created () const;
33 bool is_opened () const;
34 size_t size () const;
35
36 const std::string& name () const;
37
38 // get/set function for read/write timeout
39 void wtmo (DWORD msec);
40 DWORD wtmo () const;
41 void rtmo (DWORD msec);
42 DWORD rtmo () const;
43
44 // data access function
45 virtual bool write (const void* data);
46 virtual bool read (void* data);
47
48protected:
49 // Lock control
50 virtual bool rdlock ();
51 virtual void rdunlock ();
52 virtual bool wrlock ();
53 virtual void wrunlock ();
54
58 void get (void* data) const;
59 void put (const void* data);
60 const void* dataptr () const;
61 void* dataptr ();
63private:
64#pragma pack(push, 1)
66 struct syncblk
67 {
68 HANDLE wrex;
69 HANDLE rdgate;
70 DWORD wrid;
71 // long rdreq; ///< requesting readers
72 LONG rc;
73 LONG wc;
74 };
75#pragma pack(pop)
76
77 std::string name_;
78 LONG in_rdlock;
79 LONG in_wrlock;
80 bool mem_created;
81 HANDLE file;
82 HANDLE rdgate, wrex;
83 DWORD rtmo_, wtmo_;
84 size_t sz;
85 struct syncblk* syn;
86 void* mem;
87};
88
90template <class S, class B = shmem_base>
91class shmem : public B
92{
93public:
95 shmem (const char* name);
96 bool open (const char* name, size_t sz = sizeof (S));
97 void operator>> (S& data);
98 void operator<< (const S& data);
99
100protected:
101 const S* dataptr () const;
102 S* dataptr ();
103
104 template <class S, class B>
105 friend class lockr;
106 template <class S, class B>
107 friend class lockw;
108};
109
111template <class S, class B>
113 : B (){};
114
116template <class S, class B>
118 : B (name, sizeof (S)){};
119
121template <class S, class B>
122bool shmem<S, B>::open (const char* name, size_t sz)
123{
124 return B::open (name, sz);
125};
126
128template <class S, class B>
130{
131 this->read (&data);
132};
133
135template <class S, class B>
136void shmem<S, B>::operator<< (const S& data)
137{
138 this->write (&data);
139};
140
164
165template <class S, class B = shmem_base>
166class lockr
167{
168public:
169 lockr (shmem<S, B>& mem_);
170 ~lockr ()
171 {
172 mem.rdunlock ();
173 };
174
175 const S* operator->()
176 {
177 return mem.dataptr ();
178 };
179 operator const S* ()
180 {
181 return const_cast<const S*> (mem.dataptr ());
182 };
183
184private:
185 shmem<S, B>& mem;
186};
187
188template <class S, class B>
189lockr<S, B>::lockr (shmem<S, B>& mem_)
190 : mem (mem_)
191{
192 if (!mem.rdlock ())
193 throw std::runtime_error ("rdlock failure");
194}
195
218
219template <class S, class B = shmem_base>
220class lockw
221{
222public:
223 lockw (shmem<S, B>& mem_);
224 ~lockw ()
225 {
226 mem.wrunlock ();
227 };
228
231 {
232 return mem.dataptr ();
233 };
234 operator S* ()
235 {
236 return mem.dataptr ();
237 };
238
239private:
240 shmem<S, B>& mem;
241};
242
243template <class S, class B>
244lockw<S, B>::lockw (shmem<S, B>& mem_)
245 : mem (mem_)
246{
247 if (!mem.wrlock ())
248 throw std::runtime_error ("wrlock failed");
249};
250
251//---------------------------------------------------------------------------
252
255inline
257{
258 return mem_created;
259}
260
262inline
264{
265 return mem != NULL;
266}
267
269inline
270size_t shmem_base::size () const
271{
272 return sz;
273}
274
276inline
277const std::string& shmem_base::name () const
278{
279 return name_;
280}
281
283inline
284void shmem_base::wtmo (DWORD msec)
285{
286 wtmo_ = msec;
287}
288
290inline
291DWORD shmem_base::wtmo () const
292{
293 return wtmo_;
294}
295
297inline
298void shmem_base::rtmo (DWORD msec)
299{
300 rtmo_ = msec;
301}
302
304inline
305DWORD shmem_base::rtmo () const
306{
307 return rtmo_;
308}
309
311inline
312void shmem_base::put (const void* data)
313{
314 memcpy (dataptr (), data, size ());
315}
316
318inline
319void shmem_base::get (void* data) const
320{
321 memcpy (data, dataptr (), size ());
322}
323
325inline
326const void* shmem_base::dataptr () const
327{
328 return mem;
329}
330
332inline
334{
335 return mem;
336}
337
339template <class S, class B>
340const S* shmem<S, B>::dataptr () const
341{
342 return (const S*)B::dataptr ();
343}
344
346template <class S, class B>
348{
349 return (S*)B::dataptr ();
350}
351
352} // namespace mlib
S * operator->()
Dereferences pointer to shared memory area.
Definition shmem.h:230
DWORD wtmo() const
Return current write timeout value (in milliseconds)
Definition shmem.h:291
virtual void wrunlock()
Release a previously obtained writer lock.
Definition shmem.cpp:261
virtual void rdunlock()
Release a previously obtained reader lock.
Definition shmem.cpp:193
DWORD rtmo() const
Return current timeout value for read operations (in milliseconds)
Definition shmem.h:305
void put(const void *data)
Write new data into the SMA.
Definition shmem.h:312
virtual bool rdlock()
Obtain a reader lock on SMA.
Definition shmem.cpp:167
shmem_base()
Default constructor.
Definition shmem.cpp:19
bool is_opened() const
Return true if shared memory is opened.
Definition shmem.h:263
virtual ~shmem_base()
Destructor.
Definition shmem.cpp:61
virtual bool wrlock()
Obtain a writer lock on SMA.
Definition shmem.cpp:213
const void * dataptr() const
Return a pointer to content of SMA (const variant)
Definition shmem.h:326
const std::string & name() const
Return the name of the shared memory area.
Definition shmem.h:277
virtual bool read(void *data)
Obtain a reader lock and retrieve SMA content.
Definition shmem.cpp:299
virtual bool write(const void *data)
Obtain writer lock and update SMA.
Definition shmem.cpp:282
size_t size() const
Return size of shared memory area.
Definition shmem.h:270
bool created() const
Definition shmem.h:256
void get(void *data) const
Retrieve current content of SMA.
Definition shmem.h:319
virtual bool open(const std::string &name, size_t size)
Open a SMA.
Definition shmem.cpp:78
virtual bool close()
Close a SMA.
Definition shmem.cpp:137
Shared memory area with support for single-writer multiple-readers.
Definition shmem.h:92
shmem()
Default constructor.
Definition shmem.h:112
shmem(const char *name)
Creates and opens a shared memory area with the given name.
Definition shmem.h:117
void operator<<(const S &data)
Update (write) the content of a shared memory area.
Definition shmem.h:136
S * dataptr()
Return a pointer to content of SMA (const variant)
Definition shmem.h:347
const S * dataptr() const
Return a pointer to content of SMA (const variant)
Definition shmem.h:340
bool open(const char *name, size_t sz=sizeof(S))
Opens a shared memory area.
Definition shmem.h:122
void operator>>(S &data)
Read the content of shared memory area.
Definition shmem.h:129