2018-03-14 18:12:34 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-03-14 18:12:34 +11:00
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2018-03-23 14:10:20 +11:00
|
|
|
#ifndef CRUFT_UTIL_THREAD_SEMAPHORE_HPP
|
|
|
|
#define CRUFT_UTIL_THREAD_SEMAPHORE_HPP
|
2018-03-14 18:12:34 +11:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::thread {
|
2018-03-14 18:12:34 +11:00
|
|
|
/// Explicitly does not conform to BasicLockable.
|
|
|
|
class semaphore {
|
|
|
|
public:
|
|
|
|
semaphore (int initial);
|
|
|
|
semaphore ();
|
|
|
|
|
|
|
|
semaphore (const semaphore&) = delete;
|
|
|
|
semaphore& operator= (const semaphore&) = delete;
|
|
|
|
semaphore (semaphore&&) = delete;
|
|
|
|
semaphore& operator= (semaphore&&) = delete;
|
|
|
|
|
|
|
|
int acquire (void);
|
2018-03-22 13:37:28 +11:00
|
|
|
int acquire (int count);
|
2018-03-14 18:12:34 +11:00
|
|
|
int release (void);
|
2018-03-22 13:39:16 +11:00
|
|
|
int release (int count);
|
2018-03-14 18:12:34 +11:00
|
|
|
|
|
|
|
auto lock (void) { return acquire (); }
|
|
|
|
auto unlock (void) { return release (); }
|
|
|
|
|
|
|
|
int value (void) const;
|
|
|
|
|
|
|
|
int operator++ (void);
|
|
|
|
int operator-- (void);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::atomic<int> m_value;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|