[alsa-devel] Unblock MIDI IN in blocking read mode
Hi,
for best performance, I use a blocking snd_rawmidi_read() on an ALSA MIDI input device, byte by byte. It works fine, except:
1) How do I close the device from a different thread? I.e. how can I unblock the blocking read() call when it's time to close the device and no MIDI data is currently coming in? Just calling snd_rawmidi_close() will cause a crash. Using snd_rawmidi_drop() did not release the blocking read() in the other thread.
2) How is a blocking read() typically implemented in the ALSA MIDI driver? Maybe I can resort to non-blocking mode and avoid the situation above.
Thanks, Florian
Florian wrote:
for best performance, I use a blocking snd_rawmidi_read() on an ALSA MIDI input device, byte by byte. It works fine, except:
- How do I close the device from a different thread?
You don't. The ALSA library isn't thread-safe; you cannot close the device as long as the read() is still executing.
I.e. how can I unblock the blocking read() call when it's time to close the device and no MIDI data is currently coming in?
You cannot. The blocking read() unblocks when there is data to read, or when an error occurs. The raw MIDI functions do not implement an API for non-MIDI messages.
Maybe I can resort to non-blocking mode and avoid the situation above.
Yes; use another file descriptor (a pipe or something like that) that can be used to let poll() return.
HTH Clemens
participants (2)
-
Clemens Ladisch
-
Florian