Skip to content

Commit

Permalink
kref: add kref_test_and_get
Browse files Browse the repository at this point in the history
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: Jerome Marchand <jmarchan@redhat.com>
Cc: stable@kernel.org
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
  • Loading branch information
Jerome Marchand authored and Jens Axboe committed Jan 5, 2011
1 parent a6e8dc4 commit e4a683c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/linux/kref.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct kref {

void kref_init(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));

#endif /* _KREF_H_ */
12 changes: 12 additions & 0 deletions lib/kref.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ void kref_get(struct kref *kref)
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: object.
Expand Down

0 comments on commit e4a683c

Please sign in to comment.