-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added also Lock interface (implemented by RecursiveLock and Mutex). LockGuard moved to used the generic Lock object instead of the Mutex
- Loading branch information
1 parent
d1acfa0
commit b94cde8
Showing
13 changed files
with
388 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- | ||
|
||
/* | ||
* Copyright (C) 2015 Robotics and Cognitive Sciences Department. IIT | ||
* Authors: Francesco Romano | ||
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT | ||
* | ||
*/ | ||
|
||
#ifndef YARP_LOCK_H | ||
#define YARP_LOCK_H | ||
|
||
#include <yarp/os/api.h> | ||
|
||
namespace yarp { | ||
namespace os { | ||
class Lock; | ||
} | ||
} | ||
/** | ||
* This interface defines the Lock object. | ||
* A Lock is a synchronization primitive that can be used to protect shared data from being | ||
* simultaneously accessed by multiple threads. | ||
* | ||
* | ||
*/ | ||
class YARP_OS_API yarp::os::Lock | ||
{ | ||
public: | ||
virtual ~Lock(); | ||
|
||
/** | ||
* @brief Lock the associated resource, waiting if the resource is busy. | ||
* | ||
* See the actual implementation documentation for the correct use | ||
*/ | ||
virtual void lock() = 0; | ||
|
||
/** | ||
* @brief Lock the associated resource if it is free. | ||
* | ||
* If the lock is successfully taken returns true, otherwise returns false | ||
* and no lock is taken. | ||
* See the actual implementation documentation for the correct use | ||
* | ||
* @return true if the associated resource was successfully locked. | ||
* | ||
*/ | ||
virtual bool tryLock() = 0; | ||
|
||
/** | ||
* @brief Unlock the associated resource thus freeing waiting threads. | ||
* | ||
* See the actual implementation documentation for the correct use | ||
* | ||
*/ | ||
virtual void unlock() = 0; | ||
}; | ||
|
||
|
||
#endif /* end of include guard: YARP_LOCK_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- | ||
|
||
/* | ||
* Copyright (C) 2015 Robotics and Cognitive Sciences Department. IIT | ||
* Authors: Francesco Romano | ||
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT | ||
* | ||
*/ | ||
|
||
#ifndef YARP_RECURSIVELOCK_H | ||
#define YARP_RECURSIVELOCK_H | ||
|
||
#include "Lock.h" | ||
|
||
namespace yarp { | ||
namespace os { | ||
class RecursiveLock; | ||
} | ||
} | ||
|
||
/** | ||
* RecursiveLock offers exclusive, recursive ownership semantics: | ||
* - A calling thread owns a RecursiveLock for a period of time that starts when it successfully calls either lock or tryLock. During this period, the thread may make additional calls to lock or tryLock. The period of ownership ends when the thread makes a matching number of calls to unlock. | ||
* - When a thread owns a RecursiveLock, all other threads will block (for calls to lock) or receive a false return value (for tryLock) if they attempt to claim ownership of the RecursiveLock. | ||
* The behavior of a program is undefined if a RecursiveLock is destroyed while still owned by some thread. | ||
* The behavior of a program is undefined if a RecursiveLock is unlocked by a thread which is not | ||
* currently owning the RecursiveLock | ||
* | ||
*/ | ||
class YARP_OS_API yarp::os::RecursiveLock : public yarp::os::Lock { | ||
public: | ||
|
||
/** | ||
* | ||
* Constructor. | ||
* | ||
*/ | ||
RecursiveLock(); | ||
|
||
/** | ||
* | ||
* Destructor. | ||
* | ||
*/ | ||
virtual ~RecursiveLock(); | ||
|
||
/** | ||
* @brief Lock the associated resource, waiting if the resource is busy. | ||
* | ||
* If the thread which is currently owning the resource calls | ||
* this function, it will not block, and a reference count will be increased | ||
* Thu number of calls to lock() must be balanced by the same number of | ||
* calls to unlock() | ||
* | ||
*/ | ||
virtual void lock(); | ||
|
||
/** | ||
* @brief Lock the associated resource if it is free. | ||
* | ||
* @see RecursiveLock#lock() for more detailed description | ||
* @return true if the associated resource was successfully locked. False otherwise | ||
* | ||
*/ | ||
virtual bool tryLock(); | ||
|
||
/** | ||
* @brief Unlock the associated resource thus freeing waiting threads. | ||
* | ||
* If the resource is not currently locked by the calling thread, | ||
* the behavior is undefined. | ||
* | ||
*/ | ||
virtual void unlock(); | ||
|
||
private: | ||
void *implementation; | ||
}; | ||
|
||
|
||
#endif /* end of include guard: YARP_RECURSIVELOCK_H */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- | ||
|
||
/* | ||
* Copyright (C) 2015 Robotics and Cognitive Sciences Department. IIT | ||
* Authors: Francesco Romano | ||
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT | ||
* | ||
*/ | ||
|
||
#ifndef YARP_ACELOCKIMPL_H | ||
#define YARP_ACELOCKIMPL_H | ||
|
||
#include <ace/Recursive_Thread_Mutex.h> | ||
#include <yarp/os/LogStream.h> | ||
|
||
class YARP_OS_impl_API yarp::os::impl::RecursiveLockImpl { | ||
public: | ||
void lock() { | ||
int result = mutex.acquire(); | ||
if (result != -1) return; | ||
yError("Mutex lock failed (errno %d)", ACE_OS::last_error()); | ||
} | ||
|
||
bool tryLock() { | ||
int result = mutex.tryacquire(); | ||
if (result != -1) return true; | ||
//if errno is EBUSY this is a "clean" failure: lock is busy | ||
if (ACE_OS::last_error() == EBUSY) return false; | ||
//different error: return false and print error | ||
yError("Mutex tryLock failed (errno %d)", ACE_OS::last_error()); | ||
return false; | ||
|
||
} | ||
|
||
// unlock | ||
void unlock() { | ||
mutex.release(); | ||
} | ||
|
||
private: | ||
ACE_Recursive_Thread_Mutex mutex; | ||
}; | ||
|
||
#endif /* end of include guard: YARP_ACELOCKIMPL_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- | ||
|
||
/* | ||
* Copyright (C) 2015 Robotics and Cognitive Sciences Department. IIT | ||
* Authors: Francesco Romano | ||
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT | ||
* | ||
*/ | ||
|
||
#ifndef YARP_CXX11LOCKIMPL_H | ||
#define YARP_CXX11LOCKIMPL_H | ||
|
||
#include <mutex> | ||
|
||
class YARP_OS_impl_API yarp::os::impl::RecursiveLockImpl { | ||
public: | ||
RecursiveLockImpl() {} | ||
RecursiveLockImpl(RecursiveLockImpl&) = delete; | ||
RecursiveLockImpl& operator= (RecursiveLockImpl&) = delete; | ||
|
||
void lock() { | ||
mutex.lock(); | ||
} | ||
|
||
bool tryLock() { | ||
return mutex.try_lock(); | ||
} | ||
|
||
// increment | ||
void unlock() { | ||
mutex.unlock(); | ||
} | ||
|
||
private: | ||
std::recursive_mutex mutex; | ||
}; | ||
|
||
#endif /* end of include guard: YARP_CXX11LOCKIMPL_H */ |
Oops, something went wrong.