From patchwork Tue Mar 18 22:58:53 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 26526 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pb0-f71.google.com (mail-pb0-f71.google.com [209.85.160.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5B63B202FA for ; Tue, 18 Mar 2014 22:58:58 +0000 (UTC) Received: by mail-pb0-f71.google.com with SMTP id up15sf19992421pbc.2 for ; Tue, 18 Mar 2014 15:58:57 -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 :in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe:content-type; bh=nqWLHy6eZ02lfOF7oMaKxLCGbKvJcbl6xm1TjpFRq7M=; b=AhuAqslVHPEJWv7cE+899xzK/eqDer+6lHAOYpVtUU0JGzrIEZ58zxaO7Dsv9HgSgU 6TXHZT0kvnmLJ1D9VIgeohk1x5wrKHuODC9mbP60MzfxBmdaO2Ek2pXNXIxNcA/nYW87 z4zAC0etBqZzrJ2hMvpRGa1h5O+p5hsfVuLWX7MivsLW/+h1nqEdOd8mMzUi+Q9CR3QT 5wnHbwoTnuvrWSPi6AcWsomExUhcyiTuJuOeTvEaVjnbAFK926FAXntbE2afDwx5x9GI eXLV4Us4GiGgc46driFzg/UPyi9YFelpUH5eyYMV/NKWd+8li3CxhQbXqhjdtzk4LMZ8 Aaug== X-Gm-Message-State: ALoCoQmIfUsYt8r/hNv5dqGtQdpwy0aYOhF1ypLMbq1uI3gV3b0qaYitnbj6zCdV80Ss5j51hE3O X-Received: by 10.66.221.137 with SMTP id qe9mr13478777pac.4.1395183537519; Tue, 18 Mar 2014 15:58:57 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: lng-odp@linaro.org Received: by 10.50.56.46 with SMTP id x14ls2040972igp.5.canary; Tue, 18 Mar 2014 15:58:57 -0700 (PDT) X-Received: by 10.60.173.233 with SMTP id bn9mr29735729oec.9.1395183537309; Tue, 18 Mar 2014 15:58:57 -0700 (PDT) Received: from mail-oa0-f42.google.com (mail-oa0-f42.google.com [209.85.219.42]) by mx.google.com with ESMTPS id kb5si19521482obb.41.2014.03.18.15.58.57 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 18 Mar 2014 15:58:57 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.219.42 is neither permitted nor denied by best guess record for domain of bill.fischofer@linaro.org) client-ip=209.85.219.42; Received: by mail-oa0-f42.google.com with SMTP id i4so7663540oah.15 for ; Tue, 18 Mar 2014 15:58:57 -0700 (PDT) X-Received: by 10.60.37.199 with SMTP id a7mr29735815oek.41.1395183537190; Tue, 18 Mar 2014 15:58:57 -0700 (PDT) Received: from localhost.localdomain (cpe-70-112-164-196.austin.res.rr.com. [70.112.164.196]) by mx.google.com with ESMTPSA id kf10sm26509042obb.21.2014.03.18.15.58.56 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 18 Mar 2014 15:58:56 -0700 (PDT) From: Bill Fischofer To: lng-odp@linaro.org Cc: Bill Fischofer Subject: [lng-odp] [ODP/PATCH] Remove race condition and simplify barrier implementation Date: Tue, 18 Mar 2014 17:58:53 -0500 Message-Id: <1395183533-5328-2-git-send-email-bill.fischofer@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1395183533-5328-1-git-send-email-bill.fischofer@linaro.org> References: <1395183533-5328-1-git-send-email-bill.fischofer@linaro.org> X-Original-Sender: bill.fischofer@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.219.42 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 | 48 +++++++++++++---------------- 2 files changed, 23 insertions(+), 28 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..fc0f13f 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; - odp_sync_stores(); + barrier->bar = 0; } +/* + * 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(); + } } + }