Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback version of PortMidi for Windows, bad idea? #76

Open
cogled4 opened this issue Oct 29, 2024 · 1 comment
Open

Callback version of PortMidi for Windows, bad idea? #76

cogled4 opened this issue Oct 29, 2024 · 1 comment

Comments

@cogled4
Copy link

cogled4 commented Oct 29, 2024

I needed a callback version of PortMidi for my Windows app. I modified the library to accept a callback function when opening a midi input stream. Then, in the Windows implementation, I call this callback instead of inserting the PmEvent into the queue.

For my purposes this seems to work well, the queue stays empty and I get the PmEvent's, both short and Sysex, sent straight to my callback function. Of course this isn't portable and I'm not recommending anyone else do this :) But I did want to ask, should I anticipate any major problems with this approach? It looks like the Windows Midi callback does use EnterCriticalSection() and LeaveCriticalSection(). I need to do more research about what that entails.

Forgive me if I'm posting this question in the wrong place. I couldn't find any Portmidi forum anywhere else.

@rbdannenberg
Copy link
Contributor

What do you do in your callback? The callback is not running under your application thread, so if it reads or writes anything in your application that might also be read or written by the application, then you can expect non-deterministic and probably wrong behavior, but it won't happen often, so it will be hard to detect or debug. If you use critical sections correctly, you can probably avoid wrong behavior, but it can be hard to get right and even harder to test. The safest thing you could do is probably to put incoming MIDI into a data structure to be safely processed later by the application, but that's exactly what PortMidi does already. The other problem is priority inversion: Suppose you use critical sections and your application holds the lock and some other application is running at an intermediate priority between that of the MIDI callback and the application. This unknown intermediary process can remain busy indefinitely, effectively blocking the MIDI callback that has higher priority. I don't know what priority is given to the MIDI callback or what happens if it is blocked for an unusual amount of time, and I don't think Microsoft documents this or makes any promises, which is why PortMidi takes a safer and more portable implementation strategy (it's also free from priority inversion issues).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants