summaryrefslogtreecommitdiffstats
path: root/misc
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-05-21 01:04:47 +0200
committerwm4 <wm4@nowhere>2014-05-21 02:21:18 +0200
commit8e7cf4bc992f13dbb523bb42d6b9de4bc2f486c2 (patch)
tree77f21515a336c368d2bb97eab11d950bed9f5c4d /misc
parent2f65f0e2548f95b3b8ba6620efe6c0e3cb02420b (diff)
downloadmpv-8e7cf4bc992f13dbb523bb42d6b9de4bc2f486c2.tar.bz2
mpv-8e7cf4bc992f13dbb523bb42d6b9de4bc2f486c2.tar.xz
atomics: switch to C11 stdatomic.h
In my opinion, we shouldn't use atomics at all, but ok. This switches the mpv code to use C11 stdatomic.h, and for compilers that don't support stdatomic.h yet, we emulate the subset used by mpv using the builtins commonly provided by gcc and clang. This supersedes an earlier similar attempt by Kovensky. That attempt unfortunately relied on a big copypasted freebsd header (which also depended on much more highly compiler-specific functionality, defined reserved symbols, etc.), so it had to be NIH'ed. Some issues: - C11 says default initialization of atomics "produces a valid state", but it's not sure whether the stored value is really 0. But we rely on this. - I'm pretty sure our use of the __atomic... builtins is/was incorrect. We don't use atomic load/store intrinsics, and access stuff directly. - Our wrapper actually does stricter typechecking than the stdatomic.h implementation by gcc 4.9. We make the atomic types incompatible with normal types by wrapping them into structs. (The FreeBSD wrapper does the same.) - I couldn't test on MinGW.
Diffstat (limited to 'misc')
-rw-r--r--misc/ring.c21
1 files changed, 8 insertions, 13 deletions
diff --git a/misc/ring.c b/misc/ring.c
index eb139c2cab..28e610c9bb 100644
--- a/misc/ring.c
+++ b/misc/ring.c
@@ -30,19 +30,17 @@ struct mp_ring {
/* Positions of the first readable/writeable chunks. Do not read this
* fields but use the atomic private accessors `mp_ring_get_wpos`
* and `mp_ring_get_rpos`. */
- uint32_t rpos, wpos;
+ atomic_uint_least32_t rpos, wpos;
};
static uint32_t mp_ring_get_wpos(struct mp_ring *buffer)
{
- mp_memory_barrier();
- return buffer->wpos;
+ return atomic_load(&buffer->wpos);
}
static uint32_t mp_ring_get_rpos(struct mp_ring *buffer)
{
- mp_memory_barrier();
- return buffer->rpos;
+ return atomic_load(&buffer->rpos);
}
struct mp_ring *mp_ring_new(void *talloc_ctx, int size)
@@ -61,8 +59,7 @@ int mp_ring_drain(struct mp_ring *buffer, int len)
{
int buffered = mp_ring_buffered(buffer);
int drain_len = FFMIN(len, buffered);
- mp_atomic_add_and_fetch(&buffer->rpos, drain_len);
- mp_memory_barrier();
+ atomic_fetch_add(&buffer->rpos, drain_len);
return drain_len;
}
@@ -81,8 +78,7 @@ int mp_ring_read(struct mp_ring *buffer, unsigned char *dest, int len)
memcpy(dest, buffer->buffer + read_ptr, len1);
memcpy(dest + len1, buffer->buffer, len2);
- mp_atomic_add_and_fetch(&buffer->rpos, read_len);
- mp_memory_barrier();
+ atomic_fetch_add(&buffer->rpos, read_len);
return read_len;
}
@@ -100,16 +96,15 @@ int mp_ring_write(struct mp_ring *buffer, unsigned char *src, int len)
memcpy(buffer->buffer + write_ptr, src, len1);
memcpy(buffer->buffer, src + len1, len2);
- mp_atomic_add_and_fetch(&buffer->wpos, write_len);
- mp_memory_barrier();
+ atomic_fetch_add(&buffer->wpos, write_len);
return write_len;
}
void mp_ring_reset(struct mp_ring *buffer)
{
- buffer->wpos = buffer->rpos = 0;
- mp_memory_barrier();
+ atomic_store(&buffer->wpos, 0);
+ atomic_store(&buffer->rpos, 0);
}
int mp_ring_available(struct mp_ring *buffer)