From patchwork Mon May 21 09:08:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leonid Myravjev X-Patchwork-Id: 40254 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id BE857AAC9; Mon, 21 May 2018 11:09:51 +0200 (CEST) Received: from mail-lf0-f66.google.com (mail-lf0-f66.google.com [209.85.215.66]) by dpdk.org (Postfix) with ESMTP id 36F53AAC6 for ; Mon, 21 May 2018 11:09:51 +0200 (CEST) Received: by mail-lf0-f66.google.com with SMTP id x145-v6so10826424lff.1 for ; Mon, 21 May 2018 02:09:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id; bh=+CGXcxRIVXEzY6uzCcbOGcQItL5WrmEOmg6LDTu9klE=; b=sOe/KNSkDrA8WwFOx7WojQMbPBEI3kEnJT0e6J5Um48BrFfwCyDqwkhxnqdCyENt1t tMLSsMzB1CvLZ9yO0y8tMrXJ3Rf3ek0/kITf03sczn9kcddrtQhVX0ANaSsJ1/9WLcH3 ljTiInGakVIG++lIf7JfWlfNtBnR7ebmRvdWFQkivixXcQzjv2d1p2rt12POj3P9qk27 2IA/faqhOVUDkSN9UsBXXxUze2luT3u+tNgyDOqC4GAU5n5f9gpghz+eYWWvaoIW30c1 +Iyrflteq8ZQDPQjXd7XWK/hULjNIb98fbcLrii+DdqG9c75T5lwprTEjaiiM7Dp5+Ne vo0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=+CGXcxRIVXEzY6uzCcbOGcQItL5WrmEOmg6LDTu9klE=; b=Du6qIpXq7DjlkpJUuMxg0dloArx4Q+MlOj7gC26M2c3+NsEI7Vowf5KTm01brTxWVn wI+RiTA3vf6QdR4DzMiocv6GTIV9htxe8kszta6F2gUl8VQe7FDXZBYZYBs6TKFDyWK9 kS0+n762S+jA8k4d8zEvjAntgdTI+XiyOYaThJUcqvXX6BPXxWndFeMVYnveXYk0KOye lScfwpKw7s4sN21VPhy9OTRk54CjV6LXBYPhKkJ0YK1DvyLjykNSMrtbuuMTenH+Xisp 3vuDIwcxEvKXaUd1plw0iBTyN86onHkZUmbgvJKq3xjQkuP4k8VYNGMERwcVV5ngh2q8 8/mQ== X-Gm-Message-State: ALKqPwfhB6/bVtfDJea5ac7JLt5vZtbhoe2D06yonPSoOK1R1sJlbVt/ IccbnX+eZO4VqGkIVrFntdlFFg== X-Google-Smtp-Source: AB8JxZob00+3ZoUanGOypCuuio7U3GKYT6Mg3EOXp40qT2SASyplkp0ZvQRsNuOmyLzvqyiZ0EyeIg== X-Received: by 2002:a19:9f8e:: with SMTP id i136-v6mr8300954lfe.5.1526893790485; Mon, 21 May 2018 02:09:50 -0700 (PDT) Received: from localhost.localdomain ([77.108.111.98]) by smtp.gmail.com with ESMTPSA id v9-v6sm3321649lfa.30.2018.05.21.02.09.49 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Mon, 21 May 2018 02:09:49 -0700 (PDT) From: Leonid Myravjev To: dev@dpdk.org Cc: Leonid Myravjev Date: Mon, 21 May 2018 12:08:12 +0300 Message-Id: <20180521090812.27058-1-asm@asm.pp.ru> X-Mailer: git-send-email 2.13.0 Subject: [dpdk-dev] [PATCH] eal/rwlocks: Try read/write and relock write to read locks added. X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" From: Leonid Myravjev Signed-off-by: Leonid Myravjev --- lib/librte_eal/common/include/generic/rte_rwlock.h | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/lib/librte_eal/common/include/generic/rte_rwlock.h b/lib/librte_eal/common/include/generic/rte_rwlock.h index 899e9bc43..11212e2b8 100644 --- a/lib/librte_eal/common/include/generic/rte_rwlock.h +++ b/lib/librte_eal/common/include/generic/rte_rwlock.h @@ -76,6 +76,30 @@ rte_rwlock_read_lock(rte_rwlock_t *rwl) } /** + * Try take lock a read lock. + * + * @param rwl + * A pointer to a rwlock structure. + * @return + * 1 if the lock is successfully taken; 0 otherwise. + */ +static inline int +rte_rwlock_read_trylock(rte_rwlock_t *rwl) +{ + int32_t x; + int success = 0; + + x = rwl->cnt; + /* write lock is held */ + if (x < 0) + return 0; + success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt, x, x + 1); + if (success == 0) + return 0; + return 1; +} + +/** * Release a read lock. * * @param rwl @@ -110,6 +134,29 @@ rte_rwlock_write_lock(rte_rwlock_t *rwl) 0, -1); } } +/** + * Try take a write lock. + * + * @param rwl + * A pointer to a rwlock structure. + * @return + * 1 if the lock is successfully taken; 0 otherwise. + */ +static inline int +rte_rwlock_write_trylock(rte_rwlock_t *rwl) +{ + int32_t x; + int success = 0; + + x = rwl->cnt; + /* a lock is held */ + if (x != 0) + return 0; + success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt, 0, -1); + if (success == 0) + return 0; + return 1; +} /** * Release a write lock. @@ -124,6 +171,20 @@ rte_rwlock_write_unlock(rte_rwlock_t *rwl) } /** + * Relock write lock to read + * + * @param rwl + * A pointer to a rwlock structure. + */ +static inline void +rte_rwlock_write_relock_read(rte_rwlock_t *rwl) +{ + rte_atomic32_add((rte_atomic32_t *)(intptr_t)&rwl->cnt, 2); +} + + + +/** * Try to execute critical section in a hardware memory transaction, if it * fails or not available take a read lock *