Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 31 Dec 2017 00:33:28 +0000 (UTC)
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r327394 - head/sys/kern
Message-ID:  <201712310033.vBV0XS7Y021984@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mjg
Date: Sun Dec 31 00:33:28 2017
New Revision: 327394
URL: https://svnweb.freebsd.org/changeset/base/327394

Log:
  mtx: pre-read the lock value in thread_lock_flags_
  
  Since this function is effectively slow path, if we get here the lock is most
  likely already taken in which case it is cheaper to not blindly attempt the
  atomic op.
  
  While here move hwpmc probe out of the loop to match other primitives.

Modified:
  head/sys/kern/kern_mutex.c

Modified: head/sys/kern/kern_mutex.c
==============================================================================
--- head/sys/kern/kern_mutex.c	Sun Dec 31 00:31:14 2017	(r327393)
+++ head/sys/kern/kern_mutex.c	Sun Dec 31 00:33:28 2017	(r327394)
@@ -899,6 +899,10 @@ thread_lock_flags_(struct thread *td, int opts, const 
 
 	lock_delay_arg_init(&lda, &mtx_spin_delay);
 
+#ifdef HWPMC_HOOKS
+	PMC_SOFT_CALL( , , lock, failed);
+#endif
+
 #ifdef LOCK_PROFILING
 	doing_lockprof = 1;
 #elif defined(KDTRACE_HOOKS)
@@ -908,22 +912,20 @@ thread_lock_flags_(struct thread *td, int opts, const 
 #endif
 	for (;;) {
 retry:
-		v = MTX_UNOWNED;
 		spinlock_enter();
 		m = td->td_lock;
 		thread_lock_validate(m, opts, file, line);
+		v = MTX_READ_VALUE(m);
 		for (;;) {
-			if (_mtx_obtain_lock_fetch(m, &v, tid))
-				break;
-			if (v == MTX_UNOWNED)
+			if (v == MTX_UNOWNED) {
+				if (_mtx_obtain_lock_fetch(m, &v, tid))
+					break;
 				continue;
+			}
 			if (v == tid) {
 				m->mtx_recurse++;
 				break;
 			}
-#ifdef HWPMC_HOOKS
-			PMC_SOFT_CALL( , , lock, failed);
-#endif
 			lock_profile_obtain_lock_failed(&m->lock_object,
 			    &contested, &waittime);
 			/* Give interrupts a chance while we spin. */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201712310033.vBV0XS7Y021984>