bonding: process the err returned by dev_set_allmulti properly in bond_enslave
[pandora-kernel.git] / include / linux / kref.h
1 /*
2  * kref.h - library routines for handling generic reference counted objects
3  *
4  * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004 IBM Corp.
6  *
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
10  *
11  * This file is released under the GPLv2.
12  *
13  */
14
15 #ifndef _KREF_H_
16 #define _KREF_H_
17
18 #include <linux/types.h>
19 #include <linux/atomic.h>
20
21 struct kref {
22         atomic_t refcount;
23 };
24
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));
30
31 /**
32  * kref_get_unless_zero - Increment refcount for object unless it is zero.
33  * @kref: object.
34  *
35  * Return non-zero if the increment succeeded. Otherwise return 0.
36  *
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.
46  */
47 static inline int __must_check kref_get_unless_zero(struct kref *kref)
48 {
49         return atomic_add_unless(&kref->refcount, 1, 0);
50 }
51 #endif /* _KREF_H_ */