From patchwork Wed Mar 19 16:55:15 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 26601 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pb0-f69.google.com (mail-pb0-f69.google.com [209.85.160.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id F3A28203C3 for ; Wed, 19 Mar 2014 16:55:24 +0000 (UTC) Received: by mail-pb0-f69.google.com with SMTP id md12sf22857555pbc.4 for ; Wed, 19 Mar 2014 09:55:24 -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=cC+su70Y/ta4oLH7kpdtdpM75JK+mpI8uTvVy3ijQmA=; b=U98PjMOP6DEfk12KDrfrq5l8KGbSHLCG0Rx8xdr8YQ+RwwyHFrLa33Bv3ClRPT/O5F pwkjOJTprDX+2x2VSiLuDOJ8Tdm4iykR42ihGkdwCVh0G7PVezfDChoO69H07km9vh2x +OuzExNF+B/K4UTdc3CV/v6vvjmluJECufKF2NHPCg3AE/+1Y+TcAHeJ1ltdHKphyVTN 4sB921O/PuUSI93JZJuYPp3pdeolFgQJ27Pcn2RLkL0iyQfuRo0whmXlDIJyqbX1h41L Nzvgs2GbnTYX1ggnCvuq/Ig5MtdU1/wIWRvuiglL47tKAeYFzKLjBI3cTO9GxDf6evs4 Bglw== X-Gm-Message-State: ALoCoQmH83vuNJS/nX6zuOPjFbCtBPyDJUthRUWZT79UiQMw4p+JUvFYP0zXVzdFsL3MN8EXCaEH X-Received: by 10.66.252.1 with SMTP id zo1mr15831434pac.40.1395248124221; Wed, 19 Mar 2014 09:55:24 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: lng-odp@linaro.org Received: by 10.182.19.165 with SMTP id g5ls1000733obe.59.gmail; Wed, 19 Mar 2014 09:55:23 -0700 (PDT) X-Received: by 10.60.60.33 with SMTP id e1mr33703983oer.36.1395248123863; Wed, 19 Mar 2014 09:55:23 -0700 (PDT) Received: from mail-oa0-f41.google.com (mail-oa0-f41.google.com [209.85.219.41]) by mx.google.com with ESMTPS id f10si23228868obr.33.2014.03.19.09.55.23 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 19 Mar 2014 09:55:23 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.219.41 is neither permitted nor denied by best guess record for domain of bill.fischofer@linaro.org) client-ip=209.85.219.41; Received: by mail-oa0-f41.google.com with SMTP id j17so8674062oag.28 for ; Wed, 19 Mar 2014 09:55:23 -0700 (PDT) X-Received: by 10.182.102.134 with SMTP id fo6mr2894991obb.10.1395248123616; Wed, 19 Mar 2014 09:55:23 -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 tz6sm38413238obc.10.2014.03.19.09.55.21 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 19 Mar 2014 09:55:22 -0700 (PDT) From: Bill Fischofer To: lng-odp@linaro.org Cc: Bill Fischofer Subject: [lng-odp] [ODP/PATCH v3] Remove race condition and simplify barrier implementation Date: Wed, 19 Mar 2014 11:55:15 -0500 Message-Id: <1395248115-3027-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.219.41 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: , Adding odp_sync_stores() to odp_barrier_init() per Petri's comments. Signed-off-by: Bill Fischofer --- include/odp_barrier.h | 3 +- platform/linux-generic/source/odp_barrier.c | 46 +++++++++++++---------------- 2 files changed, 21 insertions(+), 28 deletions(-) 1.8.3.2 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..9dc6fb5 100644 --- a/platform/linux-generic/source/odp_barrier.c +++ b/platform/linux-generic/source/odp_barrier.c @@ -11,41 +11,35 @@ void odp_barrier_init_count(odp_barrier_t *barrier, int count) { barrier->count = count; - barrier->in = 0; - barrier->out = count - 1; - odp_sync_stores(); + 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(); - - 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(); + wasless = barrier->bar < barrier->count; + count = odp_atomic_fetch_inc_int(&barrier->bar); + if (count == 2*barrier->count-1) { + barrier->bar = 0; } else { - /* Wait for the last thread*/ - while (barrier->in) - odp_spin(); - - /* Ready */ - odp_atomic_dec_int(&barrier->out); - odp_mem_barrier(); + while ((barrier->bar < barrier->count) == wasless) + odp_spin(); } } --