From patchwork Wed Jul 17 20:23:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 1133414 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="TdjCGGZg"; dkim-atps=neutral Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45ppfV13jPz9s8m for ; Thu, 18 Jul 2019 06:25:06 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id EEB3BEC3; Wed, 17 Jul 2019 20:25:02 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 7F017E7C for ; Wed, 17 Jul 2019 20:25:01 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 46479879 for ; Wed, 17 Jul 2019 20:24:59 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id o13so11685819pgp.12 for ; Wed, 17 Jul 2019 13:24:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=L1EwVb7IbIh+7cWBkLSlOjR9w1oPIeVCmDAVVljJel8=; b=TdjCGGZgq4oYvNem4FcMlJ+BENXr4mXp4PqwebbAbsn/XpHSgPvaq7eO1LbpW4hNGu zykYXfOUk7DkqLB6RwP7u8xaGcyms6++9FiKGEH0q+DGKTS2o1yP+Nsr6OLYPF+PiSB5 Ov9HO/gEi+Yg94RydugJUzCVwngmV8ZGAstI2e0SZsV1N8w3wCuKgGy2Ix6BafSe1y2q HZDPj6JE+IjLy+Ah0oQSU7r9PQfBl2E83YSG/KP4nvHWwJ2Kq7uM98Efn+2eJ+0ba2jU 3DF/GPpJKRg0lO+M35rsssmDmURzJ2OXSUSDtawQDasdKDmAx/D2RFjkbGVUAPL/alzM r+qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=L1EwVb7IbIh+7cWBkLSlOjR9w1oPIeVCmDAVVljJel8=; b=jeUQBiEbJgJMrBypuGWO0s6wZyX8casf6QHDhXlzb7wQjHKNxp16XUpyPwDSva6IMp V+AB96cEAflWlTcvvkdP+Vrt8FlZbudHlfFgSyxZB76PhqlxUmFof8gfLMZOQpcIday+ RAdgA4Lm6oxOqBenQDVOH6jN/TKzG+TGRS039KIkQEj4bdSFT4wG3IaGBAMY3SjwWA3m 78lh+VsU1qGgWJEMd8zY6j2SV2+XtB7cNaF56sAiqojOPgXEN/nUw5j0KE9hgiU8fUna FcSCnnJ8gYmb3T9/Qo5HDPhrQfY7YUxXpEP0CWiQ7pQeWYCOLFpTigSBVVZNxr5MD2TY dNcw== X-Gm-Message-State: APjAAAWKjmyIQjvqbPSCLq4+lXNwBzy1mH+OvtD4C7p3tcTEWUgxeXg1 IyWU2UBM4PzqJ7rL8xsk722SQYkG X-Google-Smtp-Source: APXvYqx8vq/bOFjR5YQvJEM88aYqxc4WkkUzojdslIdYqtpZtLOTFJVuBcfr4Txx44GGLusHb4IIRQ== X-Received: by 2002:a63:b20f:: with SMTP id x15mr44685810pge.453.1563395098399; Wed, 17 Jul 2019 13:24:58 -0700 (PDT) Received: from sc9-mailhost3.vmware.com ([66.170.99.2]) by smtp.gmail.com with ESMTPSA id a21sm29476002pfi.27.2019.07.17.13.24.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 17 Jul 2019 13:24:57 -0700 (PDT) From: William Tu To: dev@openvswitch.org Date: Wed, 17 Jul 2019 13:23:33 -0700 Message-Id: <1563395014-41764-1-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE autolearn=no version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: i.maximets@samsung.com Subject: [ovs-dev] [PATCHv17 1/2] ovs-thread: Add pthread spin lock support. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org The patch adds the basic spin lock functions: ovs_spin_{lock, try_lock, unlock, init, destroy}. OSX does not support pthread spin lock, so make it linux only. Signed-off-by: William Tu Acked-by: Ilya Maximets Acked-by: Ben Pfaff --- configure.ac | 1 + include/openvswitch/thread.h | 24 ++++++++++++++++++++++++ lib/ovs-thread.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/configure.ac b/configure.ac index a9f0a06dc140..dd2a674af0c9 100644 --- a/configure.ac +++ b/configure.ac @@ -108,6 +108,7 @@ AC_CHECK_MEMBERS([struct sockaddr_in6.sin6_scope_id], [], [], #include #include ]]) AC_CHECK_FUNCS([mlockall strnlen getloadavg statvfs getmntent_r sendmmsg clock_gettime]) +AC_CHECK_FUNCS([pthread_spin_lock]) AC_CHECK_HEADERS([mntent.h sys/statvfs.h linux/types.h linux/if_ether.h]) AC_CHECK_HEADERS([linux/net_namespace.h stdatomic.h bits/floatn-common.h]) AC_CHECK_HEADERS([net/if_mib.h], [], [], [[#include diff --git a/include/openvswitch/thread.h b/include/openvswitch/thread.h index 2987db37c9dc..b07b7167b303 100644 --- a/include/openvswitch/thread.h +++ b/include/openvswitch/thread.h @@ -17,6 +17,8 @@ #ifndef OPENVSWITCH_THREAD_H #define OPENVSWITCH_THREAD_H 1 +#include + #include #include #include @@ -33,6 +35,13 @@ struct OVS_LOCKABLE ovs_mutex { const char *where; /* NULL if and only if uninitialized. */ }; +#ifdef HAVE_PTHREAD_SPIN_LOCK +struct OVS_LOCKABLE ovs_spin { + pthread_spinlock_t lock; + const char *where; /* NULL if and only if uninitialized. */ +}; +#endif + /* "struct ovs_mutex" initializer. */ #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP #define OVS_MUTEX_INITIALIZER { PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, \ @@ -70,6 +79,21 @@ int ovs_mutex_trylock_at(const struct ovs_mutex *mutex, const char *where) void ovs_mutex_cond_wait(pthread_cond_t *, const struct ovs_mutex *mutex) OVS_REQUIRES(mutex); + +#ifdef HAVE_PTHREAD_SPIN_LOCK +void ovs_spin_init(const struct ovs_spin *); +void ovs_spin_destroy(const struct ovs_spin *); +void ovs_spin_unlock(const struct ovs_spin *spin) OVS_RELEASES(spin); +void ovs_spin_lock_at(const struct ovs_spin *spin, const char *where) + OVS_ACQUIRES(spin); +#define ovs_spin_lock(spin) \ + ovs_spin_lock_at(spin, OVS_SOURCE_LOCATOR) + +int ovs_spin_trylock_at(const struct ovs_spin *spin, const char *where) + OVS_TRY_LOCK(0, spin); +#define ovs_spin_trylock(spin) \ + ovs_spin_trylock_at(spin, OVS_SOURCE_LOCATOR) +#endif /* Convenient once-only execution. * diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c index 159d87e5b0ca..b686e4548127 100644 --- a/lib/ovs-thread.c +++ b/lib/ovs-thread.c @@ -75,6 +75,9 @@ static bool multithreaded; LOCK_FUNCTION(mutex, lock); LOCK_FUNCTION(rwlock, rdlock); LOCK_FUNCTION(rwlock, wrlock); +#ifdef HAVE_PTHREAD_SPIN_LOCK +LOCK_FUNCTION(spin, lock); +#endif #define TRY_LOCK_FUNCTION(TYPE, FUN) \ int \ @@ -103,6 +106,9 @@ LOCK_FUNCTION(rwlock, wrlock); TRY_LOCK_FUNCTION(mutex, trylock); TRY_LOCK_FUNCTION(rwlock, tryrdlock); TRY_LOCK_FUNCTION(rwlock, trywrlock); +#ifdef HAVE_PTHREAD_SPIN_LOCK +TRY_LOCK_FUNCTION(spin, trylock); +#endif #define UNLOCK_FUNCTION(TYPE, FUN, WHERE) \ void \ @@ -125,6 +131,10 @@ UNLOCK_FUNCTION(mutex, unlock, ""); UNLOCK_FUNCTION(mutex, destroy, NULL); UNLOCK_FUNCTION(rwlock, unlock, ""); UNLOCK_FUNCTION(rwlock, destroy, NULL); +#ifdef HAVE_PTHREAD_SPIN_LOCK +UNLOCK_FUNCTION(spin, unlock, ""); +UNLOCK_FUNCTION(spin, destroy, NULL); +#endif #define XPTHREAD_FUNC1(FUNCTION, PARAM1) \ void \ @@ -268,6 +278,27 @@ ovs_mutex_cond_wait(pthread_cond_t *cond, const struct ovs_mutex *mutex_) } } +#ifdef HAVE_PTHREAD_SPIN_LOCK +static void +ovs_spin_init__(const struct ovs_spin *l_, int pshared) +{ + struct ovs_spin *l = CONST_CAST(struct ovs_spin *, l_); + int error; + + l->where = ""; + error = pthread_spin_init(&l->lock, pshared); + if (OVS_UNLIKELY(error)) { + ovs_abort(error, "pthread_spin_init failed"); + } +} + +void +ovs_spin_init(const struct ovs_spin *spin) +{ + ovs_spin_init__(spin, PTHREAD_PROCESS_PRIVATE); +} +#endif + /* Initializes the 'barrier'. 'size' is the number of threads * expected to hit the barrier. */ void