Skip to contentSkip to conversation

perf cgroup: Use atomic.h for refcounting

tip:perf/core tip-bot for Arnaldo Carvalho de Melo 1 participant 1 message May 20, 2015 perf events

lore
Share & tools
lore
#0 lore raw
Commit-ID:  f812d3045c2385ac16237e68b156859c4005526e
Gitweb:     http://git.kernel.org/tip/f812d3045c2385ac16237e68b156859c4005526e
Author:     Arnaldo Carvalho de Melo <[email protected]>
AuthorDate: Fri, 15 May 2015 16:12:20 -0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 15 May 2015 16:12:20 -0300

perf cgroup: Use atomic.h for refcounting

Now that we have atomic.h, we should convert all of the existing refcounts to use it.

Cc Adrian Hunter <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Ahern <[email protected]>
Cc Don Zickus <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/cgroup.c | 10 +++-------
 tools/perf/util/cgroup.h |  4 +++-
 2 files changed, 6 insertions(+), 8 deletions(-)
tools/perf/util/cgroup.c +3 -7
index 88f7be3..32e12ec 100644
@@ -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);
tools/perf/util/cgroup.h +3 -1
index 89acd6d..b4b8cb4 100644
@@ -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;
 };