1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2015 Intel
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg * IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#ifndef _SIMPLE_MTX_H
25b8e80941Smrg#define _SIMPLE_MTX_H
26b8e80941Smrg
27b8e80941Smrg#include "util/futex.h"
28b8e80941Smrg
29b8e80941Smrg#include "c11/threads.h"
30b8e80941Smrg
31b8e80941Smrg#if defined(__GNUC__) && defined(HAVE_LINUX_FUTEX_H)
32b8e80941Smrg
33b8e80941Smrg/* mtx_t - Fast, simple mutex
34b8e80941Smrg *
35b8e80941Smrg * While modern pthread mutexes are very fast (implemented using futex), they
36b8e80941Smrg * still incur a call to an external DSO and overhead of the generality and
37b8e80941Smrg * features of pthread mutexes.  Most mutexes in mesa only needs lock/unlock,
38b8e80941Smrg * and the idea here is that we can inline the atomic operation and make the
39b8e80941Smrg * fast case just two intructions.  Mutexes are subtle and finicky to
40b8e80941Smrg * implement, so we carefully copy the implementation from Ulrich Dreppers
41b8e80941Smrg * well-written and well-reviewed paper:
42b8e80941Smrg *
43b8e80941Smrg *   "Futexes Are Tricky"
44b8e80941Smrg *   http://www.akkadia.org/drepper/futex.pdf
45b8e80941Smrg *
46b8e80941Smrg * We implement "mutex3", which gives us a mutex that has no syscalls on
47b8e80941Smrg * uncontended lock or unlock.  Further, the uncontended case boils down to a
48b8e80941Smrg * locked cmpxchg and an untaken branch, the uncontended unlock is just a
49b8e80941Smrg * locked decr and an untaken branch.  We use __builtin_expect() to indicate
50b8e80941Smrg * that contention is unlikely so that gcc will put the contention code out of
51b8e80941Smrg * the main code flow.
52b8e80941Smrg *
53b8e80941Smrg * A fast mutex only supports lock/unlock, can't be recursive or used with
54b8e80941Smrg * condition variables.
55b8e80941Smrg */
56b8e80941Smrg
57b8e80941Smrgtypedef struct {
58b8e80941Smrg   uint32_t val;
59b8e80941Smrg} simple_mtx_t;
60b8e80941Smrg
61b8e80941Smrg#define _SIMPLE_MTX_INITIALIZER_NP { 0 }
62b8e80941Smrg
63b8e80941Smrgstatic inline void
64b8e80941Smrgsimple_mtx_init(simple_mtx_t *mtx, MAYBE_UNUSED int type)
65b8e80941Smrg{
66b8e80941Smrg   assert(type == mtx_plain);
67b8e80941Smrg
68b8e80941Smrg   mtx->val = 0;
69b8e80941Smrg}
70b8e80941Smrg
71b8e80941Smrgstatic inline void
72b8e80941Smrgsimple_mtx_destroy(UNUSED simple_mtx_t *mtx)
73b8e80941Smrg{
74b8e80941Smrg}
75b8e80941Smrg
76b8e80941Smrgstatic inline void
77b8e80941Smrgsimple_mtx_lock(simple_mtx_t *mtx)
78b8e80941Smrg{
79b8e80941Smrg   uint32_t c;
80b8e80941Smrg
81b8e80941Smrg   c = __sync_val_compare_and_swap(&mtx->val, 0, 1);
82b8e80941Smrg   if (__builtin_expect(c != 0, 0)) {
83b8e80941Smrg      if (c != 2)
84b8e80941Smrg         c = __sync_lock_test_and_set(&mtx->val, 2);
85b8e80941Smrg      while (c != 0) {
86b8e80941Smrg         futex_wait(&mtx->val, 2, NULL);
87b8e80941Smrg         c = __sync_lock_test_and_set(&mtx->val, 2);
88b8e80941Smrg      }
89b8e80941Smrg   }
90b8e80941Smrg}
91b8e80941Smrg
92b8e80941Smrgstatic inline void
93b8e80941Smrgsimple_mtx_unlock(simple_mtx_t *mtx)
94b8e80941Smrg{
95b8e80941Smrg   uint32_t c;
96b8e80941Smrg
97b8e80941Smrg   c = __sync_fetch_and_sub(&mtx->val, 1);
98b8e80941Smrg   if (__builtin_expect(c != 1, 0)) {
99b8e80941Smrg      mtx->val = 0;
100b8e80941Smrg      futex_wake(&mtx->val, 1);
101b8e80941Smrg   }
102b8e80941Smrg}
103b8e80941Smrg
104b8e80941Smrg#else
105b8e80941Smrg
106b8e80941Smrgtypedef mtx_t simple_mtx_t;
107b8e80941Smrg
108b8e80941Smrg#define _SIMPLE_MTX_INITIALIZER_NP _MTX_INITIALIZER_NP
109b8e80941Smrg
110b8e80941Smrgstatic inline void
111b8e80941Smrgsimple_mtx_init(simple_mtx_t *mtx, int type)
112b8e80941Smrg{
113b8e80941Smrg   mtx_init(mtx, type);
114b8e80941Smrg}
115b8e80941Smrg
116b8e80941Smrgstatic inline void
117b8e80941Smrgsimple_mtx_destroy(simple_mtx_t *mtx)
118b8e80941Smrg{
119b8e80941Smrg   mtx_destroy(mtx);
120b8e80941Smrg}
121b8e80941Smrg
122b8e80941Smrgstatic inline void
123b8e80941Smrgsimple_mtx_lock(simple_mtx_t *mtx)
124b8e80941Smrg{
125b8e80941Smrg   mtx_lock(mtx);
126b8e80941Smrg}
127b8e80941Smrg
128b8e80941Smrgstatic inline void
129b8e80941Smrgsimple_mtx_unlock(simple_mtx_t *mtx)
130b8e80941Smrg{
131b8e80941Smrg   mtx_unlock(mtx);
132b8e80941Smrg}
133b8e80941Smrg
134b8e80941Smrg#endif
135b8e80941Smrg
136b8e80941Smrg#endif
137