kref.h revision 282513
1169689Skan/*-
2169689Skan * Copyright (c) 2010 Isilon Systems, Inc.
3169689Skan * Copyright (c) 2010 iX Systems, Inc.
4169689Skan * Copyright (c) 2010 Panasas, Inc.
5169689Skan * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6169689Skan * All rights reserved.
7169689Skan *
8169689Skan * Redistribution and use in source and binary forms, with or without
9169689Skan * modification, are permitted provided that the following conditions
10169689Skan * are met:
11169689Skan * 1. Redistributions of source code must retain the above copyright
12169689Skan *    notice unmodified, this list of conditions, and the following
13169689Skan *    disclaimer.
14169689Skan * 2. Redistributions in binary form must reproduce the above copyright
15169689Skan *    notice, this list of conditions and the following disclaimer in the
16169689Skan *    documentation and/or other materials provided with the distribution.
17169689Skan *
18169689Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19169689Skan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20169689Skan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21169689Skan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22169689Skan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23169689Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24169689Skan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25169689Skan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26169689Skan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27169689Skan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28169689Skan */
29169689Skan#ifndef _LINUX_KREF_H_
30169689Skan#define _LINUX_KREF_H_
31169689Skan
32169689Skan#include <sys/types.h>
33169689Skan#include <sys/refcount.h>
34169689Skan
35169689Skanstruct kref {
36169689Skan        volatile u_int count;
37169689Skan};
38169689Skan
39169689Skanstatic inline void
40169689Skankref_init(struct kref *kref)
41169689Skan{
42169689Skan
43169689Skan	refcount_init(&kref->count, 1);
44169689Skan}
45169689Skan
46169689Skanstatic inline void
47169689Skankref_get(struct kref *kref)
48169689Skan{
49169689Skan
50169689Skan	refcount_acquire(&kref->count);
51169689Skan}
52169689Skan
53169689Skanstatic inline int
54169689Skankref_put(struct kref *kref, void (*rel)(struct kref *kref))
55169689Skan{
56169689Skan
57169689Skan	if (refcount_release(&kref->count)) {
58169689Skan		rel(kref);
59169689Skan		return 1;
60169689Skan	}
61169689Skan	return 0;
62169689Skan}
63169689Skan
64169689Skan#endif /* _LINUX_KREF_H_ */
65169689Skan