Header And Logo

PostgreSQL
| The world's most advanced open source database.

noblock.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * noblock.c
00004  *    set a file descriptor as non-blocking
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  * IDENTIFICATION
00010  *    src/port/noblock.c
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 
00015 #include "c.h"
00016 
00017 #include <fcntl.h>
00018 
00019 
00020 bool
00021 pg_set_noblock(pgsocket sock)
00022 {
00023 #if !defined(WIN32)
00024     return (fcntl(sock, F_SETFL, O_NONBLOCK) != -1);
00025 #else
00026     unsigned long ioctlsocket_ret = 1;
00027 
00028     /* Returns non-0 on failure, while fcntl() returns -1 on failure */
00029     return (ioctlsocket(sock, FIONBIO, &ioctlsocket_ret) == 0);
00030 #endif
00031 }
00032 
00033 
00034 bool
00035 pg_set_block(pgsocket sock)
00036 {
00037 #if !defined(WIN32)
00038     int         flags;
00039 
00040     flags = fcntl(sock, F_GETFL);
00041     if (flags < 0 || fcntl(sock, F_SETFL, (long) (flags & ~O_NONBLOCK)))
00042         return false;
00043     return true;
00044 #else
00045     unsigned long ioctlsocket_ret = 0;
00046 
00047     /* Returns non-0 on failure, while fcntl() returns -1 on failure */
00048     return (ioctlsocket(sock, FIONBIO, &ioctlsocket_ret) == 0);
00049 #endif
00050 }