kref.h 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * kref.h - library routines for handling generic reference counted objects
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
 * Copyright (C) 2004 IBM Corp.
 *
 * based on kobject.h which was:
 * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
 * Copyright (C) 2002-2003 Open Source Development Labs
 *
 * This file is released under the GPLv2.
 *
 */

#ifndef _KREF_H_
#define _KREF_H_

18 19
#include <linux/bug.h>
#include <linux/atomic.h>
20
#include <linux/kernel.h>
A
Al Viro 已提交
21
#include <linux/mutex.h>
L
Linus Torvalds 已提交
22 23 24 25 26

struct kref {
	atomic_t refcount;
};

P
Peter Zijlstra 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * kref_init - initialize object.
 * @kref: object in question.
 */
static inline void kref_init(struct kref *kref)
{
	atomic_set(&kref->refcount, 1);
}

/**
 * kref_get - increment refcount for object.
 * @kref: object.
 */
static inline void kref_get(struct kref *kref)
{
	WARN_ON(!atomic_read(&kref->refcount));
	atomic_inc(&kref->refcount);
}

/**
47
 * kref_sub - subtract a number of refcounts for object.
P
Peter Zijlstra 已提交
48
 * @kref: object.
49
 * @count: Number of recounts to subtract.
P
Peter Zijlstra 已提交
50 51 52
 * @release: pointer to the function that will clean up the object when the
 *	     last reference to the object is released.
 *	     This pointer is required, and it is not acceptable to pass kfree
53 54 55 56
 *	     in as this function.  If the caller does pass kfree to this
 *	     function, you will be publicly mocked mercilessly by the kref
 *	     maintainer, and anyone else who happens to notice it.  You have
 *	     been warned.
P
Peter Zijlstra 已提交
57
 *
58
 * Subtract @count from the refcount, and if 0, call release().
P
Peter Zijlstra 已提交
59 60 61 62 63
 * Return 1 if the object was removed, otherwise return 0.  Beware, if this
 * function returns 0, you still can not count on the kref from remaining in
 * memory.  Only use the return value if you want to see if the kref is now
 * gone, not present.
 */
64 65
static inline int kref_sub(struct kref *kref, unsigned int count,
	     void (*release)(struct kref *kref))
P
Peter Zijlstra 已提交
66 67 68
{
	WARN_ON(release == NULL);

69
	if (atomic_sub_and_test((int) count, &kref->refcount)) {
P
Peter Zijlstra 已提交
70 71 72 73 74 75 76
		release(kref);
		return 1;
	}
	return 0;
}

/**
77
 * kref_put - decrement refcount for object.
P
Peter Zijlstra 已提交
78 79 80 81
 * @kref: object.
 * @release: pointer to the function that will clean up the object when the
 *	     last reference to the object is released.
 *	     This pointer is required, and it is not acceptable to pass kfree
82 83 84 85
 *	     in as this function.  If the caller does pass kfree to this
 *	     function, you will be publicly mocked mercilessly by the kref
 *	     maintainer, and anyone else who happens to notice it.  You have
 *	     been warned.
P
Peter Zijlstra 已提交
86
 *
87
 * Decrement the refcount, and if 0, call release().
P
Peter Zijlstra 已提交
88 89 90 91 92
 * Return 1 if the object was removed, otherwise return 0.  Beware, if this
 * function returns 0, you still can not count on the kref from remaining in
 * memory.  Only use the return value if you want to see if the kref is now
 * gone, not present.
 */
93
static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
P
Peter Zijlstra 已提交
94
{
95
	return kref_sub(kref, 1, release);
P
Peter Zijlstra 已提交
96
}
A
Al Viro 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

static inline int kref_put_mutex(struct kref *kref,
				 void (*release)(struct kref *kref),
				 struct mutex *lock)
{
	WARN_ON(release == NULL);
        if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) {
		mutex_lock(lock);
		if (unlikely(!atomic_dec_and_test(&kref->refcount))) {
			mutex_unlock(lock);
			return 0;
		}
		release(kref);
		return 1;
	}
	return 0;
}
L
Linus Torvalds 已提交
114
#endif /* _KREF_H_ */