| 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 February 24, 2013. |
| 28 |
.Dt ck_cohort 3 |
| 29 |
.Sh NAME |
| 30 |
.Nm ck_cohort |
| 31 |
.Nd generalized interface for lock cohorts |
| 32 |
.Sh LIBRARY |
| 33 |
Concurrency Kit (libck, \-lck) |
| 34 |
.Sh SYNOPSIS |
| 35 |
.In ck_cohort.h |
| 36 |
.Fn CK_COHORT_PROTOTYPE "COHORT_NAME cohort_name" "LOCK_FXN global_lock_method" \ |
| 37 |
"LOCK_FXN global_unlock_method" "LOCK_FXN local_lock_method" "LOCK_FXN local_unlock_method" |
| 38 |
.Fn CK_COHORT_TRYLOCK_PROTOTYPE "COHORT_NAME cohort_name" \ |
| 39 |
"LOCK_FXN global_lock_method" "LOCK_FXN global_unlock_method" \ |
| 40 |
"BOOL_LOCK_FXN global_locked_method" "BOOL_LOCK_FXN global_trylock_method" \ |
| 41 |
"LOCK_FXN local_lock_method" "LOCK_FXN local_unlock_method" \ |
| 42 |
"BOOL_LOCK_FXN local_locked_method" "BOOL_LOCK_FXN local_trylock_method" |
| 43 |
.Fn CK_COHORT_INSTANCE "COHORT_NAME cohort_name" |
| 44 |
.Fn CK_COHORT_INIT "COHORT_NAME cohort_name" "ck_cohort *cohort" \ |
| 45 |
"void *global_lock" "void *local_lock" "unsigned int pass_limit" |
| 46 |
.Fn CK_COHORT_LOCK "COHORT_NAME cohort_name" "ck_cohort *cohort" \ |
| 47 |
"void *global_context" "void *local_context" |
| 48 |
.Fn CK_COHORT_UNLOCK "COHORT_NAME cohort_name" "ck_cohort *cohort" \ |
| 49 |
"void *global_context" "void *local_context" |
| 50 |
.Pp |
| 51 |
Where LOCK_FXN refers to a method with the signature |
| 52 |
.br |
| 53 |
void(void *lock, void *context) |
| 54 |
.br |
| 55 |
BOOL_LOCK_FXN refers to a method with the signature |
| 56 |
.br |
| 57 |
bool(void *lock, void *context) |
| 58 |
.Pp |
| 59 |
The |
| 60 |
.Fa context |
| 61 |
argument in each signature is used to pass along any additional information that |
| 62 |
the lock might need for its lock, unlock and trylock methods. The values for this |
| 63 |
argument are provided to each call to |
| 64 |
.Xr CK_COHORT_LOCK 3 , |
| 65 |
.Xr CK_COHORT_UNLOCK 3 , |
| 66 |
.Xr CK_COHORT_LOCKED 3 , |
| 67 |
and |
| 68 |
.Xr CK_COHORT_TRYLOCK 3 |
| 69 |
. |
| 70 |
.Sh DESCRIPTION |
| 71 |
ck_cohort.h provides an interface for defining lock cohorts with |
| 72 |
arbitrary lock types. Cohorts are a mechanism for coordinating |
| 73 |
threads on NUMA architectures in order to reduce the frequency |
| 74 |
with which a lock is passed between threads on different clusters. |
| 75 |
.Pp |
| 76 |
Before using a cohort, the user must define a cohort type using |
| 77 |
either the |
| 78 |
.Fn CK_COHORT_PROTOTYPE |
| 79 |
or the |
| 80 |
.Fn CK_COHORT_TRYLOCK_PROTOTYPE |
| 81 |
macros. These macros allow the user to specify the lock methods that |
| 82 |
they would like the cohort to use. See the |
| 83 |
.Xr CK_COHORT_PROTOTYPE 3 |
| 84 |
and |
| 85 |
.Xr CK_COHORT_TRYLOCK_PROTOTYPE 3 |
| 86 |
man pages for more details. |
| 87 |
.Pp |
| 88 |
.Sh EXAMPLE |
| 89 |
.Bd -literal -offset indent |
| 90 |
#include <stdlib.h> |
| 91 |
#include <pthread.h> |
| 92 |
|
| 93 |
#include <ck_pr.h> |
| 94 |
#include <ck_cohort.h> |
| 95 |
#include <ck_spinlock.h> |
| 96 |
|
| 97 |
/* |
| 98 |
* Create cohort methods with signatures that match |
| 99 |
* the required signature |
| 100 |
*/ |
| 101 |
static void |
| 102 |
ck_spinlock_lock_with_context(ck_spinlock_t *lock, void *context) |
| 103 |
{ |
| 104 |
(void)context; |
| 105 |
ck_spinlock_lock(lock); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
ck_spinlock_unlock_with_context(ck_spinlock_t *lock, void *context) |
| 111 |
{ |
| 112 |
(void)context; |
| 113 |
ck_spinlock_unlock(lock); |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
static bool |
| 118 |
ck_spinlock_locked_with_context(ck_spinlock_t *lock, void *context) |
| 119 |
{ |
| 120 |
(void)context; |
| 121 |
return ck_spinlock_locked(lock); |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* define a cohort type named "test_cohort" that will use |
| 126 |
* the above methods for both its global and local locks |
| 127 |
*/ |
| 128 |
CK_COHORT_PROTOTYPE(test_cohort, |
| 129 |
ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context, ck_spinlock_locked_with_context, |
| 130 |
ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context, ck_spinlock_locked_with_context) |
| 131 |
|
| 132 |
static ck_spinlock_t global_lock = CK_SPINLOCK_INITIALIZER; |
| 133 |
static unsigned int ready; |
| 134 |
|
| 135 |
static void * |
| 136 |
function(void *context) |
| 137 |
{ |
| 138 |
CK_COHORT_INSTANCE(test_cohort) *cohort = context; |
| 139 |
|
| 140 |
while (ready == 0); |
| 141 |
|
| 142 |
while (ready > 0) { |
| 143 |
/* |
| 144 |
* acquire the cohort lock before performing critical section. |
| 145 |
* note that we pass NULL for both the global and local context |
| 146 |
* arguments because neither the lock nor unlock functions |
| 147 |
* will use them. |
| 148 |
*/ |
| 149 |
CK_COHORT_LOCK(test_cohort, cohort, NULL, NULL); |
| 150 |
|
| 151 |
/* perform critical section */ |
| 152 |
|
| 153 |
/* relinquish cohort lock */ |
| 154 |
CK_COHORT_UNLOCK(test_cohort, cohort, NULL, NULL); |
| 155 |
} |
| 156 |
|
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
int |
| 161 |
main(void) |
| 162 |
{ |
| 163 |
unsigned int nthr = 4; |
| 164 |
unsigned int n_cohorts = 2; |
| 165 |
unsigned int i; |
| 166 |
|
| 167 |
/* allocate 2 cohorts of the defined type */ |
| 168 |
CK_COHORT_INSTANCE(test_cohort) *cohorts = |
| 169 |
calloc(n_cohorts, sizeof(CK_COHORT_INSTANCE(test_cohort))); |
| 170 |
|
| 171 |
/* create local locks to use with each cohort */ |
| 172 |
ck_spinlock_t *local_locks = |
| 173 |
calloc(n_cohorts, sizeof(ck_spinlock_t)); |
| 174 |
|
| 175 |
pthread_t *threads = |
| 176 |
calloc(nthr, sizeof(pthread_t)); |
| 177 |
|
| 178 |
/* initialize each of the cohorts before using them */ |
| 179 |
for (i = 0 ; i < n_cohorts ; ++i) { |
| 180 |
CK_COHORT_INIT(test_cohort, cohorts + i, &global_lock, local_locks + i, |
| 181 |
CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT); |
| 182 |
} |
| 183 |
|
| 184 |
/* start each thread and assign cohorts equally */ |
| 185 |
for (i = 0 ; i < nthr ; ++i) { |
| 186 |
pthread_create(threads + i, NULL, function, cohorts + (i % n_cohorts)); |
| 187 |
} |
| 188 |
|
| 189 |
ck_pr_store_uint(&ready, 1); |
| 190 |
sleep(10); |
| 191 |
ck_pr_store_uint(&ready, 0); |
| 192 |
|
| 193 |
for (i = 0 ; i < nthr ; ++i) { |
| 194 |
pthread_join(threads[i], NULL); |
| 195 |
} |
| 196 |
|
| 197 |
return 0; |
| 198 |
} |
| 199 |
.Ed |
| 200 |
.Sh SEE ALSO |
| 201 |
.Xr CK_COHORT_PROTOTYPE 3 , |
| 202 |
.Xr CK_COHORT_TRYLOCK_PROTOTYPE 3 , |
| 203 |
.Xr CK_COHORT_INSTANCE 3 , |
| 204 |
.Xr CK_COHORT_INITIALIZER 3 , |
| 205 |
.Xr CK_COHORT_INIT 3 , |
| 206 |
.Xr CK_COHORT_LOCK 3 , |
| 207 |
.Xr CK_COHORT_UNLOCK 3 , |
| 208 |
.Xr CK_COHORT_LOCKED 3 , |
| 209 |
.Xr CK_COHORT_TRYLOCK 3 , |
| 210 |
.Pp |
| 211 |
Additional information available at http://concurrencykit.org/ |