[FFmpeg-devel] [RFC] dummy barrier macros for fifo.c

Reimar Döffinger Reimar.Doeffinger
Sun Mar 8 01:15:23 CET 2009


Hello,
assuming you consider this desirable...
As usual with such stuff it is easy to miss something and I did not
think _that_ long about it.
I split the barriers by type, since I think e.g. x86 does not do
speculative reads - no idea if the compiler would be allowed to
generate them anyway though.

Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavutil/fifo.c
===================================================================
--- libavutil/fifo.c	(revision 17866)
+++ libavutil/fifo.c	(working copy)
@@ -22,6 +22,14 @@
 #include "common.h"
 #include "fifo.h"
 
+// dummy macros for a future memory barrier implementation
+//! disallows all out-of-order memory accesses to cross
+#define OOO_MEM_BARRIER
+//! disallows all out-of-order memory writes to cross
+#define OOO_WRITE_BARRIER
+//! disallows speculative reads after this barrier that depend on reads before it
+#define SPEC_READ_BARRIER
+
 int av_fifo_init(AVFifoBuffer *f, unsigned int size)
 {
     f->wptr = f->rptr =
@@ -40,7 +48,12 @@
 
 int av_fifo_size(AVFifoBuffer *f)
 {
-    return (uint32_t)(f->wndx - f->rndx);
+    uint32_t size = f->wndx - f->rndx;
+    // catches the case av_fifo_generic_read(b, av_fifo_size(b), ...)
+    // where a speculative read reads buffer contents before the buffer
+    // and wndx are written but size is calculated from the updated wndx
+    SPEC_READ_BARRIER
+    return size;
 }
 
 int av_fifo_read(AVFifoBuffer *f, uint8_t *buf, int buf_size)
@@ -65,6 +78,8 @@
             return -1;
         av_fifo_read(f, f2.buffer, len);
         f2.wptr += len;
+        // make sure additional data becomes not "visible" before it is written.
+        OOO_WRITE_BARRIER
         f2.wndx += len;
         av_free(f->buffer);
         *f= f2;
@@ -94,6 +109,8 @@
         f->wptr += len;
         if (f->wptr >= f->end)
             f->wptr = f->buffer;
+        // make sure additional data becomes not "visible" before it is written.
+        OOO_WRITE_BARRIER
         f->wndx += len;
         size -= len;
     } while (size > 0);
@@ -110,6 +127,8 @@
             memcpy(dest, f->rptr, len);
             dest = (uint8_t*)dest + len;
         }
+        // make sure we do not make buffer space available before we read all data.
+        OOO_MEM_BARRIER
         av_fifo_drain(f, len);
         buf_size -= len;
     } while (buf_size > 0);



More information about the ffmpeg-devel mailing list