1.\" $NetBSD: pserialize.9,v 1.10 2015/03/09 01:55:09 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 November 21, 2014 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 kmutex_t frobbotzim_lock; 85 struct frotz *frobbotzim; 86 pserialize_t frobbotzim_psz; 87.Ed 88.Pp 89Create a frotz and publish it, as a writer: 90.Bd -literal 91 struct frotz *f = pool_get(\*[Am]frotz_pool, PR_WAITOK); 92 93 /* Initialize f. */ 94 ... 95 96 mutex_enter(\*[Am]frobbotzim_lock); 97 f->f_next = frobbotzim; 98 /* 99 * Publish the contents of f->f_next before we publish the 100 * pointer to f in frobbotzim. 101 */ 102 membar_producer(); 103 frobbotzim = f; 104 mutex_exit(\*[Am]frobbotzim_lock); 105.Ed 106.Pp 107Find a frotz, as a reader: 108.Bd -literal 109 struct frotz *f; 110 int error = ENOENT; 111 int s; 112 113 s = pserialize_read_enter(); 114 for (f = frobbotzim; f != NULL; f = f->f_next) { 115 /* Fetch f before we fetch anything f points to. */ 116 membar_datadep_consumer(); 117 if (f->f_... == key) { 118 *resultp = f->f_...; 119 error = 0; 120 break; 121 } 122 } 123 pserialize_read_exit(s); 124 125 return error; 126.Ed 127.Pp 128Remove a frotz, as a writer, and free it once there are no more 129readers: 130.Bd -literal 131 struct frotz **fp, *f; 132 133 mutex_enter(\*[Am]frobbotzim_lock); 134 for (fp = \*[Am]frobbotzim; (f = *fp) != NULL; fp = &f->f_next) { 135 if (f->f_... == key) { 136 /* 137 * Unhook it from the list. Readers may still 138 * be traversing the list at this point, so 139 * the next pointer must remain valid and 140 * memory must remain allocated. 141 */ 142 *fp = f->f_next; 143 break; 144 } 145 } 146 /* 147 * Wait for all existing readers to complete. New readers will 148 * not see f because the list no longer points to it. 149 */ 150 pserialize_perform(frobbotzim_psz); 151 /* Now nobody else can be touching f, so it is safe to free. */ 152 mutex_exit(\*[Am]frobbotzim_lock); 153 154 if (f != NULL) 155 pool_put(\*[Am]frotz_pool, f); 156.Ed 157.\" ----- 158.Sh CODE REFERENCES 159The 160.Nm 161is implemented within the file 162.Pa sys/kern/subr_pserialize.c . 163.Sh SEE ALSO 164.Xr membar_ops 3 , 165.Xr condvar 9 , 166.Xr mutex 9 , 167.Xr rwlock 9 168.Rs 169.%A Hennessy, et al. 170.%T "Passive serialization in a multitasking environment" 171.%I US Patent and Trademark Office 172.%D February 28, 1989 173.%N US Patent 4809168 174.Re 175.Sh HISTORY 176Passive serialization mechanism first appeared in 177.Nx 6.0 . 178