summaryrefslogtreecommitdiffstats
path: root/doc/ck_rwcohort
blob: ba2b5f9f57028a69b5a5cf104b52b9f2a64d6f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
.\"
.\" Copyright 2013 Brendon Scheinman.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\"
.Dd April 23, 2013.
.Dt ck_rwcohort 3
.Sh NAME
.Nm ck_rwcohort
.Nd generalized interface for reader-writer locks using cohort locks
.Sh LIBRARY
Concurrency Kit (libck, \-lck)
.Sh SYNOPSIS
.In ck_rwcohort.h
In each of the following macros, "STRATEGY" should be replaced with either "NEUTRAL", "RP", or "WP"
depending on which locking strategy the user prefers.  RP and WP represent reader preference and
writer preference, respectively, while NEUTRAL represents a strategy neutral to reads vs. writes.
.Fn CK_RWCOHORT_STRATEGY_PROTOTYPE "COHORT_NAME cohort_name"
.Fn CK_RWCOHORT_STRATEGY_NAME "COHORT_NAME cohort_name"
.Fn CK_RWCOHORT_STRATEGY_INSTANCE "COHORT_NAME cohort_name"
.Fn CK_RWCOHORT_STRATEGY_INIT "COHORT_NAME cohort_name" "RWCOHORT lock" "unsigned int wait_limit"
Note: the wait_limit argument should be omitted for locks using the neutral strategy
.Fn CK_RWCOHORT_STRATEGY_READ_LOCK "COHORT_NAME cohort_name" "RWCOHORT lock" "COHORT cohort" \
"void *global_context" "void *local_context"
.Fn CK_RWCOHORT_STRATEGY_READ_UNLOCK "COHORT_NAME cohort_name" "RWCOHORT lock" "COHORT cohort" \
"void *global_context" "void *local_context"
.Fn CK_RWCOHORT_STRATEGY_WRITE_LOCK "COHORT_NAME cohort_name" "RWCOHORT lock" "COHORT cohort" \
"void *global_context" "void *local_context"
.Fn CK_RWCOHORT_STRATEGY_WRITE_UNLOCK "COHORT_NAME cohort_name" "RWCOHORT lock" "COHORT cohort" \
"void *global_context" "void *local_context"
.Pp
Arguments of type RWCOHORT must be pointers to structs defined using the
.Xr CK_RWCOHORT_STRATEGY_PROTOTYPE 3
macro with the same strategy and cohort name as the current call.
.Pp
Arguments of type COHORT must be pointers to structs defined using the
.Xr CK_COHORT_PROTOTYPE 3
macro.
.Sh DESCRIPTION
ck_rwcohort.h provides an interface for defining reader-writer locks
that use cohort locks internally to increase performance on NUMA
architectures.  See
.Xr ck_cohort 3
for more information about cohort locks.
.Pp
Before using a reader-writer cohort lock, the user must define a cohort type using
either the
.Xr CK_COHORT_PROTOTYPE 3
or the
.Xr CK_COHORT_TRYLOCK_PROTOTYPE 3
macros, and define a reader-writer lock type using the
.Xr CK_RWCOHORT_PROTOTYPE 3
macro.
.Pp
.Sh EXAMPLE
.Bd -literal -offset indent
#include <stdlib.h>
#include <pthread.h>

#include <ck_pr.h>
#include <ck_cohort.h>
#include <ck_rwcohort.h>
#include <ck_spinlock.h>

/* Create cohort methods with signatures that match the required signature */

static void
ck_spinlock_lock_with_context(ck_spinlock_t *lock, void *context)
{
	(void)context;
	ck_spinlock_lock(lock);
	return;
}

static void
ck_spinlock_unlock_with_context(ck_spinlock_t *lock, void *context)
{
	(void)context;
	ck_spinlock_unlock(lock);
	return;
}

static bool
ck_spinlock_locked_with_context(ck_spinlock_t *lock, void *context)
{
	(void)context;
	return ck_spinlock_locked(lock);
}

/*
 * define a cohort type named "test_cohort" that will use
 * the above methods for both its global and local locks
 */
CK_COHORT_PROTOTYPE(test_cohort,
	ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context, ck_spinlock_locked_with_context,
	ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context, ck_spinlock_locked_with_context)

/* define a reader-writer type using the same cohort type */
CK_RWCOHORT_WP_PROTOTYPE(test_cohort)

static ck_spinlock_t global_lock = CK_SPINLOCK_INITIALIZER;
static CK_COHORT_INSTANCE(test_cohort) *cohorts;
static CK_RWCOHORT_WP_INSTANCE(test_cohort) rw_cohort = CK_RWCOHORT_WP_INITIALIZER;
static unsigned int ready;

static void *
function(void *context)
{
	CK_COHORT_INSTANCE(test_cohort) *cohort = context;

	while (ck_pr_load_uint(&ready) == 0);

	while (ck_pr_load_uint(&ready) > 0) {
		/*
		 * acquire the cohort lock before performing critical section.
		 * note that we pass NULL for both the global and local context
		 * arguments because neither the lock nor unlock functions
		 * will use them.
		 */
		CK_COHORT_LOCK(test_cohort, cohort, NULL, NULL);

		/* perform critical section */

		/* relinquish cohort lock */
		CK_COHORT_UNLOCK(test_cohort, cohort, NULL, NULL);
	}

	return NULL;
}

int
main(void)
{
	unsigned int nthr = 4;
	unsigned int n_cohorts = 2;
	unsigned int i;

	/* allocate 2 cohorts of the defined type */
	CK_COHORT_INSTANCE(test_cohort) *cohorts =
	    calloc(n_cohorts, sizeof(CK_COHORT_INSTANCE(test_cohort)));

	/* create local locks to use with each cohort */
	ck_spinlock_t *local_locks =
		calloc(n_cohorts, sizeof(ck_spinlock_t));

	pthread_t *threads =
		calloc(nthr, sizeof(pthread_t));

	/* initialize each of the cohorts before using them */
	for (i = 0 ; i < n_cohorts ; ++i) {
		CK_COHORT_INIT(test_cohort, cohorts + i, &global_lock, local_locks + i,
			CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT);
	}

	/* start each thread and assign cohorts equally */
	for (i = 0 ; i < nthr ; ++i) {
		pthread_create(threads + i, NULL, function, cohorts + (i % n_cohorts));
	}

	ck_pr_store_uint(&ready, 1);
	sleep(10);
	ck_pr_store_uint(&ready, 0);

	for (i = 0 ; i < nthr ; ++i) {
		pthread_join(threads[i], NULL);
	}

	return 0;
}
.Ed
.Sh SEE ALSO
.Xr CK_COHORT_PROTOTYPE 3 ,
.Xr CK_COHORT_TRYLOCK_PROTOTYPE 3 ,
.Xr CK_COHORT_INSTANCE 3 ,
.Xr CK_COHORT_INITIALIZER 3 ,
.Xr CK_COHORT_INIT 3 ,
.Xr CK_COHORT_LOCK 3 ,
.Xr CK_COHORT_UNLOCK 3 ,
.Xr CK_COHORT_LOCKED 3 ,
.Xr CK_COHORT_TRYLOCK 3 ,
.Pp
Additional information available at http://concurrencykit.org/