update copyright date
[gnash.git] / libbase / SharedMem.h
blob5f5107b2688509c956f56ef38e8c95a6c428eb47
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 // 2011 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
19 #ifndef GNASH_SHM_H
20 #define GNASH_SHM_H
22 #ifdef HAVE_CONFIG_H
23 # include "gnashconfig.h"
24 #endif
26 #include <boost/cstdint.hpp>
28 #if defined (WIN32)
29 // Include for HANDLE
30 #include <windows.h>
31 #else
32 // key_t
33 #include <sys/types.h>
34 #endif
36 #include "dsodefs.h" //For DSOEXPORT
38 // Forward declarations
39 namespace gnash {
40 class fn_call;
43 namespace gnash {
45 class SharedMem
47 public:
49 typedef boost::uint8_t* iterator;
51 /// The beginning of the SharedMem section.
53 /// This is only valid after attach() has returned true. You can check
54 /// with the function attached().
55 iterator begin() const {
56 return _addr;
59 /// The end of the SharedMem section.
61 /// This is only valid after attach() has returned true.
62 iterator end() const {
63 return _addr + _size;
66 /// Construct a SharedMem with the requested size.
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);
73 /// Destructor.
74 DSOEXPORT ~SharedMem();
76 /// Initialize the shared memory segment
78 /// This is called by LocalConnection when either connect() or send()
79 /// is called.
80 DSOEXPORT bool attach();
82 /// Use to get a scoped semaphore lock on the shared memory.
83 class Lock
85 public:
86 Lock(const SharedMem& s) : _s(s), _locked(s.lock()) {}
87 ~Lock() { if (_locked) _s.unlock(); }
88 bool locked() const {
89 return _locked;
91 private:
92 const SharedMem& _s;
93 bool _locked;
96 private:
98 /// Get a semaphore lock if possible
100 /// @return true if successful, false if not.
101 DSOEXPORT bool lock() const;
103 /// Release a semaphore lock if possible
105 /// @return true if successful, false if not.
106 DSOEXPORT bool unlock() const;
108 iterator _addr;
110 const size_t _size;
112 // Semaphore ID.
113 int _semid;
115 // Shared memory ID.
116 int _shmid;
118 #if !defined(WIN32)
119 key_t _shmkey;
120 #else
121 long _shmkey;
122 HANDLE _shmhandle;
123 #endif
126 /// Check if the SharedMem has been attached.
128 /// This only checks whether the attach operation was successful, not whether
129 /// the shared memory still exists and is still attached where it was
130 /// initially. It is always possible for other processes to remove it while
131 /// Gnash is using it, but there is nothing we can do about this.
132 inline bool
133 attached(const SharedMem& mem) {
134 return (mem.begin());
137 } // end of gnash namespace
139 #endif
141 // Local Variables:
142 // mode: C++
143 // indent-tabs-mode: t
144 // End: