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