1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 #ifndef GNASH_SHM_H
20 #define GNASH_SHM_H
21
22 #ifdef HAVE_CONFIG_H
23 # include "gnashconfig.h"
24 #endif
25
26 #include <cstdint>
27
28 #if defined (WIN32)
29 // Include for HANDLE
30 #include <windows.h>
31 #else
32 // key_t
33 #include <sys/types.h>
34 #endif
35
36 #include "dsodefs.h" //For DSOEXPORT
37
38 // Forward declarations
39 namespace gnash {
40 class fn_call;
41 }
42
43 namespace gnash {
44
45 class SharedMem
46 {
47 public:
48
49 typedef std::uint8_t* iterator;
50
51 /// The beginning of the SharedMem section.
52 //
53 /// This is only valid after attach() has returned true. You can check
54 /// with the function attached().
begin()55 iterator begin() const {
56 return _addr;
57 }
58
59 /// The end of the SharedMem section.
60 //
61 /// This is only valid after attach() has returned true.
end()62 iterator end() const {
63 return _addr + _size;
64 }
65
66 /// Construct a SharedMem with the requested size.
67 //
68 /// @param size The size of the shared memory section. If successfully
69 /// created, the segment will be exactly this size and
70 /// is not resizable.
71 DSOEXPORT SharedMem(size_t size);
72
73 /// Destructor.
74 DSOEXPORT ~SharedMem();
75
76 /// Initialize the shared memory segment
77 //
78 /// This is called by LocalConnection when either connect() or send()
79 /// is called.
80 DSOEXPORT bool attach();
81
82 /// Use to get a scoped semaphore lock on the shared memory.
83 class Lock
84 {
85 public:
Lock(const SharedMem & s)86 Lock(const SharedMem& s) : _s(s), _locked(s.lock()) {}
~Lock()87 ~Lock() { if (_locked) _s.unlock(); }
locked()88 bool locked() const {
89 return _locked;
90 }
91 private:
92 const SharedMem& _s;
93 bool _locked;
94 };
95
96 private:
97
98 /// Get a semaphore lock if possible
99 //
100 /// @return true if successful, false if not.
101 DSOEXPORT bool lock() const;
102
103 /// Release a semaphore lock if possible
104 //
105 /// @return true if successful, false if not.
106 DSOEXPORT bool unlock() const;
107
108 /// Obtain a semaphore.
109 /// @return true on success; false otherwise.
110 bool getSemaphore();
111
112 iterator _addr;
113
114 const size_t _size;
115
116 // Semaphore ID.
117 int _semid;
118
119 // Shared memory ID.
120 int _shmid;
121
122 #if !defined(WIN32)
123 key_t _shmkey;
124 #else
125 long _shmkey;
126 HANDLE _shmhandle;
127 #endif
128 };
129
130 /// Check if the SharedMem has been attached.
131 //
132 /// This only checks whether the attach operation was successful, not whether
133 /// the shared memory still exists and is still attached where it was
134 /// initially. It is always possible for other processes to remove it while
135 /// Gnash is using it, but there is nothing we can do about this.
136 inline bool
attached(const SharedMem & mem)137 attached(const SharedMem& mem) {
138 return (mem.begin());
139 }
140
141 } // end of gnash namespace
142
143 #endif
144
145 // Local Variables:
146 // mode: C++
147 // indent-tabs-mode: t
148 // End:
149