Open System Services Programmer's Guide
thread-aware library (spt_*x() functions) or the thread-aware library for regular files (spt_*z()
functions). If you were to use this code with the thread-aware library instead of the nonblocking
thread aware library, you risk blocking the whole process if the file descriptor does not have the
O_NONBLOCK flag set.
Example 78 Nonblocking Thread-Aware read() Function
/* This program fragment assumes you are using the nonblocking thread- */
/* aware library. If you used the thread-aware library */
/* (define SPT_THREAD_AWARE instead of define SPT_THREAD_AWARE_NONBLOCK),
/* and the file descriptor does not have O_NONBLOCK set,*/
/* the read() call could block the whole program. */
...
#define SPT_TRHEAD_AWARE_NONBLOCK /* maps read() to spt_readx() */
#include <spthread.h>
...
{
int rv;
while (1)
{
rv = read(filedes, buffer, nbytes);
/* If read() returned an error. */
if (rv < 0)
{
/* If not read ready then wait (assumes EWOULBLOCK would be sent.) */
if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
{
rv = spt_fd_read_ready(filedes, NULL);
/* If spt_fd_read_ready() returned an error. */
if (rv != 0)
{
errno = rv;
return -1;
}
continue;
}
}
break;
}
return rv;
}
Example 79 shows a code fragment that uses the READX() function in a thread-aware manner,
which is appropriate when you are not using the thread-aware library. An alternative to this
approach is to use the SPT_READX() thread-aware function.
Reentrant OSS Functions 371