kcat / openal-soft

OpenAL Soft is a software implementation of the OpenAL 3D audio API.
Other
2.22k stars 536 forks source link

Is alGetError() error state independent for each thread? #973

Closed SephiRok closed 9 months ago

SephiRok commented 9 months ago

The common quoted pattern to use to detect errors is to call alGetError() to reset error, call the needed function and then grab error for that function with alGetError().

alGetError();
alGenBuffers(...);
ALenum error = alGetError();

Is this thread safe? I.e. does each thread have its own alGetError error state?

kcat commented 9 months ago

No, each context has its own error state. I'm not sure if per-thread error states would be allowable, but it might be something to consider adding, at least as an option if nothing else.

SephiRok commented 9 months ago

It would be nice, since it's pretty hard to use that pattern in multiple threads without it ...

Is there a suggested way to check for errors when using multiple threads?

kcat commented 9 months ago

I added thread-local context errors with commit 0584aec5d43f52d762f335cca0d62513f48328bd (or if on Apple systems, commit 85d5a6092cb270001e87eb759367e8479e721a0a). This is currently a preliminary test, that may be reverted or done differently if any significant problems crop up, but as long as there are no problems, it'll very likely stay.

Otherwise, the only reliable way to avoid race conditions with error checking and multi-threaded use is to use a mutex (or other mutual exclusion method) around calls that can generate errors and the alGetError call to check the error. Which is pretty wasteful in the case of no errors and makes OpenAL's thread-safe guarantee rather pointless.

SephiRok commented 9 months ago

Awesome. Cheers for the change.