Skip to content

Commit 70a8845

Browse files
Add wait_all_consumed method (#62)
* Add string to Timestamp converter Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> * Add wait_all_consumed method Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> * Notify all when threshold reached Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> * Wait on a different condition variable Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> --------- Signed-off-by: Juan López Fernández <juanlopez@eprosima.com>
1 parent bfb55e7 commit 70a8845

8 files changed

Lines changed: 117 additions & 4 deletions

File tree

cpp_utils/include/cpp_utils/thread_pool/pool/SlotThreadPool.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ class SlotThreadPool
112112
const TaskId& task_id,
113113
Task&& task);
114114

115+
/**
116+
* @brief Wait until all queued tasks are executed.
117+
*
118+
* This method will wait until all scheduled tasks are executed.
119+
* In case there is no programmed task at the moment of calling this method, it returns immediately.
120+
*
121+
* @param timeout maximum time to wait in milliseconds. If 0, not time limit. [default 0].
122+
* @return AwakeReason Whether the method returned due to timeout or because all tasks were executed.
123+
*/
124+
CPP_UTILS_DllAPI utils::event::AwakeReason wait_all_consumed(
125+
const utils::Duration_ms& timeout = 0);
126+
115127
protected:
116128

117129
/**

cpp_utils/include/cpp_utils/wait/ConsumerWaitHandler.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ class ConsumerWaitHandler : protected CounterWaitHandler
108108
T consume(
109109
const utils::Duration_ms& timeout = 0);
110110

111+
/////
112+
// Synchronization methods
113+
114+
/**
115+
* @brief Wait until all elements in the internal collection are consumed.
116+
*
117+
* This method will wait until there is no more data to be consumed in the internal collection.
118+
* In case there is no data at the moment of calling this method, it returns immediately.
119+
*
120+
* @param timeout maximum time to wait in milliseconds. If 0, not time limit. [default 0].
121+
* @return AwakeReason Whether the method returned due to timeout or because all elements were consumed.
122+
*/
123+
AwakeReason wait_all_consumed(
124+
const utils::Duration_ms& timeout = 0);
125+
111126
protected:
112127

113128
/**

cpp_utils/include/cpp_utils/wait/CounterWaitHandler.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ class CounterWaitHandler : protected WaitHandler<CounterType>
9999
CPP_UTILS_DllAPI AwakeReason wait_and_decrement(
100100
const utils::Duration_ms& timeout = 0) noexcept;
101101

102+
/**
103+
* @brief Wait current thread until counter reaches \c threshold.
104+
*
105+
* @param timeout maximum time in milliseconds that should wait until awaking for timeout
106+
*
107+
* @return reason why thread was awaken
108+
*/
109+
CPP_UTILS_DllAPI AwakeReason wait_threshold_reached(
110+
const utils::Duration_ms& timeout = 0) noexcept;
111+
102112
/////
103113
// Value methods
104114

@@ -121,6 +131,8 @@ class CounterWaitHandler : protected WaitHandler<CounterType>
121131
void decrease_1_nts_();
122132

123133
const CounterType threshold_;
134+
135+
std::condition_variable threshold_reached_cv_;
124136
};
125137

126138
} /* namespace event */

cpp_utils/include/cpp_utils/wait/impl/ConsumerWaitHandler.ipp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ T ConsumerWaitHandler<T>::consume(
7878
}
7979
}
8080

81+
template <typename T>
82+
AwakeReason ConsumerWaitHandler<T>::wait_all_consumed(
83+
const utils::Duration_ms& timeout /* = 0 */)
84+
{
85+
return wait_threshold_reached(timeout);
86+
}
87+
8188
} /* namespace event */
8289
} /* namespace utils */
8390
} /* namespace eprosima */

cpp_utils/src/cpp/event/FileWatcherHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ FileWatcherHandler::FileWatcherHandler(
3535
, filewatcher_started_(false)
3636
{
3737
logDebug(
38-
DDSROUTER_PERIODICHANDLER,
38+
UTILS_PERIODICHANDLER,
3939
"FileWatcher Event Handler created with file path " << file_path_ << " .");
4040
}
4141

cpp_utils/src/cpp/event/PeriodicEventHandler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PeriodicEventHandler::PeriodicEventHandler(
3838
}
3939

4040
logDebug(
41-
DDSROUTER_PERIODICHANDLER,
41+
UTILS_PERIODICHANDLER,
4242
"Periodic Event Handler created with period time " << period_time_ << " .");
4343
}
4444

@@ -93,7 +93,7 @@ void PeriodicEventHandler::start_period_thread_nts_() noexcept
9393
&PeriodicEventHandler::period_thread_routine_, this);
9494

9595
logDebug(
96-
DDSROUTER_PERIODICHANDLER,
96+
UTILS_PERIODICHANDLER,
9797
"Periodic Event Handler thread starts with period time " << period_time_ << " .");
9898
}
9999

@@ -110,7 +110,7 @@ void PeriodicEventHandler::stop_period_thread_nts_() noexcept
110110
period_thread_.join();
111111

112112
logDebug(
113-
DDSROUTER_PERIODICHANDLER,
113+
UTILS_PERIODICHANDLER,
114114
"Periodic Event Handler thread stops.");
115115
}
116116

cpp_utils/src/cpp/thread_pool/pool/SlotThreadPool.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ void SlotThreadPool::slot(
114114
}
115115
}
116116

117+
utils::event::AwakeReason SlotThreadPool::wait_all_consumed(
118+
const utils::Duration_ms& timeout /* = 0 */)
119+
{
120+
return task_queue_.wait_all_consumed(timeout);
121+
}
122+
117123
void SlotThreadPool::thread_routine_()
118124
{
119125
logDebug(UTILS_THREAD_POOL, "Starting thread routine: " << std::this_thread::get_id() << ".");

cpp_utils/src/cpp/wait/CounterWaitHandler.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,62 @@ AwakeReason CounterWaitHandler::wait_and_decrement(
6262
return result;
6363
}
6464

65+
AwakeReason CounterWaitHandler::wait_threshold_reached(
66+
const utils::Duration_ms& timeout /* = 0 */) noexcept
67+
{
68+
// Do wait with mutex taken
69+
std::unique_lock<std::mutex> lock(wait_condition_variable_mutex_);
70+
71+
// Check if it is disabled and exit
72+
if (!enabled())
73+
{
74+
return AwakeReason::disabled;
75+
}
76+
77+
// Increment number of threads waiting
78+
// WARNING: mutex must be taken
79+
threads_waiting_++;
80+
81+
utils::Timestamp time_to_wait_until;
82+
83+
// If timeout is 0, use wait, if not use wait for timeout
84+
if (timeout > 0)
85+
{
86+
time_to_wait_until = utils::now() + utils::duration_to_ms(timeout);
87+
}
88+
else
89+
{
90+
time_to_wait_until = utils::the_end_of_time();
91+
}
92+
93+
bool finished_for_condition_met = threshold_reached_cv_.wait_until(
94+
lock,
95+
time_to_wait_until,
96+
[this]
97+
{
98+
// Exit if threshold reached or if this has been disabled
99+
return !enabled_.load() || (value_ == threshold_);
100+
});
101+
102+
// Decrement number of threads waiting
103+
// NOTE: mutex is still taken
104+
threads_waiting_--;
105+
106+
// Check awake reason. Mutex is taken so it can not change while checking
107+
if (!enabled_.load())
108+
{
109+
return AwakeReason::disabled;
110+
}
111+
else if (finished_for_condition_met)
112+
{
113+
return AwakeReason::condition_met;
114+
}
115+
else
116+
{
117+
return AwakeReason::timeout;
118+
}
119+
}
120+
65121
CounterWaitHandler& CounterWaitHandler::operator ++()
66122
{
67123
// NOTE: This operation could be done using the WaitHandler methods, but it will be less efficient than
@@ -91,6 +147,11 @@ void CounterWaitHandler::decrease_1_nts_()
91147
{
92148
wait_condition_variable_.notify_one();
93149
}
150+
// If the threshold is reached, notify threads waiting for this event
151+
else if (value_ == threshold_)
152+
{
153+
threshold_reached_cv_.notify_all();
154+
}
94155
}
95156

96157
} /* namespace event */

0 commit comments

Comments
 (0)