Display server main control loop

Robert Ancell robert.ancell at canonical.com
Wed Apr 3 22:25:23 UTC 2013

On 04/04/13 04:51, Alexandros Frantzis wrote:
> On Tue, Apr 02, 2013 at 03:40:45PM +0300, Alexandros Frantzis wrote:
>> Hi all,
>> while working on adding VT switching I noticed that the code started to
>> become more complicated, as I needed to handle asynchronous events (a
>> signal in this case) and coordinate cross-component operations.
>> One way to improve the situation is to add a main control loop to the
>> display server. This is *not* meant to be a loop for all kinds of events
>> (e.g.  input, graphics), only for external asynchronous events that need
>> to change the state of the main components of the display server in a
>> coordinated manner.
>> Such events are:
>> * Quitting (signals)
>> * VT switching (and more generally, display server pause/resume) (signals)
>> * Display reconfiguration (e.g. watching a udev FD on the desktop)
>> Although, we could conceivably handle these in a distributed manner, I
>> feel we would be adding a lot of complexity and fragility to the system.
>> There are a couple of mechanisms we could use to implement a main
>> control loop and that could handle events from all our sources (signals
>> and FDs for now).  We could use boost asio, or we could implement our
>> own mechanism, using signalfd to route the signals to an fd we can
>> select() on.
>> The main loop could live in the DisplayServer::run() method, replacing
>> the wait on the exit_cv condition variable that is currently there, or
>> it could run in a separate thread like SignalDispatcher currently does.
>> I prefer the first approach since the main display server thread is idle
>> anyway, and it's a natural place for such an event loop.
>> Thoughts? Any other events of this kind that we should be aware of?
>> Thanks,
>> Alexandros
> To get a better feeling of what's involved, I created a prototype
> abstraction for a main loop and an implementation using boost asio.
> Get it and use it with:
> $ bzr branch lp:~afrantzis/+junk/mainloop
> $ cd mainloop && make
> $ ./mainloop
> The main loop abstraction was designed so that event creation and
> handling are completely dissociated. The display server (or other
> consumer) can handle an event without caring if it is coming from a
> signal, a read from a fd, or another mechanism.  So, we could have
> something like this in the mir main loop:
> auto main_loop = main_loop_factory->create_main_loop();
> auto pause_event = platform->create_pause_event(main_loop_factory);
> auto disp_conf_event = platform->create_display_configuration_event(main_loop_factory);
> auto quit_event = main_loop_factory->create_signal_event(SIGINT);
> main_loop->register_event_handler(pause_event, pause_handler);
> main_loop->register_event_handler(disp_conf_event, disp_conf_handler);
> main_loop->register_event_handler(quit_event, [main_loop]{main_loop->stop();});
> main_loop->run();
> Thoughts?
> Thanks,
> Alexandros
I like having the event objects as opposed to just having these as
parameters when registering.

However it would seem to me more logical to register the handlers on the
event object and not the main loop. That would allow parameters on the
event handler functions (I'm not sure void() is going to be sufficient
for all events). I can't think of a good case to be able to use the same
handler for different event types.

I'm assuming the FD event is unfinished? It reads a requested amount of
bytes from a FD then calls the handler without giving access to the data?

More information about the Mir-devel mailing list