From patchwork Sat Mar 5 20:10:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 63625 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp786716lbc; Sat, 5 Mar 2016 16:44:13 -0800 (PST) X-Received: by 10.55.31.82 with SMTP id f79mr19547755qkf.42.1457225053094; Sat, 05 Mar 2016 16:44:13 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id t79si10217063qkt.80.2016.03.05.16.44.12; Sat, 05 Mar 2016 16:44:13 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 980EF61818; Sun, 6 Mar 2016 00:44:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00, DATE_IN_PAST_03_06, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 341CC6184D; Sun, 6 Mar 2016 00:43:42 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 070E0617DC; Sun, 6 Mar 2016 00:43:37 +0000 (UTC) Received: from mail-pa0-f43.google.com (mail-pa0-f43.google.com [209.85.220.43]) by lists.linaro.org (Postfix) with ESMTPS id 2AE6B617DC for ; Sun, 6 Mar 2016 00:43:35 +0000 (UTC) Received: by mail-pa0-f43.google.com with SMTP id fi3so54457699pac.3 for ; Sat, 05 Mar 2016 16:43:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6kiGK8mOhG6738YAOlXYptOKFp2atM1Fjj7R9S81n0U=; b=WOhJ+hwzJaRw4NnDYKooUI1i7DvXlkNWqEPuvnXAK1iOX2IVHL2Y10fJw1bEt4xD2R j3QFfidiyhL7FWYUd0tdYdzWHhyJYQcn5rC1n0J5YBzxnzkilhtnP8HXYgxUVCkF7MbN GKOR9k9CHBo7yLv18uL1WFfZGmGw3sk5mXjh9Ic/HmEq1EU7DGE5b1cFoYKmJEwC8CYu KcUCWLJRUPFuKH3gYkmgQE7NW8igpO8KMbvbWfZ9bp0fR/OnSaWjMUp9dM9kcspvUSAe KidyhOSCvWjDNPoVAzDVC33aywglDeC84Yo2RL7S/JMUPOsVYGUkx+ZWayxl/aNuwIxL 2IBg== X-Gm-Message-State: AD7BkJLFpwnuEMGi0Rh97WP6OGt1c/9/SsAn7RU+iTPGM/W6K5j9Z+fyZSHjHmjx2It//152pWk= X-Received: by 10.66.90.226 with SMTP id bz2mr23439254pab.31.1457225014506; Sat, 05 Mar 2016 16:43:34 -0800 (PST) Received: from Ubuntu15.localdomain (110-170-137-3.static.asianet.co.th. [110.170.137.3]) by smtp.gmail.com with ESMTPSA id w20sm14775280pfi.31.2016.03.05.16.43.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 05 Mar 2016 16:43:33 -0800 (PST) From: Bill Fischofer To: lng-odp@lists.linaro.org Date: Sun, 6 Mar 2016 07:10:31 +1100 Message-Id: <1457208633-1453-2-git-send-email-bill.fischofer@linaro.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1457208633-1453-1-git-send-email-bill.fischofer@linaro.org> References: <1457208633-1453-1-git-send-email-bill.fischofer@linaro.org> X-Topics: patch Subject: [lng-odp] [API-NEXT PATCHv2 2/4] linux-generic: rwlock: implement trylock variants of rwlock and rwlock_recursive X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" 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" Signed-off-by: Bill Fischofer --- platform/linux-generic/odp_rwlock.c | 14 ++++++++++ platform/linux-generic/odp_rwlock_recursive.c | 37 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/platform/linux-generic/odp_rwlock.c b/platform/linux-generic/odp_rwlock.c index f50fd5f..13c17a2 100644 --- a/platform/linux-generic/odp_rwlock.c +++ b/platform/linux-generic/odp_rwlock.c @@ -31,6 +31,13 @@ void odp_rwlock_read_lock(odp_rwlock_t *rwlock) } } +int odp_rwlock_read_trylock(odp_rwlock_t *rwlock) +{ + uint32_t zero = 0; + + return odp_atomic_cas_acq_u32(&rwlock->cnt, &zero, (uint32_t)1); +} + void odp_rwlock_read_unlock(odp_rwlock_t *rwlock) { odp_atomic_sub_rel_u32(&rwlock->cnt, 1); @@ -54,6 +61,13 @@ void odp_rwlock_write_lock(odp_rwlock_t *rwlock) } } +int odp_rwlock_write_trylock(odp_rwlock_t *rwlock) +{ + uint32_t zero = 0; + + return odp_atomic_cas_acq_u32(&rwlock->cnt, &zero, (uint32_t)-1); +} + void odp_rwlock_write_unlock(odp_rwlock_t *rwlock) { odp_atomic_store_rel_u32(&rwlock->cnt, 0); diff --git a/platform/linux-generic/odp_rwlock_recursive.c b/platform/linux-generic/odp_rwlock_recursive.c index 2338b53..6b02281 100644 --- a/platform/linux-generic/odp_rwlock_recursive.c +++ b/platform/linux-generic/odp_rwlock_recursive.c @@ -31,6 +31,24 @@ void odp_rwlock_recursive_read_lock(odp_rwlock_recursive_t *rlock) rlock->rd_cnt[thr] = 1; } +/* Multiple readers can recurse the lock concurrently */ +int odp_rwlock_recursive_read_trylock(odp_rwlock_recursive_t *rlock) +{ + int thr = odp_thread_id(); + + if (rlock->rd_cnt[thr]) { + rlock->rd_cnt[thr]++; + return 1; + } + + if (odp_rwlock_read_trylock(&rlock->lock)) { + rlock->rd_cnt[thr] = 1; + return 1; + } + + return 0; +} + void odp_rwlock_recursive_read_unlock(odp_rwlock_recursive_t *rlock) { int thr = odp_thread_id(); @@ -58,6 +76,25 @@ void odp_rwlock_recursive_write_lock(odp_rwlock_recursive_t *rlock) rlock->wr_cnt = 1; } +/* Only one writer can recurse the lock */ +int odp_rwlock_recursive_write_trylock(odp_rwlock_recursive_t *rlock) +{ + int thr = odp_thread_id(); + + if (rlock->wr_owner == thr) { + rlock->wr_cnt++; + return 1; + } + + if (odp_rwlock_write_trylock(&rlock->lock)) { + rlock->wr_owner = thr; + rlock->wr_cnt = 1; + return 1; + } + + return 0; +} + void odp_rwlock_recursive_write_unlock(odp_rwlock_recursive_t *rlock) { rlock->wr_cnt--;