Imported Upstream version 1.51.0
[platform/upstream/boost.git] / boost / interprocess / sync / shm / named_recursive_mutex.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2011. 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_SHM_NAMED_RECURSIVE_MUTEX_HPP
12 #define BOOST_INTERPROCESS_SHM_NAMED_RECURSIVE_MUTEX_HPP
13
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/creation_tags.hpp>
21 #include <boost/interprocess/exceptions.hpp>
22 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
23 #include <boost/interprocess/shared_memory_object.hpp>
24 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
25 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
26 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
27 #include <boost/interprocess/permissions.hpp>
28
29 //!\file
30 //!Describes a named shm_named_recursive_mutex class for inter-process synchronization
31
32 namespace boost {
33 namespace interprocess {
34 namespace ipcdetail {
35
36 /// @cond
37 class interprocess_tester;
38 /// @endcond
39
40 class shm_named_recursive_mutex
41 {
42    /// @cond
43    //Non-copyable
44    shm_named_recursive_mutex();
45    shm_named_recursive_mutex(const shm_named_recursive_mutex &);
46    shm_named_recursive_mutex &operator=(const shm_named_recursive_mutex &);
47    /// @endcond
48    public:
49
50    //!Creates a global recursive_mutex with a name.
51    //!If the recursive_mutex can't be created throws interprocess_exception
52    shm_named_recursive_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
53
54    //!Opens or creates a global recursive_mutex with a name.
55    //!If the recursive_mutex is created, this call is equivalent to
56    //!shm_named_recursive_mutex(create_only_t, ... )
57    //!If the recursive_mutex is already created, this call is equivalent
58    //!shm_named_recursive_mutex(open_only_t, ... )
59    //!Does not throw
60    shm_named_recursive_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
61
62    //!Opens a global recursive_mutex with a name if that recursive_mutex is previously
63    //!created. If it is not previously created this function throws
64    //!interprocess_exception.
65    shm_named_recursive_mutex(open_only_t open_only, const char *name);
66
67    //!Destroys *this and indicates that the calling process is finished using
68    //!the resource. The destructor function will deallocate
69    //!any system resources allocated by the system for use by this process for
70    //!this resource. The resource can still be opened again calling
71    //!the open constructor overload. To erase the resource from the system
72    //!use remove().
73    ~shm_named_recursive_mutex();
74
75    //!Unlocks a previously locked
76    //!shm_named_recursive_mutex.
77    void unlock();
78
79    //!Locks shm_named_recursive_mutex, sleeps when shm_named_recursive_mutex is already locked.
80    //!Throws interprocess_exception if a severe error is found.
81    void lock();
82
83    //!Tries to lock the shm_named_recursive_mutex, returns false when shm_named_recursive_mutex
84    //!is already locked, returns true when success.
85    //!Throws interprocess_exception if a severe error is found.
86    bool try_lock();
87
88    //!Tries to lock the shm_named_recursive_mutex until time abs_time,
89    //!Returns false when timeout expires, returns true when locks.
90    //!Throws interprocess_exception if a severe error is found
91    bool timed_lock(const boost::posix_time::ptime &abs_time);
92
93    //!Erases a named recursive mutex
94    //!from the system
95    static bool remove(const char *name);
96
97    /// @cond
98    private:
99    friend class interprocess_tester;
100    void dont_close_on_destruction();
101
102    interprocess_recursive_mutex *mutex() const
103    {  return static_cast<interprocess_recursive_mutex*>(m_shmem.get_user_address()); }
104
105    managed_open_or_create_impl<shared_memory_object> m_shmem;
106    typedef named_creation_functor<interprocess_recursive_mutex> construct_func_t;
107    /// @endcond
108 };
109
110 inline shm_named_recursive_mutex::~shm_named_recursive_mutex()
111 {}
112
113 inline void shm_named_recursive_mutex::dont_close_on_destruction()
114 {  interprocess_tester::dont_close_on_destruction(m_shmem);  }
115
116 inline shm_named_recursive_mutex::shm_named_recursive_mutex(create_only_t, const char *name, const permissions &perm)
117    :  m_shmem  (create_only
118                ,name
119                ,sizeof(interprocess_recursive_mutex) +
120                   managed_open_or_create_impl<shared_memory_object>::
121                      ManagedOpenOrCreateUserOffset
122                ,read_write
123                ,0
124                ,construct_func_t(DoCreate)
125                ,perm)
126 {}
127
128 inline shm_named_recursive_mutex::shm_named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm)
129    :  m_shmem  (open_or_create
130                ,name
131                ,sizeof(interprocess_recursive_mutex) +
132                   managed_open_or_create_impl<shared_memory_object>::
133                      ManagedOpenOrCreateUserOffset
134                ,read_write
135                ,0
136                ,construct_func_t(DoOpenOrCreate)
137                ,perm)
138 {}
139
140 inline shm_named_recursive_mutex::shm_named_recursive_mutex(open_only_t, const char *name)
141    :  m_shmem  (open_only
142                ,name
143                ,read_write
144                ,0
145                ,construct_func_t(DoOpen))
146 {}
147
148 inline void shm_named_recursive_mutex::lock()
149 {  this->mutex()->lock();  }
150
151 inline void shm_named_recursive_mutex::unlock()
152 {  this->mutex()->unlock();  }
153
154 inline bool shm_named_recursive_mutex::try_lock()
155 {  return this->mutex()->try_lock();  }
156
157 inline bool shm_named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
158 {
159    if(abs_time == boost::posix_time::pos_infin){
160       this->lock();
161       return true;
162    }
163    return this->mutex()->timed_lock(abs_time);
164 }
165
166 inline bool shm_named_recursive_mutex::remove(const char *name)
167 {  return shared_memory_object::remove(name); }
168
169 }  //namespace ipcdetail {
170 }  //namespace interprocess {
171 }  //namespace boost {
172
173 #include <boost/interprocess/detail/config_end.hpp>
174
175 #endif   //BOOST_INTERPROCESS_SHM_NAMED_RECURSIVE_MUTEX_HPP