annotate DEPENDENCIES/generic/include/boost/interprocess/sync/named_upgradable_mutex.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6 //
Chris@16 7 // See http://www.boost.org/libs/interprocess for documentation.
Chris@16 8 //
Chris@16 9 //////////////////////////////////////////////////////////////////////////////
Chris@16 10
Chris@16 11 #ifndef BOOST_INTERPROCESS_named_upgradable_mutex_HPP
Chris@16 12 #define BOOST_INTERPROCESS_named_upgradable_mutex_HPP
Chris@16 13
Chris@101 14 #ifndef BOOST_CONFIG_HPP
Chris@101 15 # include <boost/config.hpp>
Chris@101 16 #endif
Chris@101 17 #
Chris@101 18 #if defined(BOOST_HAS_PRAGMA_ONCE)
Chris@16 19 # pragma once
Chris@16 20 #endif
Chris@16 21
Chris@16 22 #include <boost/interprocess/detail/config_begin.hpp>
Chris@16 23 #include <boost/interprocess/detail/workaround.hpp>
Chris@16 24 #include <boost/interprocess/creation_tags.hpp>
Chris@16 25 #include <boost/interprocess/exceptions.hpp>
Chris@16 26 #include <boost/interprocess/shared_memory_object.hpp>
Chris@16 27 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
Chris@16 28 #include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
Chris@16 29 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
Chris@16 30 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
Chris@16 31 #include <boost/interprocess/permissions.hpp>
Chris@16 32
Chris@16 33 //!\file
Chris@16 34 //!Describes a named upgradable mutex class for inter-process synchronization
Chris@16 35
Chris@16 36 namespace boost {
Chris@16 37 namespace interprocess {
Chris@16 38
Chris@101 39 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 40 namespace ipcdetail{ class interprocess_tester; }
Chris@101 41 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 42
Chris@16 43 class named_condition;
Chris@16 44
Chris@16 45 //!A upgradable mutex with a global name, so it can be found from different
Chris@16 46 //!processes. This mutex can't be placed in shared memory, and
Chris@16 47 //!each process should have it's own named upgradable mutex.
Chris@16 48 class named_upgradable_mutex
Chris@16 49 {
Chris@101 50 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 51 //Non-copyable
Chris@16 52 named_upgradable_mutex();
Chris@16 53 named_upgradable_mutex(const named_upgradable_mutex &);
Chris@16 54 named_upgradable_mutex &operator=(const named_upgradable_mutex &);
Chris@16 55 friend class named_condition;
Chris@101 56 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 57 public:
Chris@16 58
Chris@16 59 //!Creates a global upgradable mutex with a name.
Chris@16 60 //!If the upgradable mutex can't be created throws interprocess_exception
Chris@16 61 named_upgradable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
Chris@16 62
Chris@16 63 //!Opens or creates a global upgradable mutex with a name.
Chris@16 64 //!If the upgradable mutex is created, this call is equivalent to
Chris@16 65 //!named_upgradable_mutex(create_only_t, ...)
Chris@16 66 //!If the upgradable mutex is already created, this call is equivalent to
Chris@16 67 //!named_upgradable_mutex(open_only_t, ... ).
Chris@16 68 named_upgradable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
Chris@16 69
Chris@16 70 //!Opens a global upgradable mutex with a name if that upgradable mutex
Chris@16 71 //!is previously.
Chris@16 72 //!created. If it is not previously created this function throws
Chris@16 73 //!interprocess_exception.
Chris@16 74 named_upgradable_mutex(open_only_t open_only, const char *name);
Chris@16 75
Chris@16 76 //!Destroys *this and indicates that the calling process is finished using
Chris@16 77 //!the resource. The destructor function will deallocate
Chris@16 78 //!any system resources allocated by the system for use by this process for
Chris@16 79 //!this resource. The resource can still be opened again calling
Chris@16 80 //!the open constructor overload. To erase the resource from the system
Chris@16 81 //!use remove().
Chris@16 82 ~named_upgradable_mutex();
Chris@16 83
Chris@16 84 //Exclusive locking
Chris@16 85
Chris@16 86 //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
Chris@16 87 //! and if another thread has exclusive, sharable or upgradable ownership of
Chris@16 88 //! the mutex, it waits until it can obtain the ownership.
Chris@16 89 //!Throws: interprocess_exception on error.
Chris@16 90 void lock();
Chris@16 91
Chris@16 92 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
Chris@16 93 //! without waiting. If no other thread has exclusive, sharable or upgradable
Chris@16 94 //! ownership of the mutex this succeeds.
Chris@16 95 //!Returns: If it can acquire exclusive ownership immediately returns true.
Chris@16 96 //! If it has to wait, returns false.
Chris@16 97 //!Throws: interprocess_exception on error.
Chris@16 98 bool try_lock();
Chris@16 99
Chris@16 100 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
Chris@16 101 //! waiting if necessary until no other thread has exclusive, sharable or
Chris@16 102 //! upgradable ownership of the mutex or abs_time is reached.
Chris@16 103 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
Chris@16 104 //!Throws: interprocess_exception on error.
Chris@16 105 bool timed_lock(const boost::posix_time::ptime &abs_time);
Chris@16 106
Chris@16 107 //!Precondition: The thread must have exclusive ownership of the mutex.
Chris@16 108 //!Effects: The calling thread releases the exclusive ownership of the mutex.
Chris@16 109 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 110 void unlock();
Chris@16 111
Chris@16 112 //Sharable locking
Chris@16 113
Chris@16 114 //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
Chris@16 115 //! and if another thread has exclusive ownership of the mutex,
Chris@16 116 //! waits until it can obtain the ownership.
Chris@16 117 //!Throws: interprocess_exception on error.
Chris@16 118 void lock_sharable();
Chris@16 119
Chris@16 120 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
Chris@16 121 //! without waiting. If no other thread has exclusive ownership
Chris@16 122 //! of the mutex this succeeds.
Chris@16 123 //!Returns: If it can acquire sharable ownership immediately returns true. If it
Chris@16 124 //! has to wait, returns false.
Chris@16 125 //!Throws: interprocess_exception on error.
Chris@16 126 bool try_lock_sharable();
Chris@16 127
Chris@16 128 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
Chris@16 129 //! waiting if necessary until no other thread has exclusive
Chris@16 130 //! ownership of the mutex or abs_time is reached.
Chris@16 131 //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
Chris@16 132 //!Throws: interprocess_exception on error.
Chris@16 133 bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
Chris@16 134
Chris@16 135 //!Precondition: The thread must have sharable ownership of the mutex.
Chris@16 136 //!Effects: The calling thread releases the sharable ownership of the mutex.
Chris@16 137 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 138 void unlock_sharable();
Chris@16 139
Chris@16 140 //Upgradable locking
Chris@16 141
Chris@16 142 //!Effects: The calling thread tries to obtain upgradable ownership of the mutex,
Chris@16 143 //! and if another thread has exclusive or upgradable ownership of the mutex,
Chris@16 144 //! waits until it can obtain the ownership.
Chris@16 145 //!Throws: interprocess_exception on error.
Chris@16 146 void lock_upgradable();
Chris@16 147
Chris@16 148 //!Effects: The calling thread tries to acquire upgradable ownership of the mutex
Chris@16 149 //! without waiting. If no other thread has exclusive or upgradable ownership
Chris@16 150 //! of the mutex this succeeds.
Chris@16 151 //!Returns: If it can acquire upgradable ownership immediately returns true.
Chris@16 152 //! If it has to wait, returns false.
Chris@16 153 //!Throws: interprocess_exception on error.
Chris@16 154 bool try_lock_upgradable();
Chris@16 155
Chris@16 156 //!Effects: The calling thread tries to acquire upgradable ownership of the mutex
Chris@16 157 //! waiting if necessary until no other thread has exclusive or upgradable
Chris@16 158 //! ownership of the mutex or abs_time is reached.
Chris@16 159 //!Returns: If acquires upgradable ownership, returns true. Otherwise returns false.
Chris@16 160 //!Throws: interprocess_exception on error.
Chris@16 161 bool timed_lock_upgradable(const boost::posix_time::ptime &abs_time);
Chris@16 162
Chris@16 163 //!Precondition: The thread must have upgradable ownership of the mutex.
Chris@16 164 //!Effects: The calling thread releases the upgradable ownership of the mutex.
Chris@16 165 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 166 void unlock_upgradable();
Chris@16 167
Chris@16 168 //Demotions
Chris@16 169
Chris@16 170 //!Precondition: The thread must have exclusive ownership of the mutex.
Chris@16 171 //!Effects: The thread atomically releases exclusive ownership and acquires
Chris@16 172 //! upgradable ownership. This operation is non-blocking.
Chris@16 173 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 174 void unlock_and_lock_upgradable();
Chris@16 175
Chris@16 176 //!Precondition: The thread must have exclusive ownership of the mutex.
Chris@16 177 //!Effects: The thread atomically releases exclusive ownership and acquires
Chris@16 178 //! sharable ownership. This operation is non-blocking.
Chris@16 179 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 180 void unlock_and_lock_sharable();
Chris@16 181
Chris@16 182 //!Precondition: The thread must have upgradable ownership of the mutex.
Chris@16 183 //!Effects: The thread atomically releases upgradable ownership and acquires
Chris@16 184 //! sharable ownership. This operation is non-blocking.
Chris@16 185 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 186 void unlock_upgradable_and_lock_sharable();
Chris@16 187
Chris@16 188 //Promotions
Chris@16 189
Chris@16 190 //!Precondition: The thread must have upgradable ownership of the mutex.
Chris@16 191 //!Effects: The thread atomically releases upgradable ownership and acquires
Chris@16 192 //! exclusive ownership. This operation will block until all threads with
Chris@16 193 //! sharable ownership release it.
Chris@16 194 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 195 void unlock_upgradable_and_lock();
Chris@16 196
Chris@16 197 //!Precondition: The thread must have upgradable ownership of the mutex.
Chris@16 198 //!Effects: The thread atomically releases upgradable ownership and tries to
Chris@16 199 //! acquire exclusive ownership. This operation will fail if there are threads
Chris@16 200 //! with sharable ownership, but it will maintain upgradable ownership.
Chris@16 201 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
Chris@16 202 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 203 bool try_unlock_upgradable_and_lock();
Chris@16 204
Chris@16 205 //!Precondition: The thread must have upgradable ownership of the mutex.
Chris@16 206 //!Effects: The thread atomically releases upgradable ownership and tries to acquire
Chris@16 207 //! exclusive ownership, waiting if necessary until abs_time. This operation will
Chris@16 208 //! fail if there are threads with sharable ownership or timeout reaches, but it
Chris@16 209 //! will maintain upgradable ownership.
Chris@16 210 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
Chris@16 211 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 212 bool timed_unlock_upgradable_and_lock(const boost::posix_time::ptime &abs_time);
Chris@16 213
Chris@16 214 //!Precondition: The thread must have sharable ownership of the mutex.
Chris@16 215 //!Effects: The thread atomically releases sharable ownership and tries to acquire
Chris@16 216 //! exclusive ownership. This operation will fail if there are threads with sharable
Chris@16 217 //! or upgradable ownership, but it will maintain sharable ownership.
Chris@16 218 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
Chris@16 219 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 220 bool try_unlock_sharable_and_lock();
Chris@16 221
Chris@16 222 //!Precondition: The thread must have sharable ownership of the mutex.
Chris@16 223 //!Effects: The thread atomically releases sharable ownership and tries to acquire
Chris@16 224 //! upgradable ownership. This operation will fail if there are threads with sharable
Chris@16 225 //! or upgradable ownership, but it will maintain sharable ownership.
Chris@16 226 //!Returns: If acquires upgradable ownership, returns true. Otherwise returns false.
Chris@16 227 //!Throws: An exception derived from interprocess_exception on error.
Chris@16 228 bool try_unlock_sharable_and_lock_upgradable();
Chris@16 229
Chris@16 230 //!Erases a named upgradable mutex from the system.
Chris@16 231 //!Returns false on error. Never throws.
Chris@16 232 static bool remove(const char *name);
Chris@16 233
Chris@101 234 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 235 private:
Chris@16 236 friend class ipcdetail::interprocess_tester;
Chris@16 237 void dont_close_on_destruction();
Chris@16 238
Chris@16 239 interprocess_upgradable_mutex *mutex() const
Chris@16 240 { return static_cast<interprocess_upgradable_mutex*>(m_shmem.get_user_address()); }
Chris@16 241
Chris@16 242 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
Chris@16 243 open_create_impl_t m_shmem;
Chris@16 244 typedef ipcdetail::named_creation_functor<interprocess_upgradable_mutex> construct_func_t;
Chris@101 245 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 246 };
Chris@16 247
Chris@101 248 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 249
Chris@16 250 inline named_upgradable_mutex::~named_upgradable_mutex()
Chris@16 251 {}
Chris@16 252
Chris@16 253 inline named_upgradable_mutex::named_upgradable_mutex
Chris@16 254 (create_only_t, const char *name, const permissions &perm)
Chris@16 255 : m_shmem (create_only
Chris@16 256 ,name
Chris@16 257 ,sizeof(interprocess_upgradable_mutex) +
Chris@16 258 open_create_impl_t::ManagedOpenOrCreateUserOffset
Chris@16 259 ,read_write
Chris@16 260 ,0
Chris@16 261 ,construct_func_t(ipcdetail::DoCreate)
Chris@16 262 ,perm)
Chris@16 263 {}
Chris@16 264
Chris@16 265 inline named_upgradable_mutex::named_upgradable_mutex
Chris@16 266 (open_or_create_t, const char *name, const permissions &perm)
Chris@16 267 : m_shmem (open_or_create
Chris@16 268 ,name
Chris@16 269 ,sizeof(interprocess_upgradable_mutex) +
Chris@16 270 open_create_impl_t::ManagedOpenOrCreateUserOffset
Chris@16 271 ,read_write
Chris@16 272 ,0
Chris@16 273 ,construct_func_t(ipcdetail::DoOpenOrCreate)
Chris@16 274 ,perm)
Chris@16 275 {}
Chris@16 276
Chris@16 277 inline named_upgradable_mutex::named_upgradable_mutex
Chris@16 278 (open_only_t, const char *name)
Chris@16 279 : m_shmem (open_only
Chris@16 280 ,name
Chris@16 281 ,read_write
Chris@16 282 ,0
Chris@16 283 ,construct_func_t(ipcdetail::DoOpen))
Chris@16 284 {}
Chris@16 285
Chris@16 286 inline void named_upgradable_mutex::dont_close_on_destruction()
Chris@16 287 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
Chris@16 288
Chris@16 289 inline void named_upgradable_mutex::lock()
Chris@16 290 { this->mutex()->lock(); }
Chris@16 291
Chris@16 292 inline void named_upgradable_mutex::unlock()
Chris@16 293 { this->mutex()->unlock(); }
Chris@16 294
Chris@16 295 inline bool named_upgradable_mutex::try_lock()
Chris@16 296 { return this->mutex()->try_lock(); }
Chris@16 297
Chris@16 298 inline bool named_upgradable_mutex::timed_lock
Chris@16 299 (const boost::posix_time::ptime &abs_time)
Chris@16 300 { return this->mutex()->timed_lock(abs_time); }
Chris@16 301
Chris@16 302 inline void named_upgradable_mutex::lock_upgradable()
Chris@16 303 { this->mutex()->lock_upgradable(); }
Chris@16 304
Chris@16 305 inline void named_upgradable_mutex::unlock_upgradable()
Chris@16 306 { this->mutex()->unlock_upgradable(); }
Chris@16 307
Chris@16 308 inline bool named_upgradable_mutex::try_lock_upgradable()
Chris@16 309 { return this->mutex()->try_lock_upgradable(); }
Chris@16 310
Chris@16 311 inline bool named_upgradable_mutex::timed_lock_upgradable
Chris@16 312 (const boost::posix_time::ptime &abs_time)
Chris@16 313 { return this->mutex()->timed_lock_upgradable(abs_time); }
Chris@16 314
Chris@16 315 inline void named_upgradable_mutex::lock_sharable()
Chris@16 316 { this->mutex()->lock_sharable(); }
Chris@16 317
Chris@16 318 inline void named_upgradable_mutex::unlock_sharable()
Chris@16 319 { this->mutex()->unlock_sharable(); }
Chris@16 320
Chris@16 321 inline bool named_upgradable_mutex::try_lock_sharable()
Chris@16 322 { return this->mutex()->try_lock_sharable(); }
Chris@16 323
Chris@16 324 inline bool named_upgradable_mutex::timed_lock_sharable
Chris@16 325 (const boost::posix_time::ptime &abs_time)
Chris@16 326 { return this->mutex()->timed_lock_sharable(abs_time); }
Chris@16 327
Chris@16 328 inline void named_upgradable_mutex::unlock_and_lock_upgradable()
Chris@16 329 { this->mutex()->unlock_and_lock_upgradable(); }
Chris@16 330
Chris@16 331 inline void named_upgradable_mutex::unlock_and_lock_sharable()
Chris@16 332 { this->mutex()->unlock_and_lock_sharable(); }
Chris@16 333
Chris@16 334 inline void named_upgradable_mutex::unlock_upgradable_and_lock_sharable()
Chris@16 335 { this->mutex()->unlock_upgradable_and_lock_sharable(); }
Chris@16 336
Chris@16 337 inline void named_upgradable_mutex::unlock_upgradable_and_lock()
Chris@16 338 { this->mutex()->unlock_upgradable_and_lock(); }
Chris@16 339
Chris@16 340 inline bool named_upgradable_mutex::try_unlock_upgradable_and_lock()
Chris@16 341 { return this->mutex()->try_unlock_upgradable_and_lock(); }
Chris@16 342
Chris@16 343 inline bool named_upgradable_mutex::timed_unlock_upgradable_and_lock
Chris@16 344 (const boost::posix_time::ptime &abs_time)
Chris@16 345 { return this->mutex()->timed_unlock_upgradable_and_lock(abs_time); }
Chris@16 346
Chris@16 347 inline bool named_upgradable_mutex::try_unlock_sharable_and_lock()
Chris@16 348 { return this->mutex()->try_unlock_sharable_and_lock(); }
Chris@16 349
Chris@16 350 inline bool named_upgradable_mutex::try_unlock_sharable_and_lock_upgradable()
Chris@16 351 { return this->mutex()->try_unlock_sharable_and_lock_upgradable(); }
Chris@16 352
Chris@16 353 inline bool named_upgradable_mutex::remove(const char *name)
Chris@16 354 { return shared_memory_object::remove(name); }
Chris@16 355
Chris@101 356 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 357
Chris@16 358 } //namespace interprocess {
Chris@16 359 } //namespace boost {
Chris@16 360
Chris@16 361 #include <boost/interprocess/detail/config_end.hpp>
Chris@16 362
Chris@16 363 #endif //BOOST_INTERPROCESS_named_upgradable_mutex_HPP