Why use liboop?
Developers often wish to write applications which serve as a mediator between
several logical interfaces simultaneously; in fact, most applications work
this way. For example, a browser application might wish to maintain a user
interface while also managing a network connection and occasionally exchanging
data with the local filesystem. A server application might be communicating
with several clients at once while also occasionally receiving a signal from
the administrator directing it to reload its configuration. A multiplayer game
might want to maintain several active user interfaces at once.
Furthermore, each of these interfaces may be quite complex, sufficiently so to
merit shared code modules which specialize in managing the interface.
Widget sets deal with the details of the X protocol and graphical user
interface management; "curses" deals with the arcana of character-based
terminals; WWW libraries offer high-level access to whole families of Internet
transfer protocols; standard I/O and database routines manage filesystem data.
However, the existing techniques available for multiplexing interface code are
very poor. Most of these libraries work in "blocking" fashion; once
instructed to complete a task (such as downloading a file, or presenting a
dialog to the user), they do not return until the task is complete (or failed),
even though this may mean waiting an arbitrary amount of time for some external
agent (such as the user or the network) to respond. Some of the better systems
are able to manage several concurrent tasks internally, but cannot work with
Developers are thus left with several unpalatable choices:
The paucity of options is reflected in the quality of applications. How many
programs hang unpleasantly while performing simple network operations like
hostname resolution? How many user interfaces are unnecessarily "modal"?
How many simple servers fork for no good reason? How many network applications
simply don't exist because it's so difficult to write them?
- Accept "blocking" operation. User interfaces stop functioning while the
application waits for the network; one network client's access is stalled
while another client performs a transaction. As more data moves from local
storage (where access is fast enough that blocking is acceptable) to
delay-prone networked media, this is becoming less and less acceptable.
- Use multiple threads for concurrency. While this is a good solution for
some problems, developers who choose this route must struggle with relatively
immature and unportable threading models, and deal with the many libraries
which are not thread-safe; furthermore, threaded programming requires
thought-intensive and error-prone synchronization.
- Use multiple processes ("forking") for concurrency. This can also work,
but requires all communication between modules to use some form of
inter-process communication, which increases complexity and decreases
performance. Forking itself is a slow operation, leading to complex
"pre-forking" schemes for better performance. Worst of all, each process
must somehow multiplex IPC from other processes with whatever I/O task it had
to accomplish in the first place; this brings back the very problem forking
was designed to address.
- Attempt to multiplex each library's I/O operations directly in a master
"select loop". This requires the developer to understand intimately the
exact details of each library's I/O interactions, thus breaking modularity,
fostering unhealthy dependency and leading to a single central snarl through
which all I/O must pass.
Liboop offers a single, simple, central event loop. Modules wishing to perform
I/O without blocking request callbacks from the central event
source. These callbacks may be tied to file-descriptor activity, the
system time, or process signals. Liboop is responsible for invoking these
callbacks as appropriate.
With this system, each module "owns" its own I/O; it can perform arbitrarily
complex operations without blocking anything else in the program. But since
callbacks are executed purely sequentially, there is no complex concurrent code
to manage. From the application developer's point of view, working with liboop
is very simple; the developer simply makes calls to libraries which work their
magic and call the application back when they finish. Applications can easily
manage an arbitrary amount of multiplexed I/O operations using as many
interface libraries as they like without blocking.
To work with this system, libraries and applications must be liboop-aware.
Development with legacy code uses adapters which translate the I/O
model of an application or library into liboop's model. This does require
knowledge of the code's I/O structure, but can at least keep the modules in
an application independent of each other.
For more about liboop, see the documentation.
- Why don't you just use (favorite widget set), which lets you register
callbacks on file descriptors and all that good stuff?
- Because not everyone might want to be tied to that widget set. In
particular, the developer of a general-purpose I/O library would want to
allow everyone to use it, without requiring a particular widget set.
Liboop lets the library developer write to a standard interface,
which can then be used with most widget sets and other event loops.
- Doesn't GLib's Main
Event Loop do all this, and more?
- Not quite. GLib is a fine implementation of an event loop (with
bells and whistles) that supports some extensibility (such as the ability to
add extra sources). However, I'm doubtful that it extends far enough that
it could run on top of someone else's event loop (such as the Tk event loop).
Furthermore, the GLib event loop doesn't manage signals; synchronous handling
of asynchronous signals is very difficult to do properly and safely in most
existing systems (without kludges like polling).
In any case, we do have a
GLib source adapter so you can use the GLib event loop
with the liboop interface.
- How does liboop compare to Niels Provos' libevent?
- Like GLib, libevent is a concrete implementation of an event loop, not
an abstract interface for many event loops; also like GLib, libevent does not
manage signals. Libevent is smaller and simpler than either liboop or Glib.
While liboop and GLib are both licensed under the
Lesser GPL, libevent
appears to be licensed under the original BSD license, including the
advertising clause. Note that the advertising clause renders libevent
incompatible with GPL software!
It is entirely possible to imagine a libevent source adapter for liboop.
If anyone is interested in such an adapter, please contact me.