1 #ifndef MF_SHARED_RING_H_
2 #define MF_SHARED_RING_H_
5 #include "../os/event.h"
7 #include <condition_variable>
40 enum : thread_state { idle = 0, accessing = -1 };
46 std::atomic<time_unit> end_time_{-1};
49 mutable std::mutex mutex_;
50 event reader_idle_event_;
51 event reader_seek_event_;
52 event writer_idle_event_;
54 std::atomic<thread_state> reader_state_{idle};
55 std::atomic<thread_state> writer_state_{idle};
57 std::atomic<time_unit> read_start_time_{0};
60 void read_and_discard_(
time_unit duration);
Synchronization primitive representing event that a thread can wait for.
Definition: event.h:21
std::ptrdiff_t time_unit
Discrete time unit type.
Definition: common.h:52
const frame_format & format() const noexcept
Definition: ndarray_generic.h:45
time_unit readable_duration() const
Currently readable duration.
Definition: shared_ring.h:190
shared_ring(const frame_array_properties &, bool seekable, time_unit end_time=-1)
Definition: shared_ring.cc:7
timed_ring::section_view_type section_view_type
Definition: shared_ring.h:34
section_view_type begin_read_span(time_span)
Begin reading frames at time span span.
Definition: shared_ring.cc:142
void skip(time_unit skip_duration)
Skips duration frames.
Definition: shared_ring.cc:263
bool writer_reached_end() const
True if writer has written last frame.
Definition: shared_ring.cc:436
time_unit capacity() const
Capacity of buffer.
Definition: shared_ring.h:71
void end_write(time_unit written_duration, bool mark_end=false)
End writing written_duration frames.
Definition: shared_ring.cc:122
time_unit writable_duration() const
Currently writable duration.
Definition: shared_ring.h:186
bool wait_readable(event &break_event)
Wait until a frame become readable, or break_event occurs.
Definition: shared_ring.cc:229
time_unit end_time() const
End of file time. */.
Definition: shared_ring.h:195
time_unit read_start_time() const
Read start time.
Definition: shared_ring.cc:412
bool end_time_is_defined() const
True if end of file time is known. */.
Definition: shared_ring.h:198
time_span writable_time_span() const
Currently writable time span.
Definition: shared_ring.cc:421
Timed ring buffer with changed semantics, for dual-thread use.
Definition: shared_ring.h:32
bool can_seek(time_unit target_time) const
Verifies if is is possible to seek to read time t.
Definition: shared_ring.cc:336
void seek(time_unit target_time)
Seeks to read time t.
Definition: shared_ring.cc:342
time_unit current_time() const
Time of last written frame in buffer.
Definition: shared_ring.cc:401
bool is_seekable() const
Check if the ring is seekable.
Definition: shared_ring.h:146
time_unit duration() const
Definition: common.h:72
Ring buffer which adds absolute time index to frames.
Definition: timed_ring.h:13
const shape_type & shape() const noexcept
Definition: ndarray.h:101
section_view_type try_begin_read(time_unit read_duration)
Begin reading read_duration frames at current read start time, if they are available.
Definition: shared_ring.cc:206
bool reader_reached_end() const
True if reader has read last frame.
Definition: shared_ring.cc:442
const frame_format & format() const noexcept
Definition: shared_ring.h:67
One-dimensional time span.
Definition: common.h:61
time_unit write_start_time() const
Presumptive start time of next write.
Definition: shared_ring.cc:406
section_view_type begin_write(time_unit write_duration)
Begin writing write_duration frames at current write start time.
Definition: shared_ring.cc:23
section_view_type try_begin_write(time_unit write_duration)
Begin writing write_duration frames at current write start time, if they are available.
Definition: shared_ring.cc:84
bool wait_writable(event &break_event)
Wait until a frame become writable, or break_event occurs.
Definition: shared_ring.cc:104
void initialize()
Definition: shared_ring.cc:14
ndarray_generic_properties frame_array_properties
Definition: frame.h:13
time_span readable_time_span() const
Currently readable time span.
Definition: shared_ring.cc:430
Generic ndarray_timed_view where lower dimension(s) are type-erased.
Definition: ndarray_timed_view_generic.h:12
section_view_type begin_read(time_unit read_duration)
Begin reading read_duration frames at current read start time.
Definition: shared_ring.cc:161
timed_frame_array_view section_view_type
Definition: timed_ring.h:23
void end_read(time_unit read_duration)
End reading read_duration frames.
Definition: shared_ring.cc:248