From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 7B354A052A; Wed, 27 Jan 2021 11:26:46 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 44905140CF7; Wed, 27 Jan 2021 11:26:46 +0100 (CET) Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-eopbgr70050.outbound.protection.outlook.com [40.107.7.50]) by mails.dpdk.org (Postfix) with ESMTP id EF2E5140CF5 for ; Wed, 27 Jan 2021 11:26:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Z8fHAR1eQRiYtu6tORC90ebti/UGNxCvge0uENZPiuE=; b=ejrWyfMaeMeV8lzusvxx6T/dWn7Jwe1CMAa/Gzi1neYmeoheJAbGW6aWfyJNSXYfJejN26CTeMJn+2DAHP0hp9kK6P1YuhP6wr6EgIjIVmlQiOJRXDWOfxj/1TT5m8JwNJvp8vnAGDfT15aiPtagmy1Nh0cmoqKfQSUOzxlzP/I= Received: from DB6PR1001CA0047.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:4:55::33) by PAXPR08MB6446.eurprd08.prod.outlook.com (2603:10a6:102:12d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.9; Wed, 27 Jan 2021 10:25:30 +0000 Received: from DB5EUR03FT022.eop-EUR03.prod.protection.outlook.com (2603:10a6:4:55:cafe::e3) by DB6PR1001CA0047.outlook.office365.com (2603:10a6:4:55::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3805.16 via Frontend Transport; Wed, 27 Jan 2021 10:25:29 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dpdk.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dpdk.org; dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DB5EUR03FT022.mail.protection.outlook.com (10.152.20.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.11 via Frontend Transport; Wed, 27 Jan 2021 10:25:29 +0000 Received: ("Tessian outbound 28c96a6c9d2e:v71"); Wed, 27 Jan 2021 10:25:29 +0000 X-CR-MTA-TID: 64aa7808 Received: from 9cf33f043df6.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 7AF90F44-64D2-4912-8044-3ABE8878C9DC.1; Wed, 27 Jan 2021 10:25:24 +0000 Received: from EUR03-VE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 9cf33f043df6.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 27 Jan 2021 10:25:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Jl58nlc6IspK8MWgOl6TGHNK0S1gP9i1ALF9i7ggrtSNrWSdh1gNr0CYbW1+GML7Zt7CtrgFP80NaUcC8wzcKzehMZ5b+NyHmV117ahAlgb26p3qkx/Vrvp8dGDvtA7+iBE5bqOQfukk9UFxtbwvxOlRdYWpV+EzJbxLICkOCrMt5gRwJFtk19dgdCxY/k9oy+1VS5SVs9tApBlfjj9iLBc4A5bWK2I9fJwKpxEzHvpii0kh6fcEEO9uyAYT/jzMMFqYGVxYDbb72pupSKJ8Lohom0czogNpBpWyncdCbhvTrTycr0bjo0+Df3VxmFeLfJhJxnVv0s/RrX/DzxW6bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Z8fHAR1eQRiYtu6tORC90ebti/UGNxCvge0uENZPiuE=; b=CGutj8lw8mwDbjnJ166F466+wnoO5rD1O4YgXSWTPeABrjCdPuM2YrNWgyZaa5/KxLDQdS1lB06f6LJNRrxPWYwMo2+EPvkNQpioYU4BjTGIM/dDjHib8wyIupDSyxgSKicUtY1qk1jDQ54ghIR+geih4Y4Z8dH4dNLuyM5vWNONzQ4A5MOB4dPk8X2HH9zyhF4TWzdP1Ba2un58/VcNPsTOKv8p/DnJJh9XNoF+Q0GBjfgjTeenLGmgEr2FvDotlC2wzkyT1LSNBxSvHiOhNLlfDgwpW2/05+9Bj5RarTdCwZlGRMe4J/QT4uW4hJ3npsE4vNo9mGhowEB5IEQH+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Z8fHAR1eQRiYtu6tORC90ebti/UGNxCvge0uENZPiuE=; b=ejrWyfMaeMeV8lzusvxx6T/dWn7Jwe1CMAa/Gzi1neYmeoheJAbGW6aWfyJNSXYfJejN26CTeMJn+2DAHP0hp9kK6P1YuhP6wr6EgIjIVmlQiOJRXDWOfxj/1TT5m8JwNJvp8vnAGDfT15aiPtagmy1Nh0cmoqKfQSUOzxlzP/I= Received: from VI1PR0802MB2351.eurprd08.prod.outlook.com (2603:10a6:800:a0::10) by VI1PR0801MB1678.eurprd08.prod.outlook.com (2603:10a6:800:51::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3805.17; Wed, 27 Jan 2021 10:25:16 +0000 Received: from VI1PR0802MB2351.eurprd08.prod.outlook.com ([fe80::b950:7a8:744:62bb]) by VI1PR0802MB2351.eurprd08.prod.outlook.com ([fe80::b950:7a8:744:62bb%10]) with mapi id 15.20.3805.017; Wed, 27 Jan 2021 10:25:16 +0000 From: Ruifeng Wang To: Stephen Hemminger , "dev@dpdk.org" CC: nd Thread-Topic: [dpdk-dev] [PATCH v1] eal: add ticket based reader writer lock Thread-Index: AQHW6pugj/fjzM5aWUyIuzzXk5qk3qo7UcQA Date: Wed, 27 Jan 2021 10:25:15 +0000 Message-ID: References: <20210112060524.409412-1-stephen@networkplumber.org> <20210114173454.56657-1-stephen@networkplumber.org> In-Reply-To: <20210114173454.56657-1-stephen@networkplumber.org> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ts-tracking-id: 845BA2BE44856E49A9B693334F673822.0 x-checkrecipientchecked: true Authentication-Results-Original: networkplumber.org; dkim=none (message not signed) header.d=none;networkplumber.org; dmarc=none action=none header.from=arm.com; x-originating-ip: [203.126.0.111] x-ms-publictraffictype: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 789b724f-13d9-4f24-07f5-08d8c2addef9 x-ms-traffictypediagnostic: VI1PR0801MB1678:|PAXPR08MB6446: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true nodisclaimer: true x-ms-oob-tlc-oobclassifiers: OLM:8273;OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: tOQL6zs1WZvssTcfBqUilkklb5hWQt9CQPNbJON/O0rY0FxixCldWTTycKOdAGETD9FX0X2pqo6MKr0wULJ4+Z6NC5Cex4xq0grgHhQun8EmGDcQyEnZa/qWjCp8NDL9h3aXKcdVvYp2tkCslpt88iD2qoFRSVTUQXqkTKZDaXDU1dRlXBRGWHBvoMOyXltNYBh2A2HSKxwC7KlUkk2CfanykxuqWD2IDTsk1HSps2lUzbEdEvXsmGaEeQS4BdIgeu6TSOW2VXsD5lffTm5SDrED4zLnTcgcrf1C6jP7aYO13xj5EQyCYjGaiOwAKYuoE8HrtJHc2Cng5f0EBTZPunfLVvmp3VfTcyA5wUi2VSpcF6WUKXQhcn4/Kwuhhv+uG/whO4qNeV8D+hN/856q6qZGFrJxjUmBguFnjkoDiYRwKs4DK6k6kcwuAb1rfixPEFFwGce/xl0wIAJWSnC0cA== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI1PR0802MB2351.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(39860400002)(366004)(376002)(346002)(396003)(136003)(4326008)(110136005)(55016002)(86362001)(9686003)(316002)(2906002)(7696005)(53546011)(6506007)(26005)(71200400001)(8936002)(30864003)(186003)(8676002)(33656002)(76116006)(66446008)(66556008)(66476007)(66946007)(64756008)(83380400001)(5660300002)(478600001)(966005)(52536014)(45080400002); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata: =?us-ascii?Q?kG9ctFjgWivAtS+PMIcKU+3OZCXjW00zMWQFiqvgqzOTacOXHrvcyFpSIFas?= =?us-ascii?Q?gJ5EdKNi+z3aHvdlup0yXFj7A9jtddj/MrrIzp1Us//dQx9zadbeoOPTONfa?= =?us-ascii?Q?gPF/eDAKk0hX9L1K0psDqE7AdPNqTM05dIlDEv49xkc5Ez7YcrMU+1KFozvB?= =?us-ascii?Q?2UBRHTwHJkXdia/DCQM4U8R8dlMESk/OpPsm3PtbZuFj9npz3qwTHXRqjkON?= =?us-ascii?Q?DGSD0lvV/P4Y4bVnZSwk2Izr9lX5oPfQ5RrHOKYBYCnmon0LWU5COEk7dl+1?= =?us-ascii?Q?M4uN+rtVpTF1tYDfZzJrerCdBSQvaN6bkMPZmewTyjynqadE6hqQ6/oryThu?= =?us-ascii?Q?fYa8v3vjARWGoLqF8L26hqaqa1NJVL7HgPi0VxJN6mrYPK2ljVAHHO4QToUQ?= =?us-ascii?Q?D2/Mk6wuqitjI3t2EMnA7XKugG8KubRDprD8EBfIeuCVqfApzPb+Ch7plETL?= =?us-ascii?Q?eBLkccws43UmTDTlkUAHd6dWF9EFp+A4PJfAjjSQBfTbHII0QF4flBqadTWt?= =?us-ascii?Q?ilPMuzBcxa62Svoae9LKpm1sPtUVaL9uUB1E9If+qGGeDYTGuX6sAr2EkUwl?= =?us-ascii?Q?HO7474x8dxWjOTJMFk0A4Yh5cUjF9iPU5yL+RPxIcn2VM5kup5AVjyHHBecv?= =?us-ascii?Q?2rt4kEU32w/rBgLIW1YCIiuRyEI1FVbAWaC5YgZu8KhbYL7dC2ac/r5qsK5J?= =?us-ascii?Q?ubfexrn7ldStMN2zlEawaIoVzUFlGvIgkzBnDl+6jTgYcsLxa5D0sPRMiUqx?= =?us-ascii?Q?oFO6ZW//5dA+rOPkWFysWDX/8xywc6SVYOddU0uS0u5V+5t+zs0wkaCIgrOP?= =?us-ascii?Q?ehRgraIceE4eLqMIcTIv1xiQmfN1kcOeMyOLaDYsL+gYtg1u/ZHTcY6p8a+F?= =?us-ascii?Q?dbrNrsJ53HnhppfrA7S5Zo+lAmqC9jtgJ4ele64meuHoHyuWGg3Nl9r6NRBL?= =?us-ascii?Q?vsvOWJwW3RG3JG8SJwlrFKujXn2sSXsJhPPdyXjjm5y/P9motd9bH17blmCU?= =?us-ascii?Q?fkATnyW55uUgB+vK9WIUILidTcIWWeVb3SUcA1/0c45kPwjQ9V9B+/zDCe11?= =?us-ascii?Q?F96TYbf+?= x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0801MB1678 Original-Authentication-Results: networkplumber.org; dkim=none (message not signed) header.d=none; networkplumber.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB5EUR03FT022.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 9d7ec19b-3f8d-4401-9011-08d8c2add6b2 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bfXOUQ/RrPGA9oVK+JfirYizf6ey3q+j8qgyNPhCr3rVpirzHwcrTYWwunBA7Ji7s9qQ++jdqNiLtfKTfVfIfrbyXOUXHAPQb+lIZEucIygjYjsL2vmR7CRvcKAnbMwfjm4KfQxcrd9pBrgjnT4/CPEzNtNR0G8dM9htfOj5fpPONDJUWjnZRXpmP/9DVKhp0AqKvrSmwlJPus9DaiJva8DeOdj4eZRe+0cgRnCaflOBt2MltlFMheQ9YjqvxkpMzKBsRTqQUSTv3YbXS/wqDdGwzsxn3XvVssf1yEohcGG+NpQZUGI1qEva+bF+yDS1qbYXlgFI+eWJJSc10qD923k84m4/R6R3D0m4yLuBzvTjMi8Co1pJLuuMfC7Up9jhndqUxbzWAJKrvMyxzaqfN8ujTucXm9/u61A/1wbQewS/rWAHsSyqILO4rTwgCp8R3XoRN5psH65GMmzClalCcHe2F/FR3eT0YFaSjdBjbzJH0pj1krX6Kf4xwzBLv+YX5t20VNndfS/QkLhkIcqcHJ6vefdVsYyuyzLLDaCKEtQ9uDXmNNSiVltzfg2jLtcev1aVLtlctt3PuM4NLTmbuJMG+mE8yq08kHCwYmHExu2UrS+sGosUoL4OabU71SOptJ85PbcQJ93TBRS3ZuyHIjlMrFN2H51WdwoGZch8Bjw= X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(4636009)(136003)(396003)(376002)(346002)(39860400002)(46966006)(7696005)(52536014)(70586007)(70206006)(9686003)(186003)(478600001)(82310400003)(8676002)(82740400003)(81166007)(8936002)(336012)(110136005)(26005)(966005)(2906002)(55016002)(5660300002)(316002)(53546011)(83380400001)(4326008)(86362001)(33656002)(6506007)(45080400002)(47076005)(30864003)(356005); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jan 2021 10:25:29.8838 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 789b724f-13d9-4f24-07f5-08d8c2addef9 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: DB5EUR03FT022.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR08MB6446 Subject: Re: [dpdk-dev] [PATCH v1] eal: add ticket based reader writer lock X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 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" > -----Original Message----- > From: dev On Behalf Of Stephen Hemminger > Sent: Friday, January 15, 2021 1:35 AM > To: dev@dpdk.org > Cc: Stephen Hemminger > Subject: [dpdk-dev] [PATCH v1] eal: add ticket based reader writer lock >=20 > This patch implements a reader/writer ticket lock. > This lock type acts like rte_rwlock() but uses a ticket algorithm and are= fair for > multiple writers and readers. > Writers have priority over readers. The lock is ticket based to be fair. So writers should have no priority? >=20 > The tests are just a clone of existing rte_rwlock with test and function = names > changed. So the new ticket rwlocks should be drop in replacement for most > users. >=20 > Signed-off-by: Stephen Hemminger > --- > Ps: I have additional tests for rwlock that test for fairness. > Would these be valuable? >=20 > app/test/autotest_data.py | 6 + > app/test/meson.build | 5 + > app/test/test_ticket_rwlock.c | 554 ++++++++++++++++++ > doc/api/doxy-api-index.md | 1 + > lib/librte_eal/arm/include/meson.build | 1 + > .../arm/include/rte_ticket_rwlock.h | 22 + > .../include/generic/rte_ticket_rwlock.h | 218 +++++++ > lib/librte_eal/include/meson.build | 1 + > lib/librte_eal/ppc/include/meson.build | 1 + > .../ppc/include/rte_ticket_rwlock.h | 18 + > lib/librte_eal/x86/include/meson.build | 1 + > .../x86/include/rte_ticket_rwlock.h | 18 + > 12 files changed, 846 insertions(+) > create mode 100644 app/test/test_ticket_rwlock.c create mode 100644 > lib/librte_eal/arm/include/rte_ticket_rwlock.h > create mode 100644 lib/librte_eal/include/generic/rte_ticket_rwlock.h > create mode 100644 lib/librte_eal/ppc/include/rte_ticket_rwlock.h > create mode 100644 lib/librte_eal/x86/include/rte_ticket_rwlock.h >=20 > diff --git a/lib/librte_eal/include/generic/rte_ticket_rwlock.h > b/lib/librte_eal/include/generic/rte_ticket_rwlock.h > new file mode 100644 > index 000000000000..b3637358c1f7 > --- /dev/null > +++ b/lib/librte_eal/include/generic/rte_ticket_rwlock.h > @@ -0,0 +1,218 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2021 Microsoft Corporation */ > + > +#ifndef _RTE_TICKET_RWLOCK_H_ > +#define _RTE_TICKET_RWLOCK_H_ > + > +/** > + * @file > + * > + * Ticket based reader/writer lock > + * > + * This file defines an API for ticket style read-write locks. > + * This types of lock act like rte_rwlock but provide fairness > + * and requests are handled first come, first serviced. > + * > + * All locks must be initialized before use, and only initialized once. > + * > + * References: > + * "Spinlocks and Read-Write Locks" > + * http://locklessinc.com/articles/locks/ > + * "Scalable Read-Writer Synchronization for Shared-Memory > Multiprocessors" > + * > https://www.cs.rochester.edu/research/synchronization/pseudocode/rw.ht > ml > + */ > + > +#ifdef __cplusplus > +extern "C" { > +#endif > + > +typedef union { > + uint64_t tickets; > + struct { > + union { > + struct { > + uint16_t write; /* current writer */ > + uint16_t read; /* current reader */ > + }; > + uint32_t readwrite; /* atomic for both read and > write */ > + }; > + uint16_t next; /* next ticket */ > + }; > +} rte_rwticketlock_t; > + > +/** > + * A static rwticket initializer. > + */ > +#define RTE_RWTICKETLOCK_INITIALIZER { 0 } > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice. > + * > + * Initialize the rwticketlock to an unlocked state. > + * > + * @param rwl > + * A pointer to the rwticketlock structure. > + */ > +__rte_experimental > +static inline void > +rte_rwticketlock_init(rte_rwticketlock_t *rwl) { > + rwl->tickets =3D 0; > +} > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice. > + * Take a write lock. Loop until the lock is held. > + * > + * @param rwl > + * A pointer to a rwticketlock structure. > + */ > +__rte_experimental > +static inline void > +rte_rwticket_write_lock(rte_rwticketlock_t *rwl) { > + uint16_t me; > + > + me =3D __atomic_fetch_add(&rwl->next, 1, __ATOMIC_RELAXED); > + rte_wait_until_equal_16(&rwl->write, me, __ATOMIC_ACQUIRE); } > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice. > + * > + * Try to take a write lock. > + * > + * @param rwl > + * A pointer to a rwticketlock structure. > + * @return > + * - zero if the lock is successfully taken > + * - -EBUSY if lock could not be acquired for writing because > + * it was already locked for reading or writing > + */ > +__rte_experimental > +static inline int > +rte_rwticket_write_trylock(rte_rwticketlock_t *rwl) { > + rte_rwticketlock_t old, new; > + > + old.tickets =3D __atomic_load_n(&rwl->tickets, __ATOMIC_RELAXED); > + if (old.write !=3D old.next) > + return -EBUSY; > + > + new.tickets =3D old.tickets; > + new.next =3D old.next + 1; > + if (__atomic_compare_exchange_n(&rwl->tickets, &old.tickets, > new.tickets, > + 0, __ATOMIC_ACQUIRE, > __ATOMIC_RELAXED)) > + return 0; > + else > + return -EBUSY; > +} > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice. > + * > + * Release a write lock. > + * > + * @param rwl > + * A pointer to a rwticketlock structure. > + */ > +__rte_experimental > +static inline void > +rte_rwticket_write_unlock(rte_rwticketlock_t *rwl) { > + rte_rwticketlock_t t; > + > + t.tickets =3D __atomic_load_n(&rwl->tickets, __ATOMIC_RELAXED); > + t.write++; > + t.read++; > + __atomic_store_n(&rwl->readwrite, t.readwrite, > __ATOMIC_RELEASE); } > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice. > + * > + * > + * Take a read lock. Loop until the lock is held. > + * > + * @param l Nit, 'rwl'. > + * A pointer to a rwticketlock structure. > + */ > +__rte_experimental > +static inline void > +rte_rwticket_read_lock(rte_rwticketlock_t *rwl) { > + uint16_t me; > + > + me =3D __atomic_fetch_add(&rwl->next, 1, __ATOMIC_RELAXED); > + rte_wait_until_equal_16(&rwl->read, me, __ATOMIC_ACQUIRE); > + __atomic_fetch_add(&rwl->read, 1, __ATOMIC_RELAXED); } > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice. > + * > + * Try to take a read lock. > + * > + * @param rwl > + * A pointer to a rwticketlock structure. > + * > + * @return > + * - zero if the lock is successfully taken > + * - -EBUSY if lock could not be acquired for reading because a > + * writer holds the lock > + */ > +__rte_experimental > +static inline int > +rte_rwticket_read_trylock(rte_rwticketlock_t *rwl) { > + rte_rwticketlock_t old, new; > + int success; > + > + old.tickets =3D __atomic_load_n(&rwl->tickets, __ATOMIC_RELAXED); > + > + do { > + uint16_t me =3D old.next; /* this is our ticket */ When __atomic_compare_exchange_n fails, old.tickets needs a reload. =20 > + > + /* does writer have the lock now? */ > + if (old.read !=3D me && old.write !=3D me) Check (old.read !=3D me) should be enough? > + return -EBUSY; > + > + /* expect to be the next reader */ > + new.tickets =3D old.tickets; > + old.read =3D me; This line is unnecessary? > + new.read =3D new.next =3D me + 1; > + success =3D __atomic_compare_exchange_n(&rwl->tickets, > &old.tickets, new.tickets, > + 0, __ATOMIC_ACQUIRE, > __ATOMIC_RELAXED); > + } while (!success); > + > + return 0; > +} > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice. > + * > + * Release a read lock. > + * > + * @param rwl > + * A pointer to the rwticketlock structure. > + */ > +__rte_experimental > +static inline void > +rte_rwticket_read_unlock(rte_rwticketlock_t *rwl) { > + __atomic_add_fetch(&rwl->write, 1, __ATOMIC_RELEASE); } > + > +#ifdef __cplusplus > +} > +#endif > + > +#endif /* _RTE_TICKET_RWLOCK_H_ */ > diff --git a/lib/librte_eal/include/meson.build > b/lib/librte_eal/include/meson.build > index 0dea342e1deb..fe5c19748926 100644 > --- a/lib/librte_eal/include/meson.build > +++ b/lib/librte_eal/include/meson.build > @@ -65,6 +65,7 @@ generic_headers =3D files( > 'generic/rte_rwlock.h', > 'generic/rte_spinlock.h', > 'generic/rte_ticketlock.h', > + 'generic/rte_ticket_rwlock.h', > 'generic/rte_vect.h', > ) > install_headers(generic_headers, subdir: 'generic') diff --git > a/lib/librte_eal/ppc/include/meson.build > b/lib/librte_eal/ppc/include/meson.build > index dae40ede546e..0bc560327749 100644 > --- a/lib/librte_eal/ppc/include/meson.build > +++ b/lib/librte_eal/ppc/include/meson.build > @@ -16,6 +16,7 @@ arch_headers =3D files( > 'rte_rwlock.h', > 'rte_spinlock.h', > 'rte_ticketlock.h', > + 'rte_ticket_rwlock.h', > 'rte_vect.h', > ) > install_headers(arch_headers, subdir: get_option('include_subdir_arch')) > diff --git a/lib/librte_eal/ppc/include/rte_ticket_rwlock.h > b/lib/librte_eal/ppc/include/rte_ticket_rwlock.h > new file mode 100644 > index 000000000000..4768d5bfa8ef > --- /dev/null > +++ b/lib/librte_eal/ppc/include/rte_ticket_rwlock.h > @@ -0,0 +1,18 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2021 Microsoft Corporation */ > + > +#ifndef _RTE_FAIR_RWLOCK_PPC_64_H_ > +#define _RTE_FAIR_RWLOCK_PPC_64_H_ > + > +#ifdef __cplusplus > +extern "C" { > +#endif > + > +#include "generic/rte_ticket_rwlock.h" > + > +#ifdef __cplusplus > +} > +#endif > + > +#endif /* _RTE_FAIR_RWLOCK_PPC_64_H_ */ > diff --git a/lib/librte_eal/x86/include/meson.build > b/lib/librte_eal/x86/include/meson.build > index 549cc21a42ed..e9169f0d1da5 100644 > --- a/lib/librte_eal/x86/include/meson.build > +++ b/lib/librte_eal/x86/include/meson.build > @@ -20,6 +20,7 @@ arch_headers =3D files( > 'rte_rwlock.h', > 'rte_spinlock.h', > 'rte_ticketlock.h', > + 'rte_ticket_rwlock.h', > 'rte_vect.h', > ) > install_headers(arch_headers, subdir: get_option('include_subdir_arch')) > diff --git a/lib/librte_eal/x86/include/rte_ticket_rwlock.h > b/lib/librte_eal/x86/include/rte_ticket_rwlock.h > new file mode 100644 > index 000000000000..83c8bd0899d3 > --- /dev/null > +++ b/lib/librte_eal/x86/include/rte_ticket_rwlock.h > @@ -0,0 +1,18 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2021 Microsoft Corporation */ > + > +#ifndef _RTE_FAIR_RWLOCK_X86_64_H_ > +#define _RTE_FAIR_RWLOCK_X86_64_H_ > + > +#ifdef __cplusplus > +extern "C" { > +#endif > + > +#include "generic/rte_ticket_rwlock.h" > + > +#ifdef __cplusplus > +} > +#endif > + > +#endif /* _RTE_FAIR_RWLOCK_X86_64_H_ */ > -- > 2.29.2