From nobody Thu Oct 24 17:14:36 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XZCFn0Cj7z5ZgSf; Thu, 24 Oct 2024 17:14:37 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XZCFm6q0vz4c2L; Thu, 24 Oct 2024 17:14:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729790077; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6nnrBTRqajwjutm5d+ElfU/6eInmwRV7RiGyZMLI098=; b=fgocgEqHjan9zghhJr3tf5auehtWYDkcDIQZZwE/uLo2FaaPSCepmTus/wDkQWVMJlvFhf epvvBAW43AlyESlV67th2gp/d4MTt1PZ7AMGasTs7bgW/J6pMx8JWXddgC3o+D3CjbPaLz tJF1G+xexE21jdaMSP0YjdtRAmue3HQofUyejt9AZDOarxtGKDfenfTN7pnwNTE1KIZ/3l CSE0vBp+MvT6Cz3I8r3JckYyhFJnDUJZWztTCzPlzm9e3U1EsBvUSjf7S28olba+JdMIbL WgXX+Mm/FYlqNojPX1TWrZBZMrpZos6f5YbFjFoWi4V4Y3qfKqGroZVP8xWBlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729790077; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6nnrBTRqajwjutm5d+ElfU/6eInmwRV7RiGyZMLI098=; b=K5x9druCFC1vh6KQoLEFP/8eJpbEHzLNrUww1U7wXr2DhoimsSWy3VxKaYCySc0LolsmhF IXyoSmNo5YOpbRsNupgYs9uWge4R0Ac+PXz1nN9d8halVaUvnua2p4LqjIrRSWtqPdBrEa BphHTgmCknFXJ/FMW9WzUNpVaunRxLJnTRRXmMCkKcAL0s5LEVBmbsLYusAGkDGC4FjYRm LQqQhVdws/IKEzfr2EaPQDcS0T0nvJh1s3VhLPg/eJrGESmHBqphrqHQxbEAgozy8aLOpi 0OCUYWqXqweHjCR9QiC+urVRknQr+ivt04oM2/glGNsvIqiEPvEnTeMhZbkX+Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729790077; a=rsa-sha256; cv=none; b=cCJmH/ec6670Cy2X42jsJd+M5Y9FM9OgDamjpU25bpwcwYBdiKHzbJLr0J4Fx1MG+Xe8oh DbqEDjiEsHj9nYgzphmEjZ2SeUb5QH+ItE+uU2HEyjVYa+a+MMNsO6tquHf2WWIXp6jteT 2rmFkB80tImR2TYaoEjScZF6x08aSRS4MwB01MWHj+3bVFzy59lU6JnspfQw/SMR9Aoq/2 DRo32y+2H4j7St5o8OYbGRaKYKwO1oSh1NTn7OH/FlbDXAxD60YCXW39G1ARj2G7KGnDJP iz/fLSDsQkmgVWTYLmgdBRYtC1QyYqRegErQIzMrj7g7An3MBn7hqP2hOVXt8w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XZCFm6PwFzr3g; Thu, 24 Oct 2024 17:14:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49OHEaVZ000280; Thu, 24 Oct 2024 17:14:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49OHEaiP000277; Thu, 24 Oct 2024 17:14:36 GMT (envelope-from git) Date: Thu, 24 Oct 2024 17:14:36 GMT Message-Id: <202410241714.49OHEaiP000277@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: 656991b0c629 - main - locks: augment lock_class with lc_trylock method List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 656991b0c629038beddf1847a6c20318d3ac4181 Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=656991b0c629038beddf1847a6c20318d3ac4181 commit 656991b0c629038beddf1847a6c20318d3ac4181 Author: Gleb Smirnoff AuthorDate: 2024-10-24 16:57:57 +0000 Commit: Gleb Smirnoff CommitDate: 2024-10-24 17:14:03 +0000 locks: augment lock_class with lc_trylock method Implement for mutex(9) and rwlock(9). Reviewed by: jtl Differential Revision: https://reviews.freebsd.org/D45745 --- sys/kern/kern_mutex.c | 18 ++++++++++++++++++ sys/kern/kern_rwlock.c | 14 ++++++++++++++ sys/sys/lock.h | 1 + 3 files changed, 33 insertions(+) diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index 0fa624cc4bb1..fa043fa7e124 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -100,6 +100,8 @@ static void db_show_mtx(const struct lock_object *lock); #endif static void lock_mtx(struct lock_object *lock, uintptr_t how); static void lock_spin(struct lock_object *lock, uintptr_t how); +static int trylock_mtx(struct lock_object *lock, uintptr_t how); +static int trylock_spin(struct lock_object *lock, uintptr_t how); #ifdef KDTRACE_HOOKS static int owner_mtx(const struct lock_object *lock, struct thread **owner); @@ -118,6 +120,7 @@ struct lock_class lock_class_mtx_sleep = { .lc_ddb_show = db_show_mtx, #endif .lc_lock = lock_mtx, + .lc_trylock = trylock_mtx, .lc_unlock = unlock_mtx, #ifdef KDTRACE_HOOKS .lc_owner = owner_mtx, @@ -131,6 +134,7 @@ struct lock_class lock_class_mtx_spin = { .lc_ddb_show = db_show_mtx, #endif .lc_lock = lock_spin, + .lc_trylock = trylock_spin, .lc_unlock = unlock_spin, #ifdef KDTRACE_HOOKS .lc_owner = owner_mtx, @@ -216,6 +220,20 @@ lock_spin(struct lock_object *lock, uintptr_t how) mtx_lock_spin((struct mtx *)lock); } +static int +trylock_mtx(struct lock_object *lock, uintptr_t how) +{ + + return (mtx_trylock((struct mtx *)lock)); +} + +static int +trylock_spin(struct lock_object *lock, uintptr_t how) +{ + + return (mtx_trylock_spin((struct mtx *)lock)); +} + static uintptr_t unlock_mtx(struct lock_object *lock) { diff --git a/sys/kern/kern_rwlock.c b/sys/kern/kern_rwlock.c index 31ff8a7213fd..c7e377c8f77a 100644 --- a/sys/kern/kern_rwlock.c +++ b/sys/kern/kern_rwlock.c @@ -72,6 +72,7 @@ static void db_show_rwlock(const struct lock_object *lock); #endif static void assert_rw(const struct lock_object *lock, int what); static void lock_rw(struct lock_object *lock, uintptr_t how); +static int trylock_rw(struct lock_object *lock, uintptr_t how); #ifdef KDTRACE_HOOKS static int owner_rw(const struct lock_object *lock, struct thread **owner); #endif @@ -85,6 +86,7 @@ struct lock_class lock_class_rw = { .lc_ddb_show = db_show_rwlock, #endif .lc_lock = lock_rw, + .lc_trylock = trylock_rw, .lc_unlock = unlock_rw, #ifdef KDTRACE_HOOKS .lc_owner = owner_rw, @@ -176,6 +178,18 @@ lock_rw(struct lock_object *lock, uintptr_t how) rw_wlock(rw); } +static int +trylock_rw(struct lock_object *lock, uintptr_t how) +{ + struct rwlock *rw; + + rw = (struct rwlock *)lock; + if (how) + return (rw_try_rlock(rw)); + else + return (rw_try_wlock(rw)); +} + static uintptr_t unlock_rw(struct lock_object *lock) { diff --git a/sys/sys/lock.h b/sys/sys/lock.h index 65064aad5aa8..9d81a49ab52a 100644 --- a/sys/sys/lock.h +++ b/sys/sys/lock.h @@ -66,6 +66,7 @@ struct lock_class { int (*lc_owner)(const struct lock_object *lock, struct thread **owner); uintptr_t (*lc_unlock)(struct lock_object *lock); + int (*lc_trylock)(struct lock_object *lock, uintptr_t how); }; #define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */