Prosody application note: waiting on multiple channels

One popular architecture for applications is to have one thread which handles the routine work, such as reading and writing bulk data, on all channels, while another handles the other activities, such as starting and stopping channels. This application note suggests a way to achieve this reliably on Prosody.

In general, the approach taken is to have one thread (the worker thread) which loops, waiting for any of a set of events, while another (the manager thread) performs the channel setup and clearing. To achieve this, the two threads must ensure that the set of events is properly updated. This is done by making the worker thread include an extra item in the set of events it waits for. The manager uses this to signal that items are to be inserted into or deleted from the set of events handled by the worker. On Windows, this can be an event, while on Unix it can be a pipe. Since a pipe carries data, it would be possible to use it to carry commands to the worker. Alternatively, commands can be sent by another means, with the pipe only serving to wake the worker (by writing dummy data to it).

Care is required to ensure that access to the various objects is properly synchronised. In particular, after handing a channel to the worker for processing, the manager can only perform operations on that channel which are valid even if the worker has decided that the channel is finished. Similarly, the manager must not perform an operation, such as deleting the channel, which causes any worker thread operation to be invalid. If the manager wants to do such an operation, a request should be sent to the worker to do it, as this ensures that the channel accesses are performed in a consistent order.

An example implementation of a worker thread model is provided in test/Testlib/eventset.*, which is documented in EVENTSETs, and a replay program which uses it is provided in test/locplay. To make the code as similar as possible across operating systems, the Unix variant only uses the pipe to wake the worker and not to carry commands.


Document reference: AN 1383