kref.h revision 270710
150477Speter/*-
240269Srnordier * Copyright (c) 2010 Isilon Systems, Inc.
3213136Spjd * Copyright (c) 2010 iX Systems, Inc.
440326Srnordier * Copyright (c) 2010 Panasas, Inc.
5172940Sjhb * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6253211Swblock * All rights reserved.
7172940Sjhb *
880751Sjhb * Redistribution and use in source and binary forms, with or without
980751Sjhb * modification, are permitted provided that the following conditions
1042480Srnordier * are met:
1142480Srnordier * 1. Redistributions of source code must retain the above copyright
1240541Srnordier *    notice unmodified, this list of conditions, and the following
1340541Srnordier *    disclaimer.
14104673Sgreen * 2. Redistributions in binary form must reproduce the above copyright
1540269Srnordier *    notice, this list of conditions and the following disclaimer in the
16172940Sjhb *    documentation and/or other materials provided with the distribution.
1740269Srnordier *
18125537Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19172940Sjhb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20172940Sjhb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21172940Sjhb * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22104635Sphk * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23213136Spjd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24225530Savg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25213136Spjd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26172940Sjhb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27125932Sru * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28125932Sru */
29125932Sru#ifndef _LINUX_KREF_H_
3097860Sphk#define _LINUX_KREF_H_
31213136Spjd
32173026Sjhb#include <sys/refcount.h>
33172940Sjhb
34213568Sphostruct kref {
3540269Srnordier        volatile u_int count;
3640269Srnordier};
37169732Skan
38169732Skanstatic inline void
3940269Srnordierkref_init(struct kref *kref)
40125621Sru{
4140269Srnordier
42125537Sru	refcount_init(&kref->count, 1);
43125537Sru}
4440269Srnordier
45172940Sjhbstatic inline void
46125537Srukref_get(struct kref *kref)
47172940Sjhb{
48172940Sjhb
49172940Sjhb	refcount_acquire(&kref->count);
50109886Sphk}
51172940Sjhb
52125537Srustatic inline int
53172940Sjhbkref_put(struct kref *kref, void (*rel)(struct kref *kref))
54172940Sjhb{
5540269Srnordier
56172940Sjhb	if (refcount_release(&kref->count)) {
57172940Sjhb		rel(kref);
5840269Srnordier		return 1;
59219483Sjhb	}
60219483Sjhb	return 0;
6196424Speter}
62172940Sjhb
63172940Sjhb#endif /* _LINUX_KREF_H_ */
6480751Sjhb