From patchwork Thu Mar 20 11:54:46 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 26681 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f198.google.com (mail-qc0-f198.google.com [209.85.216.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 0F9B520143 for ; Thu, 20 Mar 2014 11:54:54 +0000 (UTC) Received: by mail-qc0-f198.google.com with SMTP id r5sf1782918qcx.5 for ; Thu, 20 Mar 2014 04:54:54 -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=DwteAEZG3EdtpZxtmhHLqx7kfFab72JqEj8IBD5qYzU=; b=cwBUxomV4fDYBRsWXGSQZMBZ+PivmUhaklw1eEg1Q3VpNMIpJiBUgsCKYUzotHmpD1 oZy5MBeE+DovJw1nbRQZHFV0az6QtPRnlFh4Dz4Xx2a23Pz4KsL1t9+DovWOpD0jxGJr ncEvfUzzangV+YZxncW1Ec+HsY95R/DyPhSaG5V5pANsNsD0FPACdlu+gVAfUZts+Cr+ W/h3FqPg12x/B/9tgDyuO0n2/e0mQhIQxONcmn5tPHrpiqKMS+0TbNmSIrCgSvefiB5p ilCO263RE9QO+ul+vL3nPsmtXfp6SUS2Nu0WYUV0YyOcynD4+6CKYkj0/pagAS1p63zx 20jg== X-Gm-Message-State: ALoCoQmI6VHcG9wDo0Lm4tgDDl7P8tFqC07OtOAutvtOPtilnyrHl4H5CYdQhEI77byU6poLSD0r X-Received: by 10.236.181.74 with SMTP id k50mr8821882yhm.46.1395316494691; Thu, 20 Mar 2014 04:54:54 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: lng-odp@linaro.org Received: by 10.182.246.232 with SMTP id xz8ls99113obc.76.gmail; Thu, 20 Mar 2014 04:54:54 -0700 (PDT) X-Received: by 10.60.136.170 with SMTP id qb10mr37287549oeb.34.1395316494518; Thu, 20 Mar 2014 04:54:54 -0700 (PDT) Received: from mail-ob0-f170.google.com (mail-ob0-f170.google.com [209.85.214.170]) by mx.google.com with ESMTPS id so6si2522819obb.17.2014.03.20.04.54.54 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 20 Mar 2014 04:54:54 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.214.170 is neither permitted nor denied by best guess record for domain of bill.fischofer@linaro.org) client-ip=209.85.214.170; Received: by mail-ob0-f170.google.com with SMTP id uz6so748877obc.1 for ; Thu, 20 Mar 2014 04:54:54 -0700 (PDT) X-Received: by 10.182.153.226 with SMTP id vj2mr6710441obb.26.1395316494416; Thu, 20 Mar 2014 04:54:54 -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 kf10sm2620032obb.21.2014.03.20.04.54.53 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 20 Mar 2014 04:54:53 -0700 (PDT) From: Bill Fischofer To: lng-odp@linaro.org Cc: Bill Fischofer Subject: [lng-odp] [ODP/PATCH v4] Correct race condition and simplify barrier implementation Date: Thu, 20 Mar 2014 06:54:46 -0500 Message-Id: <1395316486-7328-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.170 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: , 1. Added odp_sync_stores() back to both functions to resolve Petri's comments. 2. Used linux-c-mode to remove all checkpatch warnings Signed-off-by: Bill Fischofer --- include/odp_barrier.h | 3 +-- platform/linux-generic/source/odp_barrier.c | 42 +++++++++++++---------------- 2 files changed, 20 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..c78e802 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; + 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(); } }