提交 e4a683c8 编写于 作者: J Jerome Marchand 提交者: Jens Axboe

kref: add kref_test_and_get

Add kref_test_and_get() function, which atomically add a reference only if
refcount is not zero. This prevent to add a reference to an object that is
already being removed.
Signed-off-by: NJerome Marchand <jmarchan@redhat.com>
Cc: stable@kernel.org
Signed-off-by: NJens Axboe <jaxboe@fusionio.com>
上级 a6e8dc46
...@@ -23,6 +23,7 @@ struct kref { ...@@ -23,6 +23,7 @@ struct kref {
void kref_init(struct kref *kref); void kref_init(struct kref *kref);
void kref_get(struct kref *kref); void kref_get(struct kref *kref);
int kref_test_and_get(struct kref *kref);
int kref_put(struct kref *kref, void (*release) (struct kref *kref)); int kref_put(struct kref *kref, void (*release) (struct kref *kref));
#endif /* _KREF_H_ */ #endif /* _KREF_H_ */
...@@ -36,6 +36,18 @@ void kref_get(struct kref *kref) ...@@ -36,6 +36,18 @@ void kref_get(struct kref *kref)
smp_mb__after_atomic_inc(); smp_mb__after_atomic_inc();
} }
/**
* kref_test_and_get - increment refcount for object only if refcount is not
* zero.
* @kref: object.
*
* Return non-zero if the refcount was incremented, 0 otherwise
*/
int kref_test_and_get(struct kref *kref)
{
return atomic_inc_not_zero(&kref->refcount);
}
/** /**
* kref_put - decrement refcount for object. * kref_put - decrement refcount for object.
* @kref: object. * @kref: object.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册