[Mesa-dev] [PATCH v3] mesa: Add new fast mtx_t mutex type for basic use cases

Emil Velikov emil.l.velikov at gmail.com
Mon Oct 16 12:36:03 UTC 2017


On 16 October 2017 at 08:06, Timothy Arceri <tarceri at itsqueeze.com> wrote:
> While modern pthread mutexes are very fast, they still incur a call to an
> external DSO and overhead of the generality and features of pthread mutexes.
> Most mutexes in mesa only needs lock/unlock, and the idea here is that we can
> inline the atomic operation and make the fast case just two intructions.
> Mutexes are subtle and finicky to implement, so we carefully copy the
> implementation from Ulrich Dreppers well-written and well-reviewed paper:
>
>   "Futexes Are Tricky"
>   http://www.akkadia.org/drepper/futex.pdf
>
> We implement "mutex3", which gives us a mutex that has no syscalls on
> uncontended lock or unlock.  Further, the uncontended case boils down to a
> cmpxchg and an untaken branch and the uncontended unlock is just a locked decr
> and an untaken branch.  We use __builtin_expect() to indicate that contention
> is unlikely so that gcc will put the contention code out of the main code
> flow.
>
Worth using likely/unlikely macros from src/util/macros.h?

>  configure.ac                     |   3 +
>  src/intel/vulkan/anv_allocator.c |  24 +-----
>  src/util/Makefile.sources        |   1 +
>  src/util/meson.build             |   1 +
>  src/util/simple_mtx.h            | 153 +++++++++++++++++++++++++++++++++++++++
>  5 files changed, 159 insertions(+), 23 deletions(-)
>  create mode 100644 src/util/simple_mtx.h
>
> diff --git a/configure.ac b/configure.ac
> index 62d33a1941c..c113e714638 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -894,6 +894,9 @@ if test "x$pthread_stubs_possible" = xyes; then
>      PKG_CHECK_MODULES(PTHREADSTUBS, pthread-stubs >= 0.4)
>  fi
>
> +dnl Check for futex for fast inline simple_mtx_t.
> +AC_CHECK_HEADER([linux/futex.h], [DEFINES="$DEFINES -DHAVE_FUTEX"])
> +
Dylan you might want a similar check for Meson?


> +static inline int futex_wake(uint32_t *addr, int count) {
> +   return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
> +}
> +
> +static inline int futex_wait(uint32_t *addr, int32_t value) {
> +   return sys_futex(addr, FUTEX_WAIT, value, NULL, NULL, 0);
> +}
Coding style: please keep { on the next line.

-Emil


More information about the mesa-dev mailing list