You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by st...@apache.org on 2011/07/02 02:55:45 UTC

svn commit: r1142137 - in /subversion/branches/svn_mutex/subversion/libsvn_subr: cache-inprocess.c cache-membuffer.c

Author: stefan2
Date: Sat Jul  2 00:55:44 2011
New Revision: 1142137

URL: http://svn.apache.org/viewvc?rev=1142137&view=rev
Log:
Replace usage of plain APR thread mutex API with the new svn_mutex
API in cache implementations.

* subversion/libsvn_subr/cache-membuffer.c
  (svn_membuffer_t): switch mutex to svn_mutex__t
  (lock_cache, unlock_cache): drop obsolete functions
  (svn_cache__membuffer_cache_create, membuffer_cache_set,
   membuffer_cache_get, membuffer_cache_get_partial,
   membuffer_cache_set_partial, svn_membuffer_cache_get_info):
   switch to svn_mutex API
  (svn_membuffer_cache_get_partial, svn_membuffer_cache_set_partial,
   svn_membuffer_cache_is_cachable): add docstrings

* subversion/libsvn_subr/cache-inprocess.c
  (inprocess_cache_t): switch mutex to svn_mutex__t
  (lock_cache, unlock_cache): drop obsolete functions
  (inprocess_cache_get, inprocess_cache_set, inprocess_cache_iter,
   inprocess_cache_get_partial, inprocess_cache_set_partial,
   inprocess_cache_get_info, svn_cache__create_inprocess): 
   switch to svn_mutex API

Modified:
    subversion/branches/svn_mutex/subversion/libsvn_subr/cache-inprocess.c
    subversion/branches/svn_mutex/subversion/libsvn_subr/cache-membuffer.c

Modified: subversion/branches/svn_mutex/subversion/libsvn_subr/cache-inprocess.c
URL: http://svn.apache.org/viewvc/subversion/branches/svn_mutex/subversion/libsvn_subr/cache-inprocess.c?rev=1142137&r1=1142136&r2=1142137&view=diff
==============================================================================
--- subversion/branches/svn_mutex/subversion/libsvn_subr/cache-inprocess.c (original)
+++ subversion/branches/svn_mutex/subversion/libsvn_subr/cache-inprocess.c Sat Jul  2 00:55:44 2011
@@ -30,6 +30,7 @@
 #include "svn_private_config.h"
 
 #include "cache.h"
+#include "private/svn_mutex.h"
 
 /* The (internal) cache object. */
 typedef struct inprocess_cache_t {
@@ -80,12 +81,10 @@ typedef struct inprocess_cache_t {
    */
   apr_size_t data_size;
 
-#if APR_HAS_THREADS
   /* A lock for intra-process synchronization to the cache, or NULL if
    * the cache's creator doesn't feel the cache needs to be
    * thread-safe. */
-  apr_thread_mutex_t *mutex;
-#endif
+  svn_mutex__t mutex;
 } inprocess_cache_t;
 
 /* A cache page; all items on the page are allocated from the same
@@ -182,41 +181,6 @@ duplicate_key(inprocess_cache_t *cache,
     return apr_pmemdup(pool, key, cache->klen);
 }
 
-/* If applicable, locks CACHE's mutex. */
-static svn_error_t *
-lock_cache(inprocess_cache_t *cache)
-{
-#if APR_HAS_THREADS
-  apr_status_t status;
-  if (! cache->mutex)
-    return SVN_NO_ERROR;
-
-  status = apr_thread_mutex_lock(cache->mutex);
-  if (status)
-    return svn_error_wrap_apr(status, _("Can't lock cache mutex"));
-#endif
-
-  return SVN_NO_ERROR;
-}
-
-/* If applicable, unlocks CACHE's mutex, then returns ERR. */
-static svn_error_t *
-unlock_cache(inprocess_cache_t *cache,
-             svn_error_t *err)
-{
-#if APR_HAS_THREADS
-  apr_status_t status;
-  if (! cache->mutex)
-    return err;
-
-  status = apr_thread_mutex_unlock(cache->mutex);
-  if (status && !err)
-    return svn_error_wrap_apr(status, _("Can't unlock cache mutex"));
-#endif
-
-  return err;
-}
-
 static svn_error_t *
 inprocess_cache_get(void **value_p,
                     svn_boolean_t *found,
@@ -229,13 +193,13 @@ inprocess_cache_get(void **value_p,
   struct cache_entry *entry;
   char* buffer;
 
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   entry = apr_hash_get(cache->hash, key, cache->klen);
   if (! entry)
     {
       *found = FALSE;
-      return unlock_cache(cache, SVN_NO_ERROR);
+      return svn_mutex__unlock(cache->mutex, SVN_NO_ERROR);
     }
 
   SVN_ERR(move_page_to_front(cache, entry->page));
@@ -245,7 +209,7 @@ inprocess_cache_get(void **value_p,
   memcpy(buffer, entry->value, entry->size);
 
   /* the cache is no longer being accessed */
-  SVN_ERR(unlock_cache(cache, SVN_NO_ERROR));
+  SVN_ERR(svn_mutex__unlock(cache->mutex, SVN_NO_ERROR));
 
   /* deserialize the buffer content. Usually, this will directly
      modify the buffer content directly.
@@ -301,7 +265,7 @@ inprocess_cache_set(void *cache_void,
   struct cache_entry *existing_entry;
   svn_error_t *err = SVN_NO_ERROR;
 
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   existing_entry = apr_hash_get(cache->hash, key, cache->klen);
 
@@ -419,7 +383,7 @@ inprocess_cache_set(void *cache_void,
   }
 
  cleanup:
-  return unlock_cache(cache, err);
+  return svn_mutex__unlock(cache->mutex, err);
 }
 
 /* Baton type for svn_cache__iter. */
@@ -455,10 +419,10 @@ inprocess_cache_iter(svn_boolean_t *comp
   b.user_cb = user_cb;
   b.user_baton = user_baton;
 
-  SVN_ERR(lock_cache(cache));
-  return unlock_cache(cache,
-                      svn_iter_apr_hash(completed, cache->hash, iter_cb, &b,
-                                        scratch_pool));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
+  return svn_mutex__unlock(cache->mutex,
+                           svn_iter_apr_hash(completed, cache->hash, 
+                                             iter_cb, &b, scratch_pool));
 }
 
 static svn_error_t *
@@ -473,21 +437,21 @@ inprocess_cache_get_partial(void **value
   inprocess_cache_t *cache = cache_void;
   struct cache_entry *entry;
 
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   entry = apr_hash_get(cache->hash, key, cache->klen);
   if (! entry)
     {
       *found = FALSE;
-      return unlock_cache(cache, SVN_NO_ERROR);
+      return svn_mutex__unlock(cache->mutex, SVN_NO_ERROR);
     }
 
   SVN_ERR(move_page_to_front(cache, entry->page));
 
   *found = TRUE;
-  return unlock_cache(cache,
-                      func(value_p, entry->value, entry->size, baton,
-                           result_pool));
+  return svn_mutex__unlock(cache->mutex,
+                           func(value_p, entry->value, entry->size,
+                                baton, result_pool));
 }
 
 static svn_error_t *
@@ -501,11 +465,11 @@ inprocess_cache_set_partial(void *cache_
   struct cache_entry *entry;
   svn_error_t *err = SVN_NO_ERROR;
 
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   entry = apr_hash_get(cache->hash, key, cache->klen);
   if (! entry)
-    return unlock_cache(cache, err);
+    return svn_mutex__unlock(cache->mutex, err);
 
   SVN_ERR(move_page_to_front(cache, entry->page));
 
@@ -516,7 +480,7 @@ inprocess_cache_set_partial(void *cache_
              entry->page->page_pool);
   cache->data_size += entry->size;
 
-  return unlock_cache(cache, err);
+  return svn_mutex__unlock(cache->mutex, err);
 }
 
 static svn_boolean_t
@@ -539,7 +503,7 @@ inprocess_cache_get_info(void *cache_voi
 {
   inprocess_cache_t *cache = cache_void;
 
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   info->id = apr_pstrdup(result_pool, cache->id);
 
@@ -552,7 +516,7 @@ inprocess_cache_get_info(void *cache_voi
                    + cache->items_per_page * sizeof(struct cache_page)
                    + info->used_entries * sizeof(struct cache_entry);
 
-  return unlock_cache(cache, SVN_NO_ERROR);
+  return svn_mutex__unlock(cache->mutex, SVN_NO_ERROR);
 }
 
 
@@ -602,17 +566,7 @@ svn_cache__create_inprocess(svn_cache__t
   /* The sentinel doesn't need a pool.  (We're happy to crash if we
    * accidentally try to treat it like a real page.) */
 
-#if APR_HAS_THREADS
-  if (thread_safe)
-    {
-      apr_status_t status = apr_thread_mutex_create(&(cache->mutex),
-                                                    APR_THREAD_MUTEX_DEFAULT,
-                                                    pool);
-      if (status)
-        return svn_error_wrap_apr(status,
-                                  _("Can't create cache mutex"));
-    }
-#endif
+  SVN_ERR(svn_mutex__init(&cache->mutex, thread_safe, pool));
 
   cache->cache_pool = pool;
 

Modified: subversion/branches/svn_mutex/subversion/libsvn_subr/cache-membuffer.c
URL: http://svn.apache.org/viewvc/subversion/branches/svn_mutex/subversion/libsvn_subr/cache-membuffer.c?rev=1142137&r1=1142136&r2=1142137&view=diff
==============================================================================
--- subversion/branches/svn_mutex/subversion/libsvn_subr/cache-membuffer.c (original)
+++ subversion/branches/svn_mutex/subversion/libsvn_subr/cache-membuffer.c Sat Jul  2 00:55:44 2011
@@ -30,6 +30,7 @@
 #include "cache.h"
 #include "svn_string.h"
 #include "private/svn_dep_compat.h"
+#include "private/svn_mutex.h"
 
 /*
  * This svn_cache__t implementation actually consists of two parts:
@@ -423,13 +424,11 @@ struct svn_membuffer_t
    */
   apr_uint64_t total_hits;
 
-#if APR_HAS_THREADS
   /* A lock for intra-process synchronization to the cache, or NULL if
    * the cache's creator doesn't feel the cache needs to be
    * thread-safe.
    */
-  apr_thread_mutex_t *mutex;
-#endif
+  svn_mutex__t mutex;
 };
 
 /* Align integer VALUE to the next ITEM_ALIGNMENT boundary.
@@ -440,43 +439,6 @@ struct svn_membuffer_t
  */
 #define ALIGN_POINTER(pointer) ((void*)ALIGN_VALUE((apr_size_t)(char*)(pointer)))
 
-/* Acquire the cache mutex, if necessary.
- */
-static svn_error_t *
-lock_cache(svn_membuffer_t *cache)
-{
-#if APR_HAS_THREADS
-  if (cache->mutex)
-  {
-    apr_status_t status = apr_thread_mutex_lock(cache->mutex);
-    if (status)
-      return svn_error_wrap_apr(status, _("Can't lock cache mutex"));
-  }
-#endif
-
-  return SVN_NO_ERROR;
-}
-
-/* Release the cache mutex, if necessary.
- */
-static svn_error_t *
-unlock_cache(svn_membuffer_t *cache, svn_error_t *err)
-{
-#if APR_HAS_THREADS
-  if (cache->mutex)
-  {
-    apr_status_t status = apr_thread_mutex_unlock(cache->mutex);
-    if (err)
-      return err;
-
-    if (status)
-      return svn_error_wrap_apr(status, _("Can't unlock cache mutex"));
-  }
-#endif
-
-  return err;
-}
-
 /* Resolve a dictionary entry reference, i.e. return the entry
  * for the given IDX.
  */
@@ -1076,25 +1038,10 @@ svn_cache__membuffer_cache_create(svn_me
           return svn_error_wrap_apr(APR_ENOMEM, _("OOM"));
         }
 
-#if APR_HAS_THREADS
       /* A lock for intra-process synchronization to the cache, or NULL if
        * the cache's creator doesn't feel the cache needs to be
        * thread-safe. */
-
-      c[seg].mutex = NULL;
-      if (thread_safe)
-        {
-          apr_status_t status =
-              apr_thread_mutex_create(&(c[seg].mutex),
-                                      APR_THREAD_MUTEX_DEFAULT,
-                                      pool);
-          if (status)
-            return svn_error_wrap_apr(status, _("Can't create cache mutex"));
-        }
-#else
-      if (thread_safe)
-        return svn_error_wrap_apr(APR_ENOTIMPL, _("APR doesn't support threads"));
-#endif
+      SVN_ERR(svn_mutex__init(&c[seg].mutex, thread_safe, pool));
     }
 
   /* done here
@@ -1141,7 +1088,7 @@ membuffer_cache_set(svn_membuffer_t *cac
 
   /* The actual cache data access needs to sync'ed
    */
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   /* if necessary, enlarge the insertion window.
    */
@@ -1185,7 +1132,7 @@ membuffer_cache_set(svn_membuffer_t *cac
 
   /* done here -> unlock the cache
    */
-  return unlock_cache(cache, SVN_NO_ERROR);
+  return svn_mutex__unlock(cache->mutex, SVN_NO_ERROR);
 }
 
 /* Look for the *ITEM identified by KEY. If no item has been stored
@@ -1219,7 +1166,7 @@ membuffer_cache_get(svn_membuffer_t *cac
       return SVN_NO_ERROR;
     }
 
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   /* The actual cache data access needs to sync'ed
    */
@@ -1230,7 +1177,7 @@ membuffer_cache_get(svn_membuffer_t *cac
       /* no such entry found.
        */
       *item = NULL;
-      return unlock_cache(cache, SVN_NO_ERROR);
+      return svn_mutex__unlock(cache->mutex, SVN_NO_ERROR);
     }
 
   size = ALIGN_VALUE(entry->size);
@@ -1258,7 +1205,7 @@ membuffer_cache_get(svn_membuffer_t *cac
   cache->hit_count++;
   cache->total_hits++;
 
-  SVN_ERR(unlock_cache(cache, SVN_NO_ERROR));
+  SVN_ERR(svn_mutex__unlock(cache->mutex, SVN_NO_ERROR));
 
   /* re-construct the original data object from its serialized form.
    */
@@ -1289,7 +1236,7 @@ membuffer_cache_get_partial(svn_membuffe
 
   group_index = get_group_index(&cache, key, key_len, to_find, result_pool);
 
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   entry = find_entry(cache, group_index, to_find, FALSE);
   cache->total_reads++;
@@ -1333,7 +1280,7 @@ membuffer_cache_get_partial(svn_membuffe
 
   /* done here -> unlock the cache
    */
-  return unlock_cache(cache, err);
+  return svn_mutex__unlock(cache->mutex, err);
 }
 
 /* Look for the cache entry identified by KEY and KEY_LEN. If no entry
@@ -1359,7 +1306,7 @@ membuffer_cache_set_partial(svn_membuffe
    */
   group_index = get_group_index(&cache, key, key_len, to_find, scratch_pool);
 
-  SVN_ERR(lock_cache(cache));
+  SVN_ERR(svn_mutex__lock(cache->mutex));
 
   entry = find_entry(cache, group_index, to_find, FALSE);
   cache->total_reads++;
@@ -1443,7 +1390,7 @@ membuffer_cache_set_partial(svn_membuffe
 
   /* done here -> unlock the cache
    */
-  return unlock_cache(cache, err);
+  return svn_mutex__unlock(cache->mutex, err);
 }
 
 /* Implement the svn_cache__t interface on top of a shared membuffer cache.
@@ -1652,6 +1599,8 @@ svn_membuffer_cache_iter(svn_boolean_t *
                           _("Can't iterate a membuffer-based cache"));
 }
 
+/* Implement svn_cache__vtable_t.get_partial
+ */
 static svn_error_t *
 svn_membuffer_cache_get_partial(void **value_p,
                                 svn_boolean_t *found,
@@ -1695,6 +1644,8 @@ svn_membuffer_cache_get_partial(void **v
   return SVN_NO_ERROR;
 }
 
+/* Implement svn_cache__vtable_t.set_partial
+ */
 static svn_error_t *
 svn_membuffer_cache_set_partial(void *cache_void,
                                 const void *key,
@@ -1728,6 +1679,8 @@ svn_membuffer_cache_set_partial(void *ca
   return SVN_NO_ERROR;
 }
 
+/* Implement svn_cache__vtable_t.is_cachable
+ */
 static svn_boolean_t
 svn_membuffer_cache_is_cachable(void *cache_void, apr_size_t size)
 {
@@ -1740,6 +1693,8 @@ svn_membuffer_cache_is_cachable(void *ca
       && (size < APR_UINT32_MAX - ITEM_ALIGNMENT);
 }
 
+/* Implement svn_cache__vtable_t.get_info
+ */
 static svn_error_t *
 svn_membuffer_cache_get_info(void *cache_void,
                              svn_cache__info_t *info,
@@ -1766,7 +1721,7 @@ svn_membuffer_cache_get_info(void *cache
     {
       svn_membuffer_t *segment = cache->membuffer + i;
 
-      SVN_ERR(lock_cache(segment));
+      SVN_ERR(svn_mutex__lock(segment->mutex));
 
       info->data_size += segment->data_size;
       info->used_size += segment->data_used;
@@ -1776,7 +1731,7 @@ svn_membuffer_cache_get_info(void *cache
       info->used_entries += segment->used_entries;
       info->total_entries += segment->group_count * GROUP_SIZE;
 
-      SVN_ERR(unlock_cache(segment, SVN_NO_ERROR));
+      SVN_ERR(svn_mutex__unlock(segment->mutex, SVN_NO_ERROR));
     }
 
   return SVN_NO_ERROR;