From patchwork Tue Mar 18 23:55:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 26529 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f72.google.com (mail-pa0-f72.google.com [209.85.220.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B95F1203C3 for ; Tue, 18 Mar 2014 23:55:51 +0000 (UTC) Received: by mail-pa0-f72.google.com with SMTP id bj1sf20456048pad.7 for ; Tue, 18 Mar 2014 16:55:50 -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=ZplgvKV/LjnK4Jxzw68i2WeZjVSs3gN2sLjWPJuCWgY=; b=QhX/EPeiWuviKM09MeENxEZnmeuCCCnXs04g7kg0hoexo8qLCPg/mt12tMMocqd03F EHDmshzy+Yu4uehaofI6GtU1GGlBOkdzA2YJSqaIMxTQYzhG4cTPWy8RJlbjYQUGuB7O ouqzvs4y4/QJgYjERpd/I0+btGdKdHVU2lDWVGGYqN/m9AYZqQVHcmLXrSFJOnrhu7k5 /8wrW6tz1VPD4GPs0r56cYC1U5pRx28cyA0TxDNNfT+BPW8SWbGwEyp1cVsWk0aU0ltZ Bx2a+xa7OqnlzgVQF5dqyLc7c/aL1GqOy64MpTMBFLXa4DtBZwUw8cp/ecgSrIDbTKjr pYxQ== X-Gm-Message-State: ALoCoQlDHC8dRJVNHNPFflTH+PRg9/cyymeYdnCBP7MUumIrnXx4uNq0bfuES/UqvSThyRjoA0mm X-Received: by 10.66.146.65 with SMTP id ta1mr13588864pab.19.1395186950644; Tue, 18 Mar 2014 16:55:50 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: lng-odp@linaro.org Received: by 10.50.110.105 with SMTP id hz9ls2084586igb.5.gmail; Tue, 18 Mar 2014 16:55:50 -0700 (PDT) X-Received: by 10.60.44.42 with SMTP id b10mr4455041oem.70.1395186950268; Tue, 18 Mar 2014 16:55:50 -0700 (PDT) Received: from mail-ob0-f180.google.com (mail-ob0-f180.google.com [209.85.214.180]) by mx.google.com with ESMTPS id dq4si19512959oeb.206.2014.03.18.16.55.50 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 18 Mar 2014 16:55:50 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.214.180 is neither permitted nor denied by best guess record for domain of bill.fischofer@linaro.org) client-ip=209.85.214.180; Received: by mail-ob0-f180.google.com with SMTP id wn1so7505215obc.39 for ; Tue, 18 Mar 2014 16:55:50 -0700 (PDT) X-Received: by 10.60.83.234 with SMTP id t10mr29461023oey.4.1395186950076; Tue, 18 Mar 2014 16:55:50 -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 zm8sm26713804obc.16.2014.03.18.16.55.48 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 18 Mar 2014 16:55:49 -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 18:55:58 -0500 Message-Id: <1395186958-5594-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.180 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(-) 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,40 +11,34 @@ 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(); } }