From patchwork Thu Mar 20 12:03:54 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 26687 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ig0-f197.google.com (mail-ig0-f197.google.com [209.85.213.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 357EC20143 for ; Thu, 20 Mar 2014 12:04:03 +0000 (UTC) Received: by mail-ig0-f197.google.com with SMTP id hl1sf2973325igb.0 for ; Thu, 20 Mar 2014 05:04:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:to:cc:subject:date:message-id :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe:content-type; bh=bwNiO80lMEd5OaBudV5KGoDAlCbbDCzKjXbkzKnl/yY=; b=FgRo1O205qkioJyfXSoogwR7+YFuvPN2i89iaoTBmdIEhGII5wUsV/8eTeZYzFw5jl BgCaGfidn+C4Sg4IQZjm43rzQ3m2CVwS8RZaDEMTnZKbwfARLKUtE+9q1w3a+wttn72C lIhzJyCvM20+OtNQU4sbLdOzNoJbgJIXDjciaK9HiV+KqF+AlETyEgdZf114ckiw9Ask tvv490R+VieuUtqa8yI38R2WIuxMkW01ti9XUL1bjBeZPaCqgniF2Fah1fss24UaGQoe mRPfZ+EwF9Ie/JJs7W57GtFTE9ibKq9+hTbpMG2TZnjbSR0tLV2IBfycRxx4kExQZ7d0 CS2Q== X-Gm-Message-State: ALoCoQmj52ZoZ80lm339v0jidO78UWA8hdv/U1P3IsEj4mI6hZHlk8CaXhIG60F4CqUucqPUjMfJ X-Received: by 10.50.33.101 with SMTP id q5mr1127291igi.3.1395317042569; Thu, 20 Mar 2014 05:04:02 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: lng-odp@linaro.org Received: by 10.182.5.196 with SMTP id u4ls74229obu.83.gmail; Thu, 20 Mar 2014 05:04:02 -0700 (PDT) X-Received: by 10.60.233.202 with SMTP id ty10mr37300552oec.25.1395317042346; Thu, 20 Mar 2014 05:04:02 -0700 (PDT) Received: from mail-ob0-f182.google.com (mail-ob0-f182.google.com [209.85.214.182]) by mx.google.com with ESMTPS id ds9si2556591obc.26.2014.03.20.05.04.02 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 20 Mar 2014 05:04:02 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.214.182 is neither permitted nor denied by best guess record for domain of bill.fischofer@linaro.org) client-ip=209.85.214.182; Received: by mail-ob0-f182.google.com with SMTP id uz6so741730obc.41 for ; Thu, 20 Mar 2014 05:04:02 -0700 (PDT) X-Received: by 10.60.15.227 with SMTP id a3mr5701852oed.58.1395317042156; Thu, 20 Mar 2014 05:04:02 -0700 (PDT) Received: from localhost.localdomain (cpe-24-28-70-239.austin.res.rr.com. [24.28.70.239]) by mx.google.com with ESMTPSA id wj7sm2668312obc.8.2014.03.20.05.04.01 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 20 Mar 2014 05:04:01 -0700 (PDT) From: Bill Fischofer To: lng-odp@linaro.org Cc: Bill Fischofer Subject: [lng-odp] [ODP/PATCH v5] Correct race condition and simplify barrier implementation Date: Thu, 20 Mar 2014 07:03:54 -0500 Message-Id: <1395317034-7456-1-git-send-email-bill.fischofer@linaro.org> X-Mailer: git-send-email 1.8.3.2 X-Original-Sender: bill.fischofer@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.214.182 is neither permitted nor denied by best guess record for domain of bill.fischofer@linaro.org) smtp.mail=bill.fischofer@linaro.org Precedence: list Mailing-list: list lng-odp@linaro.org; contact lng-odp+owners@linaro.org List-ID: X-Google-Group-Id: 474323889996 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Signed-off-by: Bill Fischofer --- include/odp_barrier.h | 3 +-- platform/linux-generic/source/odp_barrier.c | 42 +++++++++++++---------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/include/odp_barrier.h b/include/odp_barrier.h index bb4a6c5..0a1404b 100644 --- a/include/odp_barrier.h +++ b/include/odp_barrier.h @@ -28,8 +28,7 @@ extern "C" { */ typedef struct odp_barrier_t { int count; - odp_atomic_int_t in; - odp_atomic_int_t out; + odp_atomic_int_t bar; } odp_barrier_t; diff --git a/platform/linux-generic/source/odp_barrier.c b/platform/linux-generic/source/odp_barrier.c index 64fbdb9..c78e802 100644 --- a/platform/linux-generic/source/odp_barrier.c +++ b/platform/linux-generic/source/odp_barrier.c @@ -11,40 +11,36 @@ void odp_barrier_init_count(odp_barrier_t *barrier, int count) { barrier->count = count; - barrier->in = 0; - barrier->out = count - 1; + barrier->bar = 0; odp_sync_stores(); } +/* + * Efficient barrier_sync - + * + * Barriers are initialized with a count of the number of callers + * that must sync on the barrier before any may proceed. + * + * To avoid race conditions and to permit the barrier to be fully + * reusable, the barrier value cycles between 0..2*count-1. When + * synchronizing the wasless variable simply tracks which half of + * the cycle the barrier was in upon entry. Exit is when the + * barrier crosses to the other half of the cycle. + */ void odp_barrier_sync(odp_barrier_t *barrier) { int count; + int wasless; odp_sync_stores(); + wasless = barrier->bar < barrier->count; + count = odp_atomic_fetch_inc_int(&barrier->bar); - count = odp_atomic_fetch_inc_int(&barrier->in); - - if (count == barrier->count - 1) { - /* If last thread, release others */ - barrier->in = 0; - odp_sync_stores(); - - /* Wait for others to exit */ - while (barrier->out) - odp_spin(); - - /* Ready, reset out counter */ - barrier->out = barrier->count - 1; - odp_sync_stores(); - + if (count == 2*barrier->count-1) { + barrier->bar = 0; } else { - /* Wait for the last thread*/ - while (barrier->in) + while ((barrier->bar < barrier->count) == wasless) odp_spin(); - - /* Ready */ - odp_atomic_dec_int(&barrier->out); - odp_mem_barrier(); } }