2 * Copyright (C) 2000 Paul Davis & Benno Senoner
3 * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef playback_buffer_h
21 #define playback_buffer_h
27 #include "pbd/libpbd_visibility.h"
28 #include "pbd/spinlock.h"
33 class /*LIBPBD_API*/ PlaybackBuffer
36 static guint power_of_two_size (guint sz) {
38 for (power_of_two = 1; 1U << power_of_two < sz; ++power_of_two);
39 return 1U << power_of_two;
42 PlaybackBuffer (guint sz, guint res = 8191)
46 size = power_of_two_size (sz);
50 g_atomic_int_set (&read_idx, 0);
54 virtual ~PlaybackBuffer () {
59 T *buffer () { return buf; }
61 guint bufsize () const { return size; }
65 /* writer, when seeking, may block */
66 Glib::Threads::Mutex::Lock lm (_reset_lock);
67 SpinLock sl (_reservation_lock);
68 g_atomic_int_set (&write_idx, g_atomic_int_get (&read_idx));
69 g_atomic_int_set (&reserved, 0);
73 guint write_space () const {
76 w = g_atomic_int_get (&write_idx);
77 r = g_atomic_int_get (&read_idx);
82 rv = (r - w + size) & size_mask;
88 /* it may hapen that the read/invalidation-pointer moves backwards
89 * e.g. after rec-stop, declick fade-out.
90 * At the same time the butler may already have written data.
91 * (it's safe as long as the disk-reader does not move backwards by more
93 * XXX disk-reading de-click should not move the invalidation-pointer
95 if (rv > reservation) {
96 return rv - 1 - reservation;
102 guint read_space () const {
105 w = g_atomic_int_get (&write_idx);
106 r = g_atomic_int_get (&read_idx);
111 return (w - r + size) & size_mask;
116 guint read (T *dest, guint cnt, bool commit = true, guint offset = 0);
119 guint write (T const * src, guint cnt);
121 guint write_zero (guint cnt);
126 SpinLock sl (_reservation_lock);
127 g_atomic_int_set (&read_idx, g_atomic_int_get (&write_idx));
128 g_atomic_int_set (&reserved, 0);
132 guint decrement_read_ptr (guint cnt)
134 SpinLock sl (_reservation_lock);
135 guint r = g_atomic_int_get (&read_idx);
136 guint res = g_atomic_int_get (&reserved);
138 cnt = std::min (cnt, res);
140 r = (r + size - cnt) & size_mask;
143 g_atomic_int_set (&read_idx, r);
144 g_atomic_int_set (&reserved, res);
150 guint increment_read_ptr (guint cnt)
152 cnt = std::min (cnt, read_space ());
154 SpinLock sl (_reservation_lock);
155 g_atomic_int_set (&read_idx, (g_atomic_int_get (&read_idx) + cnt) & size_mask);
156 g_atomic_int_set (&reserved, std::min (reservation, g_atomic_int_get (&reserved) + cnt));
162 bool can_seek (int64_t cnt) {
164 return read_space() >= cnt;
167 return g_atomic_int_get (&reserved) >= -cnt;
180 mutable gint write_idx;
181 mutable gint read_idx;
182 mutable gint reserved;
184 /* spinlock will be used to update write_idx and reserved in sync */
185 spinlock_t _reservation_lock;
186 /* reset_lock is used to prevent concurrent reading and reset (seek, transport reversal etc). */
187 Glib::Threads::Mutex _reset_lock;
190 template<class T> /*LIBPBD_API*/ guint
191 PlaybackBuffer<T>::write (T const *src, guint cnt)
193 guint w = g_atomic_int_get (&write_idx);
194 const guint free_cnt = write_space ();
200 const guint to_write = cnt > free_cnt ? free_cnt : cnt;
201 const guint cnt2 = w + to_write;
206 n2 = cnt2 & size_mask;
212 memcpy (&buf[w], src, n1 * sizeof (T));
213 w = (w + n1) & size_mask;
216 memcpy (buf, src+n1, n2 * sizeof (T));
220 g_atomic_int_set (&write_idx, w);
224 template<class T> /*LIBPBD_API*/ guint
225 PlaybackBuffer<T>::write_zero (guint cnt)
227 guint w = g_atomic_int_get (&write_idx);
228 const guint free_cnt = write_space ();
234 const guint to_write = cnt > free_cnt ? free_cnt : cnt;
235 const guint cnt2 = w + to_write;
240 n2 = cnt2 & size_mask;
246 memset (&buf[w], 0, n1 * sizeof (T));
247 w = (w + n1) & size_mask;
250 memset (buf, 0, n2 * sizeof (T));
254 g_atomic_int_set (&write_idx, w);
258 template<class T> /*LIBPBD_API*/ guint
259 PlaybackBuffer<T>::read (T *dest, guint cnt, bool commit, guint offset)
261 Glib::Threads::Mutex::Lock lm (_reset_lock, Glib::Threads::TRY_LOCK);
263 /* seek, reset in progress */
267 guint r = g_atomic_int_get (&read_idx);
268 const guint w = g_atomic_int_get (&write_idx);
270 guint free_cnt = (w > r) ? (w - r) : ((w - r + size) & size_mask);
272 if (!commit && offset > 0) {
273 if (offset > free_cnt) {
277 r = (r + offset) & size_mask;
280 const guint to_read = cnt > free_cnt ? free_cnt : cnt;
282 const guint cnt2 = r + to_read;
287 n2 = cnt2 & size_mask;
293 memcpy (dest, &buf[r], n1 * sizeof (T));
294 r = (r + n1) & size_mask;
297 memcpy (dest + n1, buf, n2 * sizeof (T));
302 SpinLock sl (_reservation_lock);
303 g_atomic_int_set (&read_idx, r);
304 g_atomic_int_set (&reserved, std::min (reservation, g_atomic_int_get (&reserved) + to_read));
309 } /* end namespace */
311 #endif /* __ringbuffer_h__ */