1.\" $NetBSD: pserialize.9,v 1.14 2021/10/10 11:20:46 riastradh 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 are typically additionally serialized with a separate 71mechanism, e.g. 72.Xr mutex 9 , 73to remove objects used by readers from a published list. 74Operation blocks and it may only be performed from thread context. 75.El 76.\" ----- 77.Sh EXAMPLES 78Given a global database of frotz records: 79.Bd -literal 80 struct frotz { 81 ... 82 struct frotz *f_next; 83 }; 84 85 static struct { 86 kmutex_t lock; 87 pserialize_t psz; 88 struct frotz *first; 89 } frobbotzim __cacheline_aligned; 90.Ed 91.Pp 92Create a frotz and publish it, as a writer: 93.Bd -literal 94 struct frotz *f = pool_get(&frotz_pool, PR_WAITOK); 95 96 /* Initialize f. */ 97 ... 98 99 mutex_enter(&frobbotzim.lock); 100 f->f_next = frobbotzim.first; 101 /* 102 * Publish the contents of f->f_next before we publish the 103 * pointer to f in frobbotzim.first. 104 */ 105 membar_producer(); 106 frobbotzim.first = f; 107 mutex_exit(&frobbotzim.lock); 108.Ed 109.Pp 110Find a frotz, as a reader: 111.Bd -literal 112 struct frotz *f; 113 int error = ENOENT; 114 int s; 115 116 s = pserialize_read_enter(); 117 for (f = frobbotzim.first; f != NULL; f = f->f_next) { 118 /* Fetch f before we fetch anything f points to. */ 119 membar_datadep_consumer(); 120 if (f->f_... == key) { 121 /* 122 * Grab whatever part of the frotz we need. 123 * Note that we can't use the frotz after 124 * pserialize_read_exit, without a stronger 125 * kind of reference, say a reference count 126 * managed by atomic_ops(3). 127 */ 128 *resultp = f->f_...; 129 error = 0; 130 break; 131 } 132 } 133 pserialize_read_exit(s); 134 135 return error; 136.Ed 137.Pp 138Remove a frotz, as a writer, and free it once there are no more 139readers: 140.Bd -literal 141 struct frotz **fp, *f; 142 143 mutex_enter(&frobbotzim.lock); 144 for (fp = &frobbotzim.first; (f = *fp) != NULL; fp = &f->f_next) { 145 if (f->f_... == key) { 146 /* 147 * Unhook it from the list. Readers may still 148 * be traversing the list at this point, so 149 * the next pointer must remain valid and 150 * memory must remain allocated. 151 */ 152 *fp = f->f_next; 153 break; 154 } 155 } 156 mutex_exit(&frobbotzim.lock); 157 158 /* 159 * Wait for all existing readers to complete. New readers will 160 * not see f because the list no longer points to it. 161 */ 162 pserialize_perform(frobbotzim.psz); 163 164 /* Now nobody else can be touching f, so it is safe to free. */ 165 if (f != NULL) 166 pool_put(&frotz_pool, f); 167.Ed 168.\" ----- 169.Sh CODE REFERENCES 170The 171.Nm 172is implemented within the file 173.Pa sys/kern/subr_pserialize.c . 174.Sh SEE ALSO 175.Xr membar_ops 3 , 176.Xr condvar 9 , 177.Xr mutex 9 , 178.Xr rwlock 9 179.Rs 180.%A Hennessy, et al. 181.%T "Passive serialization in a multitasking environment" 182.%I US Patent and Trademark Office 183.%D February 28, 1989 184.%N US Patent 4809168 185.Re 186.Sh HISTORY 187Passive serialization mechanism first appeared in 188.Nx 6.0 . 189