From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755734AbdIGR4g (ORCPT ); Thu, 7 Sep 2017 13:56:36 -0400 Received: from mga09.intel.com ([134.134.136.24]:39783 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755602AbdIGR4c (ORCPT ); Thu, 7 Sep 2017 13:56:32 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,359,1500966000"; d="scan'208";a="149347736" 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 06/10] perf tools: lock to protect comm_str rb tree Date: Thu, 7 Sep 2017 10:55:50 -0700 Message-Id: <1504806954-150842-7-git-send-email-kan.liang@intel.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1504806954-150842-1-git-send-email-kan.liang@intel.com> References: <1504806954-150842-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 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, 14 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/comm.c b/tools/perf/util/comm.c index 7bc981b..1bdfef1 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) { @@ -24,11 +26,13 @@ static struct comm_str *comm_str__get(struct comm_str *cs) static void comm_str__put(struct comm_str *cs) { + pthread_mutex_lock(&comm_str_lock); if (cs && refcount_dec_and_test(&cs->refcnt)) { rb_erase(&cs->rb_node, &comm_str_root); zfree(&cs->str); free(cs); } + pthread_mutex_unlock(&comm_str_lock); } static struct comm_str *comm_str__alloc(const char *str) @@ -52,18 +56,22 @@ static struct comm_str *comm_str__alloc(const char *str) 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; struct comm_str *iter, *new; + struct rb_node **p; int cmp; + pthread_mutex_lock(&comm_str_lock); + p = &root->rb_node; while (*p != NULL) { parent = *p; iter = rb_entry(parent, struct comm_str, rb_node); cmp = strcmp(str, iter->str); - if (!cmp) - return comm_str__get(iter); + if (!cmp) { + new = comm_str__get(iter); + goto unlock; + } if (cmp < 0) p = &(*p)->rb_left; @@ -73,11 +81,13 @@ static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root) new = comm_str__alloc(str); if (!new) - return NULL; + goto unlock; rb_link_node(&new->rb_node, parent, p); rb_insert_color(&new->rb_node, root); +unlock: + pthread_mutex_unlock(&comm_str_lock); return new; } -- 2.5.5