Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / sync / xsi / xsi_named_mutex.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP
12 #define BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP
13
14 #if defined(_MSC_VER)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20
21 #if defined(BOOST_INTERPROCESS_WINDOWS)
22 #error "This header can't be used in Windows operating systems"
23 #endif
24
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>
33 #include <cstddef>
34 #include <boost/assert.hpp>
35 #include <boost/cstdint.hpp>
36 #include <string>
37 #include <boost/assert.hpp>
38
39 //!\file
40 //!Describes a class representing a xsi-based named_mutex.
41
42 namespace boost {
43 namespace interprocess {
44
45 //!A class that wraps a XSI (System V)-based named semaphore
46 //!that undoes the operation if the process crashes.
47 class xsi_named_mutex
48 {
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
54
55    public:
56    BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_named_mutex)
57
58    //!Default constructor.
59    //!Represents an empty xsi_named_mutex.
60    xsi_named_mutex();
61
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);  }
68
69    //!Moves the ownership of "moved"'s named mutex to *this.
70    //!After the call, "moved" does not represent any named mutex
71    //!Does not throw
72    xsi_named_mutex(BOOST_RV_REF(xsi_named_mutex) moved)
73    {  this->swap(moved);   }
74
75    //!Moves the ownership of "moved"'s named mutex to *this.
76    //!After the call, "moved" does not represent any named mutex.
77    //!Does not throw
78    xsi_named_mutex &operator=(BOOST_RV_REF(xsi_named_mutex) moved)
79    {
80       xsi_named_mutex tmp(boost::move(moved));
81       this->swap(tmp);
82       return *this;
83    }
84
85    //!Swaps two xsi_named_mutex. Does not throw
86    void swap(xsi_named_mutex &other);
87
88    //!Destroys *this. The named mutex is still valid after
89    //!destruction. use remove() to destroy the named mutex.
90    ~xsi_named_mutex();
91
92    //!Returns the path used to construct the
93    //!named mutex.
94    const char *get_path() const;
95
96    //!Returns access
97    //!permissions
98    int get_permissions() const;
99
100    //!Returns the mapping handle.
101    //!Never throws
102    mapping_handle_t get_mapping_handle() const;
103
104    //!Erases a XSI-based named mutex from the system.
105    //!Returns false on error. Never throws
106    bool remove();
107
108    void lock();
109
110    void unlock();
111
112    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
113    private:
114
115    //!Closes a previously opened file mapping. Never throws.
116    void priv_close();
117
118    //!Closes a previously opened file mapping. Never throws.
119    bool priv_open_or_create( ipcdetail::create_enum_t type
120                            , const char *path
121                            , boost::uint8_t id
122                            , int perm);
123    int            m_semid;
124    key_t          m_key;
125    boost::uint8_t m_id;
126    int            m_perm;
127    std::string    m_path;
128    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
129 };
130
131 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
132
133 inline xsi_named_mutex::xsi_named_mutex()
134    :  m_semid(-1), m_key(-1), m_id(0), m_perm(0), m_path()
135 {}
136
137 inline xsi_named_mutex::~xsi_named_mutex()
138 {  this->priv_close(); }
139
140 inline const char *xsi_named_mutex::get_path() const
141 {  return m_path.c_str(); }
142
143 inline void xsi_named_mutex::swap(xsi_named_mutex &other)
144 {
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);
150 }
151
152 inline mapping_handle_t xsi_named_mutex::get_mapping_handle() const
153 {  mapping_handle_t mhnd = { m_semid, true};   return mhnd;   }
154
155 inline int xsi_named_mutex::get_permissions() const
156 {  return m_perm; }
157
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)
160 {
161    key_t key;
162    if(path){
163       key  = ::ftok(path, id);
164       if(((key_t)-1) == key){
165          error_info err = system_error_code();
166          throw interprocess_exception(err);
167       }
168    }
169    else{
170       key = IPC_PRIVATE;
171    }
172
173    perm &= 0x01FF;
174
175    int semid;
176    if(!xsi::simple_sem_open_or_create(key, 1, semid, perm)){
177       error_info err = system_error_code();
178       throw interprocess_exception(err);
179    }
180
181    m_perm = perm;
182    m_semid = semid;
183    m_path = path ? path : "";
184    m_id   = id;
185    m_key  = key;
186
187    return true;
188 }
189
190 inline void xsi_named_mutex::priv_close()
191 {
192 }
193
194 inline void xsi_named_mutex::lock()
195 {
196    if(!xsi::simple_sem_op(m_semid, -1)){
197       error_info err = system_error_code();
198       throw interprocess_exception(err);
199    }
200 }
201
202 inline void xsi_named_mutex::unlock()
203 {
204    bool success = xsi::simple_sem_op(m_semid, 1);
205    (void)success;
206    BOOST_ASSERT(success);
207 }
208
209 inline bool xsi_named_mutex::remove()
210 {
211    if(m_semid != -1){
212       int ret = ::semctl(m_semid, IPC_RMID, 0);
213       if(-1 == ret)
214          return false;
215       //Now put it in default-constructed state
216       m_semid  = -1;
217       m_key    = -1;
218       m_id     = 0;
219       m_perm   = 0;
220       m_path.clear();
221    }
222    return false;
223 }
224
225 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
226
227 }  //namespace interprocess {
228 }  //namespace boost {
229
230 #include <boost/interprocess/detail/config_end.hpp>
231
232 #endif   //BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP