Debian Codegraph @

doc/ck_elide

raw
1 .\"
2 .\" Copyright 2013 Samy Al Bahra.
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\"
14 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 .\" SUCH DAMAGE.
25 .\"
26 .\"
27 .Dd July 13, 2013.
28 .Dt ck_elide 3
29 .Sh NAME
30 .Nm CK_ELIDE_PROTOTYPE ,
31 .Nm CK_ELIDE_LOCK_ADAPTIVE ,
32 .Nm CK_ELIDE_UNLOCK_ADAPTIVE ,
33 .Nm CK_ELIDE_LOCK ,
34 .Nm CK_ELIDE_UNLOCK ,
35 .Nm CK_ELIDE_TRYLOCK_PROTOTYPE ,
36 .Nm CK_ELIDE_TRYLOCK
37 .Nd lock elision wrappers
38 .Sh LIBRARY
39 Concurrency Kit (libck, \-lck)
40 .Sh SYNOPSIS
41 .In ck_elide.h
42 .Pp
43 .Dv ck_elide_stat_t stat = CK_ELIDE_STAT_INITIALIZER;
44 .Pp
45 .Ft void
46 .Fn ck_elide_stat_init "ck_elide_stat_t *"
47 .Pp
48 .Dv struct ck_elide_config config = CK_ELIDE_CONFIG_DEFAULT_INITIALIZER;
49 .Pp
50 .Bd -literal -offset
51 struct ck_elide_config {
52 unsigned short skip_busy;
53 short retry_busy;
54 unsigned short skip_other;
55 short retry_other;
56 unsigned short skip_conflict;
57 short retry_conflict;
58 };
59 .Ed
60 .Pp
61 .Fn CK_ELIDE_PROTOTYPE "NAME" "TYPE" "LOCK_PREDICATE" "LOCK_FUNCTION" "UNLOCK_PREDICATE" "UNLOCK_FUNCTION"
62 .Fn CK_ELIDE_LOCK_ADAPTIVE "NAME" "ck_elide_stat_t *" "struct ck_elide_config *" "TYPE *"
63 .Fn CK_ELIDE_UNLOCK_ADAPTIVE "NAME" "ck_elide_stat_t *" "TYPE *"
64 .Fn CK_ELIDE_LOCK "NAME" "TYPE *"
65 .Fn CK_ELIDE_UNLOCK "NAME" "TYPE *"
66 .Fn CK_ELIDE_TRYLOCK_PROTOTYPE "NAME" "TYPE" "LOCK_PREDICATE" "TRYLOCK_FUNCTION"
67 .Sh DESCRIPTION
68 These macros implement lock elision wrappers for a user-specified single-argument
69 lock interface. The wrappers will attempt to elide lock acquisition, allowing
70 concurrent execution of critical sections that do not issue conflicting memory
71 operations. If any threads have successfully elided a lock acquisition,
72 conflicting memory operations will roll-back any side-effects of the critical
73 section and force every thread to retry the lock acquisition regularly.
74 .Pp
75 .Fn CK_ELIDE_LOCK ,
76 .Fn CK_ELIDE_UNLOCK ,
77 .Fn CK_ELIDE_LOCK_ADAPTIVE ,
78 and
79 .Fn CK_ELIDE_UNLOCK_ADAPTIVE
80 macros require
81 a previous
82 .Fn CK_ELIDE_PROTOTYPE
83 with the same
84 .Fa NAME .
85 Elision is attempted if the
86 .Fa LOCK_PREDICATE
87 function returns false. If
88 .Fa LOCK_PREDICATE
89 returns true then elision is aborted and
90 .Fa LOCK_FUNCTION
91 is executed instead. If any threads are in an elided critical section,
92 .Fa LOCK_FUNCTION
93 must force them to rollback through a conflicting memory operation.
94 The
95 .Fa UNLOCK_PREDICATE
96 function must return true if the lock is acquired by the caller, meaning
97 that the lock was not successfully elided. If
98 .Fa UNLOCK_PREDICATE
99 returns true, then the
100 .Fa UNLOCK_FUNCTION
101 is executed. If RTM is unsupported (no CK_F_PR_RTM macro) then
102 .Fn CK_ELIDE_LOCK
103 and
104 .Fn CK_ELIDE_LOCK_ADAPTIVE
105 will immediately call
106 .Fn LOCK_FUNCTION .
107 .Fn CK_ELIDE_UNLOCK
108 and
109 .Fn CK_ELIDE_UNLOCK_ADAPTIVE
110 will immediately call
111 .Fn UNLOCK_FUNCTION .
112 .Pp
113 .Fn CK_ELIDE_TRYLOCK
114 requires a previous
115 .Fn CK_ELIDE_TRYLOCK_PROTOTYPE
116 with the same name.
117 Elision is attempted if the
118 .Fa LOCK_PREDICATE
119 function returns false. If
120 .Fa LOCK_PREDICATE
121 returns true or if elision fails then the
122 operation is aborted. If RTM is unsupported
123 (no CK_F_PR_RTM macro) then
124 .Fn CK_ELIDE_TRYLOCK
125 will immediately call
126 .Fn TRYLOCK_FUNCTION .
127 .Pp
128 .Fn CK_ELIDE_LOCK_ADAPTIVE
129 and
130 .Fn CK_ELIDE_UNLOCK_ADAPTIVE
131 will adapt the elision behavior associated with lock operations
132 according to the run-time behavior of the program. This behavior
133 is defined by the ck_elide_config structure pointer passed to
134 .Fn CK_ELIDE_LOCK_ADAPTIVE .
135 A thread-local ck_elide_stat structure must be passed to both
136 .Fn CK_ELIDE_LOCK_ADAPTIVE
137 and
138 .Fn CK_ELIDE_UNLOCK_ADAPTIVE .
139 This structure is expected to be unique for different workloads,
140 may not be re-used in recursive acquisitions and must match the
141 lifetime of the lock it is associated with. It is safe to mix
142 adaptive calls with best-effort calls.
143 .Pp
144 Both ck_spinlock.h and ck_rwlock.h define ck_elide wrappers under
145 the ck_spinlock and ck_rwlock namespace, respectively.
146 .Sh EXAMPLES
147 This example utilizes built-in lock elision facilities in ck_rwlock and ck_spinlock.
148 .Bd -literal -offset indent
149 #include <ck_rwlock.h>
150 #include <ck_spinlock.h>
151
152 static ck_rwlock_t rw = CK_RWLOCK_INITIALIZER;
153 static struct ck_elide_config rw_config =
154 CK_ELIDE_CONFIG_DEFAULT_INITIALIZER;
155 static __thread ck_elide_stat_t rw_stat =
156 CK_ELIDE_STAT_INITIALIZER;
157
158 static ck_spinlock_t spinlock = CK_SPINLOCK_INITIALIZER;
159 static struct ck_elide_config spinlock_config =
160 CK_ELIDE_CONFIG_DEFAULT_INITIALIZER;
161 static __thread ck_elide_stat_t spinlock_stat =
162 CK_ELIDE_STAT_INITIALIZER;
163
164 void
165 function(void)
166 {
167
168 /* Lock-unlock write-side lock in weak best-effort manner. */
169 CK_ELIDE_LOCK(ck_rwlock_write, &rw);
170 CK_ELIDE_UNLOCK(ck_rwlock_write, &rw);
171
172 /* Attempt to acquire the write-side lock. */
173 if (CK_ELIDE_TRYLOCK(ck_rwlock_write, &rw) == true)
174 CK_ELIDE_UNLOCK(ck_rwlock_write, &rw);
175
176 /* Lock-unlock read-side lock in weak best-effort manner. */
177 CK_ELIDE_LOCK(ck_rwlock_read, &rw);
178 CK_ELIDE_UNLOCK(ck_rwlock_read, &rw);
179
180 /* Attempt to acquire the read-side lock. */
181 if (CK_ELIDE_TRYLOCK(ck_rwlock_read, &rw) == true)
182 CK_ELIDE_UNLOCK(ck_rwlock_read, &rw);
183
184 /* Lock-unlock write-side lock in an adaptive manner. */
185 CK_ELIDE_LOCK_ADAPTIVE(ck_rwlock_write, &rw_stat,
186 &rw_config, &rw);
187 CK_ELIDE_UNLOCK_ADAPTIVE(ck_rwlock_write, &rw_stat,
188 &rw_config, &rw);
189
190 /* Lock-unlock read-side lock in an adaptive manner. */
191 CK_ELIDE_LOCK_ADAPTIVE(ck_rwlock_read, &rw_stat,
192 &rw_config, &rw);
193 CK_ELIDE_UNLOCK_ADAPTIVE(ck_rwlock_read, &rw_stat,
194 &rw_config, &rw);
195
196 /* Lock-unlock spinlock in weak best-effort manner. */
197 CK_ELIDE_LOCK(ck_spinlock, &spinlock);
198 CK_ELIDE_UNLOCK(ck_spinlock, &spinlock);
199
200 /* Attempt to acquire the lock. */
201 if (CK_ELIDE_TRYLOCK(ck_spinlock, &lock) == true)
202 CK_ELIDE_UNLOCK(ck_spinlock, &spinlock);
203
204 /* Lock-unlock spinlock in an adaptive manner. */
205 CK_ELIDE_LOCK_ADAPTIVE(ck_spinlock, &spinlock_stat,
206 &spinlock_config, &spinlock);
207 CK_ELIDE_UNLOCK_ADAPTIVE(ck_spinlock, &spinlock_stat,
208 &spinlock_config, &spinlock);
209 }
210 .Ed
211 .Pp
212 In this example, user-defined locking functions are provided an elision
213 implementation.
214 .Bd -literal -offset indent
215 /* Assume lock_t has been previously defined. */
216 #include <ck_elide.h>
217
218 /*
219 * This function returns true if the lock is unavailable at the time
220 * it was called or false if the lock is available.
221 */
222 bool is_locked(lock_t *);
223
224 /*
225 * This function acquires the supplied lock.
226 */
227 void lock(lock_t *);
228
229 /*
230 * This function releases the lock.
231 */
232 void unlock(lock_t *);
233
234 CK_ELIDE_PROTOTYPE(my_lock, lock_t, is_locked, lock, is_locked, unlock)
235
236 static lock_t lock;
237
238 void
239 function(void)
240 {
241
242 CK_ELIDE_LOCK(my_lock, &lock);
243 CK_ELIDE_UNLOCK(my_lock, &lock);
244 }
245 .Ed
246 .Sh SEE ALSO
247 .Xr ck_rwlock 3 ,
248 .Xr ck_spinlock 3
249 .Pp
250 Ravi Rajwar and James R. Goodman. 2001. Speculative lock elision: enabling highly concurrent multithreaded execution. In Proceedings of the 34th annual ACM/IEEE international symposium on Microarchitecture (MICRO 34). IEEE Computer Society, Washington, DC, USA, 294-305.
251 .Pp
252 Additional information available at http://en.wikipedia.org/wiki/Transactional_Synchronization_Extensions and http://concurrencykit.org/