Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752477AbdIKCZW (ORCPT ); Sun, 10 Sep 2017 22:25:22 -0400 Received: from mga05.intel.com ([192.55.52.43]:17265 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752502AbdIKCX6 (ORCPT ); Sun, 10 Sep 2017 22:23:58 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,375,1500966000"; d="scan'208";a="1013017758" From: kan.liang@intel.com To: acme@kernel.org, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: jolsa@kernel.org, namhyung@kernel.org, adrian.hunter@intel.com, lukasz.odzioba@intel.com, ak@linux.intel.com, Kan Liang Subject: [PATCH RFC V2 06/10] perf tools: lock to protect comm_str rb tree Date: Sun, 10 Sep 2017 19:23:19 -0700 Message-Id: <1505096603-215017-7-git-send-email-kan.liang@intel.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1505096603-215017-1-git-send-email-kan.liang@intel.com> References: <1505096603-215017-1-git-send-email-kan.liang@intel.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1948 Lines: 69 From: Kan Liang Add comm_str_lock to protect comm_str rb tree. Signed-off-by: Kan Liang --- tools/perf/util/comm.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/comm.c b/tools/perf/util/comm.c index 7bc981b..cb7f98c 100644 --- a/tools/perf/util/comm.c +++ b/tools/perf/util/comm.c @@ -5,6 +5,7 @@ #include #include #include +#include struct comm_str { char *str; @@ -14,6 +15,7 @@ struct comm_str { /* Should perhaps be moved to struct machine */ static struct rb_root comm_str_root; +static pthread_mutex_t comm_str_lock = PTHREAD_MUTEX_INITIALIZER; static struct comm_str *comm_str__get(struct comm_str *cs) { @@ -25,7 +27,9 @@ static struct comm_str *comm_str__get(struct comm_str *cs) static void comm_str__put(struct comm_str *cs) { if (cs && refcount_dec_and_test(&cs->refcnt)) { + pthread_mutex_lock(&comm_str_lock); rb_erase(&cs->rb_node, &comm_str_root); + pthread_mutex_unlock(&comm_str_lock); zfree(&cs->str); free(cs); } @@ -50,7 +54,8 @@ static struct comm_str *comm_str__alloc(const char *str) return cs; } -static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root) +static +struct comm_str *__comm_str__findnew(const char *str, struct rb_root *root) { struct rb_node **p = &root->rb_node; struct rb_node *parent = NULL; @@ -81,6 +86,17 @@ static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root) return new; } +static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root) +{ + struct comm_str *cs; + + pthread_mutex_lock(&comm_str_lock); + cs = __comm_str__findnew(str, root); + pthread_mutex_unlock(&comm_str_lock); + + return cs; +} + struct comm *comm__new(const char *str, u64 timestamp, bool exec) { struct comm *comm = zalloc(sizeof(*comm)); -- 2.5.5