MP safe event-driven archtecture without explicit locking
Posted: Sat Oct 20, 2007 4:34 pm
Since this is kinda related to the GUI thread I just started, and which already has nice amount of interesting observations from other people, and I kinda thought about this related topic today as well, I thought I'd start another thread.
Was reading a paper about this libasync_mp thingie (google if you care, I'll cover the basics anyway) which has an interesting concept:
Basically, you have a single event-driven multiplexing system, with asynchronous I/O and a callback system (with the library doing some C++ magic to provide currying of arguments to callbacks and such). That's the basis for libasync as far as I understand.
Now, the _mp version goes further by handling the events in multiple threads (one per CPU) for hardware parallelism. To prevent conflicts from concurrently run callbacks, it has a concept of coloring, with the simple rule that no two events of the same color will ever be run at the same time...
So if several events need to access the same data structures, they can be given the same color, and no locking is necessary. Further (and I'm not sure if libasync_mp actually guarantees this) if the events are kept in order, and the first event that is runnable (not blocked by another event of same color) is already scheduled next, then events of a given color will always be run in the order they were added to the queue (with respect to each other that is). So not only could one control access to shared structures, but also control the order of processing for events where that is important (say, input from the user).
The paper I read, also mentions multi-color events in passing, so I kinda started thinking: if one allows several colors per event and guarantees that no two concurrently running events share any of the colors, then allows dynamic allocation of the colors and dynamic coloring of the events, then associates different colors to different data-structures (or parts of data-structures), then one essentially ends up with all the locking in the same place (the multi-threaded event-loop), and completely avoids potential for dead-locks (in the worst case the system would degenerate into a single-threaded event-loop if no two events can run concurrently, but would not completely dead-lock).
Am I overlooking something, or does this look like a nice model? I mean, in OS kernels one typically ends up using some sort of deferred procedure call mechanism all the time anyway, so maybe it would be worth the trouble to associate the coloring (essentially locking) with the DPC queuing system, and essentially get two fish with the same bait.
Was reading a paper about this libasync_mp thingie (google if you care, I'll cover the basics anyway) which has an interesting concept:
Basically, you have a single event-driven multiplexing system, with asynchronous I/O and a callback system (with the library doing some C++ magic to provide currying of arguments to callbacks and such). That's the basis for libasync as far as I understand.
Now, the _mp version goes further by handling the events in multiple threads (one per CPU) for hardware parallelism. To prevent conflicts from concurrently run callbacks, it has a concept of coloring, with the simple rule that no two events of the same color will ever be run at the same time...
So if several events need to access the same data structures, they can be given the same color, and no locking is necessary. Further (and I'm not sure if libasync_mp actually guarantees this) if the events are kept in order, and the first event that is runnable (not blocked by another event of same color) is already scheduled next, then events of a given color will always be run in the order they were added to the queue (with respect to each other that is). So not only could one control access to shared structures, but also control the order of processing for events where that is important (say, input from the user).
The paper I read, also mentions multi-color events in passing, so I kinda started thinking: if one allows several colors per event and guarantees that no two concurrently running events share any of the colors, then allows dynamic allocation of the colors and dynamic coloring of the events, then associates different colors to different data-structures (or parts of data-structures), then one essentially ends up with all the locking in the same place (the multi-threaded event-loop), and completely avoids potential for dead-locks (in the worst case the system would degenerate into a single-threaded event-loop if no two events can run concurrently, but would not completely dead-lock).
Am I overlooking something, or does this look like a nice model? I mean, in OS kernels one typically ends up using some sort of deferred procedure call mechanism all the time anyway, so maybe it would be worth the trouble to associate the coloring (essentially locking) with the DPC queuing system, and essentially get two fish with the same bait.