Previous Page Contents Page Next Page

2.2 Scheduling Asynchronous Events

The method that an application uses to schedule asynchronous events depends on which of the following types of application it is:

Single-threaded applications

Applications that are based around a single main thread of execution to receive and process requests

Multithreaded applications

Applications that can have several threads of execution receiving and processing requests

Motif applications

Applications that use the Motif interface and for which the application code consists mainly of callbacks from the Motif libraries

2.2.1 Single-Threaded Applications

To schedule asynchronous events, single-threaded applications can use either the application scheduling mode or the signal-based scheduling mode.

Application Scheduling Mode

Application scheduling mode provides a way for callback functions to be called from the application's main thread of execution. With this mode of operation, the NOF library has no need for multiple threads or signals.

Application scheduling mode works on the assumption that at the heart of an application there is a scheduling loop which includes a call to select or poll to determine whether there is work from one or more work sources. Application scheduling provides access to the file descriptor the NOF library uses to communicate with the SNAP-IX node. The application adds this file descriptor to its main scheduling select call, and when select indicates events on that file descriptor, the application calls a function to process the events and, if necessary, make the NOF callbacks. For more information about callbacks, see The Callback Routine Specified on the nof_async Entry Point.

Note

SNA callbacks are not restricted to the event processor. Callbacks can be made from within any SNA library functions.

To use application scheduling mode, incorporate the following steps into your application code:

  1. In the initialization section of the application, indicate that you want to use application scheduling mode by adding the following function call before the first call into any SNA library:

    SNA_USE_FD_SCHED();  

    The SNA_USE_FD_SCHED call has no return value.

  2. Write the main scheduling loop to set up the file descriptors and call select according to the following logic. Note that SNA_GET_FD returns an integer which is either a valid SNA file descriptor or -1 if there is no open SNA file descriptor. (The return of -1 indicates either that there has been no call into any SNA library, or that there has been a serious error resulting in the file descriptor being closed.)

     fd= SNA_GET_FD();
     if (fd != -1)
     {
        FD_SET(fd, &fdset);
     }
     select(nfds, *fdset, *fdset, *fdset, timeout);
  3. If the select call returns with an indication of events on the SNA file descriptor, the application simply calls SNA_EVENT_FD. This function (which has no return value) processes all the events on the SNA file descriptor. If these events cause any NOF verbs to complete, the NOF callback functions will be issued from within SNA_EVENT_FD.

    Note that there is not a one-to-one mapping between calls to SNA_EVENT_FD and callbacks. There may be no callbacks (if the event does not complete a verb), or there may be many callbacks (because SNA_EVENT_FD processes all events before it returns).

    SNA_EVENT_FD(); 

Notes

  1. In general, SNA_GET_FD returns the same file descriptor each time you call it. However, you should call it before each call to select or poll, because there are some situations (such as when your application has issued the fork call, or in error cases) when the file descriptor may change.

  2. The SNA event handler processes internal control messages between SNAP-IX components, which do not result in a call to your application's callback routine, in addition to processing NOF indications and the completion of NOF verbs. In addition, if there are multiple NOF verbs outstanding, the SNA event handler may call the callback routine for any of these verbs (because the same file descriptor is used for all verbs).

    Because of this, you cannot assume that an event on the SNA file descriptor corresponds to the completion of a particular verb. The correlator information passed to the callback routine is the correct method of checking which verb has completed, or whether this is an indication rather than a verb completion. The callback routine may either handle the verb completion or indication itself, or pass information to the main processing loop to indicate how the verb completion or indication should be processed. For more details of how the callback routine should operate, see The Callback Routine Specified on the nof_async Entry Point.

2.2.2 Multithreaded Applications

SNAP-IX API libraries are available for linking with multithreaded applications. When you develop applications to operate in a multithreaded environment, the following restrictions apply:

2.2.3 Motif Applications

Applications that use the Motif interface and whose code consists mainly of callbacks from the Motif libraries are required to add SNA events to the main Xt library scheduling loop. The SNA events allow the SNAP-IX library to run callbacks in order to process asynchronous verb completions.

Add the following lines to your code before the first call into any SNA library:

#include <Xt.h>
int app_context;
...
XtAppInitialize(app_context...)
...
SNA_USE_XT_SCHED(app_context);

The SNA_USE_XT_SCHED call has no return values. It calls the XtAppAddInput function to register the SNA work sources. When work subsequently arrives on the SNA file descriptor, the Xt library scheduling loop calls the SNA event handler, which then makes any required API callbacks to the application.

Note

SNA callbacks can also be made from within other calls into the SNA library

Previous Page Contents Page Top of Page Next page