Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:09:31

0001 // (C) Copyright 2012 Vicente Botet
0002 //
0003 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_THREAD_LOCKABLE_CONCEPTS_HPP
0007 #define BOOST_THREAD_LOCKABLE_CONCEPTS_HPP
0008 
0009 #include <boost/chrono/chrono.hpp>
0010 #include <boost/concept_check.hpp>
0011 
0012 namespace boost
0013 {
0014 
0015   /**
0016    * BasicLockable object supports the basic features
0017    * required to delimit a critical region
0018    * Supports the basic lock and unlock functions.
0019    */
0020 
0021   //[BasicLockable
0022   template <typename Mutex>
0023   struct BasicLockable
0024   {
0025 
0026     BOOST_CONCEPT_USAGE(BasicLockable)
0027     {
0028       l.lock();
0029       l.unlock();
0030     }
0031     BasicLockable() : l(*static_cast<Mutex*>(0)) {}
0032   private:
0033     BasicLockable operator=(BasicLockable const&);
0034 
0035     Mutex& l;
0036   }
0037   ;
0038   //]
0039   /**
0040    * Lockable extends BasicLockable
0041    * with try_lock functions.
0042    */
0043 
0044   //[Lockable
0045   template <typename Mutex>
0046   struct Lockable
0047   {
0048     BOOST_CONCEPT_ASSERT(( BasicLockable<Mutex> ));
0049 
0050     BOOST_CONCEPT_USAGE(Lockable)
0051     {
0052       if (l.try_lock()) return;
0053     }
0054     Lockable() : l(*static_cast<Mutex*>(0)) {}
0055   private:
0056     Lockable operator=(Lockable const&);
0057     Mutex& l;
0058   };
0059   //]
0060 
0061   /**
0062    * TimedLockable object extends Lockable
0063    * with timed lock functions: try_lock_until and try_lock_for and the exception based lock_until and lock_for
0064    */
0065 
0066   //[TimedLockable
0067   template <typename Mutex>
0068   struct TimedLockable
0069   {
0070     BOOST_CONCEPT_ASSERT(( Lockable<Mutex> ));
0071 
0072     BOOST_CONCEPT_USAGE(TimedLockable)
0073     {
0074       if (l.try_lock_until(t)) return;
0075       if (l.try_lock_for(d)) return;
0076     }
0077     TimedLockable() : l(*static_cast<Mutex*>(0)) {}
0078   private:
0079     TimedLockable operator=(TimedLockable const&);
0080     Mutex& l;
0081     chrono::system_clock::time_point t;
0082     chrono::system_clock::duration d;
0083   };
0084   //]
0085 
0086   /**
0087    * SharedLockable object extends TimedLockable
0088    * with the lock_shared, lock_shared_until, lock_shared_for, try_lock_shared_until, try_lock_shared
0089    * and unlock_shared functions
0090    */
0091   //[SharedLockable
0092   template <typename Mutex>
0093   struct SharedLockable
0094   {
0095     BOOST_CONCEPT_ASSERT(( TimedLockable<Mutex> ));
0096 
0097     BOOST_CONCEPT_USAGE(SharedLockable)
0098     {
0099       l.lock_shared();
0100       l.unlock_shared();
0101       if (l.try_lock_shared()) return;
0102       if (l.try_lock_shared_until(t)) return;
0103       if (l.try_lock_shared_for(d)) return;
0104     }
0105     SharedLockable() : l(*static_cast<Mutex*>(0)) {}
0106   private:
0107     SharedLockable operator=(SharedLockable const&);
0108     Mutex& l;
0109     chrono::system_clock::time_point t;
0110     chrono::system_clock::duration d;
0111   };
0112   //]
0113 
0114   /**
0115    * UpgradeLockable object extends SharedLockable
0116    * with the lock_upgrade, lock_upgrade_until, unlock_upgrade_and_lock,
0117    * unlock_and_lock_shared and unlock_upgrade_and_lock_shared functions
0118    */
0119 
0120   //[UpgradeLockable
0121   template <typename Mutex>
0122   struct UpgradeLockable
0123   {
0124     BOOST_CONCEPT_ASSERT(( SharedLockable<Mutex> ));
0125 
0126     BOOST_CONCEPT_USAGE(UpgradeLockable)
0127     {
0128       l.lock_upgrade();
0129       l.unlock_upgrade();
0130       if (l.try_lock_upgrade()) return;
0131       if (l.try_lock_upgrade_until(t)) return;
0132       if (l.try_lock_upgrade_for(d)) return;
0133       if (l.try_unlock_shared_and_lock()) return;
0134       if (l.try_unlock_shared_and_lock_until(t)) return;
0135       if (l.try_unlock_shared_and_lock_for(d)) return;
0136       l.unlock_and_lock_shared();
0137       if (l.try_unlock_shared_and_lock_upgrade()) return;
0138       if (l.try_unlock_shared_and_lock_upgrade_until(t)) return;
0139       if (l.try_unlock_shared_and_lock_upgrade_for(d)) return;
0140       l.unlock_and_lock_upgrade();
0141       l.unlock_upgrade_and_lock();
0142       if (l.try_unlock_upgrade_and_lock()) return;
0143       if (l.try_unlock_upgrade_and_lock_until(t)) return;
0144       if (l.try_unlock_upgrade_and_lock_for(d)) return;
0145       l.unlock_upgrade_and_lock_shared();
0146     }
0147     UpgradeLockable() : l(*static_cast<Mutex*>(0)) {}
0148   private:
0149     UpgradeLockable operator=(UpgradeLockable const&);
0150     Mutex& l;
0151     chrono::system_clock::time_point t;
0152     chrono::system_clock::duration d;
0153   };
0154   //]
0155 
0156 }
0157 #endif