Open System Services Programmer's Guide

Example 75 Thread-Aware Toolkit Program Flow
...
FILE_OPEN(, &filenum, );
spt_regFile(filenum);
...
// Do work on filenum.
...
spt_unregFile(filenum);
FILE_CLOSE(filenum);
...
Example 76 Simplified Program Flow For a Thread-Aware Function
...
READX(filenum, ...);
spte = spt_awaitio(filenum, tag, -1, count_read, &error, NULL);
if (spte != SPT_SUCCESS) (void) CANCELREQ(filenum, tag);
return error;
...
Example 77 illustrates using a thread-aware read() function. This example assumes you are using
the thread-aware library (spt_*() functions) instead of the nonblocking thread-aware library
(spt_*x() functions) or the thread-aware library for regular files (spt_*z() functions). If you
were to rewrite this example using the nonblocking thread-aware library, you could call read()
and handle the the EWOULBLOCK error instead of checking the O_NONBLOCK flag of the file
descriptor (see Example 78 (page 371)).
Example 77 Thread-Aware read() Function
/* This example assumes youre using the thread aware library. */
/* It checks to ensure that the O_NONBLOCK flag is set for the */
/* file descriptor. If O_NONBLOCK is set, it is safe to call spt_read() */
/* because spt_read() will return EWOULDBLOCK if the file descriptor is */
/* not ready. */
...
#define SPT_THREAD_AWARE /* maps read() to spt_read() */
#include <spthread.h>
...
ssize_t
my_read(int filedes, void *buffer, size_t nbytes)
{
int before_flags;
/* Get file descriptor's current flags: */
if ((before_flags = fcntl(filedes, F_GETFL)) == -1) return -1;
/* If file descriptor is non-blocking then just issue the call and
return. */
if (before_flags & O_NONBLOCK) return read(filedes, buffer, nbytes);
/* Block this thread and wait until file descriptor is ready. */
if ((errno = spt_fd_read_ready(filedes, NULL)) != 0) return -1;
/* Issue the call and return. */
return read(filedes, buffer, nbytes);
}
...
Example 78 illustrates using a nonblocking thread-aware read() function. This example assumes
you are using the nonblocking thread-aware library (spt_*x() functions) instead of the nonblocking
370 Using the Standard POSIX Threads Library