nthm_enter_scope - select only from subsequently opened pipes
nthm_exit_scope - allow selection of previously opened pipes
#include <nthm/nthm.h>
int nthm_enter_scope ( int *err )
int nthm_exit_scope ( int *err )
These functions are an advanced feature of nthm whose understanding depends on an understanding of nthm_select and other functions. See the NOTES section below for intended use cases.
Normally nthm_select can choose any pipe tethered to the caller's thread, but nthm_enter_scope can restrict it to specific pipes.
In more general usage, these functions can effect a stack-like configuration of scopes.
These functions also affect nthm_blocked, nthm_kill_all, and nthm_truncate_all. They behave consistently with nthm_select.
The functions nthm_kill, nthm_truncate, nthm_untether, and nthm_read are also affected. Attempts to apply them to pipes that are tethered but not selectable by the above criteria cause the error status of NTHM_NOTDRN, just as if the pipes had been tethered to a different thread.
A non-zero value is returned by nthm_enter_scope and nthm_enter_scope if they succeed.
If *err is non-zero on entry, then nthm_enter_scope and nthm_exit_scope leave it unchanged. If *err is zero on entry and non-zero on exit, it may have been assigned one of the following values for the reasons noted, and the requested operation will not have completed.
NTHM_UNDFLO
A scope underflow status is reported whenever nthm_exit_scope is called more times than nthm_enter_scope.
NTHM_KILLED
The nthm_enter_scope function was interrupted because the caller's thread was killed. It may have been killed directly with nthm_kill or nthm_kill_all or as a consequence of a thread to which it is tethered being killed or terminating.
ENOMEM
Available memory is insufficient.
Various undocumented error codes within the range of -NTHM_MIN_ERR to -NTHM_MAX_ERR might also be reported for developer diagnostics if internal consistency checks fail. These codes are not actionable by users but may be helpful in bug reports.
This example illustrates pipes opened in two scopes with explanations of what to expect.
p1 = nthm_open (&f1, &x1, err); p2 = nthm_open (&f2, &x2, err); nthm_enter_scope (err); q1 = nthm_open (&g1, &y1, err); q2 = nthm_open (&g2, &y2, err); // A is guaranteed to be read from either q1 or q2. A = nthm_read (nthm_select (err), err); nthm_exit_scope (err); // The other of q1 or q2 is untethered at this // point but may continue to run. B is guaranteed // to be read from either p1 or p2. B = nthm_read (nthm_select (err), err);
Some points to note about this example are as follows.
The next example demonstrates multiple nested scopes.
a: nthm_enter_scope (err); // outer scope // Pipes opened here are selectable only here, // between c: and d:, and between e: and f:. b: nthm_enter_scope (err); // nested scope // Pipes opened here are selectable nowhere else. c: nthm_exit_scope (err); // matches b: // Pipes opened here are also selectable between // e: and f:. d: nthm_enter_scope (err); // another nested scope // Pipes opened here are selectable nowhere else. e: nthm_exit_scope (err); // matches d: // back in the outer scope f: nthm_exit_scope (err); // matches a:
These functions can be helpful for library routines needing to operate on their own locally opened pipes while other pipes opened by the main application or other library routines continue in the background. A scope prevents the library routine from mistakenly selecting or interfering with pipes opened outside it.
If a routine runs in a separate thread from the main application, and that thread is created directly by pthreads primitives rather than as a side-effect of nthm_open, then all pipes it opens with nthm_open are automatically separate from those of the application without need of scopes.
nthm, nthm_open, nthm_send, nthm_select, nthm_truncate, nthm_truncate_all, nthm_truncated, nthm_kill, nthm_kill_all, nthm_killed, nthm_untether, nthm_tether, nthm_blocked, nthm_strerror, nthm_busy, pthreads (7), nthm_sync
Dennis Furey (milonga@delayinsensitive.com)
https://github.com/gueststar/nthm