From ae3d4599606483a7cc0b1f01ba848a7390db4207 Mon Sep 17 00:00:00 2001 From: Anthony Pesch Date: Sun, 11 Dec 2016 13:25:09 -0800 Subject: [PATCH] removed failed atomic shim, compile ringbuf as c++ --- CMakeLists.txt | 5 +--- src/core/{ringbuf.c => ringbuf.cc} | 26 ++++++++----------- src/sys/atomic.h | 40 ------------------------------ 3 files changed, 12 insertions(+), 59 deletions(-) rename src/core/{ringbuf.c => ringbuf.cc} (65%) delete mode 100644 src/sys/atomic.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fb8e2ab..2db42275 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,7 +158,7 @@ set(REDREAM_SOURCES src/core/mm_heap.c src/core/option.c src/core/profiler.cc - src/core/ringbuf.c + src/core/ringbuf.cc src/core/rb_tree.c src/core/string.c src/emu/emulator.c @@ -270,9 +270,6 @@ elseif(COMPILER_MSVC) list(APPEND REDREAM_FLAGS -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -DWIN32_LEAN_AND_MEAN -DNOMINMAX /GR- /W3 /WX /wd4100 /wd4127 /wd4505 /wd4512 /wd4800 /wd4351) list(APPEND REDREAM_LIBS userenv ws2_32) - - # until msvc supports stdatomic.h, compile code using atomics as C++ - set_source_files_properties(src/core/ringbuf.c PROPERTIES LANGUAGE CXX) endif() add_executable(redream ${REDREAM_SOURCES}) diff --git a/src/core/ringbuf.c b/src/core/ringbuf.cc similarity index 65% rename from src/core/ringbuf.c rename to src/core/ringbuf.cc index 522b965c..bdc279ac 100644 --- a/src/core/ringbuf.c +++ b/src/core/ringbuf.cc @@ -1,36 +1,32 @@ -/* this file is compiled as C++ under MSVC due to it not supporting stdatomic.h */ -#ifdef __cplusplus +/* would be nice to convert this file to C once MSVC supports stdatomic.h */ +#include extern "C" { -#endif #include "core/ringbuf.h" #include "core/assert.h" -#ifdef __cplusplus } -#endif -#include "sys/atomic.h" struct ringbuf { int capacity; uint8_t *data; - struct re_atomic_long read_offset; - struct re_atomic_long write_offset; + std::atomic read_offset; + std::atomic write_offset; }; void ringbuf_advance_write_ptr(struct ringbuf *rb, int n) { - ATOMIC_FETCH_ADD(rb->write_offset, n); + rb->write_offset.fetch_add(n); } void *ringbuf_write_ptr(struct ringbuf *rb) { - int write_offset = ATOMIC_LOAD(rb->write_offset); + int64_t write_offset = rb->write_offset.load(); return rb->data + (write_offset % rb->capacity); } void ringbuf_advance_read_ptr(struct ringbuf *rb, int n) { - ATOMIC_FETCH_ADD(rb->write_offset, n); + rb->read_offset.fetch_add(n); } void *ringbuf_read_ptr(struct ringbuf *rb) { - int read_offset = ATOMIC_LOAD(rb->read_offset); + int64_t read_offset = rb->read_offset.load(); return rb->data + (read_offset % rb->capacity); } @@ -39,9 +35,9 @@ int ringbuf_remaining(struct ringbuf *rb) { } int ringbuf_available(struct ringbuf *rb) { - int read = ATOMIC_LOAD(rb->read_offset); - int write = ATOMIC_LOAD(rb->write_offset); - int available = write - read; + int64_t read = rb->read_offset.load(); + int64_t write = rb->write_offset.load(); + int available = (int)(write - read); CHECK(available >= 0 && available <= rb->capacity); return available; } diff --git a/src/sys/atomic.h b/src/sys/atomic.h deleted file mode 100644 index eebd97a9..00000000 --- a/src/sys/atomic.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef SYS_ATOMIC_H -#define SYS_ATOMIC_H - -#ifdef __cplusplus - -#include - -struct re_atomic_int { - std::atomic x; -}; - -struct re_atomic_long { - std::atomic x; -}; - -#define ATOMIC_LOAD(a) (a.x.load()) -#define ATOMIC_FETCH_ADD(a, value) (a.x.fetch_add(value)) -#define ATOMIC_STORE(a, value) (a.x.store(value)) -#define ATOMIC_EXCHANGE(a, value) (a.x.exchange(value)) - -#else - -#include - -struct re_atomic_int { - atomic_int x; -}; - -struct re_atomic_long { - atomic_long x; -}; - -#define ATOMIC_LOAD(a) atomic_load(&a.x) -#define ATOMIC_FETCH_ADD(a, value) atomic_fetch_add(&a.x, value) -#define ATOMIC_STORE(a, value) atomic_store(&a.x, value) -#define ATOMIC_EXCHANGE(a, value) atomic_exchange(&a.x, value) - -#endif - -#endif