1.\" $NetBSD: pserialize.9,v 1.13 2017/07/03 21:28:48 wiz Exp $ 2.\" 3.\" Copyright (c) 2011 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25.\" POSSIBILITY OF SUCH DAMAGE. 26.\" 27.Dd January 26, 2016 28.Dt PSERIALIZE 9 29.Os 30.Sh NAME 31.Nm pserialize 32.Nd passive serialization mechanism 33.Sh SYNOPSIS 34.In sys/pserialize.h 35.Ft pserialize_t 36.Fn pserialize_create "void" 37.Ft void 38.Fn pserialize_destroy "pserialize_t psz" 39.Ft int 40.Fn pserialize_read_enter "void" 41.Ft void 42.Fn pserialize_read_exit "int s" 43.Ft void 44.Fn pserialize_perform "pserialize_t psz" 45.\" ----- 46.Sh DESCRIPTION 47Passive serialization is a reader / writer synchronisation mechanism 48designed for lock-less read operations. 49The read operations may happen from software interrupt at 50.Dv IPL_SOFTCLOCK . 51.Sh FUNCTIONS 52.Bl -tag -width compact 53.It Fn pserialize_create 54Allocate a new synchronisation object. 55.It Fn pserialize_destroy 56Destroy the synchronisation object. 57No synchronisation activity should happen at this point. 58.It Fn pserialize_read_enter 59Enter the critical path of the reader side. 60Returns an IPL value, which must be passed to 61.Xr pserialize_read_exit 9 . 62Protected code path is not allowed to block. 63.It Fn pserialize_read_exit 64Exit the critical path of the reader side. 65Takes the IPL value returned by 66.Xr pserialize_read_enter 9 . 67.It Fn pserialize_perform 68Perform the passive serialization on the writer side. 69Passing of this function ensures that no readers are in action. 70Writers must be additionally serialized with a separate mechanism, 71e.g. 72.Xr mutex 9 . 73Operation blocks and it may only be performed from thread context. 74.El 75.\" ----- 76.Sh EXAMPLES 77Given a global database of frotz records: 78.Bd -literal 79 struct frotz { 80 ... 81 struct frotz *f_next; 82 }; 83 84 static struct { 85 kmutex_t lock; 86 pserialize_t psz; 87 struct frotz *first; 88 } frobbotzim __cacheline_aligned; 89.Ed 90.Pp 91Create a frotz and publish it, as a writer: 92.Bd -literal 93 struct frotz *f = pool_get(&frotz_pool, PR_WAITOK); 94 95 /* Initialize f. */ 96 ... 97 98 mutex_enter(&frobbotzim.lock); 99 f->f_next = frobbotzim.first; 100 /* 101 * Publish the contents of f->f_next before we publish the 102 * pointer to f in frobbotzim.first. 103 */ 104 membar_producer(); 105 frobbotzim.first = f; 106 mutex_exit(&frobbotzim.lock); 107.Ed 108.Pp 109Find a frotz, as a reader: 110.Bd -literal 111 struct frotz *f; 112 int error = ENOENT; 113 int s; 114 115 s = pserialize_read_enter(); 116 for (f = frobbotzim.first; f != NULL; f = f->f_next) { 117 /* Fetch f before we fetch anything f points to. */ 118 membar_datadep_consumer(); 119 if (f->f_... == key) { 120 /* 121 * Grab whatever part of the frotz we need. 122 * Note that we can't use the frotz after 123 * pserialize_read_exit, without a stronger 124 * kind of reference, say a reference count 125 * managed by atomic_ops(3). 126 */ 127 *resultp = f->f_...; 128 error = 0; 129 break; 130 } 131 } 132 pserialize_read_exit(s); 133 134 return error; 135.Ed 136.Pp 137Remove a frotz, as a writer, and free it once there are no more 138readers: 139.Bd -literal 140 struct frotz **fp, *f; 141 142 mutex_enter(&frobbotzim.lock); 143 for (fp = &frobbotzim.first; (f = *fp) != NULL; fp = &f->f_next) { 144 if (f->f_... == key) { 145 /* 146 * Unhook it from the list. Readers may still 147 * be traversing the list at this point, so 148 * the next pointer must remain valid and 149 * memory must remain allocated. 150 */ 151 *fp = f->f_next; 152 break; 153 } 154 } 155 /* 156 * Wait for all existing readers to complete. New readers will 157 * not see f because the list no longer points to it. 158 */ 159 pserialize_perform(frobbotzim.psz); 160 /* Now nobody else can be touching f, so it is safe to free. */ 161 mutex_exit(&frobbotzim.lock); 162 163 if (f != NULL) 164 pool_put(&frotz_pool, f); 165.Ed 166.\" ----- 167.Sh CODE REFERENCES 168The 169.Nm 170is implemented within the file 171.Pa sys/kern/subr_pserialize.c . 172.Sh SEE ALSO 173.Xr membar_ops 3 , 174.Xr condvar 9 , 175.Xr mutex 9 , 176.Xr rwlock 9 177.Rs 178.%A Hennessy, et al. 179.%T "Passive serialization in a multitasking environment" 180.%I US Patent and Trademark Office 181.%D February 28, 1989 182.%N US Patent 4809168 183.Re 184.Sh HISTORY 185Passive serialization mechanism first appeared in 186.Nx 6.0 . 187