[PD] readsf~ and writesf~, locking, blocking and real-time priority
robert at urbanstew.org
Tue Oct 6 04:00:30 CEST 2015
I’m trying to understand why readsf~ and writesf~ work so well.
I’m particularly referencing Ross Bencina’s article: http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing and his subsequent paper, http://www.rossbencina.com/static/writings/File_IO_ACMC2014_Bencina.pdf
If you are not into asynchronous message passing and lock-free queueing then I’ll summarize the articles briefly:
When engaging in file I/O (e.g reading from or writing to an audio file) do not use locks or blocking. He goes on to say that this can lead to priority inversion, unbound execution time and “scheduler paranoia”.
This is all absolutely true in my experience in the audio jungle.
Pd’s async file I/O objects (readsf~ and writesf~) use both locks and blocking via a mutex and the pthread_cond_signal and pthread_cond_init functions. Look at the source code file d_soundfile.c for more details. The gist of it is that these objects have two threads. One parent thread that sends the data to the dsp scheduler, and a child thread that grabs the data from the file, and subsequently the child signals the parent when it has more data.
Based on Bencina’s paper, readsf~ and writesf~ could (should?) glitch and may not be real-time safe.
My questions are:
1) Have I completely misunderstood d_soundfile.c and it is actually entirely safe. If so, why is it safe?
2) Why doesn’t Pd glitch more often when using these objects?
3) Does Pd need lock-free message queueing for such inter-thread communication?
4) Has anyone ever “broken” these objects or experienced glitching?
Thanks for the extra brain power.
More information about the Pd-list