| 1 |
/** |
| 2 |
* Copyright (C) 2017 - 2018 Fábio Bento (fabiobento512) |
| 3 |
* |
| 4 |
* This library is distributed under the MIT License. See notice at the end |
| 5 |
* of this file. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef CONDITIONALSEMAPHORE_H |
| 10 |
#define CONDITIONALSEMAPHORE_H |
| 11 |
|
| 12 |
#include <atomic> |
| 13 |
#include <stdexcept> |
| 14 |
|
| 15 |
/*** |
| 16 |
* Conditional Semaphore - Thread safe conditional semaphore. You can set the variable by using SetCondition(), |
| 17 |
* and unset it by using UnsetCondition() (you can set and unset multiple times). You can get the internal boolean variable state |
| 18 |
* by using ConditionIsSet(). |
| 19 |
* ConditionIsSet() return true when there were more calls to SetCondition() than to UnsetCondition() and |
| 20 |
* false if there the calls to SetCondition() and UnsetCondition() were equal. |
| 21 |
* |
| 22 |
* UnsetCondition() throws an exception if it is called more than SetCondition(). |
| 23 |
* */ |
| 24 |
|
| 25 |
namespace Cosemaphore{ |
| 26 |
|
| 27 |
class ConditionalSemaphore |
| 28 |
{ |
| 29 |
public: |
| 30 |
ConditionalSemaphore(); |
| 31 |
void SetCondition(); |
| 32 |
void UnsetCondition(); |
| 33 |
bool ConditionIsSet(); |
| 34 |
private: |
| 35 |
std::atomic<long long> internalCounter; |
| 36 |
}; |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
#endif // CONDITIONALSEMAPHORE_H |
| 41 |
|
| 42 |
/** |
| 43 |
* Copyright (C) 2017 - 2018 Fábio Bento (fabiobento512) |
| 44 |
* |
| 45 |
* Permission is hereby granted, free of charge, to any person |
| 46 |
* obtaining a copy of this software and associated documentation |
| 47 |
* files (the "Software"), to deal in the Software without |
| 48 |
* restriction, including without limitation the rights to use, |
| 49 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 50 |
* copies of the Software, and to permit persons to whom the |
| 51 |
* Software is furnished to do so, subject to the following |
| 52 |
* conditions: |
| 53 |
* |
| 54 |
* The above copyright notice and this permission notice shall be |
| 55 |
* included in all copies or substantial portions of the Software. |
| 56 |
* |
| 57 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 58 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 59 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 60 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 61 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 62 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 63 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 64 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 65 |
*/ |