From patchwork Wed Mar 19 16:46:33 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 26600 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f71.google.com (mail-oa0-f71.google.com [209.85.219.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 55903203C3 for ; Wed, 19 Mar 2014 16:46:43 +0000 (UTC) Received: by mail-oa0-f71.google.com with SMTP id j17sf34224178oag.2 for ; Wed, 19 Mar 2014 09:46:42 -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=Se+EChLDJyb+4A4tdlnMbb+Wm+GhuYqZ3YpnnpG4aT4=; b=UwyTn0f1/5I0hojnMDPs9iYTY1Jg4NdojGJFEuKSFO5FRSogfjicdu7ekA1nuU68Sx 6ZCZIs3nD03nX/izJAd1DuLwKVAHtCV4mt+3qfNACK8uVvXjfm7Ifvfyv5B41MzERbmy SG3cTxiCpBBgQTquIY2g1GdMIqPifz2C/9MMXdVT8nVD7sYD8oboywytGRU1kuzSr8Je ejCQQX2xh5akMp7P3Glg54NBptPTAjh4ETeyPOsMTTey8NK2yvZlmgdNgBG+Ia7q2Hla sUT+piJWHBO8+5BoyYkqxpaX1e3uKyHfj9y5DfipdvQJmoqNkIegdwgqUp7ZKS2oaXqX BVGQ== X-Gm-Message-State: ALoCoQlvCzI0WVqqI2mCvTl2Xh3KzfVfdkR4gGgSbgxMtygMyOujkwJ1oNmrkgeCADIxpFhladtH X-Received: by 10.182.213.5 with SMTP id no5mr1378650obc.15.1395247602760; Wed, 19 Mar 2014 09:46:42 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: lng-odp@linaro.org Received: by 10.182.5.196 with SMTP id u4ls926526obu.83.gmail; Wed, 19 Mar 2014 09:46:42 -0700 (PDT) X-Received: by 10.182.194.103 with SMTP id hv7mr2835827obc.30.1395247602399; Wed, 19 Mar 2014 09:46:42 -0700 (PDT) Received: from mail-oa0-f52.google.com (mail-oa0-f52.google.com [209.85.219.52]) by mx.google.com with ESMTPS id f10si23173251obr.195.2014.03.19.09.46.42 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 19 Mar 2014 09:46:42 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.219.52 is neither permitted nor denied by best guess record for domain of bill.fischofer@linaro.org) client-ip=209.85.219.52; Received: by mail-oa0-f52.google.com with SMTP id l6so8648138oag.11 for ; Wed, 19 Mar 2014 09:46:42 -0700 (PDT) X-Received: by 10.182.225.137 with SMTP id rk9mr2227531obc.51.1395247602204; Wed, 19 Mar 2014 09:46:42 -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 g4sm38384823obe.5.2014.03.19.09.46.40 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 19 Mar 2014 09:46:41 -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: Wed, 19 Mar 2014 11:46:33 -0500 Message-Id: <1395247593-2941-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.52 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 | 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(); } } --