// Created on: 2005-04-10
// Created by: Andrey BETENEV
-// Copyright (c) 2005-2012 OPEN CASCADE SAS
+// Copyright (c) 2005-2014 OPEN CASCADE SAS
//
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
//
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
//
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
-
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
#ifndef _Standard_Mutex_HeaderFile
#define _Standard_Mutex_HeaderFile
#include <Standard_Integer.hxx>
#include <Standard_Boolean.hxx>
-#include <Standard_ErrorHandlerCallback.hxx>
+#include <Standard_ErrorHandler.hxx>
-#ifdef WNT
-#include <windows.h>
+#if defined(_WIN32)
+ #include <windows.h>
#else
-#include <pthread.h>
-#include <sys/errno.h>
-#include <unistd.h>
-#include <time.h>
+ #include <pthread.h>
+ ///#include <sys/errno.h>
+ #include <unistd.h>
+ #include <time.h>
#endif
/**
* TryLock(), and UnregisterCallback() before Unlock() (or use Sentry classes).
*/
-class Standard_Mutex : public Standard_ErrorHandlerCallback
+class Standard_Mutex : public Standard_ErrorHandler::Callback
{
public:
/**
//! Constructor - initializes the sentry object by reference to a
//! mutex (which must be initialized) and locks the mutex immediately
- Sentry (Standard_Mutex &theMutex)
- : myMutex(theMutex)
+ Sentry (Standard_Mutex& theMutex)
+ : myMutex (&theMutex)
{
- myMutex.Lock();
- myMutex.RegisterCallback();
+ Lock();
}
+ //! Constructor - initializes the sentry object by pointer to a
+ //! mutex and locks the mutex if its pointer is not NULL
+ Sentry (Standard_Mutex* theMutex)
+ : myMutex (theMutex)
+ {
+ if (myMutex != NULL)
+ {
+ Lock();
+ }
+ }
//! Destructor - unlocks the mutex if already locked.
- ~Sentry () {
- myMutex.UnregisterCallback();
- myMutex.Unlock();
+ ~Sentry()
+ {
+ if (myMutex != NULL)
+ {
+ Unlock();
+ }
}
-
- private:
- //! This method should not be called (prohibited).
- Sentry (const Sentry &);
- //! This method should not be called (prohibited).
- Sentry& operator = (const Sentry &);
-
private:
- Standard_Mutex &myMutex;
- };
-
- /**
- * @brief Advanced Sentry class providing convenient interface to
- * manipulate a mutex from one thread.
- *
- * As compared with simple Sentry class, provides possibility to
- * lock and unlock mutex at any moment, and perform nested lock/unlock
- * actions (using lock counter). However all calls must be from within
- * the same thread; this is to be ensured by the code using this class.
- */
- class SentryNested
- {
- public:
- //! Constructor - initializes the sentry object by reference to a
- //! mutex (which must be initialized). Locks the mutex immediately
- //! unless Standard_False is given as second argument.
- SentryNested (Standard_Mutex &theMutex, Standard_Boolean doLock = Standard_True)
- : myMutex(theMutex), nbLocked(0)
- {
- if ( doLock ) Lock();
- }
-
- //! Destructor - unlocks the mutex if already locked.
- ~SentryNested ()
- {
- if ( nbLocked >0 ) {
- nbLocked = 1;
- Unlock();
- }
- }
-
//! Lock the mutex
- void Lock () {
- if ( ! nbLocked ) {
- myMutex.Lock();
- myMutex.RegisterCallback();
- }
- nbLocked++;
+ void Lock()
+ {
+ myMutex->Lock();
+ myMutex->RegisterCallback();
}
-
+
//! Unlock the mutex
- void Unlock () {
- if ( nbLocked == 1 ) {
- myMutex.UnregisterCallback();
- myMutex.Unlock();
- }
- nbLocked--;
+ void Unlock()
+ {
+ myMutex->UnregisterCallback();
+ myMutex->Unlock();
}
-
- private:
+
//! This method should not be called (prohibited).
- SentryNested (const SentryNested &);
+ Sentry (const Sentry &);
//! This method should not be called (prohibited).
- SentryNested& operator = (const SentryNested &);
+ Sentry& operator = (const Sentry &);
private:
- Standard_Mutex &myMutex;
- Standard_Boolean nbLocked; //!< Note that we do not protect this field from
- //!< concurrent access, as it should always be accessed
- //!< from within one thread, i.e. synchronously
+ Standard_Mutex* myMutex;
};
-
+
public:
//! Constructor: creates a mutex object and initializes it.
Standard_EXPORT Standard_Boolean TryLock ();
//! Method to unlock the mutex; releases it to other users
- Standard_EXPORT void Unlock ();
+ void Unlock ();
private:
//! Callback method to unlock the mutex if OCC exception or signal is raised
- virtual void DestroyCallback ();
+ Standard_EXPORT virtual void DestroyCallback() Standard_OVERRIDE;
+
+ //! This method should not be called (prohibited).
+ Standard_Mutex (const Standard_Mutex &);
+ //! This method should not be called (prohibited).
+ Standard_Mutex& operator = (const Standard_Mutex &);
private:
-#ifdef WNT
+#if (defined(_WIN32) || defined(__WIN32__))
CRITICAL_SECTION myMutex;
#else
pthread_mutex_t myMutex;
// just a shortcut to system function
inline void Standard_Mutex::Unlock ()
{
-#ifdef WNT
- LeaveCriticalSection( &myMutex );
+#if (defined(_WIN32) || defined(__WIN32__))
+ LeaveCriticalSection (&myMutex);
#else
- pthread_mutex_unlock( &myMutex );
+ pthread_mutex_unlock (&myMutex);
#endif
}