|
casacore
|
The Lane is an efficient cyclic buffer that is synchronized. More...
#include <Lane.h>
Public Types | |
| typedef std::size_t | size_type |
| Integer type used to store size types. | |
| typedef Tp | value_type |
| Type of elements stored in the Lane. | |
Public Member Functions | |
| Lane () noexcept | |
| Construct a Lane with zero elements. | |
| Lane (size_t capacity) | |
| Construct a Lane with the given capacity. | |
| Lane (const Lane< Tp > &source)=delete | |
| Lane (Lane< Tp > &&source) noexcept | |
| Move construct a Lane. | |
| ~Lane () | |
| Destructor. | |
| Lane< Tp > & | operator= (const Lane< Tp > &source)=delete |
| Lane< Tp > & | operator= (Lane< Tp > &&source) noexcept |
| Move assignment. | |
| void | swap (Lane< Tp > &other) noexcept |
| Swap the contents of this Lane with another. | |
| void | clear () noexcept |
| Clear the contents and reset the state of the Lane. | |
| void | write (const value_type &element) |
| Write a single element. | |
| template<typename... Args> | |
| void | emplace (Args &&... args) |
| Write a single element by constructing it. | |
| void | write (value_type &&element) |
| Write a single element by moving it in. | |
| void | write (const value_type *elements, size_t n) |
| void | move_write (value_type *elements, size_t n) |
| bool | read (value_type &destination) |
| size_t | read (value_type *destinations, size_t n) |
| size_t | discard (size_t n) |
| This method does the same thing as read(buffer, n) but discards the data. | |
| void | write_end () |
| size_t | capacity () const noexcept |
| size_t | size () const |
| bool | empty () const |
| bool | is_end () const |
| True when write_end() was called. | |
| bool | is_end_and_empty () const |
| True when write_end() and the lane does not contain items. | |
| void | resize (size_t new_capacity) |
| Change the capacity of the Lane. | |
| void | wait_for_empty () |
| Wait until this Lane is empty. | |
Private Types | |
| enum | { status_normal , status_end } |
Private Member Functions | |
| size_t | read_position () const noexcept |
| size_t | free_read_space () const noexcept |
| template<typename T > | |
| void | write_generic (T *elements, size_t n) |
| This is a template to allow const and non-const (to be able to move) | |
| template<typename T > | |
| void | immediate_write (T *elements, size_t n) noexcept |
| This is a template to allow const and non-const (to be able to move) | |
| void | immediate_read (value_type *elements, size_t n) noexcept |
| void | immediate_discard (size_t n) noexcept |
Private Attributes | |
| Tp * | _buffer |
| size_t | _capacity |
| size_t | _write_position |
| size_t | _free_write_space |
| enum casacore::aocommon::Lane:: { ... } | _status |
| std::mutex | _mutex |
| std::condition_variable | _writing_possible_condition |
| std::condition_variable | _reading_possible_condition |
The Lane is an efficient cyclic buffer that is synchronized.
A Lane can typically be used in a multi-threaded producer-consumer situation. The Lane also holds a state which allows for an ellegant way of communicating from producer(s) to consumer(s) that all data has been produced.
A simple example:
The various read and write methods, as well as the empty(), capacity() and size() methods are always thread safe. The other methods are not: assignment, swap(), clear() and resize() can not be called from a different thread while another thread is also accessing the Lane. The same holds obviously for the constructors and destructor. This is chosen because these methods should almost never be called in parallel with other methods, and hence it is not worth to increase every call with extra locks to make this possible.
With one reader and one writer, the order is guaranteed to be consistent. With multiple readers or writers in combination with multi-element write or read functions, a sequence of symbols might be interrupted. For example, if a multi-element write() won't fit completely in the buffer, the thread will wait for free space. Another thread might get now write access first, causing the single call to the multi-element write to be "split up".
| Tp | Type of elements to be stored in the Lane. |
| typedef std::size_t casacore::aocommon::Lane< Tp >::size_type |
| typedef Tp casacore::aocommon::Lane< Tp >::value_type |
|
private |
|
inlinenoexcept |
|
inlineexplicit |
|
delete |
|
inlinenoexcept |
|
inline |
Destructor.
The destructor is not synchronized.
Definition at line 154 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, and LANE_REPORT_DEBUG_INFO.
|
inlinenoexcept |
Definition at line 372 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_capacity.
|
inlinenoexcept |
Clear the contents and reset the state of the Lane.
After calling clear(), the Lane is in the same state as after construction. This also means that after clearing the Lane, it is as if write_end() has not been called yet.
This method is not thread safe.
Definition at line 191 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::_write_position, and casacore::aocommon::Lane< Tp >::status_normal.
|
inline |
This method does the same thing as read(buffer, n) but discards the data.
This eliminates the requirement to specify a buffer if the data is not necessary anyway, and avoids a copy of the data.
Definition at line 339 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_reading_possible_condition, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::free_read_space(), casacore::aocommon::Lane< Tp >::immediate_discard(), LANE_REGISTER_DEBUG_INFO, LANE_REGISTER_DEBUG_READ_WAIT, and casacore::aocommon::Lane< Tp >::status_normal.
|
inline |
Write a single element by constructing it.
This method is thread safe, and can be called together with other write and read methods from different threads.
If this call comes after a call to write_end(), the call will be ignored. The implementation does not construct the value in place, but rather constructs the value and then move assigns it. This is because the value that it is moved into has already been constructed (in the current implementation).
| element | Object to be moved into the cyclic buffer. |
Now that there is less free write space, there is more free read space and thus readers can possibly continue.
Definition at line 236 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_reading_possible_condition, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::_write_position, casacore::aocommon::Lane< Tp >::_writing_possible_condition, LANE_REGISTER_DEBUG_INFO, LANE_REGISTER_DEBUG_WRITE_WAIT, and casacore::aocommon::Lane< Tp >::status_normal.
|
inline |
Definition at line 379 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, and casacore::aocommon::Lane< Tp >::_mutex.
|
inlineprivatenoexcept |
Definition at line 454 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_capacity, and casacore::aocommon::Lane< Tp >::_free_write_space.
Referenced by casacore::aocommon::Lane< Tp >::discard(), casacore::aocommon::Lane< Tp >::read(), and casacore::aocommon::Lane< Tp >::read().
|
inlineprivatenoexcept |
Now that there is more free write space, writers can possibly continue.
Definition at line 543 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_free_write_space, and casacore::aocommon::Lane< Tp >::_writing_possible_condition.
Referenced by casacore::aocommon::Lane< Tp >::discard().
|
inlineprivatenoexcept |
As with write, split in two ranges if needed. The first range fits in [read_position(), _capacity), the second range in [0, end).
Now that there is more free write space, writers can possibly continue.
Definition at line 515 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_writing_possible_condition, and casacore::aocommon::Lane< Tp >::read_position().
Referenced by casacore::aocommon::Lane< Tp >::read().
|
inlineprivatenoexcept |
This is a template to allow const and non-const (to be able to move)
Split the writing in two ranges if needed. The first range fits in [_write_position, _capacity), the second range in [0, end). By doing so, we only have to calculate the modulo in the write position once.
Now that there is less free write space, there is more free read space and thus readers may continue.
Definition at line 486 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_reading_possible_condition, and casacore::aocommon::Lane< Tp >::_write_position.
Referenced by casacore::aocommon::Lane< Tp >::write_generic().
|
inline |
True when write_end() was called.
Even when end, the lane may still contain items.
Definition at line 388 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_status, and casacore::aocommon::Lane< Tp >::status_end.
|
inline |
True when write_end() and the lane does not contain items.
Definition at line 396 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_status, and casacore::aocommon::Lane< Tp >::status_end.
|
inline |
Definition at line 285 of file Lane.h.
References casacore::aocommon::Lane< Tp >::write_generic().
|
delete |
|
inlinenoexcept |
|
inline |
Now that there is more free write space, writers can possibly continue.
Definition at line 289 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_reading_possible_condition, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::_writing_possible_condition, casacore::aocommon::Lane< Tp >::free_read_space(), LANE_REGISTER_DEBUG_INFO, LANE_REGISTER_DEBUG_READ_WAIT, casacore::aocommon::Lane< Tp >::read_position(), and casacore::aocommon::Lane< Tp >::status_normal.
|
inline |
Definition at line 307 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_reading_possible_condition, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::free_read_space(), casacore::aocommon::Lane< Tp >::immediate_read(), LANE_REGISTER_DEBUG_INFO, LANE_REGISTER_DEBUG_READ_WAIT, and casacore::aocommon::Lane< Tp >::status_normal.
|
inlineprivatenoexcept |
Definition at line 450 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, and casacore::aocommon::Lane< Tp >::_write_position.
Referenced by casacore::aocommon::Lane< Tp >::immediate_read(), and casacore::aocommon::Lane< Tp >::read().
|
inline |
Change the capacity of the Lane.
This will erase all data in the Lane, and clear the state.
Definition at line 405 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::_write_position, and casacore::aocommon::Lane< Tp >::status_normal.
|
inline |
Definition at line 374 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, and casacore::aocommon::Lane< Tp >::_mutex.
|
inlinenoexcept |
Swap the contents of this Lane with another.
This operation is not thread safe: the behaviour is undefined when other threads access either Lane.
Definition at line 176 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_status, and casacore::aocommon::Lane< Tp >::_write_position.
Referenced by casacore::aocommon::Lane< Tp >::Lane(), and casacore::aocommon::Lane< Tp >::operator=().
|
inline |
Wait until this Lane is empty.
Definition at line 418 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_mutex, and casacore::aocommon::Lane< Tp >::_writing_possible_condition.
|
inline |
Write a single element.
This method is thread safe, and can be called together with other write and read methods from different threads.
If this call comes after a call to write_end(), the call will be ignored. If a write_end() call comes during write(), the write() call is aborted and returns immediately.
| element | Object to be copied into the cyclic buffer. |
Now that there is less free write space, there is more free read space and thus readers may continue.
Definition at line 206 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_reading_possible_condition, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::_write_position, casacore::aocommon::Lane< Tp >::_writing_possible_condition, LANE_REGISTER_DEBUG_INFO, LANE_REGISTER_DEBUG_WRITE_WAIT, and casacore::aocommon::Lane< Tp >::status_normal.
|
inline |
Definition at line 281 of file Lane.h.
References casacore::aocommon::Lane< Tp >::write_generic().
|
inline |
Write a single element by moving it in.
This method is thread safe, and can be called together with other write and read methods from different threads.
If this call comes after a call to write_end(), the call will be ignored.
| element | Object to be moved into the cyclic buffer. |
Now that there is less free write space, there is more free read space and thus readers can possibly continue.
Definition at line 263 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_buffer, casacore::aocommon::Lane< Tp >::_capacity, casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_reading_possible_condition, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::_write_position, casacore::aocommon::Lane< Tp >::_writing_possible_condition, LANE_REGISTER_DEBUG_INFO, LANE_REGISTER_DEBUG_WRITE_WAIT, and casacore::aocommon::Lane< Tp >::status_normal.
|
inline |
Definition at line 364 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_reading_possible_condition, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::_writing_possible_condition, LANE_REGISTER_DEBUG_INFO, and casacore::aocommon::Lane< Tp >::status_end.
|
inlineprivate |
This is a template to allow const and non-const (to be able to move)
Definition at line 460 of file Lane.h.
References casacore::aocommon::Lane< Tp >::_free_write_space, casacore::aocommon::Lane< Tp >::_mutex, casacore::aocommon::Lane< Tp >::_status, casacore::aocommon::Lane< Tp >::_writing_possible_condition, casacore::aocommon::Lane< Tp >::immediate_write(), LANE_REGISTER_DEBUG_INFO, LANE_REGISTER_DEBUG_WRITE_WAIT, and casacore::aocommon::Lane< Tp >::status_normal.
Referenced by casacore::aocommon::Lane< Tp >::move_write(), and casacore::aocommon::Lane< Tp >::write().
|
private |
Definition at line 435 of file Lane.h.
Referenced by casacore::aocommon::Lane< Tp >::emplace(), casacore::aocommon::Lane< Tp >::immediate_read(), casacore::aocommon::Lane< Tp >::immediate_write(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::resize(), casacore::aocommon::Lane< Tp >::swap(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write(), and casacore::aocommon::Lane< Tp >::~Lane().
|
private |
Definition at line 437 of file Lane.h.
Referenced by casacore::aocommon::Lane< Tp >::capacity(), casacore::aocommon::Lane< Tp >::clear(), casacore::aocommon::Lane< Tp >::emplace(), casacore::aocommon::Lane< Tp >::empty(), casacore::aocommon::Lane< Tp >::free_read_space(), casacore::aocommon::Lane< Tp >::immediate_read(), casacore::aocommon::Lane< Tp >::immediate_write(), casacore::aocommon::Lane< Tp >::is_end_and_empty(), casacore::aocommon::Lane< Tp >::read_position(), casacore::aocommon::Lane< Tp >::resize(), casacore::aocommon::Lane< Tp >::size(), casacore::aocommon::Lane< Tp >::swap(), casacore::aocommon::Lane< Tp >::wait_for_empty(), casacore::aocommon::Lane< Tp >::write(), and casacore::aocommon::Lane< Tp >::write().
|
private |
Definition at line 441 of file Lane.h.
Referenced by casacore::aocommon::Lane< Tp >::clear(), casacore::aocommon::Lane< Tp >::emplace(), casacore::aocommon::Lane< Tp >::empty(), casacore::aocommon::Lane< Tp >::free_read_space(), casacore::aocommon::Lane< Tp >::immediate_discard(), casacore::aocommon::Lane< Tp >::immediate_read(), casacore::aocommon::Lane< Tp >::immediate_write(), casacore::aocommon::Lane< Tp >::is_end_and_empty(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::read_position(), casacore::aocommon::Lane< Tp >::resize(), casacore::aocommon::Lane< Tp >::size(), casacore::aocommon::Lane< Tp >::swap(), casacore::aocommon::Lane< Tp >::wait_for_empty(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write(), and casacore::aocommon::Lane< Tp >::write_generic().
|
mutableprivate |
Definition at line 445 of file Lane.h.
Referenced by casacore::aocommon::Lane< Tp >::discard(), casacore::aocommon::Lane< Tp >::emplace(), casacore::aocommon::Lane< Tp >::empty(), casacore::aocommon::Lane< Tp >::is_end(), casacore::aocommon::Lane< Tp >::is_end_and_empty(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::size(), casacore::aocommon::Lane< Tp >::wait_for_empty(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write_end(), and casacore::aocommon::Lane< Tp >::write_generic().
|
private |
Definition at line 448 of file Lane.h.
Referenced by casacore::aocommon::Lane< Tp >::discard(), casacore::aocommon::Lane< Tp >::emplace(), casacore::aocommon::Lane< Tp >::immediate_write(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write(), and casacore::aocommon::Lane< Tp >::write_end().
| enum { ... } casacore::aocommon::Lane< Tp >::_status |
Referenced by casacore::aocommon::Lane< Tp >::clear(), casacore::aocommon::Lane< Tp >::discard(), casacore::aocommon::Lane< Tp >::emplace(), casacore::aocommon::Lane< Tp >::is_end(), casacore::aocommon::Lane< Tp >::is_end_and_empty(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::resize(), casacore::aocommon::Lane< Tp >::swap(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write_end(), and casacore::aocommon::Lane< Tp >::write_generic().
|
private |
Definition at line 439 of file Lane.h.
Referenced by casacore::aocommon::Lane< Tp >::clear(), casacore::aocommon::Lane< Tp >::emplace(), casacore::aocommon::Lane< Tp >::immediate_write(), casacore::aocommon::Lane< Tp >::read_position(), casacore::aocommon::Lane< Tp >::resize(), casacore::aocommon::Lane< Tp >::swap(), casacore::aocommon::Lane< Tp >::write(), and casacore::aocommon::Lane< Tp >::write().
|
private |
Definition at line 447 of file Lane.h.
Referenced by casacore::aocommon::Lane< Tp >::emplace(), casacore::aocommon::Lane< Tp >::immediate_discard(), casacore::aocommon::Lane< Tp >::immediate_read(), casacore::aocommon::Lane< Tp >::read(), casacore::aocommon::Lane< Tp >::wait_for_empty(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write(), casacore::aocommon::Lane< Tp >::write_end(), and casacore::aocommon::Lane< Tp >::write_generic().