The OMNI Thread AbstractionTristan Richardson
|
Platform | Preprocessor Defines |
Sun Solaris 2.x | -D__sunos__ -D__sparc__ -D__OSVERSION__=5 |
-DSVR4 -DUsePthread -D_REENTRANT |
|
x86 Linux 2.0 | -D__linux__ -D__i86__ -D__OSVERSION__=2 |
with linuxthreads 0.5 | -D_REENTRANT |
Digital Unix 3.2 | -D__osf1__ -D__alpha__ -D__OSVERSION__=3 |
-D_REENTRANT |
|
Windows NT | -D__NT__ -MD |
read(sock)
, processing the data,
then going back into the read.shutdown(sock,2)
.read(sock)
, or
is elsewhere in the loop. If the former then read will return 0,
indicating that the socket is closed. If the latter then eventually
thread A will call read(sock)
and then this will return 0.
Thread A should close(sock)
, do any other tidying up, and exit.shutdown(listenSock,2)
. Wherever thread A is in
the loop, eventually it will return ECONNABORTED
from the
accept call. It should close(listenSock)
, tidy up as necessary
and exit.shutdown(listenSock,2)
—
this returns ENOTCONN
. Instead the following strategy can be
used:getsockaddr(listenSock)
to find out
which port listenSock is on (or knows already), sets up a socket
dummySock, does connect(dummySock,
this host, port)
and
finally does close(dummySock)
.accept(listenSock)
. This will return successfully with a new
socket, say connSock. Thread A then checks to see if the "shutdown
flag" is set. If not, then it's a normal connection. If it is set,
then thread A closes listenSock and connSock, tidies up and exits.shutdown(sock,2)
.write(sock)
then it will return with
ENXIO
. If thread A calls write after thread B calls shutdown
this will return EIO
.shutdown(sock,2)
.write(sock)
then it will return the
number of bytes written before it became blocked. A subsequent call
to write will then generate SIGPIPE
(or EPIPE
will be
returned if SIGPIPE
is ignored by the thread).shutdown(sock,2)
.connect(sock)
then it will return a
successful connection. Subsequent reading or writing will show that
the socket has been shut down (i.e. read returns 0, write generates
SIGPIPE
or returns EPIPE
). If thread A calls connect
after thread B calls shutdown this will return EINVAL
.fcntl(sock,
F_SETFL, O_NONBLOCK)
. Then it calls connect
on the socket — this will return EINPROGRESS
. Then it must
call select()
, waiting for either sock to become writable or
for the pipe to become readable. If select returns that just sock is
writable then the connection has succeeded. It then needs to set the
socket back to blocking mode using fcntl(sock, F_SETFL, 0)
. If
instead select returns that the pipe is readable, thread A closes the
socket, tidies up and exits.This document was translated from LATEX by HEVEA.