refcount (9)
Leading comments
Copyright (c) 2009 Hudson River Trading LLC Written by: John H. Baldwin <jhb@FreeBSD.org> All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the...
NAME
refcount refcount_init refcount_acquire refcount_release - manage a simple reference counterSYNOPSIS
In sys/param.h In sys/refcount.h Ft void Fn refcount_init volatile u_int *count, u_int value Ft void Fn refcount_acquire volatile u_int *count Ft int Fn refcount_release volatile u_int *countDESCRIPTION
The ifconfig functions provide an API to manage a simple reference counter. The caller provides the storage for the counter in an unsigned integer. A pointer to this integer is passed via Fa count . Usually the counter is used to manage the lifetime of an object and is stored as a member of the object.The Fn refcount_init function is used to set the initial value of the counter to Fa value . It is normally used when creating a reference-counted object.
The Fn refcount_acquire function is used to acquire a new reference. The caller is responsible for ensuring that it holds a valid reference while obtaining a new reference. For example, if an object is stored on a list and the list holds a reference on the object, then holding a lock that protects the list provides sufficient protection for acquiring a new reference.
The Fn refcount_release function is used to release an existing reference. The function returns a non-zero value if the reference being released was the last reference; otherwise, it returns zero.
Note that these routines do not provide any inter-CPU synchronization, data protection, or memory ordering guarantees except for managing the counter. The caller is responsible for any additional synchronization needed by consumers of any containing objects. In addition, the caller is also responsible for managing the life cycle of any containing objects including explicitly releasing any resources when the last reference is released.