There is a kernel driver that collects data from some external hardware and passes all data to a virtual file if a reader reads this file.
Data blocks are appended to a list with the assumption the reader reads them fast enough so the list won't blow up to an unreasonable size. Only one client is supported, the last connected client wins.
All this works for a quick and dirty usage but is clumsy, scary and error prone.
Now I want to extend this to serve multilpe parallel readers / open instances of said virtual file. Each reader shall receive all data that is produced.
At the same time I want to change code from "surprisingly it works" to "clean and stable".
Is there a good example for this task that I can look at, learn how to do that nicely and avoid error sitations that I am not even aware of right now?
Of course I started thinking about that on my own. Here is a first plan, you are welcome to comment!
- A clients-list holds all clients that have opened the file until the client closes the file. Each client has it's own list of data blocks to provide to the client in a read operation.
- Clients are added or removed to or from this list in a mutex.
- For an incoming data block lock the mutex. For each client in the clients-list copy data and add this copy to the client's data list in a spinlock.
- The clients' reading function is waiting on an event. The read function locks this client's data list with the spinlock so reading and removing this data from the list doesn't collide with adding data.
Would this be a perhaps not most professional but anyway feasible solution?