SRPL_RC_INIT(9)

NAME
srpl_rc_init, SRPL_HEAD, SRPL_ENTRY, SRPL_INIT, SRPL_ENTER, SRPL_NEXT, SRPL_FOREACH, SRPL_LEAVE, SRPL_EMPTY_LOCKED, SRPL_FIRST_LOCKED, SRPL_NEXT_LOCKED, SRPL_FOREACH_LOCKED, SRPL_FOREACH_SAFE_LOCKED, SRPL_INSERT_HEAD_LOCKED, SRPL_INSERT_AFTER_LOCKED, SRPL_REMOVE_LOCKED, SRPL_RC_INITIALIZER - singly-linked shared reference pointer list

SYNOPSIS
#include <sys/srp.h>
struct srpl_rc;
void
srpl_rc_init(struct srpl_rc *rc, void (*ref)(void *, void *), void (*unref)(void *, void *), void *ctx);
SRPL_HEAD(HEADNAME, TYPE);
SRPL_ENTRY(TYPE);
SRPL_INIT(SRPL_HEAD *sl);
void *
SRPL_ENTER(struct srp_ref *sr, SRPL_HEAD *sl);
void *
SRPL_NEXT(struct srp_ref *sr, struct TYPE *listelm, FIELDNAME);
SRPL_FOREACH(VARNAME, struct srp_ref *sr, SRPL_HEAD *sl, FIELDNAME);
SRPL_LEAVE(struct srp_ref *sr);
SRPL_EMPTY_LOCKED(SRPL_HEAD *sl);
void *
SRPL_FIRST_LOCKED(SRPL_HEAD *sl);
void *
SRPL_NEXT_LOCKED(struct TYPE *listelm, FIELDNAME);
SRPL_FOREACH_LOCKED(VARNAME, SRPL_HEAD *sl, FIELDNAME);
SRPL_FOREACH_SAFE_LOCKED(VARNAME, SRPL_HEAD *sl, FIELDNAME, TEMP_VARNAME);
SRPL_INSERT_HEAD_LOCKED(struct srpl_rc *rc, SRPL_HEAD *sl, struct TYPE *elm, FIELDNAME);
SRPL_INSERT_AFTER_LOCKED(struct srpl_rc *rc, struct TYPE *listelm, struct TYPE *elm, FIELDNAME);
SRPL_REMOVE_LOCKED(struct srpl_rc *rc, SRPL_HEAD *sl, struct TYPE *listelm, TYPE, FIELDNAME);
SRPL_RC_INITIALIZER(void (*ref)(void *, void *), void (*unref)(void *, void *), void *ctx);

DESCRIPTION
The srpl macros build a linked list on top of shared reference pointers. This allows concurrent traversal of a linked list and access to the items on the list.

SRP lists are a generic type represented by a SRPL_HEAD. The elements inserted into the list must be structures that contain a SRPL_ENTRY field. Further, the elements must also support reference counting as insertion and removal operations can cause items to be temporarily referenced by multiple SRPs within the list at the same time.

srpl_rc_init() initialises the SRP list refcounts rc structure so it can be used to manage the reference counts on elements in the list. The insertion or removal of an element in an SRP list will increment the reference counts on elements within the list via calls to ref. As these references are released by the SRP infrastructure, the reference counts will be decremented by calls to unref. unref is also responsible for freeing the element when the reference count reaches 0. Both ref and unref will be called with ctx as their first argument and a pointer to the element as their second argument.

SRPL_INIT() initialises the SRP list sl to an empty state.

SRPL_ENTER() begins iterating over elements in the SRP list sl. The reference to the list item is held via sr. Every call to SRPL_ENTER() must have a corresponding call to SRPL_LEAVE() to release references to the list and its elements.

SRPL_NEXT() accesses the element in the SRP list after listelm.

SRPL_FOREACH() creates a loop for traversing the list. Every call to SRPL_FOREACH() must have a corresponding call to SRPL_LEAVE() to release references to the list and its elements.

SRPL_LEAVE() releases references to the list and its elements held by previous calls to SRPL_ENTER(), SRPL_NEXT(), or SRPL_FOREACH().

SRPL_EMPTY_LOCKED() tests whether the SRP list sl is empty.

SRPL_FIRST_LOCKED() accesses the first element in the SRP list sl.

SRPL_NEXT_LOCKED() accesses the next element in the SRP list after listelm.

SRPL_FOREACH_LOCKED() creates a loop for traversing the elements in the SRP list sl.

SRPL_FOREACH_SAFE_LOCKED() creates a loop for traversing the elements in the SRP list sl, permitting it to remove VARNAME as well as freeing it from within the loop safely without interfering with the traversal.

SRPL_INSERT_HEAD_LOCKED() inserts elm into the SRP list sl. Reference counts are adjusted on the list items using the functions specified by rc.

SRPL_INSERT_AFTER_LOCKED() inserts elm into an SRP list after the element listelm. Reference counts are adjusted on the list items using the functions specified by rc.

SRPL_REMOVE_LOCKED() iterates over the SRP list sl until it finds listelm and then removes it. Reference counts are adjusted on the list items using the functions specified by rc.

An srpl_rc declaration can be initialised with the SRPL_RC_INITIALIZER() macro.

CONTEXT
SRPL_INIT(), SRPL_ENTER(), SRPL_NEXT(), SRPL_FOREACH(), and SRPL_LEAVE() may be called during autoconf, from process context, or from interrupt context.

srpl_rc_init(), SRPL_EMPTY_LOCKED(), SRPL_FIRST_LOCKED(), SRPL_NEXT_LOCKED(), SRPL_FOREACH_LOCKED(), SRPL_INSERT_HEAD_LOCKED(), SRPL_INSERT_AFTER_LOCKED(), and SRPL_REMOVE_LOCKED() may be called during autoconf or from process context. An appropriate lock must be held that prevents concurrent modifications to the list.

RETURN VALUES
SRPL_ENTER(), SRPL_NEXT(), SRPL_FIRST_LOCKED(), and SRPL_NEXT_LOCKED() return a pointer to elements in the SRP list, or NULL if there are no more elements.

SRPL_EMPTY_LOCKED() returns non-zero when the list is empty, otherwise 0.

HISTORY
The srp API was originally written by Jonathan Matthew <jmatthew@openbsd.org> and David Gwynne <dlg@openbsd.org>. The SRP list API first appeared in OpenBSD 5.9.