From patchwork Wed Dec 10 10:55:17 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ola Liljedahl X-Patchwork-Id: 42087 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f198.google.com (mail-lb0-f198.google.com [209.85.217.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 4CDF926666 for ; Wed, 10 Dec 2014 10:55:24 +0000 (UTC) Received: by mail-lb0-f198.google.com with SMTP id p9sf1773064lbv.9 for ; Wed, 10 Dec 2014 02:55:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:subject :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=vBAkb4pPBjFENI0XF/qBn4lz/Hm8xNY1ooCsxvlj8lI=; b=PXVWUsymXO9LxX8os5E4cGlquQmmCmIe973h6r/k+Om8zRRqWsyYTqEuIXb0HrWeD5 HRL/6XEQC1O9YQHh/eh4tKF2Jev5r3h3tpKBdbC4KVt0W9q8yf/4heJxuMlJ8cJm9O8Q tOL7uiU7gEL4q8Sin0YvVk8Tbs3+72FR2vcl3kh2LroyhEkbCsSjrmF3M7MHsDLN2DiM cyC3MwdX5Q2Z3cOxSE4+U9mwaloDUUJvGJRQr/FDhOpR5xAMVi+LeeakLrBoYdA1ZE4r +ui3I7lltLdB7Symv4oF3Raa0IhO3pXJKy7p5NB39CP/dUYcois2sPItddd8UqLJN5+A 3jww== X-Gm-Message-State: ALoCoQnfObFTnxINncypmuIPho4RhXIejAkVW8+B9NI2uFOZeg37rCySENP62h3DvxUqCpqF6wvy X-Received: by 10.152.42.196 with SMTP id q4mr214466lal.6.1418208923261; Wed, 10 Dec 2014 02:55:23 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.21.40 with SMTP id s8ls176973lae.100.gmail; Wed, 10 Dec 2014 02:55:23 -0800 (PST) X-Received: by 10.112.138.9 with SMTP id qm9mr3469713lbb.62.1418208923115; Wed, 10 Dec 2014 02:55:23 -0800 (PST) Received: from mail-lb0-f172.google.com (mail-lb0-f172.google.com. [209.85.217.172]) by mx.google.com with ESMTPS id z4si809579lbf.130.2014.12.10.02.55.23 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 10 Dec 2014 02:55:23 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.172 as permitted sender) client-ip=209.85.217.172; Received: by mail-lb0-f172.google.com with SMTP id u10so2124788lbd.3 for ; Wed, 10 Dec 2014 02:55:23 -0800 (PST) X-Received: by 10.112.235.196 with SMTP id uo4mr3380666lbc.66.1418208922995; Wed, 10 Dec 2014 02:55:22 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.184.201 with SMTP id ew9csp854826lbc; Wed, 10 Dec 2014 02:55:22 -0800 (PST) X-Received: by 10.236.26.138 with SMTP id c10mr2052254yha.192.1418208921748; Wed, 10 Dec 2014 02:55:21 -0800 (PST) Received: from ip-10-35-177-41.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id q184si1284850ykd.32.2014.12.10.02.55.20 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 10 Dec 2014 02:55:21 -0800 (PST) Received-SPF: none (google.com: lng-odp-bounces@lists.linaro.org does not designate permitted sender hosts) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-35-177-41.ec2.internal) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XyevP-0005pD-BY; Wed, 10 Dec 2014 10:55:19 +0000 Received: from mail-lb0-f181.google.com ([209.85.217.181]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XyevI-0005oo-CY for lng-odp@lists.linaro.org; Wed, 10 Dec 2014 10:55:12 +0000 Received: by mail-lb0-f181.google.com with SMTP id l4so2076245lbv.26 for ; Wed, 10 Dec 2014 02:55:06 -0800 (PST) X-Received: by 10.152.21.135 with SMTP id v7mr3437273lae.5.1418208906413; Wed, 10 Dec 2014 02:55:06 -0800 (PST) Received: from macmini.lan (84-217-193-77.tn.glocalnet.net. [84.217.193.77]) by mx.google.com with ESMTPSA id nz3sm1127023lbb.25.2014.12.10.02.55.05 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Dec 2014 02:55:05 -0800 (PST) From: Ola Liljedahl To: lng-odp@lists.linaro.org Date: Wed, 10 Dec 2014 11:55:17 +0100 Message-Id: <1418208917-29915-1-git-send-email-ola.liljedahl@linaro.org> X-Mailer: git-send-email 1.9.1 X-Topics: patch Subject: [lng-odp] [PATCH] linux-generic: odp_ticketlock.h: implement trylock() X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: lng-odp-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ola.liljedahl@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.172 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Signed-off-by: Ola Liljedahl Reviewed-by: Bill Fischofer --- (This document/code contribution attached is provided under the terms of agreement LES-LTM-21309) Implemented the missing odp_ticketlock_trylock(). platform/linux-generic/odp_ticketlock.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/platform/linux-generic/odp_ticketlock.c b/platform/linux-generic/odp_ticketlock.c index 682b01b..6525786 100644 --- a/platform/linux-generic/odp_ticketlock.c +++ b/platform/linux-generic/odp_ticketlock.c @@ -34,6 +34,36 @@ void odp_ticketlock_lock(odp_ticketlock_t *ticketlock) odp_spin(); } +int odp_ticketlock_trylock(odp_ticketlock_t *tklock) +{ + /* We read 'next_ticket' and 'cur_ticket' non-atomically which should + * not be a problem as they are not independent of each other. + * 'cur_ticket' is always <= to 'next_ticket' and if we see an + * older value of 'cur_ticket', this only means the lock will + * look busy and trylock will fail. */ + uint32_t next = odp_atomic_load_u32(&tklock->next_ticket); + uint32_t cur = odp_atomic_load_u32(&tklock->cur_ticket); + /* First check that lock is available and possible to take without + * spinning. */ + if (next == cur) { + /* Then try to take the lock by incrementing 'next_ticket' + * but only if it still has the original value which is + * equal to 'cur_ticket'. + * We don't have to include 'cur_ticket' in the comparison + * because it cannot be larger than 'next_ticket' (only + * smaller if the lock is busy). + * If CAS fails, it means some other thread intercepted and + * took a ticket which means the lock is not available + * anymore */ + if (_odp_atomic_u32_cmp_xchg_strong_mm(&tklock->next_ticket, + &next, + next + 1, + _ODP_MEMMODEL_ACQ, + _ODP_MEMMODEL_RLX)) + return 1; + } + return 0; +} void odp_ticketlock_unlock(odp_ticketlock_t *ticketlock) {