1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2009-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP
12 #define BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
21 #if defined(BOOST_INTERPROCESS_WINDOWS)
22 #error "This header can't be used in Windows operating systems"
25 #include <boost/move/utility_core.hpp>
26 #include <boost/interprocess/creation_tags.hpp>
27 #include <boost/interprocess/exceptions.hpp>
28 #include <boost/interprocess/detail/utilities.hpp>
29 #include <boost/interprocess/detail/os_file_functions.hpp>
30 #include <boost/interprocess/interprocess_fwd.hpp>
31 #include <boost/interprocess/exceptions.hpp>
32 #include <boost/interprocess/sync/xsi/basic_xsi_semaphore.hpp>
34 #include <boost/assert.hpp>
35 #include <boost/cstdint.hpp>
37 #include <boost/assert.hpp>
40 //!Describes a class representing a xsi-based named_mutex.
43 namespace interprocess {
45 //!A class that wraps a XSI (System V)-based named semaphore
46 //!that undoes the operation if the process crashes.
49 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
50 //Non-copyable and non-assignable
51 xsi_named_mutex(xsi_named_mutex &);
52 xsi_named_mutex &operator=(xsi_named_mutex &);
53 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
56 BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_named_mutex)
58 //!Default constructor.
59 //!Represents an empty xsi_named_mutex.
62 //!Tries to create a new XSI-based named mutex with a key obtained from a call to ftok (with path
63 //!"path" and id "id"), and permissions "perm".
64 //!If the named mutex previously exists, it tries to open it.
65 //!Otherwise throws an error.
66 xsi_named_mutex(open_or_create_t, const char *path, boost::uint8_t id, int perm = 0666)
67 { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, path, id, perm); }
69 //!Moves the ownership of "moved"'s named mutex to *this.
70 //!After the call, "moved" does not represent any named mutex
72 xsi_named_mutex(BOOST_RV_REF(xsi_named_mutex) moved)
73 { this->swap(moved); }
75 //!Moves the ownership of "moved"'s named mutex to *this.
76 //!After the call, "moved" does not represent any named mutex.
78 xsi_named_mutex &operator=(BOOST_RV_REF(xsi_named_mutex) moved)
80 xsi_named_mutex tmp(boost::move(moved));
85 //!Swaps two xsi_named_mutex. Does not throw
86 void swap(xsi_named_mutex &other);
88 //!Destroys *this. The named mutex is still valid after
89 //!destruction. use remove() to destroy the named mutex.
92 //!Returns the path used to construct the
94 const char *get_path() const;
98 int get_permissions() const;
100 //!Returns the mapping handle.
102 mapping_handle_t get_mapping_handle() const;
104 //!Erases a XSI-based named mutex from the system.
105 //!Returns false on error. Never throws
112 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
115 //!Closes a previously opened file mapping. Never throws.
118 //!Closes a previously opened file mapping. Never throws.
119 bool priv_open_or_create( ipcdetail::create_enum_t type
128 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
131 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
133 inline xsi_named_mutex::xsi_named_mutex()
134 : m_semid(-1), m_key(-1), m_id(0), m_perm(0), m_path()
137 inline xsi_named_mutex::~xsi_named_mutex()
138 { this->priv_close(); }
140 inline const char *xsi_named_mutex::get_path() const
141 { return m_path.c_str(); }
143 inline void xsi_named_mutex::swap(xsi_named_mutex &other)
145 std::swap(m_key, other.m_key);
146 std::swap(m_id, other.m_id);
147 std::swap(m_semid, other.m_semid);
148 std::swap(m_perm, other.m_perm);
149 m_path.swap(other.m_path);
152 inline mapping_handle_t xsi_named_mutex::get_mapping_handle() const
153 { mapping_handle_t mhnd = { m_semid, true}; return mhnd; }
155 inline int xsi_named_mutex::get_permissions() const
158 inline bool xsi_named_mutex::priv_open_or_create
159 (ipcdetail::create_enum_t type, const char *path, boost::uint8_t id, int perm)
163 key = ::ftok(path, id);
164 if(((key_t)-1) == key){
165 error_info err = system_error_code();
166 throw interprocess_exception(err);
176 if(!xsi::simple_sem_open_or_create(key, 1, semid, perm)){
177 error_info err = system_error_code();
178 throw interprocess_exception(err);
183 m_path = path ? path : "";
190 inline void xsi_named_mutex::priv_close()
194 inline void xsi_named_mutex::lock()
196 if(!xsi::simple_sem_op(m_semid, -1)){
197 error_info err = system_error_code();
198 throw interprocess_exception(err);
202 inline void xsi_named_mutex::unlock()
204 bool success = xsi::simple_sem_op(m_semid, 1);
206 BOOST_ASSERT(success);
209 inline bool xsi_named_mutex::remove()
212 int ret = ::semctl(m_semid, IPC_RMID, 0);
215 //Now put it in default-constructed state
225 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
227 } //namespace interprocess {
228 } //namespace boost {
230 #include <boost/interprocess/detail/config_end.hpp>
232 #endif //BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP