| 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 26, 2013. |
| 28 |
.Dt ck_brlock 3 |
| 29 |
.Sh NAME |
| 30 |
.Nm ck_brlock_init , |
| 31 |
.Nm ck_brlock_write_lock , |
| 32 |
.Nm ck_brlock_write_unlock , |
| 33 |
.Nm ck_brlock_write_trylock , |
| 34 |
.Nm ck_brlock_read_register , |
| 35 |
.Nm ck_brlock_read_unregister , |
| 36 |
.Nm ck_brlock_read_lock , |
| 37 |
.Nm ck_brlock_read_trylock , |
| 38 |
.Nm ck_brlock_read_unlock |
| 39 |
.Nd big-reader locks |
| 40 |
.Sh LIBRARY |
| 41 |
Concurrency Kit (libck, \-lck) |
| 42 |
.Sh SYNOPSIS |
| 43 |
.In ck_brlock.h |
| 44 |
.Pp |
| 45 |
.Dv ck_brlock_t brlock = CK_BRLOCK_INITIALIZER; |
| 46 |
.Pp |
| 47 |
.Dv ck_brlock_reader_t reader = CK_BRLOCK_READER_INITIALIZER; |
| 48 |
.Pp |
| 49 |
.Ft void |
| 50 |
.Fn ck_brlock_init "ck_brlock_t *br" |
| 51 |
.Ft void |
| 52 |
.Fn ck_brlock_write_lock "ck_brlock_t *br" |
| 53 |
.Ft void |
| 54 |
.Fn ck_brlock_write_unlock "ck_brlock_t *br" |
| 55 |
.Ft bool |
| 56 |
.Fn ck_brlock_write_trylock "ck_brlock_t *br" "unsigned int factor" |
| 57 |
.Ft void |
| 58 |
.Fn ck_brlock_read_register "ck_brlock_t *br" "ck_brlock_reader_t *reader" |
| 59 |
.Ft void |
| 60 |
.Fn ck_brlock_read_unregister "ck_brlock_t *br" "ck_brlock_reader_t *reader" |
| 61 |
.Ft void |
| 62 |
.Fn ck_brlock_read_lock "ck_brlock_t *br" "ck_brlock_reader_t *reader" |
| 63 |
.Ft bool |
| 64 |
.Fn ck_brlock_read_trylock "ck_brlock_t *br" "ck_brlock_reader_t *reader" \ |
| 65 |
"unsigned int factor" |
| 66 |
.Ft void |
| 67 |
.Fn ck_brlock_read_unlock "ck_brlock_reader_t *reader" |
| 68 |
.Sh DESCRIPTION |
| 69 |
Big reader locks are distributed reader-writer locks with low latency constant time |
| 70 |
reader acquisition (with respect to number of concurrent readers). On the other |
| 71 |
hand, writer acquisitions are a relatively expensive O(n) operation. This is a write-biased |
| 72 |
lock. |
| 73 |
.Sh EXAMPLE |
| 74 |
.Bd -literal -offset indent |
| 75 |
static ck_brlock_t lock = CK_BRLOCK_INITIALIZER; |
| 76 |
static __thread ck_brlock_reader_t reader; |
| 77 |
|
| 78 |
static void |
| 79 |
reader(void) |
| 80 |
{ |
| 81 |
|
| 82 |
/* Add our thread as a lock participant. */ |
| 83 |
ck_brlock_read_register(&lock, &reader); |
| 84 |
|
| 85 |
for (;;) { |
| 86 |
ck_brlock_read_lock(&lock, &reader); |
| 87 |
/* Read-side critical section. */ |
| 88 |
ck_brlock_read_unlock(&reader); |
| 89 |
|
| 90 |
if (ck_brlock_read_trylock(&lock, &reader, 1) == true) { |
| 91 |
/* Read-side critical section. */ |
| 92 |
ck_brlock_read_unlock(&reader); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
writer(void) |
| 101 |
{ |
| 102 |
|
| 103 |
for (;;) { |
| 104 |
ck_brlock_write_lock(&lock); |
| 105 |
/* Write-side critical section. */ |
| 106 |
ck_brlock_write_unlock(&lock); |
| 107 |
|
| 108 |
if (ck_brlock_write_trylock(&lock, 1) == true) { |
| 109 |
/* Write-side critical section. */ |
| 110 |
ck_brlock_write_unlock(&lock); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return; |
| 115 |
} |
| 116 |
.Ed |
| 117 |
.Sh SEE ALSO |
| 118 |
.Xr ck_bytelock 3 , |
| 119 |
.Xr ck_rwlock 3 |
| 120 |
.Pp |
| 121 |
Additional information available at http://concurrencykit.org/ |