Author: stefan2
Date: Thu Apr 12 19:31:55 2012
New Revision: 1325458

URL: http://svn.apache.org/viewvc?rev=1325458&view=rev
Log:
On the revprop-cache branch: Expose parts of svn_io_file_lock2
as usable functions to allow for a quicker lock / unlock scheme.

* subversion/include/svn_io.h
  (svn_io_lock_open_file, svn_io_unlock_open_file): declare new API
* subversion/libsvn_subr/io.c
  (svn_io_lock_open_file, svn_io_unlock_open_file): implement
   new API; code mainly taken from svn_io_file_lock2
  (svn_io_file_lock2): call new API as sub-routines

Modified:
    subversion/branches/revprop-cache/subversion/include/svn_io.h
    subversion/branches/revprop-cache/subversion/libsvn_subr/io.c

Modified: subversion/branches/revprop-cache/subversion/include/svn_io.h
URL: 
http://svn.apache.org/viewvc/subversion/branches/revprop-cache/subversion/include/svn_io.h?rev=1325458&r1=1325457&r2=1325458&view=diff
==============================================================================
--- subversion/branches/revprop-cache/subversion/include/svn_io.h (original)
+++ subversion/branches/revprop-cache/subversion/include/svn_io.h Thu Apr 12 
19:31:55 2012
@@ -682,6 +682,39 @@ svn_io_file_lock2(const char *lock_file,
                   svn_boolean_t exclusive,
                   svn_boolean_t nonblocking,
                   apr_pool_t *pool);
+
+/**
+ * Lock the file @a lockfile_handle. If @a exclusive is TRUE,
+ * obtain exclusive lock, otherwise obtain shared lock.
+ *
+ * If @a nonblocking is TRUE, do not wait for the lock if it
+ * is not available: throw an error instead.
+ *
+ * Lock will be automatically released when @a pool is cleared or destroyed.
+ * You may also explicitly call @ref svn_io_unlock_open_file.
+ * Use @a pool for memory allocations. @a pool must be the pool that
+ * @a lockfile_handle has been created in or one of its sub-pools.
+ *
+ * @since New in 1.8.
+ */
+svn_error_t *
+svn_io_lock_open_file(apr_file_t *lockfile_handle,
+                      svn_boolean_t exclusive,
+                      svn_boolean_t nonblocking,
+                      apr_pool_t *pool);
+
+/**
+ * Unlock the file @a lockfile_handle.
+ *
+ * Use @a pool for memory allocations. @a pool must be the pool that
+ * @a lockfile_handle has been created in or one of its sub-pools.
+ *
+ * @since New in 1.8.
+ */
+svn_error_t *
+svn_io_unlock_open_file(apr_file_t *lockfile_handle,
+                        apr_pool_t *pool);
+
 /**
  * Flush any unwritten data from @a file to disk.  Use @a pool for
  * memory allocations.

Modified: subversion/branches/revprop-cache/subversion/libsvn_subr/io.c
URL: 
http://svn.apache.org/viewvc/subversion/branches/revprop-cache/subversion/libsvn_subr/io.c?rev=1325458&r1=1325457&r2=1325458&view=diff
==============================================================================
--- subversion/branches/revprop-cache/subversion/libsvn_subr/io.c (original)
+++ subversion/branches/revprop-cache/subversion/libsvn_subr/io.c Thu Apr 12 
19:31:55 2012
@@ -1866,29 +1866,23 @@ file_clear_locks(void *arg)
 #endif
 
 svn_error_t *
-svn_io_file_lock2(const char *lock_file,
-                  svn_boolean_t exclusive,
-                  svn_boolean_t nonblocking,
-                  apr_pool_t *pool)
+svn_io_lock_open_file(apr_file_t *lockfile_handle,
+                      svn_boolean_t exclusive,
+                      svn_boolean_t nonblocking,
+                      apr_pool_t *pool)
 {
   int locktype = APR_FLOCK_SHARED;
-  apr_file_t *lockfile_handle;
-  apr_int32_t flags;
   apr_status_t apr_err;
+  const char *fname;
 
   if (exclusive)
     locktype = APR_FLOCK_EXCLUSIVE;
-
-  flags = APR_READ;
-  if (locktype == APR_FLOCK_EXCLUSIVE)
-    flags |= APR_WRITE;
-
   if (nonblocking)
     locktype |= APR_FLOCK_NONBLOCK;
 
-  SVN_ERR(svn_io_file_open(&lockfile_handle, lock_file, flags,
-                           APR_OS_DEFAULT,
-                           pool));
+  /* We need this only in case of an error but this is cheap to get -
+   * so we do it here for clarity. */
+  apr_err = apr_file_name_get(&fname, lockfile_handle);
 
   /* Get lock on the filehandle. */
   apr_err = apr_file_lock(lockfile_handle, locktype);
@@ -1915,11 +1909,11 @@ svn_io_file_lock2(const char *lock_file,
         case APR_FLOCK_SHARED:
           return svn_error_wrap_apr(apr_err,
                                     _("Can't get shared lock on file '%s'"),
-                                    svn_dirent_local_style(lock_file, pool));
+                                    svn_dirent_local_style(fname, pool));
         case APR_FLOCK_EXCLUSIVE:
           return svn_error_wrap_apr(apr_err,
                                     _("Can't get exclusive lock on file '%s'"),
-                                    svn_dirent_local_style(lock_file, pool));
+                                    svn_dirent_local_style(fname, pool));
         default:
           SVN_ERR_MALFUNCTION();
         }
@@ -1936,6 +1930,58 @@ svn_io_file_lock2(const char *lock_file,
   return SVN_NO_ERROR;
 }
 
+svn_error_t *
+svn_io_unlock_open_file(apr_file_t *lockfile_handle,
+                        apr_pool_t *pool)
+{
+  const char *fname;
+  apr_status_t apr_err = apr_file_unlock(lockfile_handle);
+
+  /* We need this only in case of an error but this is cheap to get -
+   * so we do it here for clarity. */
+  apr_err = apr_file_name_get(&fname, lockfile_handle);
+
+/* On Windows and OS/2 file locks are automatically released when
+   the file handle closes */
+#if !defined(WIN32) && !defined(__OS2__)
+  apr_pool_cleanup_kill(pool, lockfile_handle, file_clear_locks);
+#endif
+
+  return apr_err
+    ? svn_error_wrap_apr(apr_err, _("Can't unlock file '%s'"),
+                                  svn_dirent_local_style(fname, pool))
+    : SVN_NO_ERROR;
+}
+
+svn_error_t *
+svn_io_file_lock2(const char *lock_file,
+                  svn_boolean_t exclusive,
+                  svn_boolean_t nonblocking,
+                  apr_pool_t *pool)
+{
+  int locktype = APR_FLOCK_SHARED;
+  apr_file_t *lockfile_handle;
+  apr_int32_t flags;
+  apr_status_t apr_err;
+
+  if (exclusive)
+    locktype = APR_FLOCK_EXCLUSIVE;
+
+  flags = APR_READ;
+  if (locktype == APR_FLOCK_EXCLUSIVE)
+    flags |= APR_WRITE;
+
+  if (nonblocking)
+    locktype |= APR_FLOCK_NONBLOCK;
+
+  SVN_ERR(svn_io_file_open(&lockfile_handle, lock_file, flags,
+                           APR_OS_DEFAULT,
+                           pool));
+
+  /* Get lock on the filehandle. */
+  return svn_io_lock_open_file(lockfile_handle, exclusive, nonblocking, pool);
+}
+
 
 
 /* Data consistency/coherency operations. */


Reply via email to