backported binary semaphore for C++20 used in ThreadObject
Build on RHEL9 docker image / build (push) Successful in 3m39s
Build on RHEL8 docker image / build (push) Successful in 5m11s
Run Simulator Tests on local RHEL9 / build (push) Successful in 18m15s
Run Simulator Tests on local RHEL8 / build (push) Successful in 21m53s

This commit is contained in:
Erik Fröjdh
2026-05-11 17:10:39 +02:00
parent 50dfba6905
commit d2e8a86ba6
3 changed files with 41 additions and 7 deletions
+3 -5
View File
@@ -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;
+2 -2
View File
@@ -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};
};
+36
View File
@@ -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