Display server main control loop

Alexandros Frantzis alexandros.frantzis at canonical.com
Thu Apr 4 07:56:48 UTC 2013


On Thu, Apr 04, 2013 at 11:25:23AM +1300, Robert Ancell wrote:
> 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?

The idea (in this prototype) is that main loop events do not carry
additional data. A particular amount of data being available on an FD (a
message) is just a notification that something interesting has happened,
in the same way a signal is; the contents of the message don't matter.
Data is being read from the FD to ensure we don't keep getting
notifications about the same message. This scheme is somewhat limited,
but does cover the use cases we currently care about.

Perhaps we can build more elaborate events (with custom handlers), on
top of this scheme. I will give this some more thought.



More information about the Mir-devel mailing list