All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] alsa-lib:Make snd_atomic_write_* truly atomic
@ 2016-04-13 10:42 Andreas x Larsson
  2016-04-13 13:49 ` Takashi Iwai
  0 siblings, 1 reply; 15+ messages in thread
From: Andreas x Larsson @ 2016-04-13 10:42 UTC (permalink / raw)
  To: alsa-devel

Under some usecases a race condition appears inside
the snd_atomic_write_* functions. The 'begin' and 'end'
variables are updated with the ++ operator which is not
atomic but needs to be. This can be achieved with the
gcc atomic_* built-ins. Combined with __ATOMIC_SEQ_CST
as the memory model, memory barriers are introduced so
those can also be removed from the code.

Signed-off-by: Andreas x Larsson <andrelan@axis.com>
Signed-off-by: Arvid Nihlgård Lindell <arvidnl@axis.com>
---
  alsa-lib/include/iatomic.h | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/alsa-lib/include/iatomic.h b/alsa-lib/include/iatomic.h
index acdd3e2..7fafe20 100644
--- a/alsa-lib/include/iatomic.h
+++ b/alsa-lib/include/iatomic.h
@@ -140,14 +140,12 @@ static __inline__ void 
snd_atomic_write_init(snd_atomic_write_t *w)

  static __inline__ void snd_atomic_write_begin(snd_atomic_write_t *w)
  {
-   w->begin++;
-   wmb();
+   __atomic_add_fetch(&w->begin, 1,__ATOMIC_SEQ_CST);
  }

  static __inline__ void snd_atomic_write_end(snd_atomic_write_t *w)
  {
-   wmb();
-   w->end++;
+   __atomic_add_fetch(&w->end, 1, __ATOMIC_SEQ_CST);
  }

  static __inline__ void snd_atomic_read_init(snd_atomic_read_t *r, 
snd_atomic_write_t *w)
-- 
2.1.4
_______________________________________________
Alsa-devel mailing list
Alsa-devel@alsa-project.org
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2016-05-17 15:37 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-13 10:42 [PATCH] alsa-lib:Make snd_atomic_write_* truly atomic Andreas x Larsson
2016-04-13 13:49 ` Takashi Iwai
2016-05-12 12:51   ` Ricard Wanderlof
2016-05-12 13:04     ` Takashi Iwai
2016-05-12 14:17       ` Ricard Wanderlof
2016-05-12 14:47         ` Takashi Iwai
2016-05-12 16:09           ` Ricard Wanderlof
2016-05-13 12:26           ` Ricard Wanderlof
2016-05-13 12:37             ` Takashi Iwai
2016-05-17 12:11               ` Ricard Wanderlof
2016-05-17 12:21                 ` Takashi Iwai
2016-05-17 13:29                   ` Ricard Wanderlof
2016-05-17 13:37                     ` Takashi Iwai
2016-05-17 15:28                       ` Ricard Wanderlof
2016-05-17 15:37                         ` Takashi Iwai

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.