kref.h revision 289644
1142425Snectar/*- 2160814Ssimon * Copyright (c) 2010 Isilon Systems, Inc. 3142425Snectar * Copyright (c) 2010 iX Systems, Inc. 4142425Snectar * Copyright (c) 2010 Panasas, Inc. 5142425Snectar * Copyright (c) 2013-2015 Mellanox Technologies, Ltd. 6142425Snectar * Copyright (c) 2013 Fran��ois Tigeot 7142425Snectar * All rights reserved. 8142425Snectar * 9142425Snectar * Redistribution and use in source and binary forms, with or without 10142425Snectar * modification, are permitted provided that the following conditions 11142425Snectar * are met: 12142425Snectar * 1. Redistributions of source code must retain the above copyright 13142425Snectar * notice unmodified, this list of conditions, and the following 14142425Snectar * disclaimer. 15142425Snectar * 2. Redistributions in binary form must reproduce the above copyright 16142425Snectar * notice, this list of conditions and the following disclaimer in the 17142425Snectar * documentation and/or other materials provided with the distribution. 18142425Snectar * 19142425Snectar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20142425Snectar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21142425Snectar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22142425Snectar * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23142425Snectar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24142425Snectar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25142425Snectar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26142425Snectar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27142425Snectar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28142425Snectar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29142425Snectar * 30142425Snectar * $FreeBSD: head/sys/ofed/include/linux/kref.h 289644 2015-10-20 19:08:26Z hselasky $ 31142425Snectar */ 32142425Snectar#ifndef _LINUX_KREF_H_ 33142425Snectar#define _LINUX_KREF_H_ 34142425Snectar 35142425Snectar#include <sys/types.h> 36142425Snectar#include <sys/refcount.h> 37142425Snectar 38142425Snectar#include <asm/atomic.h> 39194206Ssimon 40142425Snectarstruct kref { 41142425Snectar atomic_t refcount; 42142425Snectar}; 43142425Snectar 44142425Snectarstatic inline void 45142425Snectarkref_init(struct kref *kref) 46142425Snectar{ 47142425Snectar 48142425Snectar refcount_init(&kref->refcount.counter, 1); 49142425Snectar} 50142425Snectar 51142425Snectarstatic inline void 52160814Ssimonkref_get(struct kref *kref) 53160814Ssimon{ 54142425Snectar 55142425Snectar refcount_acquire(&kref->refcount.counter); 56142425Snectar} 57142425Snectar 58142425Snectarstatic inline int 59142425Snectarkref_put(struct kref *kref, void (*rel)(struct kref *kref)) 60142425Snectar{ 61142425Snectar 62142425Snectar if (refcount_release(&kref->refcount.counter)) { 63142425Snectar rel(kref); 64142425Snectar return 1; 65142425Snectar } 66142425Snectar return 0; 67142425Snectar} 68160814Ssimon 69142425Snectarstatic inline int 70142425Snectarkref_sub(struct kref *kref, unsigned int count, 71142425Snectar void (*rel)(struct kref *kref)) 72142425Snectar{ 73142425Snectar 74142425Snectar while (count--) { 75142425Snectar if (refcount_release(&kref->refcount.counter)) { 76142425Snectar rel(kref); 77142425Snectar return 1; 78142425Snectar } 79142425Snectar } 80142425Snectar return 0; 81160814Ssimon} 82160814Ssimon 83160814Ssimonstatic inline int __must_check 84160814Ssimonkref_get_unless_zero(struct kref *kref) 85160814Ssimon{ 86160814Ssimon 87142425Snectar return atomic_add_unless(&kref->refcount, 1, 0); 88160814Ssimon} 89160814Ssimon 90160814Ssimon#endif /* _LINUX_KREF_H_ */ 91160814Ssimon