2 * kref.h - library routines for handling generic reference counted objects
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Corp.
7 * based on kobject.h which was:
8 * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
9 * Copyright (C) 2002-2003 Open Source Development Labs
11 * This file is released under the GPLv2.
18 #include <linux/types.h>
19 #include <linux/atomic.h>
25 void kref_init(struct kref *kref);
26 void kref_get(struct kref *kref);
27 int kref_put(struct kref *kref, void (*release) (struct kref *kref));
28 int kref_sub(struct kref *kref, unsigned int count,
29 void (*release) (struct kref *kref));
32 * kref_get_unless_zero - Increment refcount for object unless it is zero.
35 * Return non-zero if the increment succeeded. Otherwise return 0.
37 * This function is intended to simplify locking around refcounting for
38 * objects that can be looked up from a lookup structure, and which are
39 * removed from that lookup structure in the object destructor.
40 * Operations on such objects require at least a read lock around
41 * lookup + kref_get, and a write lock around kref_put + remove from lookup
42 * structure. Furthermore, RCU implementations become extremely tricky.
43 * With a lookup followed by a kref_get_unless_zero *with return value check*
44 * locking in the kref_put path can be deferred to the actual removal from
45 * the lookup structure and RCU lookups become trivial.
47 static inline int __must_check kref_get_unless_zero(struct kref *kref)
49 return atomic_add_unless(&kref->refcount, 1, 0);