git: 656991b0c629 - main - locks: augment lock_class with lc_trylock method
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 24 Oct 2024 17:14:36 UTC
The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=656991b0c629038beddf1847a6c20318d3ac4181 commit 656991b0c629038beddf1847a6c20318d3ac4181 Author: Gleb Smirnoff <glebius@FreeBSD.org> AuthorDate: 2024-10-24 16:57:57 +0000 Commit: Gleb Smirnoff <glebius@FreeBSD.org> 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. */