add io_fd_flags so the caller can tell io_fd whether the socket is blocking

(saves one fcntl syscall)
master
leitner 10 years ago
parent 7dadd4e7c3
commit 4fa4a524d2

@ -21,6 +21,8 @@
on Linux, in iob_send, if MSG_MORE is defined, and the request type on Linux, in iob_send, if MSG_MORE is defined, and the request type
lends itself to it, used sendto/sendmsg with MSG_MORE instead of lends itself to it, used sendto/sendmsg with MSG_MORE instead of
TCP_CORK (saves two syscalls) TCP_CORK (saves two syscalls)
add io_fd_flags so the caller can tell io_fd whether the socket is blocking
(saves one fcntl syscall)
0.29: 0.29:
save 8 bytes in taia.h for 64-bit systems save 8 bytes in taia.h for 64-bit systems

10
io.h

@ -78,9 +78,19 @@ int64 io_canwrite();
/* return next descriptor with expired timeout */ /* return next descriptor with expired timeout */
int64 io_timeouted(); int64 io_timeouted();
/* 1 means: have IO_FD_CANWRITE, IO_FD_BLOCK and IO_FD_NONBLOCK,
* will be incremented if API is extended in the future */
#define HAVE_IO_FD_FLAGS 1
enum io_fd_flags {
IO_FD_CANWRITE=1, /* new TCP connection, we know it's writable */
IO_FD_BLOCK=2, /* skip the fcntl, assume fd is set to blocking */
IO_FD_NONBLOCK=4, /* skip the fcntl, assume fd is set to non-blocking */
};
/* put d on internal data structure, return 1 on success, 0 on error */ /* put d on internal data structure, return 1 on success, 0 on error */
int io_fd(int64 d); /* use this for sockets before you called connect() or accept() */ int io_fd(int64 d); /* use this for sockets before you called connect() or accept() */
int io_fd_canwrite(int64 d); /* use this for connected sockets (assumes socket is writable) */ int io_fd_canwrite(int64 d); /* use this for connected sockets (assumes socket is writable) */
int io_fd_flags(int64 d,int flags); /* can be used to tell io_fd to skip one syscall */
void io_setcookie(int64 d,void* cookie); void io_setcookie(int64 d,void* cookie);
void* io_getcookie(int64 d); void* io_getcookie(int64 d);

@ -53,12 +53,18 @@ long alt_firstwrite;
#endif #endif
/* put d on internal data structure, return 1 on success, 0 on error */ /* put d on internal data structure, return 1 on success, 0 on error */
static io_entry* io_fd_internal(int64 d) { static io_entry* io_fd_internal(int64 d,int flags) {
io_entry* e; io_entry* e;
#ifndef __MINGW32__ #ifndef __MINGW32__
long r; long r;
if ((flags&(IO_FD_BLOCK|IO_FD_NONBLOCK))==0) {
if ((r=fcntl(d,F_GETFL,0)) == -1) if ((r=fcntl(d,F_GETFL,0)) == -1)
return 0; /* file descriptor not open */ return 0; /* file descriptor not open */
} else
if (flags&IO_FD_NONBLOCK)
r=O_NDELAY;
else
r=0;
#endif #endif
/* Problem: we might be the first to use io_fds. We need to make sure /* Problem: we might be the first to use io_fds. We need to make sure
* we are the only ones to initialize it. So set io_fds_inited to 2 * we are the only ones to initialize it. So set io_fds_inited to 2
@ -146,12 +152,18 @@ static io_entry* io_fd_internal(int64 d) {
} }
int io_fd(int64 d) { int io_fd(int64 d) {
io_entry* e=io_fd_internal(d); io_entry* e=io_fd_internal(d,0);
return !!e; return !!e;
} }
int io_fd_canwrite(int64 d) { int io_fd_canwrite(int64 d) {
io_entry* e=io_fd_internal(d); io_entry* e=io_fd_internal(d,0);
if (e) e->canwrite=1; if (e) e->canwrite=1;
return !!e; return !!e;
} }
int io_fd_flags(int64 d,int flags) {
io_entry* e=io_fd_internal(d,flags);
if (e && (flags&IO_FD_CANWRITE)) e->canwrite=1;
return !!e;
}

Loading…
Cancel
Save