p
The head of a linked list is represented by a
.Vt struct pslist_head
object allocated by the caller, e.g. by embedding it in another
struct, which should be otherwise treated as opaque.
A linked list head must be initialized with
.Dv PSLIST_INITIALIZER
or
.Fn PSLIST_INIT
before it may be used.
When initialized, a list head represents an empty list.
A list should be empty and destroyed with
.Fn PSLIST_DESTROY
before the
.Vt struct pslist_head
object's memory is reused.
p
Each entry in a linked list is represented by a
.Vt struct pslist_entry
object, also opaque, and embedded as a member in a caller-allocated
structure called an
.Em element .
A
.Vt struct pslist_entry
object must be initialized with
.Dv PSLIST_ENTRY_INITIALIZER
or
.Fn PSLIST_ENTRY_INIT
before it may be used.
p
When initialized, a list entry is unassociated.
Inserting an entry associates it with a particular list.
Removing it
partially disassociates it from that list and prevents new readers from
finding it in the list, but allows extant parallel readers to continue
reading the next entry.
The caller must then wait, e.g. with
.Xr pserialize_perform 9 ,
for all extant parallel readers to finish, before destroying the list
entry with
.Fn PSLIST_ENTRY_DESTROY
and then freeing or reusing its memory.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh EXCLUSIVE OPERATIONS
The following operations may be performed on list heads and entries
when the caller has exclusive access to them \(em no parallel writers or
readers may have access to the same objects.
"""""""""""""""
l -tag -width abcd t Dv PSLIST_INITIALIZER Constant initializer for a
.Vt struct pslist_head
object.
"""""""""""""""
t Fn PSLIST_INIT head Initialize the list headed by
.Fa head
to be empty.
"""""""""""""""
t Fn PSLIST_DESTROY head Destroy the list headed by
.Fa head ,
which must be empty.
p
This has an effect only with the
.Dv DIAGNOSTIC
option, so it is not strictly necessary, but it can help to detect bugs
early; see
.Xr KASSERT 9 .
"""""""""""""""
t Dv PSLIST_ENTRY_INITIALIZER Constant initializer for an unassociated
.Vt struct pslist_entry
object.
"""""""""""""""
t Fn PSLIST_ENTRY_INIT element NAME Initialize the
.Vt struct pslist_entry
object
.Fa element Ns Li -> Ns Fa NAME .
"""""""""""""""
t Fn PSLIST_ENTRY_DESTROY element NAME Destroy the
.Vt struct pslist_entry
object
.Fa element Ns Li -> Ns Fa NAME .
Either
.Fa element
must never have been inserted into a list, or it must have been
inserted and removed, and the caller must have waited for all parallel
readers to finish reading it first.
.El
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh WRITER OPERATIONS
The following operations may be performed on list heads and entries
when the caller has exclusive
.Em write
access to them \(em parallel readers for the same objects are allowed,
but no parallel writers.
"""""""""""""""
l -tag -width abcd t Fn PSLIST_WRITER_INSERT_HEAD head element NAME Insert the element
.Fa element
at the beginning of the list headed by
.Fa head ,
before any existing elements in the list.
p
The object
.Fa element Ns Li -> Ns Fa NAME
must be a
.Vt struct pslist_entry
object which has been initialized but not inserted.
"""""""""""""""
t Fn PSLIST_WRITER_INSERT_BEFORE element new NAME Insert the element
.Fa new
into a list before the element
.Fa element .
p
The object
.Fa element Ns Li -> Ns Fa NAME
must be a
.Vt struct pslist_entry
object which has been inserted into a list.
The object
.Fa new Ns Li -> Ns Fa NAME
must be a
.Vt struct pslist_entry
"""""""""""""""
t Fn PSLIST_WRITER_INSERT_AFTER element new NAME Insert the element
.Fa new
into a list after the element
.Fa element .
p
The object
.Fa element Ns Li -> Ns Fa NAME
must be a
.Vt struct pslist_entry
object which has been inserted into a list.
The object
.Fa new Ns Li -> Ns Fa NAME
must be a
.Vt struct pslist_entry
"""""""""""""""
t Fn PSLIST_WRITER_REMOVE element NAME Remove the element
.Fa element
from the list into which it has been inserted.
p
The object
.Fa element Ns Li -> Ns Fa NAME
must be a
.Vt struct pslist_entry
object which has been inserted into a list.
"""""""""""""""
t Fn PSLIST_WRITER_FIRST head type NAME Return a pointer to the first element
.Fa o
of type
.Fa type
with a
.Vt struct pslist_entry
member
.Fa o Ns Li -> Ns Fa NAME ,
or
.Dv NULL
if the list is empty.
"""""""""""""""
t Fn PSLIST_WRITER_NEXT element type NAME Return a pointer to the next element
.Fa o
of type
.Fa type
with a
.Vt struct pslist_entry
member
.Fa o Ns Li -> Ns Fa NAME
after
.Fa element
in a list, or
.Dv NULL
if there are no elements after
.Fa element .
"""""""""""""""
t Fn PSLIST_WRITER_FOREACH element head type NAME Loop header for iterating over each element
.Fa element
of type
.Fa type
with
.Vt struct pslist_entry
member
.Fa element Ns Li -> Ns Fa NAME
starting at the list head
.Fa head .
p
The caller must not modify the list while iterating over it.
.El
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh READER OPERATIONS
The following operations may be performed on list heads and entries
when the caller is in a passively serialized read section \(em see
.Xr pserialize 9 .
l -tag -width abcd """""""""""""""
t Fn PSLIST_READER_FIRST head type NAME Return a pointer to the first element
.Fa o
of type
.Fa type
with a
.Vt struct pslist_entry
member
.Fa o Ns Li -> Ns Fa NAME ,
or
.Dv NULL
if the list is empty.
"""""""""""""""
t Fn PSLIST_READER_NEXT element type NAME Return a pointer to the next element
.Fa o
of type
.Fa type
with a
.Vt struct pslist_entry
member
.Fa o Ns Li -> Ns Fa NAME
after
.Fa element
in a list, or
.Dv NULL
if there are no elements after
.Fa element .
"""""""""""""""
t Fn PSLIST_READER_FOREACH element head type NAME Loop header for iterating over each element
.Fa element
of type
.Fa type
with
.Vt struct pslist_entry
member
.Fa element Ns Li -> Ns Fa NAME
starting at the list head
.Fa head .
.El
.Sh EXAMPLES
Example frotz structure and global state:
d -literal struct frotz {
uint64_t f_key;
uint64_t f_datum;
struct pslist_entry f_entry;
};
static struct {
kmutex_t lock;
pserialize_t psz;
struct pslist_head list;
struct pool pool;
} frobnitzem __cacheline_aligned;
.Ed
p Initialize the global state: d -literal mutex_init(&frobnitzem.lock, MUTEX_DEFAULT, IPL_NONE); frobnitzem.psz = pserialize_create(); PSLIST_INIT(&frobnitzem.list); pool_init(&frobnitzem.pool, sizeof(struct frotz), ...); .Ed
p Create and publish a frotz: d -literal uint64_t key = ...; uint64_t datum = ...; struct frotz *f = pool_get(&frobnitzem.pool, PR_WAITOK); /* Initialize f. */ f->f_key = key; f->f_datum = datum; PSLIST_ENTRY_INIT(f, f_entry); /* Publish it. */ mutex_enter(&frobnitzem.lock); PSLIST_WRITER_INSERT_HEAD(&frobnitzem.list, f, f_entry); mutex_exit(&frobnitzem.lock); .Ed
p Look up a frotz and return its associated datum: d -literal uint64_t key = ...; struct frotz *f; int error = ENOENT; int s; s = pserialize_read_enter(); PSLIST_READER_FOREACH(f, &frobnitzem.list, struct frotz, f_entry) { if (f->f_key == key) { *datump = f->f_datum; error = 0; break; } } pserialize_read_exit(s); return error; .Ed
p
Remove a frotz and wait for readers to finish using it before reusing
the memory allocated for it:
d -literal struct frotz *f = ...;
mutex_enter(&frobnitzem.lock);
PSLIST_WRITER_REMOVE(f, f_entry);
mutex_exit(&frobnitzem.lock);
pserialize_perform(&frobnitzem.psz);
PSLIST_ENTRY_DESTROY(f, f_entry);
pool_put(&frobnitzem.pool, f);
.Ed
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh CODE REFERENCES
The
.Nm
data structure is implemented by static inlines and macros in
a sys/sys/pslist.h .
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh SEE ALSO
.Xr queue 3 ,
.Xr pserialize 9 ,
.Xr psref 9
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh HISTORY
The
.Nm
data structure first appeared in
.Nx 8.0 .
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh AUTHORS
.An Taylor R Campbell Aq Mt riastradh (at] NetBSD.org