Make cache refcounting threadsafe
authorBehdad Esfahbod <behdad@behdad.org>
Mon, 8 Oct 2012 01:03:58 +0000 (21:03 -0400)
committerBehdad Esfahbod <behdad@behdad.org>
Wed, 2 Jan 2013 07:03:22 +0000 (01:03 -0600)
src/fcatomic.h
src/fccache.c

index c066d12..71c0491 100644 (file)
@@ -117,6 +117,7 @@ typedef struct _FcRef { fc_atomic_int_t count; } FcRef;
 static inline void   FcRefInit     (FcRef *r, int v) { r->count = v; }
 static inline int    FcRefInc      (FcRef *r) { return fc_atomic_int_add (r->count, +1); }
 static inline int    FcRefDec      (FcRef *r) { return fc_atomic_int_add (r->count, -1); }
+static inline int    FcRefAdd      (FcRef *r, int v) { return fc_atomic_int_add (r->count, v); }
 static inline void   FcRefSetConst (FcRef *r) { r->count = FC_REF_CONSTANT_VALUE; }
 static inline FcBool FcRefIsConst  (const FcRef *r) { return r->count == FC_REF_CONSTANT_VALUE; }
 
index 19293e2..1e9745a 100644 (file)
@@ -233,7 +233,7 @@ typedef struct _FcCacheSkip FcCacheSkip;
 
 struct _FcCacheSkip {
     FcCache        *cache;
-    int                    ref;
+    FcRef          ref;
     intptr_t       size;
     dev_t          cache_dev;
     ino_t          cache_ino;
@@ -368,7 +368,7 @@ FcCacheInsert (FcCache *cache, struct stat *cache_stat)
 
     s->cache = cache;
     s->size = cache->size;
-    s->ref = 1;
+    FcRefInit (&s->ref, 1);
     if (cache_stat)
     {
        s->cache_dev = cache_stat->st_dev;
@@ -451,7 +451,7 @@ FcCacheFindByStat (struct stat *cache_stat)
            s->cache_ino == cache_stat->st_ino &&
            s->cache_mtime == cache_stat->st_mtime)
        {
-           s->ref++;
+           FcRefInc (&s->ref);
            return s->cache;
        }
     return NULL;
@@ -481,7 +481,7 @@ FcCacheObjectReference (void *object)
     FcCacheSkip *skip = FcCacheFindByAddr (object);
 
     if (skip)
-       skip->ref++;
+       FcRefInc (&skip->ref);
 }
 
 void
@@ -491,8 +491,7 @@ FcCacheObjectDereference (void *object)
 
     if (skip)
     {
-       skip->ref--;
-       if (skip->ref <= 0)
+       if (FcRefDec (&skip->ref) <= 1)
            FcDirCacheDispose (skip->cache);
     }
 }
@@ -618,7 +617,7 @@ FcDirCacheReference (FcCache *cache, int nref)
     FcCacheSkip *skip = FcCacheFindByAddr (cache);
 
     if (skip)
-       skip->ref += nref;
+       FcRefAdd (&skip->ref, nref);
 }
 
 void