Open System Services Programmer's Guide

Example 97 Nonblocking Thread-Aware read() Function
/* This program fragment assumes that you are using the */
/* PUT Model library. For regular files, the read() function */
/* is thread-aware unless you have set the */
/* PUT_THREAD_AWARE_REGULAR_IO_DISABLE environment */
/* variable. For non-regular files, the read() */
/* function can be thread aware or thread aware nonblocking. */
/* If the file descriptor has O_NONBLOCK set, the read() */
/* function is thread-aware nonblocking, and might return */
/* EWOULDBLOCK and EAGAIN
...
#define _PUT_MODEL_ /* maps read() to thread-aware read() */
#include <pthread.h>
#include <unistd.h>
#include <errno.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 = put_fd_read_ready(filedes, NULL);
/* If put_fd_read_ready() returned an error. */
if (rv != 0)
{
errno = rv;
return -1;
}
continue;
}
}
break;
}
return rv;
}
Example 98 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 put_READX() thread-aware function.
Reentrant OSS Functions 413