You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
825 B
45 lines
825 B
#pragma once
|
|
|
|
#include <mutex>
|
|
#include <atomic>
|
|
#include <semaphore.h>
|
|
|
|
class barrier {
|
|
private:
|
|
std::mutex mutex_;
|
|
uint32_t waiting_count_;
|
|
const uint32_t barrier_size_;
|
|
sem_t semaphore_;
|
|
|
|
barrier(const barrier& other) = delete;
|
|
|
|
public:
|
|
barrier(const uint32_t size);
|
|
~barrier();
|
|
|
|
void wait();
|
|
void signal();
|
|
};
|
|
|
|
inline barrier::~barrier() {
|
|
sem_destroy(&semaphore_);
|
|
}
|
|
|
|
inline barrier::barrier(const uint32_t size) : barrier_size_(size) {
|
|
sem_init(&semaphore_, 0, 0);
|
|
waiting_count_ = 0;
|
|
}
|
|
|
|
inline void barrier::wait() {
|
|
mutex_.lock();
|
|
|
|
if (++waiting_count_ >= barrier_size_) {
|
|
for (uint32_t i = 1; i < waiting_count_; i++) sem_post(&semaphore_);
|
|
mutex_.unlock();
|
|
}
|
|
else {
|
|
mutex_.unlock();
|
|
sem_wait(&semaphore_);
|
|
}
|
|
}
|
|
|