removed failed atomic shim, compile ringbuf as c++

This commit is contained in:
Anthony Pesch 2016-12-11 13:25:09 -08:00
parent 65da96d130
commit ae3d459960
3 changed files with 12 additions and 59 deletions

View File

@ -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})

View File

@ -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 <atomic>
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<int64_t> read_offset;
std::atomic<int64_t> 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;
}

View File

@ -1,40 +0,0 @@
#ifndef SYS_ATOMIC_H
#define SYS_ATOMIC_H
#ifdef __cplusplus
#include <atomic>
struct re_atomic_int {
std::atomic<int> x;
};
struct re_atomic_long {
std::atomic<long> 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 <stdatomic.h>
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