You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by ko...@apache.org on 2017/09/15 13:31:11 UTC

svn commit: r1808457 - in /apr/apr/trunk/file_io/win32: buffer.c open.c readwrite.c

Author: kotkov
Date: Fri Sep 15 13:31:10 2017
New Revision: 1808457

URL: http://svn.apache.org/viewvc?rev=1808457&view=rev
Log:
Win32: Create and use file mutex only for files opened with APR_FOPEN_XTHREAD.

There are some cases in the Windows implementation of the file I/O that
cause the unnecessary creation and acquisitions of the file mutex.
These cases include handling O_APPEND-style writes and the implementation
of the apr_file_buffer_set().

Creating and acquiring the file mutex is only required for files opened
with the APR_FOPEN_XTHREAD flag.  Otherwise, concurrent operations on
the same apr_file_t instance should be serialized by the user, and
the mutex is not required.

This patch tweaks the implementation to only create and acquire/release
the mutex for files opened with APR_FOPEN_XTHREAD.

* file_io/win32/open.c
  (apr_file_open, apr_os_file_put): Create the file mutex only
   with APR_FOPEN_XTHREAD.

* file_io/win32/buffer.c
  (apr_file_buffer_set): Use the file mutex only with APR_FOPEN_XTHREAD.

* file_io/win32/readwrite.c
  (apr_file_write): Use the file mutex only with APR_FOPEN_XTHREAD.

Modified:
    apr/apr/trunk/file_io/win32/buffer.c
    apr/apr/trunk/file_io/win32/open.c
    apr/apr/trunk/file_io/win32/readwrite.c

Modified: apr/apr/trunk/file_io/win32/buffer.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/win32/buffer.c?rev=1808457&r1=1808456&r2=1808457&view=diff
==============================================================================
--- apr/apr/trunk/file_io/win32/buffer.c (original)
+++ apr/apr/trunk/file_io/win32/buffer.c Fri Sep 15 13:31:10 2017
@@ -23,7 +23,9 @@ APR_DECLARE(apr_status_t) apr_file_buffe
 {
     apr_status_t rv;
 
-    apr_thread_mutex_lock(file->mutex);
+    if (file->flags & APR_FOPEN_XTHREAD) {
+        apr_thread_mutex_lock(file->mutex);
+    }
  
     if(file->buffered) {
         /* Flush the existing buffer */
@@ -48,7 +50,9 @@ APR_DECLARE(apr_status_t) apr_file_buffe
             file->buffered = 0;
     }
     
-    apr_thread_mutex_unlock(file->mutex);
+    if (file->flags & APR_FOPEN_XTHREAD) {
+        apr_thread_mutex_unlock(file->mutex);
+    }
 
     return APR_SUCCESS;
 }

Modified: apr/apr/trunk/file_io/win32/open.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/win32/open.c?rev=1808457&r1=1808456&r2=1808457&view=diff
==============================================================================
--- apr/apr/trunk/file_io/win32/open.c (original)
+++ apr/apr/trunk/file_io/win32/open.c Fri Sep 15 13:31:10 2017
@@ -451,8 +451,8 @@ APR_DECLARE(apr_status_t) apr_file_open(
         (*new)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
         (*new)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
     }
-    /* Need the mutex to handled buffered and O_APPEND style file i/o */
-    if ((*new)->buffered || (*new)->append) {
+    /* Need the mutex to share an apr_file_t across multiple threads */
+    if (flag & APR_FOPEN_XTHREAD) {
         rv = apr_thread_mutex_create(&(*new)->mutex, 
                                      APR_THREAD_MUTEX_DEFAULT, pool);
         if (rv) {
@@ -644,8 +644,7 @@ APR_DECLARE(apr_status_t) apr_os_file_pu
         (*file)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
         (*file)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
     }
-
-    if ((*file)->append || (*file)->buffered) {
+    if (flags & APR_FOPEN_XTHREAD) {
         apr_status_t rv;
         rv = apr_thread_mutex_create(&(*file)->mutex, 
                                      APR_THREAD_MUTEX_DEFAULT, pool);

Modified: apr/apr/trunk/file_io/win32/readwrite.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/win32/readwrite.c?rev=1808457&r1=1808456&r2=1808457&view=diff
==============================================================================
--- apr/apr/trunk/file_io/win32/readwrite.c (original)
+++ apr/apr/trunk/file_io/win32/readwrite.c Fri Sep 15 13:31:10 2017
@@ -412,20 +412,26 @@ APR_DECLARE(apr_status_t) apr_file_write
             apr_off_t offset = 0;
             apr_status_t rc;
             if (thefile->append) {
-                /* apr_file_lock will mutex the file across processes.
-                 * The call to apr_thread_mutex_lock is added to avoid
-                 * a race condition between LockFile and WriteFile 
-                 * that occasionally leads to deadlocked threads.
-                 */
-                apr_thread_mutex_lock(thefile->mutex);
+                if (thefile->flags & APR_FOPEN_XTHREAD) {
+                    /* apr_file_lock will mutex the file across processes.
+                     * The call to apr_thread_mutex_lock is added to avoid
+                     * a race condition between LockFile and WriteFile
+                     * that occasionally leads to deadlocked threads.
+                     */
+                    apr_thread_mutex_lock(thefile->mutex);
+                }
                 rc = apr_file_lock(thefile, APR_FLOCK_EXCLUSIVE);
                 if (rc != APR_SUCCESS) {
-                    apr_thread_mutex_unlock(thefile->mutex);
+                    if (thefile->flags & APR_FOPEN_XTHREAD) {
+                        apr_thread_mutex_unlock(thefile->mutex);
+                    }
                     return rc;
                 }
                 rc = apr_file_seek(thefile, APR_END, &offset);
                 if (rc != APR_SUCCESS) {
-                    apr_thread_mutex_unlock(thefile->mutex);
+                    if (thefile->flags & APR_FOPEN_XTHREAD) {
+                        apr_thread_mutex_unlock(thefile->mutex);
+                    }
                     return rc;
                 }
             }
@@ -437,7 +443,9 @@ APR_DECLARE(apr_status_t) apr_file_write
                            thefile->pOverlapped);
             if (thefile->append) {
                 apr_file_unlock(thefile);
-                apr_thread_mutex_unlock(thefile->mutex);
+                if (thefile->flags & APR_FOPEN_XTHREAD) {
+                    apr_thread_mutex_unlock(thefile->mutex);
+                }
             }
         }
         if (rv) {