-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #458 from cryos/mutex
Mutex
- Loading branch information
Showing
10 changed files
with
167 additions
and
8 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
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 @@ | ||
/****************************************************************************** | ||
This source file is part of the Avogadro project. | ||
This source code is released under the 3-Clause BSD License, (see "LICENSE"). | ||
******************************************************************************/ | ||
|
||
#include "sharedmutex.h" | ||
|
||
#include <shared_mutex> | ||
|
||
namespace Avogadro { | ||
namespace Core { | ||
|
||
using std::shared_mutex; | ||
|
||
class SharedMutex::PIMPL | ||
{ | ||
public: | ||
PIMPL() {} | ||
|
||
shared_mutex lock; | ||
}; | ||
|
||
SharedMutex::SharedMutex() : d(new PIMPL) {} | ||
|
||
SharedMutex::~SharedMutex() | ||
{ | ||
delete d; | ||
} | ||
|
||
void SharedMutex::lockForRead() | ||
{ | ||
d->lock.lock_shared(); | ||
} | ||
|
||
bool SharedMutex::tryLockForRead() | ||
{ | ||
return d->lock.try_lock_shared(); | ||
} | ||
|
||
void SharedMutex::unlockForRead() | ||
{ | ||
d->lock.unlock_shared(); | ||
} | ||
|
||
void SharedMutex::lockForWrite() | ||
{ | ||
d->lock.lock(); | ||
} | ||
|
||
bool SharedMutex::tryLockForWrite() | ||
{ | ||
return d->lock.try_lock(); | ||
} | ||
|
||
void SharedMutex::unlockForWrite() | ||
{ | ||
d->lock.unlock(); | ||
} | ||
|
||
} // namespace Core | ||
} // namespace Avogadro |
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,69 @@ | ||
/****************************************************************************** | ||
This source file is part of the Avogadro project. | ||
This source code is released under the 3-Clause BSD License, (see "LICENSE"). | ||
******************************************************************************/ | ||
|
||
#ifndef AVOGADRO_CORE_SHAREDMUTEX_H | ||
#define AVOGADRO_CORE_SHAREDMUTEX_H | ||
|
||
#include "avogadrocore.h" | ||
|
||
namespace Avogadro { | ||
namespace Core { | ||
|
||
/** | ||
* @class SharedMutex sharedmutex.h <avogadro/core/sharedmutex.h> | ||
* @brief The SharedMutex class provides a simple wrapper for the C++17 | ||
* shared_mutex class | ||
* @author Marcus D. Hanwell | ||
* | ||
* A very simple, and thin wrapper around the C++17 shared_mutex class, allowing | ||
* for lock, tryLock and unlock. | ||
*/ | ||
|
||
class AVOGADROCORE_EXPORT SharedMutex | ||
{ | ||
public: | ||
SharedMutex(); | ||
~SharedMutex(); | ||
|
||
/** | ||
* @brief Obtain a shared read lock. | ||
*/ | ||
void lockForRead(); | ||
|
||
/** | ||
* @brief Attempt to obtain a shared read lock. | ||
* @return True on success, false on failure. | ||
*/ | ||
bool tryLockForRead(); | ||
|
||
/** | ||
* @brief Unlocks the exclusive write lock. | ||
*/ | ||
void unlockForRead(); | ||
|
||
/** | ||
* @brief Obtain an exclusive write lock. | ||
*/ | ||
void lockForWrite(); | ||
|
||
/** | ||
* @brief Attempt to obtain an exclusive write lock. | ||
* @return True on success, false on failure. | ||
*/ | ||
bool tryLockForWrite(); | ||
|
||
/** | ||
* @brief Unlocks the exclusive write lock. | ||
*/ | ||
void unlockForWrite(); | ||
|
||
private: | ||
class PIMPL; | ||
PIMPL* d; | ||
}; | ||
} // namespace Core | ||
} // namespace Avogadro | ||
|
||
#endif // AVOGADRO_CORE_SHAREDMUTEX_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
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