|
||||
File indexing completed on 2025-01-18 09:38:32
0001 ////////////////////////////////////////////////////////////////////////////// 0002 // 0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost 0004 // Software License, Version 1.0. (See accompanying file 0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0006 // 0007 // See http://www.boost.org/libs/interprocess for documentation. 0008 // 0009 ////////////////////////////////////////////////////////////////////////////// 0010 0011 #ifndef BOOST_INTERPROCESS_NAMED_UPGRADABLE_MUTEX_HPP 0012 #define BOOST_INTERPROCESS_NAMED_UPGRADABLE_MUTEX_HPP 0013 0014 #ifndef BOOST_CONFIG_HPP 0015 # include <boost/config.hpp> 0016 #endif 0017 # 0018 #if defined(BOOST_HAS_PRAGMA_ONCE) 0019 # pragma once 0020 #endif 0021 0022 #include <boost/interprocess/detail/config_begin.hpp> 0023 #include <boost/interprocess/detail/workaround.hpp> 0024 #include <boost/interprocess/creation_tags.hpp> 0025 #include <boost/interprocess/exceptions.hpp> 0026 #include <boost/interprocess/shared_memory_object.hpp> 0027 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp> 0028 #include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp> 0029 #include <boost/interprocess/sync/shm/named_creation_functor.hpp> 0030 #include <boost/interprocess/permissions.hpp> 0031 0032 //!\file 0033 //!Describes a named upgradable mutex class for inter-process synchronization 0034 0035 namespace boost { 0036 namespace interprocess { 0037 0038 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 0039 namespace ipcdetail{ class interprocess_tester; } 0040 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 0041 0042 class named_condition; 0043 0044 //!A upgradable mutex with a global name, so it can be found from different 0045 //!processes. This mutex can't be placed in shared memory, and 0046 //!each process should have it's own named upgradable mutex. 0047 class named_upgradable_mutex 0048 { 0049 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 0050 //Non-copyable 0051 named_upgradable_mutex(); 0052 named_upgradable_mutex(const named_upgradable_mutex &); 0053 named_upgradable_mutex &operator=(const named_upgradable_mutex &); 0054 friend class named_condition; 0055 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 0056 public: 0057 0058 //!Creates a global upgradable mutex with a name. 0059 //!If the upgradable mutex can't be created throws interprocess_exception 0060 named_upgradable_mutex(create_only_t, const char *name, const permissions &perm = permissions()); 0061 0062 //!Opens or creates a global upgradable mutex with a name, and an initial count. 0063 //!If the upgradable mutex is created, this call is equivalent to 0064 //!named_upgradable_mutex(create_only_t, ...) 0065 //!If the upgradable mutex is already created, this call is equivalent to 0066 //!named_upgradable_mutex(open_only_t, ... ). 0067 named_upgradable_mutex(open_or_create_t, const char *name, const permissions &perm = permissions()); 0068 0069 //!Opens a global upgradable mutex with a name if that upgradable mutex 0070 //!is previously. 0071 //!created. If it is not previously created this function throws 0072 //!interprocess_exception. 0073 named_upgradable_mutex(open_only_t, const char *name); 0074 0075 //!Destroys *this and indicates that the calling process is finished using 0076 //!the resource. The destructor function will deallocate 0077 //!any system resources allocated by the system for use by this process for 0078 //!this resource. The resource can still be opened again calling 0079 //!the open constructor overload. To erase the resource from the system 0080 //!use remove(). 0081 ~named_upgradable_mutex(); 0082 0083 //Exclusive locking 0084 0085 //!Effects: The calling thread tries to obtain exclusive ownership of the mutex, 0086 //! and if another thread has exclusive, sharable or upgradable ownership of 0087 //! the mutex, it waits until it can obtain the ownership. 0088 //!Throws: interprocess_exception on error. 0089 void lock(); 0090 0091 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex 0092 //! without waiting. If no other thread has exclusive, sharable or upgradable 0093 //! ownership of the mutex this succeeds. 0094 //!Returns: If it can acquire exclusive ownership immediately returns true. 0095 //! If it has to wait, returns false. 0096 //!Throws: interprocess_exception on error. 0097 bool try_lock(); 0098 0099 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex 0100 //! waiting if necessary until no other thread has exclusive, sharable or 0101 //! upgradable ownership of the mutex or abs_time is reached. 0102 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. 0103 //!Throws: interprocess_exception on error. 0104 template<class TimePoint> 0105 bool timed_lock(const TimePoint &abs_time); 0106 0107 //!Precondition: The thread must have exclusive ownership of the mutex. 0108 //!Effects: The calling thread releases the exclusive ownership of the mutex. 0109 //!Throws: An exception derived from interprocess_exception on error. 0110 void unlock(); 0111 0112 //Sharable locking 0113 0114 //!Effects: The calling thread tries to obtain sharable ownership of the mutex, 0115 //! and if another thread has exclusive ownership of the mutex, 0116 //! waits until it can obtain the ownership. 0117 //!Throws: interprocess_exception on error. 0118 void lock_sharable(); 0119 0120 //!Effects: The calling thread tries to acquire sharable ownership of the mutex 0121 //! without waiting. If no other thread has exclusive ownership 0122 //! of the mutex this succeeds. 0123 //!Returns: If it can acquire sharable ownership immediately returns true. If it 0124 //! has to wait, returns false. 0125 //!Throws: interprocess_exception on error. 0126 bool try_lock_sharable(); 0127 0128 //!Effects: The calling thread tries to acquire sharable ownership of the mutex 0129 //! waiting if necessary until no other thread has exclusive 0130 //! ownership of the mutex or abs_time is reached. 0131 //!Returns: If acquires sharable ownership, returns true. Otherwise returns false. 0132 //!Throws: interprocess_exception on error. 0133 template<class TimePoint> 0134 bool timed_lock_sharable(const TimePoint &abs_time); 0135 0136 //!Precondition: The thread must have sharable ownership of the mutex. 0137 //!Effects: The calling thread releases the sharable ownership of the mutex. 0138 //!Throws: An exception derived from interprocess_exception on error. 0139 void unlock_sharable(); 0140 0141 //Upgradable locking 0142 0143 //!Effects: The calling thread tries to obtain upgradable ownership of the mutex, 0144 //! and if another thread has exclusive or upgradable ownership of the mutex, 0145 //! waits until it can obtain the ownership. 0146 //!Throws: interprocess_exception on error. 0147 void lock_upgradable(); 0148 0149 //!Effects: The calling thread tries to acquire upgradable ownership of the mutex 0150 //! without waiting. If no other thread has exclusive or upgradable ownership 0151 //! of the mutex this succeeds. 0152 //!Returns: If it can acquire upgradable ownership immediately returns true. 0153 //! If it has to wait, returns false. 0154 //!Throws: interprocess_exception on error. 0155 bool try_lock_upgradable(); 0156 0157 //!Effects: The calling thread tries to acquire upgradable ownership of the mutex 0158 //! waiting if necessary until no other thread has exclusive or upgradable 0159 //! ownership of the mutex or abs_time is reached. 0160 //!Returns: If acquires upgradable ownership, returns true. Otherwise returns false. 0161 //!Throws: interprocess_exception on error. 0162 template<class TimePoint> 0163 bool timed_lock_upgradable(const TimePoint &abs_time); 0164 0165 //!Precondition: The thread must have upgradable ownership of the mutex. 0166 //!Effects: The calling thread releases the upgradable ownership of the mutex. 0167 //!Throws: An exception derived from interprocess_exception on error. 0168 void unlock_upgradable(); 0169 0170 //Demotions 0171 0172 //!Precondition: The thread must have exclusive ownership of the mutex. 0173 //!Effects: The thread atomically releases exclusive ownership and acquires 0174 //! upgradable ownership. This operation is non-blocking. 0175 //!Throws: An exception derived from interprocess_exception on error. 0176 void unlock_and_lock_upgradable(); 0177 0178 //!Precondition: The thread must have exclusive ownership of the mutex. 0179 //!Effects: The thread atomically releases exclusive ownership and acquires 0180 //! sharable ownership. This operation is non-blocking. 0181 //!Throws: An exception derived from interprocess_exception on error. 0182 void unlock_and_lock_sharable(); 0183 0184 //!Precondition: The thread must have upgradable ownership of the mutex. 0185 //!Effects: The thread atomically releases upgradable ownership and acquires 0186 //! sharable ownership. This operation is non-blocking. 0187 //!Throws: An exception derived from interprocess_exception on error. 0188 void unlock_upgradable_and_lock_sharable(); 0189 0190 //Promotions 0191 0192 //!Precondition: The thread must have upgradable ownership of the mutex. 0193 //!Effects: The thread atomically releases upgradable ownership and acquires 0194 //! exclusive ownership. This operation will block until all threads with 0195 //! sharable ownership release it. 0196 //!Throws: An exception derived from interprocess_exception on error. 0197 void unlock_upgradable_and_lock(); 0198 0199 //!Precondition: The thread must have upgradable ownership of the mutex. 0200 //!Effects: The thread atomically releases upgradable ownership and tries to 0201 //! acquire exclusive ownership. This operation will fail if there are threads 0202 //! with sharable ownership, but it will maintain upgradable ownership. 0203 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. 0204 //!Throws: An exception derived from interprocess_exception on error. 0205 bool try_unlock_upgradable_and_lock(); 0206 0207 //!Precondition: The thread must have upgradable ownership of the mutex. 0208 //!Effects: The thread atomically releases upgradable ownership and tries to acquire 0209 //! exclusive ownership, waiting if necessary until abs_time. This operation will 0210 //! fail if there are threads with sharable ownership or timeout reaches, but it 0211 //! will maintain upgradable ownership. 0212 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. 0213 //!Throws: An exception derived from interprocess_exception on error. 0214 template<class TimePoint> 0215 bool timed_unlock_upgradable_and_lock(const TimePoint &abs_time); 0216 0217 //!Precondition: The thread must have sharable ownership of the mutex. 0218 //!Effects: The thread atomically releases sharable ownership and tries to acquire 0219 //! exclusive ownership. This operation will fail if there are threads with sharable 0220 //! or upgradable ownership, but it will maintain sharable ownership. 0221 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. 0222 //!Throws: An exception derived from interprocess_exception on error. 0223 bool try_unlock_sharable_and_lock(); 0224 0225 bool try_unlock_sharable_and_lock_upgradable(); 0226 0227 //!Erases a named upgradable mutex from the system. 0228 //!Returns false on error. Never throws. 0229 static bool remove(const char *name); 0230 0231 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 0232 private: 0233 friend class ipcdetail::interprocess_tester; 0234 void dont_close_on_destruction(); 0235 0236 interprocess_upgradable_mutex *mutex() const 0237 { return static_cast<interprocess_upgradable_mutex*>(m_shmem.get_user_address()); } 0238 0239 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t; 0240 open_create_impl_t m_shmem; 0241 typedef ipcdetail::named_creation_functor<interprocess_upgradable_mutex> construct_func_t; 0242 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 0243 }; 0244 0245 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 0246 0247 inline named_upgradable_mutex::~named_upgradable_mutex() 0248 {} 0249 0250 inline named_upgradable_mutex::named_upgradable_mutex 0251 (create_only_t, const char *name, const permissions &perm) 0252 : m_shmem (create_only_t() 0253 ,name 0254 ,sizeof(interprocess_upgradable_mutex) + 0255 open_create_impl_t::ManagedOpenOrCreateUserOffset 0256 ,read_write 0257 ,0 0258 ,construct_func_t(ipcdetail::DoCreate) 0259 ,perm) 0260 {} 0261 0262 inline named_upgradable_mutex::named_upgradable_mutex 0263 (open_or_create_t, const char *name, const permissions &perm) 0264 : m_shmem (open_or_create_t() 0265 ,name 0266 ,sizeof(interprocess_upgradable_mutex) + 0267 open_create_impl_t::ManagedOpenOrCreateUserOffset 0268 ,read_write 0269 ,0 0270 ,construct_func_t(ipcdetail::DoOpenOrCreate) 0271 ,perm) 0272 {} 0273 0274 inline named_upgradable_mutex::named_upgradable_mutex 0275 (open_only_t, const char *name) 0276 : m_shmem (open_only_t() 0277 ,name 0278 ,read_write 0279 ,0 0280 ,construct_func_t(ipcdetail::DoOpen)) 0281 {} 0282 0283 inline void named_upgradable_mutex::dont_close_on_destruction() 0284 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); } 0285 0286 inline void named_upgradable_mutex::lock() 0287 { this->mutex()->lock(); } 0288 0289 inline void named_upgradable_mutex::unlock() 0290 { this->mutex()->unlock(); } 0291 0292 inline bool named_upgradable_mutex::try_lock() 0293 { return this->mutex()->try_lock(); } 0294 0295 template<class TimePoint> 0296 inline bool named_upgradable_mutex::timed_lock(const TimePoint &abs_time) 0297 { return this->mutex()->timed_lock(abs_time); } 0298 0299 inline void named_upgradable_mutex::lock_upgradable() 0300 { this->mutex()->lock_upgradable(); } 0301 0302 inline void named_upgradable_mutex::unlock_upgradable() 0303 { this->mutex()->unlock_upgradable(); } 0304 0305 inline bool named_upgradable_mutex::try_lock_upgradable() 0306 { return this->mutex()->try_lock_upgradable(); } 0307 0308 template<class TimePoint> 0309 inline bool named_upgradable_mutex::timed_lock_upgradable(const TimePoint &abs_time) 0310 { return this->mutex()->timed_lock_upgradable(abs_time); } 0311 0312 inline void named_upgradable_mutex::lock_sharable() 0313 { this->mutex()->lock_sharable(); } 0314 0315 inline void named_upgradable_mutex::unlock_sharable() 0316 { this->mutex()->unlock_sharable(); } 0317 0318 inline bool named_upgradable_mutex::try_lock_sharable() 0319 { return this->mutex()->try_lock_sharable(); } 0320 0321 template<class TimePoint> 0322 inline bool named_upgradable_mutex::timed_lock_sharable(const TimePoint &abs_time) 0323 { return this->mutex()->timed_lock_sharable(abs_time); } 0324 0325 inline void named_upgradable_mutex::unlock_and_lock_upgradable() 0326 { this->mutex()->unlock_and_lock_upgradable(); } 0327 0328 inline void named_upgradable_mutex::unlock_and_lock_sharable() 0329 { this->mutex()->unlock_and_lock_sharable(); } 0330 0331 inline void named_upgradable_mutex::unlock_upgradable_and_lock_sharable() 0332 { this->mutex()->unlock_upgradable_and_lock_sharable(); } 0333 0334 inline void named_upgradable_mutex::unlock_upgradable_and_lock() 0335 { this->mutex()->unlock_upgradable_and_lock(); } 0336 0337 inline bool named_upgradable_mutex::try_unlock_upgradable_and_lock() 0338 { return this->mutex()->try_unlock_upgradable_and_lock(); } 0339 0340 template<class TimePoint> 0341 inline bool named_upgradable_mutex::timed_unlock_upgradable_and_lock(const TimePoint &abs_time) 0342 { return this->mutex()->timed_unlock_upgradable_and_lock(abs_time); } 0343 0344 inline bool named_upgradable_mutex::try_unlock_sharable_and_lock() 0345 { return this->mutex()->try_unlock_sharable_and_lock(); } 0346 0347 inline bool named_upgradable_mutex::try_unlock_sharable_and_lock_upgradable() 0348 { return this->mutex()->try_unlock_sharable_and_lock_upgradable(); } 0349 0350 inline bool named_upgradable_mutex::remove(const char *name) 0351 { return shared_memory_object::remove(name); } 0352 0353 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 0354 0355 } //namespace interprocess { 0356 } //namespace boost { 0357 0358 #include <boost/interprocess/detail/config_end.hpp> 0359 0360 #endif //BOOST_INTERPROCESS_NAMED_UPGRADABLE_MUTEX_HPP
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |