Using sigwait to handle asynchronous signals

Client-Library and Server-Library support the installation of signal handlers using the ct_callback and srv_signal routines. To allow the use of functions that are not asynchronous-signal-safe, multithreaded applications should, instead of installing a regular signal handler, install a thread that calls sigwait to obtain any pending signals. To allow the ct_callback and srv_signal routines to correctly install signal handler functions this way, the first call to cs_ctx_alloc or cs_ctx_global blocks the signals in all but one thread. This thread can be instructed to start or stop blocking any thread signal and is known as the catcher thread. When a signal handler is installed using the ct_callback or srv_signal routines, the catcher thread blocks the corresponding signal. A separate thread is then spawned to invoke sigwait for this signal and to execute the appropriate user-provided signal handler function when the signal is received.

NoteThis functionality is possible only for applications that invoke the cs_ctx_alloc and cs_ctx_global routines before creating any threads and that install signal handlers only with ct_callback or srv_signal.

You may not want Open Client/Open Server libraries to intervene and handle thread signals. To override the Open Client/Open Server thread-handling and allow your application to handle signals itself, use the following procedure.

StepsAllowing your application to handle a thread signal by itself

  1. In the main process thread, block the signal you wish to handle before your first call to cs_ctx_alloc or cs_ctx_global.

  2. Install a dummy signal handler to avoid the signal handler being set to SIG_IGN.

  3. To create the process threads, including the catcher thread, invoke cs_ctx_global. The catcher thread will have the signal blocked.

  4. Do one of the following:

    • Install your own thread, calling sigwait, or

    • Unblock the signal, and install a regular signal handler (using, for example, sigaction). Make sure that the signal handler is asynchronous-signal-safe.

  5. From the main process thread, unblock the signal that was blocked in step 1. Now all Open Client/Open Server threads will have the signal blocked. The main process thread will not have the signal blocked, nor will any threads created directly in the main thread. Do not install a signal handler with ct_callback or srv_signal.