Debian Codegraph @

doc/ck_sequence

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 26, 2013.
28 .Dt ck_sequence 3
29 .Sh NAME
30 .Nm ck_sequence_init ,
31 .Nm ck_sequence_read_begin ,
32 .Nm ck_sequence_read_retry ,
33 .Nm ck_sequence_write_begin ,
34 .Nm ck_sequence_write_end
35 .Nd sequence locks
36 .Sh LIBRARY
37 Concurrency Kit (libck, \-lck)
38 .Sh SYNOPSIS
39 .In ck_sequence.h
40 .Pp
41 .Dv ck_sequence_t seqlock = CK_SEQUENCE_INITIALIZER;
42 .Pp
43 .Ft void
44 .Fn ck_sequence_init "ck_sequence_t *sq"
45 .Ft unsigned int
46 .Fn ck_sequence_read_begin "const ck_sequence_t *sq"
47 .Ft bool
48 .Fn ck_sequence_read_retry "const ck_sequence_t *sq" "unsigned int version"
49 .Ft void
50 .Fn ck_sequence_write_begin "ck_sequence_t *sq"
51 .Ft void
52 .Fn ck_sequence_write_end "ck_sequence_t *sq"
53 .Sh DESCRIPTION
54 It is recommended to use ck_sequence when a small amount of data that cannot be
55 accessed atomically has to be synchronized with readers in a fashion that does
56 not block any writer. Readers are able to execute their read-side critical
57 sections without any atomic operations. A ck_sequence_t must be initialized
58 before use. It may be initialized using either a static initializer
59 (CK_SEQUENCE_INITIALIZER) or using
60 .Fn ck_sequence_init .
61 Before readers attempt to
62 read data that may be concurrently modified they must first save the return
63 value of
64 .Fn ck_sequence_read_begin .
65 While or after a reader has completed copying
66 the data associated with a ck_sequence_t it must pass the earlier return value
67 of
68 .Fn ck_sequence_read_begin
69 to
70 .Fn "ck_sequence_read_retry". If
71 .Fn ck_sequence_read_retry
72 returns true then the copy of data may be inconsistent and the read process
73 must be retried. Writers must rely on their own synchronization primitives.
74 Once a writer has entered its respective critical section, it must call
75 .Fn ck_sequence_write_begin
76 to signal intent to update the data protected
77 by the ck_sequence_t. Before the writer leaves its critical section it must
78 execute
79 .Fn ck_sequence_write_end
80 to indicate that the updates have left respective objects in a consistent state.
81 .Sh EXAMPLE
82 .Bd -literal -offset indent
83 #include <ck_sequence.h>
84 #include <stdlib.h>
85
86 static struct example {
87 int a;
88 int b;
89 int c;
90 } global;
91
92 static ck_sequence_t seqlock = CK_SEQUENCE_INITIALIZER;
93
94 void
95 reader(void)
96 {
97 struct example copy;
98 unsigned int version;
99
100 /*
101 * Attempt a read of the data structure. If the structure
102 * has been modified between ck_sequence_read_begin and
103 * ck_sequence_read_retry then attempt another read since
104 * the data may be in an inconsistent state.
105 */
106 do {
107 version = ck_sequence_read_begin(&seqlock);
108 copy = global;
109 } while (ck_sequence_read_retry(&seqlock, version));
110
111 /*
112 * The previous may also be expressed using CK_SEQUENCE_READ.
113 * Generally recommend to only use ck_sequence_read_retry
114 * if you would like to detect a conflicting write at some
115 * higher granularity.
116 */
117 CK_SEQUENCE_READ(&seqlock, &version) {
118 copy = global;
119 }
120
121 return;
122 }
123
124 void
125 writer(void)
126 {
127
128 for (;;) {
129 ck_sequence_write_begin(&seqlock);
130 global.a = rand();
131 global.b = global.a + global.b;
132 global.c = global.b + global.c;
133 ck_sequence_write_end(&seqlock);
134 }
135
136 return;
137 }
138 .Ed
139 .Sh SEE ALSO
140 .Xr ck_brlock 3 ,
141 .Xr ck_bytelock 3 ,
142 .Xr ck_rwlock 3
143 .Pp
144 Additional information available at http://concurrencykit.org/