From patchwork Fri Jul 12 23:50:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 1131546 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="ipUmj73s"; 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 45lqhf117pz9s8m for ; Sat, 13 Jul 2019 10:01:37 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 9E1FD5E16; Sat, 13 Jul 2019 00:01:34 +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 470E25C12 for ; Fri, 12 Jul 2019 23:51:31 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 33343CF for ; Fri, 12 Jul 2019 23:51:30 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id y8so5498498plr.12 for ; Fri, 12 Jul 2019 16:51:30 -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=T+E/59g+cGEGjQ8j7OoG8gf2Tmoo/56Qsgfh4omj/xY=; b=ipUmj73sJwMopkMsjXP1rdwNy9skArjlhSFcyoKdW5iTIthDcgwX2ewyN/WJX61iby ykNjtCaGZp9l1oshXEYkRUbAdFwpzewqIF8hbau5I9Ff+cRVDjqbwKCEhswHxcF3Rgyw ZHaK4bBGflPTR3WedThohP0BKJuwSlJIuLWi0Re1dJ9xHQw69XM4HuTmNAdPWLHD961j emSc+dOkJ4l0r1NI3TkKgz61mnygYWJn8C9BUbuyVFj30JOk9TCw9/0ThKnEkPg5aIUN POwgDSHy+I0xGc9FL7NaKCiccquy1okQt6oIm9wwfySt0p7vL+j3PedfwtydnCHEnfWJ AB4w== 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=T+E/59g+cGEGjQ8j7OoG8gf2Tmoo/56Qsgfh4omj/xY=; b=QMi1ktNfgccZMdq/aOHnG9lpW2bWiQ3stDh8QYVAyMfwaI+jqiadnbkEIw4uVL0hta g6UtJhRXP6fmOUCSHr54tFvwMnkM055YrIOEEnkzVf167HOGAv0jBp6i7113zVvHL/fs /dWzbqEakRsDnzSSM/LOZGkLEmIqsBmu52II1Bo4Q2nqVUv/Li5T3pSiLM0iLjfcRU3m /C4rHxEKPapMaXEaetjz04iunJwOOh6QEV/4ts+7ZDNuugudqVSofoFDX5609pPQ6q8D WodobNbPJU6WvNRUS7oVv072LfC2TI7zMlNEYop/mw2oOkGf+l4DSlOjeRLEt5X3t6wO EQqQ== X-Gm-Message-State: APjAAAWr64fz4OgCSbcrIQ049yuFECun6Zww/pnATQJAYfU3oqbefaZN Fwvq+e/GtF3xNFP6Rq0YrkC2T3pG X-Google-Smtp-Source: APXvYqwqZltYGXFGsCEf2K9XF3ddBIx7HVIFEj84pYMmBe+SbR3nyJdt4u/ykQKr5lp0GjpdrUMvwA== X-Received: by 2002:a17:902:7d86:: with SMTP id a6mr14793516plm.199.1562975489523; Fri, 12 Jul 2019 16:51:29 -0700 (PDT) Received: from sc9-mailhost2.vmware.com ([66.170.99.2]) by smtp.gmail.com with ESMTPSA id d23sm7909738pjv.18.2019.07.12.16.51.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 12 Jul 2019 16:51:28 -0700 (PDT) From: William Tu To: dev@openvswitch.org Date: Fri, 12 Jul 2019 16:50:55 -0700 Message-Id: <1562975456-97888-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] [PATCHv16 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 --- include/openvswitch/thread.h | 22 ++++++++++++++++++++++ lib/ovs-thread.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/openvswitch/thread.h b/include/openvswitch/thread.h index 2987db37c9dc..14cc9ad73900 100644 --- a/include/openvswitch/thread.h +++ b/include/openvswitch/thread.h @@ -33,6 +33,13 @@ struct OVS_LOCKABLE ovs_mutex { const char *where; /* NULL if and only if uninitialized. */ }; +#ifdef __linux__ +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 +77,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 __linux__ +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..29a0b9e57acd 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 __linux__ +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 __linux__ +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 __linux__ +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 __linux__ +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_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