From 1222fd40b7b0b79b40977bd2ab51ecae5f730039 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sun, 12 Apr 2020 13:14:53 +1000 Subject: [PATCH] [common] fix FrameBuffer to use atomics correctly Might Fix #248 --- VERSION | 2 +- c-host/src/app.c | 2 +- common/include/common/framebuffer.h | 21 +++++++---------- common/src/framebuffer.c | 35 ++++++++++++++++++++++++----- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/VERSION b/VERSION index 05305d61..49410ff2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B1-163-g577c54d20d+1 \ No newline at end of file +B1-165-gb5f4c639fd+1 \ No newline at end of file diff --git a/c-host/src/app.c b/c-host/src/app.c index f2215e81..ce58c9fc 100644 --- a/c-host/src/app.c +++ b/c-host/src/app.c @@ -187,7 +187,7 @@ static int frameThread(void * opaque) fi->height = frame.height; fi->stride = frame.stride; fi->pitch = frame.pitch; - fi->offset = pageSize - sizeof(FrameBuffer); + fi->offset = pageSize - FrameBufferStructSize; frameValid = true; // put the framebuffer on the border of the next page diff --git a/common/include/common/framebuffer.h b/common/include/common/framebuffer.h index 20cd479f..21a52e37 100644 --- a/common/include/common/framebuffer.h +++ b/common/include/common/framebuffer.h @@ -23,24 +23,19 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include - -typedef struct stFrameBuffer -{ - uint64_t wp; - uint8_t data[0]; -} -FrameBuffer; - +typedef struct stFrameBuffer FrameBuffer; typedef bool (*FrameBufferReadFn)(void * opaque, const void * src, size_t size); +/** + * The size of the FrameBuffer struct + */ +extern const size_t FrameBufferStructSize; + /** * Wait for the framebuffer to fill to the specified size */ -static inline void framebuffer_wait(const FrameBuffer * frame, size_t size) -{ - while(frame->wp != size) {} -} +void framebuffer_wait(const FrameBuffer * frame, size_t size); /** * Read data from the KVMFRFrame into the dst buffer @@ -60,4 +55,4 @@ void framebuffer_prepare(FrameBuffer * frame); /** * Write data from the src buffer into the KVMFRFrame */ -bool framebuffer_write(FrameBuffer * frame, const void * src, size_t size); \ No newline at end of file +bool framebuffer_write(FrameBuffer * frame, const void * src, size_t size); diff --git a/common/src/framebuffer.c b/common/src/framebuffer.c index 4e2f7e06..6de5f8a2 100644 --- a/common/src/framebuffer.c +++ b/common/src/framebuffer.c @@ -21,19 +21,38 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "common/debug.h" #include +#include + #define FB_CHUNK_SIZE 1024 +struct stFrameBuffer +{ + atomic_uint_least32_t wp; + uint8_t data[0]; +}; + +const size_t FrameBufferStructSize = sizeof(FrameBuffer); + +void framebuffer_wait(const FrameBuffer * frame, size_t size) +{ + while(atomic_load_explicit(&frame->wp, memory_order_relaxed) != size) {} +} + bool framebuffer_read(const FrameBuffer * frame, void * dst, size_t size) { uint8_t *d = (uint8_t*)dst; uint64_t rp = 0; while(rp < size) { + uint_least32_t wp; + /* spinlock */ - while(rp == frame->wp) { } + do + wp = atomic_load_explicit(&frame->wp, memory_order_relaxed); + while(rp == wp); /* copy what we can */ - uint64_t avail = frame->wp - rp; + uint64_t avail = wp - rp; avail = avail > size ? size : avail; memcpy(d, frame->data + rp, avail); @@ -50,11 +69,15 @@ bool framebuffer_read_fn(const FrameBuffer * frame, FrameBufferReadFn fn, size_t uint64_t rp = 0; while(rp < size) { + uint_least32_t wp; + /* spinlock */ - while(rp == frame->wp) { } + do + wp = atomic_load_explicit(&frame->wp, memory_order_relaxed); + while(rp == wp); /* copy what we can */ - uint64_t avail = frame->wp - rp; + uint64_t avail = wp - rp; avail = avail > size ? size : avail; if (!fn(opaque, frame->data + rp, avail)) @@ -82,8 +105,8 @@ bool framebuffer_write(FrameBuffer * frame, const void * src, size_t size) { size_t copy = size < FB_CHUNK_SIZE ? FB_CHUNK_SIZE : size; memcpy(frame->data + frame->wp, src, copy); - __sync_fetch_and_add(&frame->wp, copy); + atomic_fetch_add(&frame->wp, copy); size -= copy; } return true; -} \ No newline at end of file +}