Debian Codegraph @

doc/ck_rwcohort

raw
1 .\"
2 .\" Copyright 2013 Brendon Scheinman.
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 April 23, 2013.
28 .Dt ck_rwcohort 3
29 .Sh NAME
30 .Nm ck_rwcohort
31 .Nd generalized interface for reader-writer locks using cohort locks
32 .Sh LIBRARY
33 Concurrency Kit (libck, \-lck)
34 .Sh SYNOPSIS
35 .In ck_rwcohort.h
36 In each of the following macros, "STRATEGY" should be replaced with either "NEUTRAL", "RP", or "WP"
37 depending on which locking strategy the user prefers. RP and WP represent reader preference and
38 writer preference, respectively, while NEUTRAL represents a strategy neutral to reads vs. writes.
39 .Fn CK_RWCOHORT_STRATEGY_PROTOTYPE "COHORT_NAME cohort_name"
40 .Fn CK_RWCOHORT_STRATEGY_NAME "COHORT_NAME cohort_name"
41 .Fn CK_RWCOHORT_STRATEGY_INSTANCE "COHORT_NAME cohort_name"
42 .Fn CK_RWCOHORT_STRATEGY_INIT "COHORT_NAME cohort_name" "RWCOHORT lock" "unsigned int wait_limit"
43 Note: the wait_limit argument should be omitted for locks using the neutral strategy
44 .Fn CK_RWCOHORT_STRATEGY_READ_LOCK "COHORT_NAME cohort_name" "RWCOHORT lock" "COHORT cohort" \
45 "void *global_context" "void *local_context"
46 .Fn CK_RWCOHORT_STRATEGY_READ_UNLOCK "COHORT_NAME cohort_name" "RWCOHORT lock" "COHORT cohort" \
47 "void *global_context" "void *local_context"
48 .Fn CK_RWCOHORT_STRATEGY_WRITE_LOCK "COHORT_NAME cohort_name" "RWCOHORT lock" "COHORT cohort" \
49 "void *global_context" "void *local_context"
50 .Fn CK_RWCOHORT_STRATEGY_WRITE_UNLOCK "COHORT_NAME cohort_name" "RWCOHORT lock" "COHORT cohort" \
51 "void *global_context" "void *local_context"
52 .Pp
53 Arguments of type RWCOHORT must be pointers to structs defined using the
54 .Xr CK_RWCOHORT_STRATEGY_PROTOTYPE 3
55 macro with the same strategy and cohort name as the current call.
56 .Pp
57 Arguments of type COHORT must be pointers to structs defined using the
58 .Xr CK_COHORT_PROTOTYPE 3
59 macro.
60 .Sh DESCRIPTION
61 ck_rwcohort.h provides an interface for defining reader-writer locks
62 that use cohort locks internally to increase performance on NUMA
63 architectures. See
64 .Xr ck_cohort 3
65 for more information about cohort locks.
66 .Pp
67 Before using a reader-writer cohort lock, the user must define a cohort type using
68 either the
69 .Xr CK_COHORT_PROTOTYPE 3
70 or the
71 .Xr CK_COHORT_TRYLOCK_PROTOTYPE 3
72 macros, and define a reader-writer lock type using the
73 .Xr CK_RWCOHORT_PROTOTYPE 3
74 macro.
75 .Pp
76 .Sh EXAMPLE
77 .Bd -literal -offset indent
78 #include <stdlib.h>
79 #include <pthread.h>
80
81 #include <ck_pr.h>
82 #include <ck_cohort.h>
83 #include <ck_rwcohort.h>
84 #include <ck_spinlock.h>
85
86 /* Create cohort methods with signatures that match the required signature */
87
88 static void
89 ck_spinlock_lock_with_context(ck_spinlock_t *lock, void *context)
90 {
91 (void)context;
92 ck_spinlock_lock(lock);
93 return;
94 }
95
96 static void
97 ck_spinlock_unlock_with_context(ck_spinlock_t *lock, void *context)
98 {
99 (void)context;
100 ck_spinlock_unlock(lock);
101 return;
102 }
103
104 static bool
105 ck_spinlock_locked_with_context(ck_spinlock_t *lock, void *context)
106 {
107 (void)context;
108 return ck_spinlock_locked(lock);
109 }
110
111 /*
112 * define a cohort type named "test_cohort" that will use
113 * the above methods for both its global and local locks
114 */
115 CK_COHORT_PROTOTYPE(test_cohort,
116 ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context, ck_spinlock_locked_with_context,
117 ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context, ck_spinlock_locked_with_context)
118
119 /* define a reader-writer type using the same cohort type */
120 CK_RWCOHORT_WP_PROTOTYPE(test_cohort)
121
122 static ck_spinlock_t global_lock = CK_SPINLOCK_INITIALIZER;
123 static CK_COHORT_INSTANCE(test_cohort) *cohorts;
124 static CK_RWCOHORT_WP_INSTANCE(test_cohort) rw_cohort = CK_RWCOHORT_WP_INITIALIZER;
125 static unsigned int ready;
126
127 static void *
128 function(void *context)
129 {
130 CK_COHORT_INSTANCE(test_cohort) *cohort = context;
131
132 while (ck_pr_load_uint(&ready) == 0);
133
134 while (ck_pr_load_uint(&ready) > 0) {
135 /*
136 * acquire the cohort lock before performing critical section.
137 * note that we pass NULL for both the global and local context
138 * arguments because neither the lock nor unlock functions
139 * will use them.
140 */
141 CK_COHORT_LOCK(test_cohort, cohort, NULL, NULL);
142
143 /* perform critical section */
144
145 /* relinquish cohort lock */
146 CK_COHORT_UNLOCK(test_cohort, cohort, NULL, NULL);
147 }
148
149 return NULL;
150 }
151
152 int
153 main(void)
154 {
155 unsigned int nthr = 4;
156 unsigned int n_cohorts = 2;
157 unsigned int i;
158
159 /* allocate 2 cohorts of the defined type */
160 CK_COHORT_INSTANCE(test_cohort) *cohorts =
161 calloc(n_cohorts, sizeof(CK_COHORT_INSTANCE(test_cohort)));
162
163 /* create local locks to use with each cohort */
164 ck_spinlock_t *local_locks =
165 calloc(n_cohorts, sizeof(ck_spinlock_t));
166
167 pthread_t *threads =
168 calloc(nthr, sizeof(pthread_t));
169
170 /* initialize each of the cohorts before using them */
171 for (i = 0 ; i < n_cohorts ; ++i) {
172 CK_COHORT_INIT(test_cohort, cohorts + i, &global_lock, local_locks + i,
173 CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT);
174 }
175
176 /* start each thread and assign cohorts equally */
177 for (i = 0 ; i < nthr ; ++i) {
178 pthread_create(threads + i, NULL, function, cohorts + (i % n_cohorts));
179 }
180
181 ck_pr_store_uint(&ready, 1);
182 sleep(10);
183 ck_pr_store_uint(&ready, 0);
184
185 for (i = 0 ; i < nthr ; ++i) {
186 pthread_join(threads[i], NULL);
187 }
188
189 return 0;
190 }
191 .Ed
192 .Sh SEE ALSO
193 .Xr CK_COHORT_PROTOTYPE 3 ,
194 .Xr CK_COHORT_TRYLOCK_PROTOTYPE 3 ,
195 .Xr CK_COHORT_INSTANCE 3 ,
196 .Xr CK_COHORT_INITIALIZER 3 ,
197 .Xr CK_COHORT_INIT 3 ,
198 .Xr CK_COHORT_LOCK 3 ,
199 .Xr CK_COHORT_UNLOCK 3 ,
200 .Xr CK_COHORT_LOCKED 3 ,
201 .Xr CK_COHORT_TRYLOCK 3 ,
202 .Pp
203 Additional information available at http://concurrencykit.org/