You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Graham Leggett <mi...@sharp.fm> on 2010/02/27 15:19:30 UTC

apr_file_open() and non blocking IO

Hi all,

I am trying to open a fifo on disk with the O_NONBLOCK flag set, and I  
notice that apr's apr_file_open() cannot do this.

As a result, if nobody else is already reading from the fifo,  
apr_file_open blocks, and this is the behaviour I want to avoid.

Does anyone know of any reason why this wouldn't work?

Index: include/apr_file_io.h
===================================================================
--- include/apr_file_io.h	(revision 916953)
+++ include/apr_file_io.h	(working copy)
@@ -81,6 +81,8 @@
  #define APR_FOPEN_SPARSE      0x08000 /**< Platform dependent flag  
to enable
                                         * sparse file support, see  
WARNING below
                                         */
+#define APR_FOPEN_NONBLOCK    0x10000 /**< Platform dependent flag to  
enable
+                                       * non blocking file io */

  #define APR_FOPEN_ROTATING  0x10000 /**< Do file file rotation  
checking */

@@ -102,6 +104,7 @@
  #define APR_FILE_NOCLEANUP   APR_FOPEN_NOCLEANUP  /**< @deprecated  
@see APR_FOPEN_NOCLEANUP */
  #define APR_SENDFILE_ENABLED APR_FOPEN_SENDFILE_ENABLED /**<  
@deprecated @see APR_FOPEN_SENDFILE_ENABLED */
  #define APR_LARGEFILE        APR_FOPEN_LARGEFILE  /**< @deprecated  
@see APR_FOPEN_LARGEFILE */
+#define APR_NONBLOCK         APR_FOPEN_NONBLOCK   /**< @deprecated  
@see APR_FOPEN_NONBLOCK */

  /** @warning APR_FOPEN_LARGEFILE flag only has effect on some
   * platforms where sizeof(apr_off_t) == 4.  Where implemented, it
Index: file_io/unix/open.c
===================================================================
--- file_io/unix/open.c	(revision 916953)
+++ file_io/unix/open.c	(working copy)
@@ -136,6 +136,12 @@
      }
  #endif

+#ifdef O_NONBLOCK
+    if (flag & APR_NONBLOCK) {
+        oflags |= O_NONBLOCK;
+    }
+#endif
+
  #ifdef O_CLOEXEC
      /* Introduced in Linux 2.6.23. Silently ignored on earlier Linux  
kernels.
       */

Regards,
Graham
--


Re: apr_file_open() and non blocking IO

Posted by Graham Leggett <mi...@sharp.fm>.
On 28 Feb 2010, at 2:20 PM, Dan Poirier wrote:

>> I am trying to open a fifo on disk with the O_NONBLOCK flag set,  
>> and I
>> notice that apr's apr_file_open() cannot do this.
>>
>> As a result, if nobody else is already reading from the fifo,
>> apr_file_open blocks, and this is the behaviour I want to avoid.
>>
>> Does anyone know of any reason why this wouldn't work?
>
> Seems reasonable.  Have you tried it, or is this still hypothetical?

I have used it extensively on unix (Linux and MacOSX) within software  
that makes extensive use of an event driven architecture, however my  
knowledge of Windows is limited.

I have code that I would like to eventually have compiled on Windows,  
but if I support Windows, I can no longer support unix, thus the  
problem. I wanted to check first that there was nothing controversial  
I was missing.

> It looks like the new flag is using the same bit as  
> APR_FOPEN_ROTATING.

That's because the patch was created before running the svn update  
that brought in APR_FOPEN_ROTATING, it's already been fixed.

Regards,
Graham
--


Re: apr_file_open() and non blocking IO

Posted by Dan Poirier <po...@pobox.com>.
On Sat, 27 Feb 2010 16:19 +0200, "Graham Leggett" <mi...@sharp.fm>
wrote:
> I am trying to open a fifo on disk with the O_NONBLOCK flag set, and I  
> notice that apr's apr_file_open() cannot do this.
> 
> As a result, if nobody else is already reading from the fifo,  
> apr_file_open blocks, and this is the behaviour I want to avoid.
> 
> Does anyone know of any reason why this wouldn't work?

Seems reasonable.  Have you tried it, or is this still hypothetical?

> Index: include/apr_file_io.h
> ===================================================================
> --- include/apr_file_io.h       (revision 916953)
> +++ include/apr_file_io.h       (working copy)
> @@ -81,6 +81,8 @@
>   #define APR_FOPEN_SPARSE      0x08000 /**< Platform dependent flag  
> to enable
>                                          * sparse file support, see  
> WARNING below
>                                          */
> +#define APR_FOPEN_NONBLOCK    0x10000 /**< Platform dependent flag to  
> enable
> +                                       * non blocking file io */
> 
>   #define APR_FOPEN_ROTATING  0x10000 /**< Do file file rotation  
> checking */

It looks like the new flag is using the same bit as APR_FOPEN_ROTATING.

Dan

Re: apr_file_open() and non blocking IO

Posted by Graham Leggett <mi...@sharp.fm>.
On 27 Feb 2010, at 11:08 PM, William A. Rowe Jr. wrote:

>> Does anyone know of any reason why this wouldn't work?
>
> Yes - principal of least surprise.  You cannot deprecate an API in  
> this way,
> and the patch deprecating that flag would be rejected anyways.  When  
> you
> modify a library, please try to observe your design from several  
> perspectives
> at the same time.

Apply the patch, then read the code, don't just look at the patch :)

It looks like this in situ:

/**
  * @defgroup apr_file_open_flags File Open Flags/Routines
  * @{
  */

/* Note to implementors: Values in the range 0x00100000--0x80000000
    are reserved for platform-specific values. */

#define APR_FOPEN_READ       0x00001  /**< Open the file for reading */
#define APR_FOPEN_WRITE      0x00002  /**< Open the file for writing */
#define APR_FOPEN_CREATE     0x00004  /**< Create the file if not  
there */
#define APR_FOPEN_APPEND     0x00008  /**< Append to the end of the  
file */
#define APR_FOPEN_TRUNCATE   0x00010  /**< Open the file and truncate
                                          to 0 length */
#define APR_FOPEN_BINARY     0x00020  /**< Open the file in binary  
mode */
#define APR_FOPEN_EXCL       0x00040  /**< Open should fail if  
APR_CREATE
                                          and file exists. */
#define APR_FOPEN_BUFFERED   0x00080  /**< Open the file for buffered  
I/O */
#define APR_FOPEN_DELONCLOSE 0x00100  /**< Delete the file after close  
*/
#define APR_FOPEN_XTHREAD    0x00200  /**< Platform dependent tag to  
open
                                          the file for use across  
multiple
                                          threads */
#define APR_FOPEN_SHARELOCK  0x00400  /**< Platform dependent support  
for
                                          higher level locked read/write
                                          access to support writes  
across
                                          process/machines */
#define APR_FOPEN_NOCLEANUP  0x00800  /**< Do not register a cleanup
                                          when the file is opened */
#define APR_FOPEN_SENDFILE_ENABLED 0x01000 /**< Advisory flag that this
                                              file should support
                                              apr_socket_sendfile  
operation */
#define APR_FOPEN_LARGEFILE   0x04000 /**< Platform dependent flag to  
enable
                                        * large file support, see  
WARNING below
                                        */
#define APR_FOPEN_SPARSE      0x08000 /**< Platform dependent flag to  
enable
                                        * sparse file support, see  
WARNING below
                                        */
#define APR_FOPEN_ROTATING  0x10000 /**< Do file file rotation  
checking */

#define APR_FOPEN_MANUAL_ROTATE  0x20000 /**< Enable Manual rotation */

#define APR_FOPEN_NONBLOCK    0x40000 /**< Platform dependent flag to  
enable
                                        * non blocking file io */


/* backcompat */
#define APR_READ             APR_FOPEN_READ       /**< @deprecated  
@see APR_FOPEN_READ */
#define APR_WRITE            APR_FOPEN_WRITE      /**< @deprecated  
@see APR_FOPEN_WRITE */
#define APR_CREATE           APR_FOPEN_CREATE     /**< @deprecated  
@see APR_FOPEN_CREATE */
#define APR_APPEND           APR_FOPEN_APPEND     /**< @deprecated  
@see APR_FOPEN_APPEND */
#define APR_TRUNCATE         APR_FOPEN_TRUNCATE   /**< @deprecated  
@see APR_FOPEN_TRUNCATE */
#define APR_BINARY           APR_FOPEN_BINARY     /**< @deprecated  
@see APR_FOPEN_BINARY */
#define APR_EXCL             APR_FOPEN_EXCL       /**< @deprecated  
@see APR_FOPEN_EXCL */
#define APR_BUFFERED         APR_FOPEN_BUFFERED   /**< @deprecated  
@see APR_FOPEN_BUFFERED */
#define APR_DELONCLOSE       APR_FOPEN_DELONCLOSE /**< @deprecated  
@see APR_FOPEN_DELONCLOSE */
#define APR_XTHREAD          APR_FOPEN_XTHREAD    /**< @deprecated  
@see APR_FOPEN_XTHREAD */
#define APR_SHARELOCK        APR_FOPEN_SHARELOCK  /**< @deprecated  
@see APR_FOPEN_SHARELOCK */
#define APR_FILE_NOCLEANUP   APR_FOPEN_NOCLEANUP  /**< @deprecated  
@see APR_FOPEN_NOCLEANUP */
#define APR_SENDFILE_ENABLED APR_FOPEN_SENDFILE_ENABLED /**<  
@deprecated @see APR_FOPEN_SENDFILE_ENABLED */
#define APR_LARGEFILE        APR_FOPEN_LARGEFILE  /**< @deprecated  
@see APR_FOPEN_LARGEFILE */
#define APR_NONBLOCK         APR_FOPEN_NONBLOCK   /**< @deprecated  
@see APR_FOPEN_NONBLOCK */

I could leave off the last deprecated value, but my expectation was  
that the whole api would be deprecated in one go, not in dribs and  
drabs. That's my principle of least surprise.

> If you wanted to submit an acceptable patch, APR would be aware of  
> the file
> handle's blocking state, and still respect the requested blocking/ 
> nonblocking
> nature of the actual IO and poll/select calls.


Can you give more details?

On Unix the non blocking behaviour is specified at the opening of the  
pipe, and poll/select and friends work as they normally would. It is  
read and write that aren't blocking, not poll/select, and these are  
already supported by APR in the form of APR_EAGAIN and friends.

The key problem right now is we cannot turn it on.

I am only familiar with the Windows interface via reading the APIs, I  
don't have access to any Windows machines so can't try it out, and  
your help would be appreciated.

Regards,
Graham
--


Re: apr_file_open() and non blocking IO

Posted by "William A. Rowe Jr." <wr...@rowe-clan.net>.
On 2/27/2010 8:19 AM, Graham Leggett wrote:
> Hi all,
> 
> I am trying to open a fifo on disk with the O_NONBLOCK flag set, and I
> notice that apr's apr_file_open() cannot do this.
> 
> As a result, if nobody else is already reading from the fifo,
> apr_file_open blocks, and this is the behaviour I want to avoid.
> 
> Does anyone know of any reason why this wouldn't work?

Yes - principal of least surprise.  You cannot deprecate an API in this way,
and the patch deprecating that flag would be rejected anyways.  When you
modify a library, please try to observe your design from several perspectives
at the same time.

If you wanted to submit an acceptable patch, APR would be aware of the file
handle's blocking state, and still respect the requested blocking/nonblocking
nature of the actual IO and poll/select calls.