From patchwork Thu Mar 20 13:37:12 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 26703 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f72.google.com (mail-oa0-f72.google.com [209.85.219.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id EDFED202E0 for ; Thu, 20 Mar 2014 13:37:18 +0000 (UTC) Received: by mail-oa0-f72.google.com with SMTP id eb12sf3485095oac.7 for ; Thu, 20 Mar 2014 06:37:18 -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=lbm6OgT2HVKrJxXHvW2Gmndv1fwSHf27hv7OC1tSKlI=; b=mgRt6aiWSdYLSgMjq033xbihgQwE7ifK8hP8OZm3wGbqIFNxMIKveYXFI1brhnmJot L6/r9LWHtFr9/6J+uudvBNnfbPKCeM5F9e6poffYAbx1es7E1BewZRCsdRha2AaivNhZ GqNru49WiKznanQ5R0bNp7VCfXi70Ws5bCZ8xkWHAyCbSnfquURMCrDrGoMfFcPYA+1j 62XE/8NQ0/dUdbQNkdd02p5sFADryVkaGqyBWJESSgYNZFo2uLL2o5NxRkG2zmX2Wzxj v54l1Gfqt/I7/L0o2hVmWu16HG9v2PI0+k/kjmwhyi8/U1BNajqi/fwXSbbI1aLoS1CT foCA== X-Gm-Message-State: ALoCoQk7KKxJLqV4O8fdOpNoE2U2yQ8WzWEqf2CUNYsjgpDRk8PMWcYgqZOqK2XjsYBN+kxOEWIj X-Received: by 10.182.87.2 with SMTP id t2mr15863509obz.2.1395322638280; Thu, 20 Mar 2014 06:37:18 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: lng-odp@linaro.org Received: by 10.182.102.73 with SMTP id fm9ls103998obb.53.gmail; Thu, 20 Mar 2014 06:37:18 -0700 (PDT) X-Received: by 10.60.138.164 with SMTP id qr4mr37865783oeb.28.1395322638063; Thu, 20 Mar 2014 06:37:18 -0700 (PDT) Received: from mail-oa0-f44.google.com (mail-oa0-f44.google.com [209.85.219.44]) by mx.google.com with ESMTPS id w10si2894375obx.129.2014.03.20.06.37.18 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 20 Mar 2014 06:37:18 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.219.44 is neither permitted nor denied by best guess record for domain of bill.fischofer@linaro.org) client-ip=209.85.219.44; Received: by mail-oa0-f44.google.com with SMTP id n16so894750oag.3 for ; Thu, 20 Mar 2014 06:37:18 -0700 (PDT) X-Received: by 10.60.132.142 with SMTP id ou14mr19564318oeb.48.1395322637906; Thu, 20 Mar 2014 06:37:17 -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 wj7sm2989497obc.8.2014.03.20.06.37.17 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 20 Mar 2014 06:37:17 -0700 (PDT) From: Bill Fischofer To: lng-odp@linaro.org Cc: Bill Fischofer Subject: [lng-odp] [ODP/PATCH v6] Correct race condition and simplify barrier implementation Date: Thu, 20 Mar 2014 08:37:12 -0500 Message-Id: <1395322632-9139-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.44 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: , Add odp_mem_barrier() before exit from odp_barrier_sync() Signed-off-by: Bill Fischofer --- include/odp_barrier.h | 3 +- platform/linux-generic/source/odp_barrier.c | 44 ++++++++++++++--------------- 2 files changed, 22 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..a82b294 100644 --- a/platform/linux-generic/source/odp_barrier.c +++ b/platform/linux-generic/source/odp_barrier.c @@ -11,40 +11,38 @@ 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(); } + + odp_mem_barrier(); }