Coding Guidelines/Asynchronous Programming

From wiki.zmanda.com
Revision as of 18:08, 25 February 2008 by Dustin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Asynchronous programming is a way of writing code which reacts to events, rather than churning mercilessly through a fixed process. In essence, the program consists of a "main loop" which waits for events to occur, and a dispatcher which invokes the appropriate piece of code to handle each event. This style is usually used when writing network daemons, where the events are incoming data from clients and timeouts.

History

Historically, Amanda used the Event API to handle asynchronous programming. As a general platform for structuring applications, this API was inadequate. Luckily, Glib provides a similar, but more advanced, service called GMainLoop. The Event API was rewritten as a wrapper around GMainLoop, and all subsequent code is expected to use GMainLoop directly.

Usage

For general usage of GMainLoop, please see the glib documentation. Remember that Amanda requires glib-2.2.0 or higher, so any functionality used must be present in glib-2.2.0. In order to maintain compatibility with the Event API, however, there are a few caveats:

  • Instead of g_main_loop_run(loop), use event_loop(0).
  • To "break" the event loop, instead of g_main_loop_quit(loop), use event_quit().