Commit c9b0a0f3 authored by Hugo Lefeuvre's avatar Hugo Lefeuvre

ringbuffer: add default value to timeout parameter

waitForDataAvailable's doc mentions that "if no deadline is provided,
the call blocks indefinitely" but in fact it is not possible not to
provide a deadline because there's no default parameter.

Add a default parameter {} initializing timeout to the clock's epoch,
that is 0.

Also, fix some code duplication and a few spellings.

Change-Id: I149c09def4d96b07d231a67c11f1681c5e0f9972
Reviewed-by: Philippe Gorley's avatarPhilippe Gorley <philippe.gorley@savoirfairelinux.com>
parent db9a70d6
......@@ -250,30 +250,29 @@ size_t RingBuffer::get(AudioBuffer& buf, const std::string &call_id)
size_t RingBuffer::waitForDataAvailable(const std::string &call_id, const size_t min_data_length, const std::chrono::high_resolution_clock::time_point& deadline) const
{
std::unique_lock<std::mutex> l(lock_);
const size_t buffer_size = buffer_.frames();
if (buffer_size < min_data_length) return 0;
ReadOffset::const_iterator read_ptr = readoffsets_.find(call_id);
if (read_ptr == readoffsets_.end()) return 0;
size_t getl = 0;
auto check = [=, &getl] {
// Re-find read_ptr: it may be destroyed during the wait
const auto read_ptr = readoffsets_.find(call_id);
if (read_ptr == readoffsets_.end())
return true;
getl = (endPos_ + buffer_size - read_ptr->second) % buffer_size;
return getl >= min_data_length;
};
if (deadline == std::chrono::high_resolution_clock::time_point()) {
not_empty_.wait(l, [=, &getl] {
// Re-find read_ptr: it may be destroyed during the wait
const auto read_ptr = readoffsets_.find(call_id);
if (read_ptr == readoffsets_.end())
return true;
getl = (endPos_ + buffer_size - read_ptr->second) % buffer_size;
return getl >= min_data_length;
});
// no timeout provided, wait as long as necessary
not_empty_.wait(l, check);
} else {
not_empty_.wait_until(l, deadline, [=, &getl]{
// Re-find read_ptr: it may be destroyed during the wait
const auto read_ptr = readoffsets_.find(call_id);
if (read_ptr == readoffsets_.end())
return true;
getl = (endPos_ + buffer_size - read_ptr->second) % buffer_size;
return getl >= min_data_length;
});
not_empty_.wait_until(l, deadline, check);
}
return getl;
}
......
......@@ -125,14 +125,14 @@ class RingBuffer {
/**
* Blocks until min_data_length samples of data is available, or until deadline is missed.
* Blocks until min_data_length samples of data is available, or until deadline has passed.
*
* @param call_id The read offset for which data should be available.
* @param min_data_length Minimum number of samples that should be vailable for the call to return
* @param deadline The call is garenteed to end after this time point. If no deadline is provided, the the call blocks indefinitely.
* @param min_data_length Minimum number of samples that should be available for the call to return
* @param deadline The call is guaranteed to end after this time point. If no deadline is provided, the call blocks indefinitely.
* @return available data for call_id after the call returned (same as calling getLength(call_id) ).
*/
size_t waitForDataAvailable(const std::string &call_id, const size_t min_data_length, const std::chrono::high_resolution_clock::time_point& deadline) const;
size_t waitForDataAvailable(const std::string &call_id, const size_t min_data_length, const std::chrono::high_resolution_clock::time_point& deadline = {}) const;
/**
* Debug function print mEnd, mStart, mBufferSize
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment