perf cgroup: Use atomic.h for refcounting
authorArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 15 May 2015 19:12:20 +0000 (16:12 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 15 May 2015 19:12:20 +0000 (16:12 -0300)
Now that we have atomic.h, we should convert all of the existing
refcounts to use it.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: David Ahern <dsahern@gmail.com>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-t3v2uma5digcj2tpkrs3m84u@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/cgroup.c
tools/perf/util/cgroup.h

index 88f7be3994321f8ef717262909ac6c38853fc691..32e12ecfe9c576767f18a3cb42e6c5dedfc3f048 100644 (file)
@@ -115,23 +115,19 @@ static int add_cgroup(struct perf_evlist *evlist, char *str)
                        goto found;
                n++;
        }
-       if (cgrp->refcnt == 0)
+       if (atomic_read(&cgrp->refcnt) == 0)
                free(cgrp);
 
        return -1;
 found:
-       cgrp->refcnt++;
+       atomic_inc(&cgrp->refcnt);
        counter->cgrp = cgrp;
        return 0;
 }
 
 void close_cgroup(struct cgroup_sel *cgrp)
 {
-       if (!cgrp)
-               return;
-
-       /* XXX: not reentrant */
-       if (--cgrp->refcnt == 0) {
+       if (cgrp && atomic_dec_and_test(&cgrp->refcnt)) {
                close(cgrp->fd);
                zfree(&cgrp->name);
                free(cgrp);
index 89acd6debdc5fc06d28889cb95eb30d7af98642f..b4b8cb42fe5e04b7458fe3b57f764b0bda6c18db 100644 (file)
@@ -1,12 +1,14 @@
 #ifndef __CGROUP_H__
 #define __CGROUP_H__
 
+#include <linux/atomic.h>
+
 struct option;
 
 struct cgroup_sel {
        char *name;
        int fd;
-       int refcnt;
+       atomic_t refcnt;
 };