mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-07-09 19:00:48 +02:00
backported binary semaphore for C++20 used in ThreadObject
This commit is contained in:
@@ -16,7 +16,6 @@ namespace sls {
|
||||
ThreadObject::ThreadObject(int index, std::string type)
|
||||
: index(index), type(type) {
|
||||
LOG(logDEBUG) << type << " thread created: " << index;
|
||||
sem_init(&semaphore, 1, 0);
|
||||
try {
|
||||
threadObject = std::thread(&ThreadObject::RunningThread, this);
|
||||
} catch (...) {
|
||||
@@ -27,9 +26,8 @@ ThreadObject::ThreadObject(int index, std::string type)
|
||||
|
||||
ThreadObject::~ThreadObject() {
|
||||
killThread = true;
|
||||
sem_post(&semaphore);
|
||||
semaphore.release();
|
||||
threadObject.join();
|
||||
sem_destroy(&semaphore);
|
||||
}
|
||||
|
||||
pid_t ThreadObject::GetThreadId() const { return threadId; }
|
||||
@@ -49,14 +47,14 @@ void ThreadObject::RunningThread() {
|
||||
ThreadExecution();
|
||||
}
|
||||
// wait till the next acquisition
|
||||
sem_wait(&semaphore);
|
||||
semaphore.acquire();
|
||||
}
|
||||
LOG(logINFOBLUE) << "Exiting [ " << type << " Thread " << index
|
||||
<< ", Tid: " << threadId << "]";
|
||||
threadId = 0;
|
||||
}
|
||||
|
||||
void ThreadObject::Continue() { sem_post(&semaphore); }
|
||||
void ThreadObject::Continue() { semaphore.release(); }
|
||||
|
||||
void ThreadObject::SetThreadPriority(int priority) {
|
||||
struct sched_param param;
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
|
||||
#include "sls/logger.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include "sls/thread_utils.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <semaphore.h>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
@@ -45,7 +45,7 @@ class ThreadObject : private virtual slsDetectorDefs {
|
||||
std::atomic<bool> killThread{false};
|
||||
std::atomic<bool> runningFlag{false};
|
||||
std::thread threadObject;
|
||||
sem_t semaphore;
|
||||
binary_semaphore semaphore{0};
|
||||
const std::string type;
|
||||
std::atomic<pid_t> threadId{0};
|
||||
};
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
* within a single process.
|
||||
*/
|
||||
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <sys/types.h> // pid_t
|
||||
|
||||
#if defined(__APPLE__)
|
||||
@@ -46,4 +48,38 @@ inline pid_t getThreadId() noexcept {
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal C++17 backport of the subset of std::binary_semaphore used in this
|
||||
* project. API matches std::binary_semaphore so call sites can switch to
|
||||
* <semaphore> verbatim once the project moves to C++20. Built on
|
||||
* std::mutex + std::condition_variable; therefore NOT async-signal-safe, do
|
||||
* not call release() from a signal handler.
|
||||
*/
|
||||
class binary_semaphore {
|
||||
public:
|
||||
explicit binary_semaphore(int desired) : count_(desired) {}
|
||||
|
||||
binary_semaphore(const binary_semaphore &) = delete;
|
||||
binary_semaphore &operator=(const binary_semaphore &) = delete;
|
||||
|
||||
void acquire() {
|
||||
std::unique_lock<std::mutex> lk(mtx_);
|
||||
cv_.wait(lk, [this] { return count_ > 0; });
|
||||
--count_;
|
||||
}
|
||||
|
||||
void release() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mtx_);
|
||||
++count_;
|
||||
}
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mtx_;
|
||||
std::condition_variable cv_;
|
||||
int count_;
|
||||
};
|
||||
|
||||
} // namespace sls
|
||||
|
||||
Reference in New Issue
Block a user