[linux-audio-dev] Re: desktop and multimedia as an afterthought?

Benno Senoner sbenno at gardena.net
Tue Jul 13 13:03:47 UTC 2004


Martijn Sipkema wrote:

>It is often heard in the Linux audio community that mutexes are not realtime
>safe and a lock-free ringbuffer should be used instead. Using such a lock-free
>ringbuffer requires non-standard atomic integer operations and does not
>guarantee memory synchronization (and should probably not perform
>significantly better than a decent mutex implementation) and is thus not
>portable.
>  
>

Why not portable ? on x86 you have guaranteed atomicity of 32bit 
read/writes and using the read_ptr / write_ptr
approach guarantees that you will never get bad values for the 
ringbuffer pointers. The worst that could happen
is that the reader reads an "old" value and thus getting a bit smaller 
available read space from the ringbuffer but
given the asynchronous nature of multithreaded apps this is completely 
meaningless.
(the audio thread does not know/care when then disk thread writes data 
into the ringbuffer)
PPC guarantees 32bit atomicity too so the atomic macros we are using in 
LinuxSampler simply translate to a load or a store on
both x86 and PPC and if future CPUs with non atomic access arise , just 
add the macro in atomic.h.
I would not call that "non portable". posix mutexes are not portable 
because they don't work on win32 so in general
applications with a certain complexity always require some platform 
dependent code (either OS or CPU dependent) to
be portable on that platform. If you design your app decently the amount 
of platform dependent code is really tiny , usually
 well below 1%.
About performance, nothing can beat lock free ringbuffers because it's 
simply a few machine instructions that access
a read_ptr, write_ptr and return the address of the object you want to 
read/write.

cheers,
Benno
http://www.linuxsampler.org





More information about the Linux-audio-dev mailing list