Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F165082694
D7192.id18492.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
D7192.id18492.diff
View Options
Index: sys/kern/kern_mutex.c
===================================================================
--- sys/kern/kern_mutex.c
+++ sys/kern/kern_mutex.c
@@ -281,6 +281,34 @@
WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
}
+int
+__mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
+ int line)
+{
+ struct mtx *m;
+
+ if (SCHEDULER_STOPPED())
+ return (1);
+
+ m = mtxlock2mtx(c);
+
+ KASSERT(m->mtx_lock != MTX_DESTROYED,
+ ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
+ KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
+ ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
+ m->lock_object.lo_name, file, line));
+ KASSERT((opts & MTX_RECURSE) == 0,
+ ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
+ m->lock_object.lo_name, file, line));
+ if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
+ LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
+ WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
+ return (1);
+ }
+ LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
+ return (0);
+}
+
void
__mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
int line)
Index: sys/sys/mutex.h
===================================================================
--- sys/sys/mutex.h
+++ sys/sys/mutex.h
@@ -112,6 +112,8 @@
int line);
void __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
int line);
+int __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts,
+ const char *file, int line);
void __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts,
const char *file, int line);
#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
@@ -152,6 +154,8 @@
__mtx_unlock_flags(&(m)->mtx_lock, o, f, l)
#define _mtx_lock_spin_flags(m, o, f, l) \
__mtx_lock_spin_flags(&(m)->mtx_lock, o, f, l)
+#define _mtx_trylock_spin_flags(m, o, f, l) \
+ __mtx_trylock_spin_flags(&(m)->mtx_lock, o, f, l)
#define _mtx_unlock_spin_flags(m, o, f, l) \
__mtx_unlock_spin_flags(&(m)->mtx_lock, o, f, l)
#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
@@ -212,6 +216,21 @@
LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, \
mp, 0, 0, file, line); \
} while (0)
+#define __mtx_trylock_spin(mp, tid, opts, file, line) __extension__ ({ \
+ uintptr_t _tid = (uintptr_t)(tid); \
+ int _ret; \
+ \
+ spinlock_enter(); \
+ if (((mp)->mtx_lock != MTX_UNOWNED || !_mtx_obtain_lock((mp), _tid))) {\
+ spinlock_exit(); \
+ _ret = 0; \
+ } else { \
+ LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, \
+ mp, 0, 0, file, line); \
+ _ret = 1; \
+ } \
+ _ret; \
+})
#else /* SMP */
#define __mtx_lock_spin(mp, tid, opts, file, line) do { \
uintptr_t _tid = (uintptr_t)(tid); \
@@ -224,6 +243,20 @@
(mp)->mtx_lock = _tid; \
} \
} while (0)
+#define __mtx_trylock_spin(mp, tid, opts, file, line) __extension__ ({ \
+ uintptr_t _tid = (uintptr_t)(tid); \
+ int _ret; \
+ \
+ spinlock_enter(); \
+ if ((mp)->mtx_lock != MTX_UNOWNED) { \
+ spinlock_exit(); \
+ _ret = 0; \
+ } else { \
+ (mp)->mtx_lock = _tid; \
+ _ret = 1; \
+ } \
+ _ret; \
+})
#endif /* SMP */
/* Unlock a normal mutex. */
@@ -284,6 +317,10 @@
* With these routines, it is possible to pass flags such as MTX_QUIET
* to the appropriate lock manipulation routines.
*
+ * mtx_trylock_spin(m) attempts to acquire MTX_SPIN mutex `m' but doesn't
+ * spin if it cannot. Rather, it returns 0 on failure and non-zero on
+ * success. It always returns failure for recursed lock attempts.
+ *
* mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if
* it cannot. Rather, it returns 0 on failure and non-zero on success.
* It does NOT handle recursion as we assume that if a caller is properly
@@ -302,6 +339,7 @@
#define mtx_lock(m) mtx_lock_flags((m), 0)
#define mtx_lock_spin(m) mtx_lock_spin_flags((m), 0)
#define mtx_trylock(m) mtx_trylock_flags((m), 0)
+#define mtx_trylock_spin(m) mtx_trylock_spin_flags((m), 0)
#define mtx_unlock(m) mtx_unlock_flags((m), 0)
#define mtx_unlock_spin(m) mtx_unlock_spin_flags((m), 0)
@@ -335,6 +373,8 @@
_mtx_unlock_flags((m), (opts), (file), (line))
#define mtx_lock_spin_flags_(m, opts, file, line) \
_mtx_lock_spin_flags((m), (opts), (file), (line))
+#define mtx_trylock_spin_flags_(m, opts, file, line) \
+ _mtx_trylock_spin_flags((m), (opts), (file), (line))
#define mtx_unlock_spin_flags_(m, opts, file, line) \
_mtx_unlock_spin_flags((m), (opts), (file), (line))
#else /* LOCK_DEBUG == 0 && !MUTEX_NOINLINE */
@@ -344,6 +384,8 @@
__mtx_unlock((m), curthread, (opts), (file), (line))
#define mtx_lock_spin_flags_(m, opts, file, line) \
__mtx_lock_spin((m), curthread, (opts), (file), (line))
+#define mtx_trylock_spin_flags_(m, opts, file, line) \
+ __mtx_trylock_spin((m), curthread, (opts), (file), (line))
#define mtx_unlock_spin_flags_(m, opts, file, line) \
__mtx_unlock_spin((m))
#endif /* LOCK_DEBUG > 0 || MUTEX_NOINLINE */
@@ -369,6 +411,8 @@
mtx_unlock_spin_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
#define mtx_trylock_flags(m, opts) \
mtx_trylock_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
+#define mtx_trylock_spin_flags(m, opts) \
+ mtx_trylock_spin_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
#define mtx_assert(m, what) \
mtx_assert_((m), (what), __FILE__, __LINE__)
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Aug 6, 9:19 PM (11 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
36134069
Default Alt Text
D7192.id18492.diff (5 KB)
Attached To
Mode
D7192: Implement mtx_trylock_spin(9).
Attached
Detach File
Event Timeline
Log In to Comment