MLIB
Loading...
Searching...
No Matches
shmem.h
Go to the documentation of this file.
1#pragma once
7
8#if __has_include("defs.h")
9#include "defs.h"
10#endif
11
12#include "safe_winsock.h"
13#include <stdexcept>
14
15namespace mlib {
16
19{
20public:
21 // constructors/destructor
22 shmem_base ();
23 shmem_base (const std::string& name, size_t size);
24 virtual ~shmem_base ();
25
26 // open/close
27 virtual bool open (const std::string& name, size_t size);
28 virtual bool close ();
29
30 bool created () const
31 {
32 return mem_created;
33 };
34 bool is_opened () const
35 {
36 return mem != NULL;
37 };
38 size_t size () const
39 {
40 return sz;
41 };
42 const std::string& name () const
43 {
44 return name_;
45 };
46
47 // get/set function for read/write timeout
48 void wtmo (DWORD msec)
49 {
50 wtmo_ = msec;
51 };
52 DWORD wtmo () const
53 {
54 return wtmo_;
55 };
56 void rtmo (DWORD msec)
57 {
58 rtmo_ = msec;
59 };
60 DWORD rtmo () const
61 {
62 return rtmo_;
63 };
64
65 // data access function
66 virtual bool write (const void* data);
67 virtual bool read (void* data);
68
69protected:
70 // Lock control
71 bool rdlock ();
72 void rdunlock ();
73 bool wrlock ();
74 void wrunlock ();
75
76 // Unprotected data access functions. Use always together with lock control functions
77 virtual void get (void* data);
78 virtual void put (const void* data);
79 void* dataptr () const
80 {
81 return mem;
82 };
83
84private:
85#pragma pack(push, 1)
87 struct syncblk
88 {
89 HANDLE wrex;
90 HANDLE rdgate;
91 DWORD wrid;
92 // long rdreq; ///< requesting readers
93 LONG rc;
94 LONG wc;
95 };
96#pragma pack(pop)
97
98 std::string name_;
99 LONG in_rdlock;
100 LONG in_wrlock;
101 bool mem_created;
102 HANDLE file;
103 HANDLE rdgate, wrex;
104 DWORD rtmo_, wtmo_;
105 size_t sz;
106 struct syncblk* syn;
107 void* mem;
108};
109
111template <class S, class B = shmem_base>
112class shmem : public B
113{
114public:
116 shmem (const char* name);
117 bool open (const char* name, size_t sz = sizeof (S));
118 void operator>> (S& data);
119 void operator<< (const S& data);
120
121protected:
122 S* dataptr () const
123 {
124 return (S*)B::dataptr ();
125 };
126
127 template <class S, class B>
128 friend class lockr;
129 template <class S, class B>
130 friend class lockw;
131};
132
134template <class S, class B>
136 : B (){};
137
139template <class S, class B>
140shmem<S, B>::shmem (const char* name)
141 : B (name, sizeof (S)){};
142
144template <class S, class B>
145bool shmem<S, B>::open (const char* name, size_t sz)
146{
147 return B::open (name, sz);
148};
149
151template <class S, class B>
153{
154 this->read (&data);
155};
156
158template <class S, class B>
159void shmem<S, B>::operator<< (const S& data)
160{
161 this->write (&data);
162};
163
187
188template <class S, class B = shmem_base>
189class lockr
190{
191public:
192 lockr (shmem<S, B>& mem_);
193 ~lockr ()
194 {
195 mem.rdunlock ();
196 };
197
198 const S* operator->()
199 {
200 return const_cast<const S*> (mem.dataptr ());
201 };
202 operator const S* ()
203 {
204 return const_cast<const S*> (mem.dataptr ());
205 };
206
207private:
208 shmem<S, B>& mem;
209};
210
211template <class S, class B>
212lockr<S, B>::lockr (shmem<S, B>& mem_)
213 : mem (mem_)
214{
215 if (!mem.rdlock ())
216 throw std::runtime_error ("rdlock failure");
217}
218
241
242template <class S, class B = shmem_base>
243class lockw
244{
245public:
246 lockw (shmem<S, B>& mem_);
247 ~lockw ()
248 {
249 mem.wrunlock ();
250 };
251
252 S* operator->()
253 {
254 return mem.dataptr ();
255 };
256 operator S* ()
257 {
258 return mem.dataptr ();
259 };
260
261private:
262 shmem<S, B>& mem;
263};
264
265template <class S, class B>
266lockw<S, B>::lockw (shmem<S, B>& mem_)
267 : mem (mem_)
268{
269 if (!mem.wrlock ())
270 throw std::runtime_error ("wrlock failed");
271};
272
273} // namespace mlib
Smart read pointer for a shared memory area.
Definition shmem.h:190
Smart write pointer for a shared memory area.
Definition shmem.h:244
void wrunlock()
Release a previously obtained writer lock.
Definition shmem.cpp:268
void rdunlock()
Release a previously obtained reader lock.
Definition shmem.cpp:198
virtual void get(void *data)
Retrieve current content of SMA.
Definition shmem.cpp:292
virtual void put(const void *data)
Write new data into the SMA.
Definition shmem.cpp:286
bool rdlock()
Obtain a reader lock on SMA.
Definition shmem.cpp:170
shmem_base()
Default constructor.
Definition shmem.cpp:20
virtual ~shmem_base()
Destructor.
Definition shmem.cpp:62
bool wrlock()
Obtain a writer lock on SMA.
Definition shmem.cpp:218
virtual bool read(void *data)
Obtain a reader lock and retrieve SMA content.
Definition shmem.cpp:318
virtual bool write(const void *data)
Obtain writer lock and update SMA.
Definition shmem.cpp:301
virtual bool open(const std::string &name, size_t size)
Open a SMA.
Definition shmem.cpp:79
virtual bool close()
Close a SMA.
Definition shmem.cpp:140
Shared memory area with support for single-writer multiple-readers.
Definition shmem.h:113
shmem()
Default constructor.
Definition shmem.h:135
shmem(const char *name)
Creates and opens a shared memory area with the given name.
Definition shmem.h:140
void operator<<(const S &data)
Update (write) the content of a shared memory area.
Definition shmem.h:159
bool open(const char *name, size_t sz=sizeof(S))
Opens a shared memory area.
Definition shmem.h:145
void operator>>(S &data)
Read the content of shared memory area.
Definition shmem.h:152