Proposal: Ability to run initialization/teardown code for driver families
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 14.1k
- Forks
- 2.2k
- Avg merge
- 7h 35m
- Merged PRs (30d)
- 51
Description
Several frameworks are used to implement multiple kinds of drivers. For example:
- SDL is used to implement video, audio, input, and controller drivers
- ALSA is used to implement audio and MIDI drivers
- Linuxraw and udev are used to implement input and controller drivers
These multi-purpose frameworks may have their own global state or configuration, such as SDL_LogSetOutputFunction or snd_lib_error_set_handler.
So let's say I want ALSA to log its errors via RARCH_ERR. I could set that up in alsa_init and clean it up in alsa_free. But here's the problem: What if I wanted to log errors in the alsa_midi driver? Who should set the log handler, and who should clean it up? What if somebody else outside of RetroArch configured snd_lib_error_set_handler (say, though a LD_PRELOAD)? Now there's an implicit dependency on the order in which drivers are set up and tear down.
Here's my proposed solution: Each driver with the same name shall be implicitly grouped into a "family". For example, the SDL drivers for video, audio, input, and controllers are all part of the sdl2 family. Here's how this works:
- For each driver family that's to be initialized, a single family-specific
initfunction will be called before its constituent drivers are initialized. This can be used to initialize framework-specific global state such as error-handling or loggers. For instance, a hypotheticalsdl_driver_family_initwould be called if any of the SDL-based drivers are to be used. - When a driver family is uninitialized, a single family-specific
freefunction will be called, for the purpose of undoing whatever state was changed ininit. This would be called after the constituent drivers are freed. - Individual drivers would still be expected to maintain their own driver-specific state. So
sdl_audio_initwould still be responsible for callingSDL_Init(SDL_INIT_AUDIO). - Driver families of size 1 are allowed, but pointless; all global state involving this driver could just be managed within the driver itself.
- There's no need to change how drivers are initialized or identified; it's enough to implicitly group drivers with the same name into families.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing alsa_init and alsa_free in audio/drivers/alsa.c, then compare the SDL driver entry points linked in the issue. Confirm how drivers are identified and initialized before designing family-level hooks. Done means a family init runs once before its drivers and family cleanup runs after them, including shared SDL or ALSA state.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- backend, operating-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100