Skip to content

Commit

Permalink
add finish to delivery event
Browse files Browse the repository at this point in the history
  • Loading branch information
tihmstar committed Oct 11, 2024
1 parent 2c3cce0 commit 03e33a3
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions include/libgeneral/DeliveryEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace tihmstar {
template <typename T>
class DeliveryEvent{
bool _isDying;
bool _isFinished;
std::atomic<uint64_t> _members;
Event _membersUpdateEvent;

Expand All @@ -30,7 +31,13 @@ namespace tihmstar {

T wait();
void post(T data);


/*
Notifies waiters that no more data will arrive in queue.
Once the que is empty, calls to wait will throw (same as if killing)
*/
void finish();

/*
Releases waiter and hints destruction of the object in near future
Further calls to wait() or post() are not allowed after calling kill()
Expand All @@ -42,7 +49,7 @@ namespace tihmstar {

template <class T>
DeliveryEvent<T>::DeliveryEvent()
: _isDying(false),_members(0)
: _isDying(false),_isFinished(false),_members(0)
{
//
}
Expand All @@ -62,6 +69,7 @@ namespace tihmstar {

std::unique_lock<std::mutex> ul(_dataLock);
while (!_dataQueue.size()) {
retassure(!_isFinished, "object finished. There will be no more data in the queue");
retassure(!_isDying, "object died while waiting on it");
uint64_t wevnet = _dataWait.getNextEvent();
ul.unlock();
Expand All @@ -88,6 +96,20 @@ namespace tihmstar {
_dataLock.unlock();
}

template <class T>
void DeliveryEvent<T>::finish(){
++_members;
cleanup([&]{
--_members;
_membersUpdateEvent.notifyAll();
});

_dataLock.lock();
_isFinished = true;
_dataWait.notifyAll();
_dataLock.unlock();
}

template <class T>
void DeliveryEvent<T>::kill(){
std::unique_lock<std::mutex> ul(_dataLock);
Expand Down

0 comments on commit 03e33a3

Please sign in to comment.