Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SemaphoreNotifiableBlock.hxx
Go to the documentation of this file.
1
38#ifndef _EXECUTOR_SEMAPHORENOTIFIABLEBLOCK_HXX_
39#define _EXECUTOR_SEMAPHORENOTIFIABLEBLOCK_HXX_
40
46{
47public:
50 SemaphoreNotifiableBlock(unsigned num_parallelism)
51 : count_(num_parallelism)
52 , sem_(num_parallelism)
53 , barriers_(new BarrierNotifiable[num_parallelism])
54 {
55 }
56
58 {
59 delete[] barriers_;
60 }
61
65 sem_.wait();
66 AtomicHolder h(this);
67 for (unsigned i = 0; i < count_; ++i) {
68 if (barriers_[i].is_done()) {
69 return barriers_[i].reset(this);
70 }
71 }
72 DIE("SempahoreNotifiableBlock: could not find a free barrier.");
73 }
74
76 void notify() override
77 {
78 sem_.post();
79 }
80
81#if OPENMRN_FEATURE_RTOS_FROM_ISR
82 void notify_from_isr() OVERRIDE
83 {
84 int woken = 0;
85 sem_.post_from_isr(&woken);
86 }
87#endif // OPENMRN_FEATURE_RTOS_FROM_ISR
88
89private:
91 unsigned count_;
96
98};
99
100#endif // _EXECUTOR_SEMAPHORENOTIFIABLEBLOCK_HXX_
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Lightweight locking class for protecting small critical sections.
Definition Atomic.hxx:130
A BarrierNotifiable allows to create a number of child Notifiable and wait for all of them to finish.
BarrierNotifiable * reset(Notifiable *done)
Resets the barrier. Returns &*this. Asserts that is_done().
An object that can schedule itself on an executor to run.
This class provides a counting semaphore API.
Definition OS.hxx:243
void post()
Post (increment) a semaphore.
Definition OS.hxx:260
void wait()
Wait on (decrement) a semaphore.
Definition OS.hxx:279
A block of BarrierNotifiable objects, with a synchronous allocation call.
void notify() override
Internal: notifies that a barrier has been returned.
BarrierNotifiable * acquire()
Gets a barrier notifiable.
SemaphoreNotifiableBlock(unsigned num_parallelism)
constructor.
OSSem sem_
Semaphore holding free barriers.
BarrierNotifiable * barriers_
The raw pointer to the block of barriernotifiables.
unsigned count_
How many barriers did we allocate in total?
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
#define DIE(MSG)
Unconditionally terminates the current process with a message.
Definition macros.h:143
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171