From bc88e1cbf86c8d70f02afbf9d9a7cb1db92a69c3 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 27 Aug 2019 14:36:03 +0200 Subject: [PATCH] stream: add SEQ_READ/SEQ_WRITE macros --- src/pipewire/private.h | 6 ++++++ src/pipewire/stream.c | 10 +++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/pipewire/private.h b/src/pipewire/private.h index 761f8b5f3..0f314b8b3 100644 --- a/src/pipewire/private.h +++ b/src/pipewire/private.h @@ -370,6 +370,12 @@ struct pw_node_activation { uint64_t max_delay; /* max of all xruns in microseconds */ }; +#define SEQ_WRITE(s) __atomic_add_fetch((s), 1, __ATOMIC_SEQ_CST) +#define SEQ_WRITE_SUCCESS(s1,s2) ((s1) + 1 == (s2) && (s2 & 1) == 0) + +#define SEQ_READ(s) __atomic_load_n((s), __ATOMIC_SEQ_CST) +#define SEQ_READ_SUCCESS(s1,s2) ((s1) == (s2) && (s2 & 1) == 0) + #define pw_node_emit(o,m,v,...) spa_hook_list_call(&o->listener_list, struct pw_node_events, m, v, ##__VA_ARGS__) #define pw_node_emit_destroy(n) pw_node_emit(n, destroy, 0) #define pw_node_emit_free(n) pw_node_emit(n, free, 0) diff --git a/src/pipewire/stream.c b/src/pipewire/stream.c index 89307ad3d..f9f8f2c0e 100644 --- a/src/pipewire/stream.c +++ b/src/pipewire/stream.c @@ -729,13 +729,13 @@ static inline void copy_position(struct stream *impl, int64_t queued) { struct spa_io_position *p = impl->position; if (p != NULL) { - __atomic_add_fetch(&impl->seq, 1, __ATOMIC_SEQ_CST); + SEQ_WRITE(&impl->seq); impl->time.now = p->clock.nsec; impl->time.rate = p->clock.rate; impl->time.ticks = p->clock.position; impl->time.delay = p->clock.delay; impl->time.queued = queued; - __atomic_add_fetch(&impl->seq, 1, __ATOMIC_SEQ_CST); + SEQ_WRITE(&impl->seq); } } @@ -1638,10 +1638,10 @@ int pw_stream_get_time(struct pw_stream *stream, struct pw_time *time) uintptr_t seq1, seq2; do { - seq1 = __atomic_load_n(&impl->seq, __ATOMIC_SEQ_CST); + seq1 = SEQ_READ(&impl->seq); *time = impl->time; - seq2 = __atomic_load_n(&impl->seq, __ATOMIC_SEQ_CST); - } while (seq1 != seq2 || seq1 & 1); + seq2 = SEQ_READ(&impl->seq); + } while (!SEQ_READ_SUCCESS(seq1, seq2)); if (impl->direction == SPA_DIRECTION_INPUT) time->queued = (int64_t)(time->queued - impl->dequeued.outcount);