Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-19 09:29:16

0001 // Created on: 2005-04-10
0002 // Created by: Andrey BETENEV
0003 // Copyright (c) 2005-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Standard_Mutex_HeaderFile
0017 #define _Standard_Mutex_HeaderFile
0018 
0019 #include <Standard_Integer.hxx>
0020 #include <Standard_Boolean.hxx>
0021 #include <Standard_ErrorHandler.hxx>
0022 #include <Standard_Macro.hxx>
0023 #include <NCollection_Shared.hxx>
0024 
0025 #if defined(_WIN32)
0026   #include <windows.h>
0027 #else
0028   #include <pthread.h>
0029   #include <unistd.h>
0030   #include <time.h>
0031 #endif
0032 
0033 /**
0034  * @brief Mutex: a class to synchronize access to shared data.
0035  *
0036  * @deprecated This class is deprecated and will be removed in OCCT 8.0.0.
0037  *             Use std::mutex or std::shared_mutex directly instead.
0038  *
0039  * This is simple encapsulation of tools provided by the
0040  * operating system to synchronize access to shared data
0041  * from threads within one process.
0042  *
0043  * Current implementation is very simple and straightforward;
0044  * it is just a wrapper around POSIX pthread library on UNIX/Linux,
0045  * and CRITICAL_SECTIONs on Windows NT. It does not provide any
0046  * advanced functionality such as recursive calls to the same mutex from
0047  * within one thread (such call will freeze the execution).
0048  *
0049  * Note that all the methods of that class are made inline, in order
0050  * to keep maximal performance. This means that a library using the mutex
0051  * might need to be linked to threads library directly.
0052  *
0053  * The typical use of this class should be as follows:
0054  * - create instance of the class Standard_Mutex in the global scope
0055  *   (whenever possible, or as a field of your class)
0056  * - create instance of class Standard_Mutex::Sentry using that Mutex
0057  *   when entering critical section
0058  *
0059  * Note that this class provides one feature specific to Open CASCADE:
0060  * safe unlocking the mutex when signal is raised and converted to OCC
0061  * exceptions (Note that with current implementation of this functionality
0062  * on UNIX and Linux, C longjumps are used for that, thus destructors of
0063  * classes are not called automatically).
0064  *
0065  * To use this feature, call RegisterCallback() after Lock() or successful
0066  * TryLock(), and UnregisterCallback() before Unlock() (or use Sentry classes).
0067  */
0068 class Standard_DEPRECATED(
0069   "Standard_Mutex is deprecated; use std::mutex instead. Will be removed in OCCT 8.0.0")
0070   Standard_Mutex : public Standard_ErrorHandler::Callback
0071 {
0072 public:
0073   /**
0074    * @brief Simple sentry class providing convenient interface to mutex.
0075    *
0076    * Provides automatic locking and unlocking a mutex in its constructor
0077    * and destructor, thus ensuring correct unlock of the mutex even in case of
0078    * raising an exception or signal from the protected code.
0079    *
0080    * Create instance of that class when entering critical section.
0081    */
0082   class Sentry
0083   {
0084   public:
0085     //! Constructor - initializes the sentry object by reference to a
0086     //! mutex (which must be initialized) and locks the mutex immediately
0087     Sentry(Standard_Mutex& theMutex)
0088         : myMutex(&theMutex)
0089     {
0090       Lock();
0091     }
0092 
0093     //! Constructor - initializes the sentry object by pointer to a
0094     //! mutex and locks the mutex if its pointer is not NULL
0095     Sentry(Standard_Mutex* theMutex)
0096         : myMutex(theMutex)
0097     {
0098       if (myMutex != nullptr)
0099       {
0100         Lock();
0101       }
0102     }
0103 
0104     //! Destructor - unlocks the mutex if already locked.
0105     ~Sentry()
0106     {
0107       if (myMutex != nullptr)
0108       {
0109         Unlock();
0110       }
0111     }
0112 
0113   private:
0114     //! Lock the mutex
0115     void Lock()
0116     {
0117       myMutex->Lock();
0118       myMutex->RegisterCallback();
0119     }
0120 
0121     //! Unlock the mutex
0122     void Unlock()
0123     {
0124       myMutex->UnregisterCallback();
0125       myMutex->Unlock();
0126     }
0127 
0128     //! This method should not be called (prohibited).
0129     Sentry(const Sentry&) = delete;
0130     //! This method should not be called (prohibited).
0131     Sentry& operator=(const Sentry&) = delete;
0132 
0133   private:
0134     Standard_Mutex* myMutex;
0135   };
0136 
0137 public:
0138   //! Constructor: creates a mutex object and initializes it.
0139   //! It is strongly recommended that mutexes were created as
0140   //! static objects whenever possible.
0141   Standard_EXPORT Standard_Mutex();
0142 
0143   //! Destructor: destroys the mutex object
0144   Standard_EXPORT ~Standard_Mutex() override;
0145 
0146   //! Method to lock the mutex; waits until the mutex is released
0147   //! by other threads, locks it and then returns
0148   Standard_EXPORT void Lock();
0149 
0150   //! Method to test the mutex; if the mutex is not hold by other thread,
0151   //! locks it and returns True; otherwise returns False without waiting
0152   //! mutex to be released.
0153   Standard_EXPORT bool TryLock();
0154 
0155   //! Method to unlock the mutex; releases it to other users
0156   void Unlock();
0157 
0158 private:
0159   //! Callback method to unlock the mutex if OCC exception or signal is raised
0160   Standard_EXPORT void DestroyCallback() override;
0161 
0162   //! This method should not be called (prohibited).
0163   Standard_Mutex(const Standard_Mutex&) = delete;
0164   //! This method should not be called (prohibited).
0165   Standard_Mutex& operator=(const Standard_Mutex&) = delete;
0166 
0167 private:
0168 #if (defined(_WIN32) || defined(__WIN32__))
0169   CRITICAL_SECTION myMutex;
0170 #else
0171   pthread_mutex_t myMutex;
0172 #endif
0173 };
0174 
0175 //! @deprecated Use std::shared_ptr<std::mutex> instead. Will be removed in OCCT 8.0.0.
0176 Standard_DISABLE_DEPRECATION_WARNINGS;
0177 
0178 Standard_DEPRECATED("Standard_HMutex is deprecated; use std::shared_ptr<std::mutex> instead. "
0179                     "Will be removed in OCCT 8.0.0")
0180 typedef NCollection_Shared<Standard_Mutex> Standard_HMutex;
0181 
0182 Standard_ENABLE_DEPRECATION_WARNINGS;
0183 
0184 // Implementation of the method Unlock is inline, since it is
0185 // just a shortcut to system function
0186 inline void Standard_Mutex::Unlock()
0187 {
0188 #if (defined(_WIN32) || defined(__WIN32__))
0189   LeaveCriticalSection(&myMutex);
0190 #else
0191   pthread_mutex_unlock(&myMutex);
0192 #endif
0193 }
0194 
0195 #endif /* _Standard_Mutex_HeaderFile */