ORIGINAL
Loading...
Searching...
No Matches
syncPoint.h
1#ifndef ORIGINAL_SYNCPOINT_H
2#define ORIGINAL_SYNCPOINT_H
3#include "mutex.h"
4#include "condition.h"
5
6
7namespace original {
24 class syncPoint {
25 const u_integer max_arrived_;
26 u_integer arrived_;
27 u_integer round_;
28 mutable mutex mutex_;
29 mutable condition condition_;
30 std::function<void()> complete_func_;
31 std::exception_ptr e_;
32
33 public:
40 syncPoint();
41
51 explicit syncPoint(u_integer max_arrived, const std::function<void()>& func = {});
52
63 void arrive();
64
69 u_integer maxArrived() const;
70
79 };
80}
81
83 : max_arrived_(0), arrived_(0), round_(0) {}
84
85inline original::syncPoint::syncPoint(const u_integer max_arrived, const std::function<void()>& func)
86 : max_arrived_(max_arrived), arrived_(0), round_(0), complete_func_(func) {}
87
89 {
90 uniqueLock lock{this->mutex_};
91 if (max_arrived_ == 0) {
92 return;
93 }
94 u_integer this_round = this->round_;
95 this->arrived_ += 1;
96 if (this->arrived_ != this->max_arrived_) {
97 this->condition_.wait(this->mutex_, [this, this_round]
98 {
99 return this->round_ != this_round;
100 });
101 } else {
102 this->round_ += 1;
103 if (this->complete_func_) {
104 try {
105 this->complete_func_();
106 }catch (...){
107 this->e_ = std::current_exception();
108 }
109 }
110 this->arrived_ -= this->max_arrived_;
111 lock.unlock();
112 this->condition_.notifyAll();
113 if (this->e_) {
114 std::rethrow_exception(this->e_);
115 }
116 }
117 }
118}
119
121{
122 return this->max_arrived_;
123}
124
126{
127 uniqueLock lock{this->mutex_};
128 return this->arrived_;
129}
130
131#endif //ORIGINAL_SYNCPOINT_H
Definition condition.h:209
Definition mutex.h:241
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Synchronization point for multiple threads.
Definition syncPoint.h:24
u_integer currentArrived() const
Get the current number of arrived threads.
Definition syncPoint.h:125
syncPoint()
Constructs a disabled synchronization point.
Definition syncPoint.h:82
u_integer maxArrived() const
Get the maximum number of threads required for synchronization.
Definition syncPoint.h:120
void arrive()
Arrive at the synchronization point.
Definition syncPoint.h:88
RAII wrapper for single mutex locking.
Definition mutex.h:280
Condition variable implementation for thread synchronization.
Cross-platform mutex and lock management utilities.
Main namespace for the project Original.
Definition algorithms.h:21