Re: Lock-free protocols?
Re: Lock-free protocols?
- Subject: Re: Lock-free protocols?
- From: Kurt Revis <email@hidden>
- Date: Sat, 27 Apr 2002 21:44:46 -0700
On Monday, April 22, 2002, at 01:04 AM, Jeremy Cooper wrote:
I've seen a lot of discussion on this list about the perils of
synchronizing IOProc threads with worker threads using locks. Several
people have mentioned that there are ways to transfer data safely
between
IOProc threads and other lower-priority threads without using locks, yet
no one has actually shown how it is done.
Can someone show some code snippets for 'lock-free' buffer passing?
Well, I got curious about this too, so I figured it out (I think!) and
put together an example.
http://www.snoize.com/Code/PlayBufferedAIFF.tar.gz
This is a command-line tool which plays an AIFF file through the default
audio output. It uses a ring buffer to moderate between between a thread
which reads data from the file, and the CoreAudio playback thread. The
ring buffer doesn't use explicit locking (it relies on atomic reads and
writes by the processor) so the playback thread will not block when it
shouldn't.
Note: I have only tested this on a single-processor machine, so my
assumptions about atomicity on SMP machines could turn out to be
completely wrong. It wouldn't be the first time SMP bit someone. If
anyone could try it and let me know your results, I would appreciate it.
That said--it works pretty well on my machine. With the default
parameters, I don't get dropouts even with lots of other stuff going on
in the background, dragging big windows all over the place, etc, and the
CPU usage seems to be negligible compared to the CoreAudio overhead. I'm
sure the parameters could use some further tuning, of course.
The ring buffer also uses a neat trick with Mach VM mapping, which makes
dealing with the wraparound a whole lot easier and faster.
Give it a try... comments and corrections are appreciated.
--
Kurt Revis
email@hidden
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.