cb8e9e
From 62ed8b36d67e073fd26c2c7353a8b7cc0961be20 Mon Sep 17 00:00:00 2001
cb8e9e
From: Niels de Vos <ndevos@redhat.com>
cb8e9e
Date: Sun, 31 May 2015 15:16:41 +0200
cb8e9e
Subject: [PATCH 144/190] core: add "gf_ref_t" for common refcounting structures
cb8e9e
cb8e9e
This is a backport of http://review.gluster.org/#/c/11022/
cb8e9e
cb8e9e
There were errors from checkpatch.pl, it is fixed as part of
cb8e9e
this backport, below are the changes:
cb8e9e
added additional parenthesis for macro definition
cb8e9e
    GF_REF_INIT
cb8e9e
    GF_REF_GET
cb8e9e
    GF_REF_PUT
cb8e9e
cb8e9e
> Checks for compiler supported atomic operations comes from client_t.h.
cb8e9e
>
cb8e9e
> An example usage of this change can be found in adding reference
cb8e9e
> counting to "struct auth_cache_entry" in http://review.gluster.org/11023
cb8e9e
>
cb8e9e
> Basic usage looks like this:
cb8e9e
>
cb8e9e
>    #include "refcount.h"
cb8e9e
>
cb8e9e
>    struct my_struct {
cb8e9e
>        GF_REF_DECL;
cb8e9e
>        ... /* more members */
cb8e9e
>    }
cb8e9e
>
cb8e9e
>    void my_destructor (void *data)
cb8e9e
>    {
cb8e9e
>        struct my_struct *my_ptr = (struct my_struct *) data;
cb8e9e
>
cb8e9e
>        ... /* do some more cleanups */
cb8e9e
>        GF_FREE (my_ptr);
cb8e9e
>    }
cb8e9e
>
cb8e9e
>    void init_ptr (struct parent *parent)
cb8e9e
>    {
cb8e9e
>        struct my_struct *my_ptr = malloc (sizeof (struct my_struct));
cb8e9e
>
cb8e9e
>        GF_REF_INIT (my_ptr, my_destructor); /* refcount is set to 1 */
cb8e9e
>
cb8e9e
>        ... /* my_ptr probably gets added to some parent structure */
cb8e9e
>        parent_add_ptr (parent, my_ptr);
cb8e9e
>    }
cb8e9e
>
cb8e9e
>    void do_something (struct parent *parent)
cb8e9e
>    {
cb8e9e
>        struct my_struct *my_ptr = NULL;
cb8e9e
>
cb8e9e
>        /* likely need to lock parent, depends on its access pattern */
cb8e9e
>        my_ptr = parent_remove_first_ptr (parent);
cb8e9e
>        /* unlock parent */
cb8e9e
>
cb8e9e
>        ... /* do something */
cb8e9e
>
cb8e9e
>        GF_REF_PUT (my_ptr); /* calls my_destructor on refcount = 0 */
cb8e9e
>    }
cb8e9e
>
cb8e9e
> URL: http://thread.gmane.org/gmane.comp.file-systems.gluster.devel/11202
cb8e9e
> Change-Id: Idb98a5861a44c31676108ed8876db12c320912ef
cb8e9e
> BUG: 1228157
cb8e9e
> Signed-off-by: Niels de Vos <ndevos@redhat.com>
cb8e9e
> Reviewed-on: http://review.gluster.org/11022
cb8e9e
> Reviewed-by: Xavier Hernandez <xhernandez@datalab.es>
cb8e9e
> Reviewed-by: Krishnan Parthasarathi <kparthas@redhat.com>
cb8e9e
> Tested-by: Gluster Build System <jenkins@build.gluster.com>
cb8e9e
> Tested-by: NetBSD Build System <jenkins@build.gluster.org>
cb8e9e
cb8e9e
Change-Id: I5630ff3fbd0e0021a83597d778e1037796be5355
cb8e9e
BUG: 1235628
cb8e9e
Signed-off-by: vmallika <vmallika@redhat.com>
cb8e9e
Reviewed-on: https://code.engineering.redhat.com/gerrit/51605
cb8e9e
Reviewed-by: Raghavendra Gowdappa <rgowdapp@redhat.com>
cb8e9e
Tested-by: Raghavendra Gowdappa <rgowdapp@redhat.com>
cb8e9e
---
cb8e9e
 libglusterfs/src/Makefile.am |    4 +-
cb8e9e
 libglusterfs/src/refcount.c  |  112 ++++++++++++++++++++++++++++++++++++++++++
cb8e9e
 libglusterfs/src/refcount.h  |  103 ++++++++++++++++++++++++++++++++++++++
cb8e9e
 3 files changed, 217 insertions(+), 2 deletions(-)
cb8e9e
 create mode 100644 libglusterfs/src/refcount.c
cb8e9e
 create mode 100644 libglusterfs/src/refcount.h
cb8e9e
cb8e9e
diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am
cb8e9e
index 80ce672..ed441d0 100644
cb8e9e
--- a/libglusterfs/src/Makefile.am
cb8e9e
+++ b/libglusterfs/src/Makefile.am
cb8e9e
@@ -22,7 +22,7 @@ libglusterfs_la_SOURCES = dict.c xlator.c logging.c \
cb8e9e
 	$(CONTRIBDIR)/rbtree/rb.c rbthash.c store.c latency.c \
cb8e9e
 	graph.c syncop.c graph-print.c trie.c run.c options.c fd-lk.c \
cb8e9e
 	circ-buff.c event-history.c gidcache.c ctx.c client_t.c event-poll.c \
cb8e9e
-	event-epoll.c syncop-utils.c cluster-syncop.c \
cb8e9e
+	event-epoll.c syncop-utils.c cluster-syncop.c refcount.c \
cb8e9e
 	$(CONTRIBDIR)/libgen/basename_r.c \
cb8e9e
 	$(CONTRIBDIR)/libgen/dirname_r.c $(CONTRIBDIR)/stdlib/gf_mkostemp.c \
cb8e9e
 	strfd.c parse-utils.c $(CONTRIBDIR)/mount/mntent.c \
cb8e9e
@@ -40,7 +40,7 @@ noinst_HEADERS = common-utils.h defaults.h dict.h glusterfs.h hashfn.h timespec.
cb8e9e
 	gf-dirent.h locking.h syscall.h iobuf.h globals.h statedump.h \
cb8e9e
 	checksum.h daemon.h $(CONTRIBDIR)/rbtree/rb.h store.h\
cb8e9e
 	rbthash.h iatt.h latency.h mem-types.h syncop.h cluster-syncop.h \
cb8e9e
-	graph-utils.h trie.h \
cb8e9e
+	graph-utils.h trie.h refcount.h \
cb8e9e
 	run.h options.h lkowner.h fd-lk.h circ-buff.h event-history.h \
cb8e9e
 	gidcache.h client_t.h glusterfs-acl.h glfs-message-id.h \
cb8e9e
 	template-component-messages.h strfd.h syncop-utils.h parse-utils.h \
cb8e9e
diff --git a/libglusterfs/src/refcount.c b/libglusterfs/src/refcount.c
cb8e9e
new file mode 100644
cb8e9e
index 0000000..96edc10
cb8e9e
--- /dev/null
cb8e9e
+++ b/libglusterfs/src/refcount.c
cb8e9e
@@ -0,0 +1,112 @@
cb8e9e
+/*
cb8e9e
+  Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com>
cb8e9e
+  This file is part of GlusterFS.
cb8e9e
+
cb8e9e
+  This file is licensed to you under your choice of the GNU Lesser
cb8e9e
+  General Public License, version 3 or any later version (LGPLv3 or
cb8e9e
+  later), or the GNU General Public License, version 2 (GPLv2), in all
cb8e9e
+  cases as published by the Free Software Foundation.
cb8e9e
+*/
cb8e9e
+
cb8e9e
+#include "common-utils.h"
cb8e9e
+#include "refcount.h"
cb8e9e
+
cb8e9e
+#ifndef REFCOUNT_NEEDS_LOCK
cb8e9e
+
cb8e9e
+unsigned int
cb8e9e
+_gf_ref_get (gf_ref_t *ref)
cb8e9e
+{
cb8e9e
+        unsigned int cnt = __sync_fetch_and_add (&ref->cnt, 1);
cb8e9e
+
cb8e9e
+        /* if cnt == 0, we're in a fatal position, the object will be free'd
cb8e9e
+         *
cb8e9e
+         * There is a race when two threads do a _gf_ref_get(). Only one of
cb8e9e
+         * them may get a 0 returned. That is acceptible, because one
cb8e9e
+         * _gf_ref_get() returning 0 should be handled as a fatal problem and
cb8e9e
+         * when correct usage/locking is used, it should never happen.
cb8e9e
+         */
cb8e9e
+        GF_ASSERT (cnt != 0);
cb8e9e
+
cb8e9e
+        return cnt;
cb8e9e
+}
cb8e9e
+
cb8e9e
+unsigned int
cb8e9e
+_gf_ref_put (gf_ref_t *ref)
cb8e9e
+{
cb8e9e
+        unsigned int cnt = __sync_fetch_and_sub (&ref->cnt, 1);
cb8e9e
+
cb8e9e
+        /* if cnt == 1, the last user just did a _gf_ref_put()
cb8e9e
+         *
cb8e9e
+         * When cnt == 0, one _gf_ref_put() was done too much and there has
cb8e9e
+         * been a thread using the refcounted structure when it was not
cb8e9e
+         * supposed to.
cb8e9e
+         */
cb8e9e
+        GF_ASSERT (cnt != 0);
cb8e9e
+
cb8e9e
+        if (cnt == 1 && ref->release) {
cb8e9e
+                ref->release (ref->data);
cb8e9e
+                /* set return value to 0 to inform the caller correctly */
cb8e9e
+                cnt = 0;
cb8e9e
+        }
cb8e9e
+
cb8e9e
+        return cnt;
cb8e9e
+}
cb8e9e
+
cb8e9e
+#else
cb8e9e
+
cb8e9e
+unsigned int
cb8e9e
+_gf_ref_get (gf_ref_t *ref)
cb8e9e
+{
cb8e9e
+        unsigned int cnt = 0;
cb8e9e
+
cb8e9e
+        LOCK (&ref->lk);
cb8e9e
+        {
cb8e9e
+                /* never can be 0, should have been free'd */
cb8e9e
+                if (ref->cnt > 0)
cb8e9e
+                        cnt = ++ref->cnt;
cb8e9e
+                else
cb8e9e
+                        GF_ASSERT (ref->cnt > 0);
cb8e9e
+        }
cb8e9e
+        UNLOCK (&ref->lk);
cb8e9e
+
cb8e9e
+        return cnt;
cb8e9e
+}
cb8e9e
+
cb8e9e
+unsigned int
cb8e9e
+_gf_ref_put (gf_ref_t *ref)
cb8e9e
+{
cb8e9e
+        unsigned int cnt = 0;
cb8e9e
+        int release = 0;
cb8e9e
+
cb8e9e
+        LOCK (&ref->lk);
cb8e9e
+        {
cb8e9e
+                if (ref->cnt != 0) {
cb8e9e
+                        cnt = --ref->cnt;
cb8e9e
+                        /* call release() only when cnt == 0 */
cb8e9e
+                        release = (cnt == 0);
cb8e9e
+                } else
cb8e9e
+                        GF_ASSERT (ref->cnt != 0);
cb8e9e
+        }
cb8e9e
+        UNLOCK (&ref->lk);
cb8e9e
+
cb8e9e
+        if (release && ref->release)
cb8e9e
+                ref->release (ref->data);
cb8e9e
+
cb8e9e
+        return cnt;
cb8e9e
+}
cb8e9e
+
cb8e9e
+#endif /* REFCOUNT_NEEDS_LOCK */
cb8e9e
+
cb8e9e
+
cb8e9e
+void
cb8e9e
+_gf_ref_init (gf_ref_t *ref, gf_ref_release_t release, void *data)
cb8e9e
+{
cb8e9e
+        GF_ASSERT (ref);
cb8e9e
+
cb8e9e
+#ifdef REFCOUNT_NEEDS_LOCK
cb8e9e
+        LOCK_INIT (&ref->lk);
cb8e9e
+#endif
cb8e9e
+        ref->cnt = 1;
cb8e9e
+        ref->release = release;
cb8e9e
+        ref->data = data;
cb8e9e
+}
cb8e9e
diff --git a/libglusterfs/src/refcount.h b/libglusterfs/src/refcount.h
cb8e9e
new file mode 100644
cb8e9e
index 0000000..c8113a2
cb8e9e
--- /dev/null
cb8e9e
+++ b/libglusterfs/src/refcount.h
cb8e9e
@@ -0,0 +1,103 @@
cb8e9e
+/*
cb8e9e
+  Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com>
cb8e9e
+  This file is part of GlusterFS.
cb8e9e
+
cb8e9e
+  This file is licensed to you under your choice of the GNU Lesser
cb8e9e
+  General Public License, version 3 or any later version (LGPLv3 or
cb8e9e
+  later), or the GNU General Public License, version 2 (GPLv2), in all
cb8e9e
+  cases as published by the Free Software Foundation.
cb8e9e
+*/
cb8e9e
+
cb8e9e
+#ifndef _REFCOUNT_H
cb8e9e
+#define _REFCOUNT_H
cb8e9e
+
cb8e9e
+/* check for compiler support for __sync_*_and_fetch()
cb8e9e
+ *
cb8e9e
+ * a more comprehensive feature test is shown at
cb8e9e
+ * http://lists.iptel.org/pipermail/semsdev/2010-October/005075.html
cb8e9e
+ * this is sufficient for RHEL5 i386 builds
cb8e9e
+ */
cb8e9e
+#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && !defined(__i386__)
cb8e9e
+#undef REFCOUNT_NEEDS_LOCK
cb8e9e
+#else
cb8e9e
+#define REFCOUNT_NEEDS_LOCK
cb8e9e
+#include "locking.h"
cb8e9e
+#endif /* compiler support for __sync_*_and_fetch() */
cb8e9e
+
cb8e9e
+typedef void (*gf_ref_release_t)(void *data);
cb8e9e
+
cb8e9e
+struct _gf_ref_t {
cb8e9e
+#ifdef REFCOUNT_NEEDS_LOCK
cb8e9e
+        gf_lock_t          lk;      /* lock for atomically adjust cnt */
cb8e9e
+#endif
cb8e9e
+        unsigned int       cnt;     /* number of users, free on 0 */
cb8e9e
+
cb8e9e
+        gf_ref_release_t   release; /* cleanup when cnt == 0 */
cb8e9e
+        void              *data;    /* parameter passed to release() */
cb8e9e
+};
cb8e9e
+typedef struct _gf_ref_t gf_ref_t;
cb8e9e
+
cb8e9e
+
cb8e9e
+/* _gf_ref_get -- increase the refcount and return the number of references
cb8e9e
+ *
cb8e9e
+ * @return: greater then 0 when a reference was taken, 0 when not
cb8e9e
+ */
cb8e9e
+unsigned int
cb8e9e
+_gf_ref_get (gf_ref_t *ref);
cb8e9e
+
cb8e9e
+/* _gf_ref_put -- decrease the refcount and return the number of references
cb8e9e
+ *
cb8e9e
+ * @return: greater then 0 when there are still references, 0 when cleanup
cb8e9e
+ *          should be done
cb8e9e
+ */
cb8e9e
+unsigned int
cb8e9e
+_gf_ref_put (gf_ref_t *ref);
cb8e9e
+
cb8e9e
+/* _gf_ref_init -- initalize an embedded refcount object
cb8e9e
+ *
cb8e9e
+ * @release: function to call when the refcount == 0
cb8e9e
+ * @data: parameter to be passed to @release
cb8e9e
+ */
cb8e9e
+void
cb8e9e
+_gf_ref_init (gf_ref_t *ref, gf_ref_release_t release, void *data);
cb8e9e
+
cb8e9e
+
cb8e9e
+/*
cb8e9e
+ * Strong suggestion to use the simplified GF_REF_* API.
cb8e9e
+ */
cb8e9e
+
cb8e9e
+/* GF_REF_DECL -- declaration to put inside your structure
cb8e9e
+ *
cb8e9e
+ * Example:
cb8e9e
+ *   struct my_struct {
cb8e9e
+ *       GF_REF_DECL;
cb8e9e
+ *
cb8e9e
+ *       ... // additional members
cb8e9e
+ *   };
cb8e9e
+ */
cb8e9e
+#define GF_REF_DECL         gf_ref_t _ref
cb8e9e
+
cb8e9e
+/* GF_REF_INIT -- initialize a GF_REF_DECL structure
cb8e9e
+ *
cb8e9e
+ * @p: allocated structure with GF_REF_DECL
cb8e9e
+ * @d: destructor to call once refcounting reaches 0
cb8e9e
+ *
cb8e9e
+ * Sets the refcount to 1.
cb8e9e
+ */
cb8e9e
+#define GF_REF_INIT(p, d)   (_gf_ref_init (&p->_ref, d, p))
cb8e9e
+
cb8e9e
+/* GF_REF_GET -- increase the refcount of a GF_REF_DECL structure
cb8e9e
+ *
cb8e9e
+ * @return: greater then 0 when a reference was taken, 0 when not
cb8e9e
+ */
cb8e9e
+#define GF_REF_GET(p)       (_gf_ref_get (&p->_ref))
cb8e9e
+
cb8e9e
+/* GF_REF_PUT -- decrease the refcount of a GF_REF_DECL structure
cb8e9e
+ *
cb8e9e
+ * @return: greater then 0 when there are still references, 0 when cleanup
cb8e9e
+ *          should be done
cb8e9e
+ */
cb8e9e
+#define GF_REF_PUT(p)       (_gf_ref_put (&p->_ref))
cb8e9e
+
cb8e9e
+
cb8e9e
+#endif /* _REFCOUNT_H */
cb8e9e
-- 
cb8e9e
1.7.1
cb8e9e