From: Eric Dumazet <eric.dumazet@gmail.com>
To: "David S. Miller" <davem@davemloft.net>, Ingo Molnar <mingo@elte.hu>
Cc: Linux Netdev List <netdev@vger.kernel.org>,
linux kernel <linux-kernel@vger.kernel.org>
Subject: [RFC,PATCH] mutex: mutex_is_owner() helper
Date: Wed, 04 Nov 2009 16:25:58 +0100 [thread overview]
Message-ID: <4AF19D06.3060401@gmail.com> (raw)
mutex_is_locked() is called most of the time to check if mutex is locked by current
thread. But it's a lazy check, because mutex might be locked by another thread.
Adds a new mutex_is_owned_by() helper, that can check ownership if CONFIG_SMP or
CONFIG_DEBUG_MUTEXES are set.
Returns are
0 if mutex is unlocked.
1 if locked
-1 if not locked by designated thread.
Last return value is possible only if CONFIG_SMP=y or CONFIG_DEBUG_MUTEXES=y
Example of use :
int rtnl_is_locked(void)
{
return mutex_is_locked(&rtnl_mutex);
}
->
int rtnl_is_locked(void)
{
return mutex_is_owned_by(&rtnl_mutex, current_thread_info()) == 1;
}
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
---
Documentation/mutex-design.txt | 1 +
include/linux/mutex.h | 20 ++++++++++++++++++++
2 files changed, 21 insertions(+)
diff --git a/Documentation/mutex-design.txt b/Documentation/mutex-design.txt
index aa60d1f..521607c 100644
--- a/Documentation/mutex-design.txt
+++ b/Documentation/mutex-design.txt
@@ -133,6 +133,7 @@ the APIs of 'struct mutex' have been streamlined:
int mutex_trylock(struct mutex *lock);
void mutex_unlock(struct mutex *lock);
int mutex_is_locked(struct mutex *lock);
+ int mutex_is_owned_by(struct mutex *lock, struct thread_info *ti);
void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
int mutex_lock_interruptible_nested(struct mutex *lock,
unsigned int subclass);
diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index 878cab4..95a8c5b 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -118,6 +118,26 @@ static inline int mutex_is_locked(struct mutex *lock)
return atomic_read(&lock->count) != 1;
}
+/**
+ * mutex_is_owned_by - check mutex ownership
+ * @lock: the mutex to be queried
+ * @ti: thread_info pointer
+ *
+ * Returns: 0 if mutex is unlocked.
+ * 1 if locked
+ * -1 if not locked by designated thread.
+ */
+static inline int mutex_is_owned_by(struct mutex *lock, struct thread_info *ti)
+{
+ if (atomic_read(&lock->count) == 1)
+ return 0;
+#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
+ if (lock->owner != ti)
+ return -1;
+#endif
+ return 1;
+}
+
/*
* See kernel/mutex.c for detailed documentation of these APIs.
* Also see Documentation/mutex-design.txt.
next reply other threads:[~2009-11-04 15:26 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-04 15:25 Eric Dumazet [this message]
2009-11-04 15:40 ` [RFC,PATCH] mutex: mutex_is_owner() helper Ingo Molnar
2009-11-04 17:19 ` Eric Dumazet
2009-11-09 18:56 ` Peter Zijlstra
2009-11-09 23:21 ` Eric Dumazet
2009-11-10 9:41 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4AF19D06.3060401@gmail.com \
--to=eric.dumazet@gmail.com \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.