Coding Guidelines/Asynchronous Programming

From wiki.zmanda.com
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.

C 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.

The default GMainLoop object is available from event.h's default_main_loop(), so you might write

g_main_loop_run(default_main_loop());

or

g_main_loop_quit(default_main_loop());

Perl Usage

See Amanda::MainLoop for an example and a list of available event sources.