glusterfs
58 строк · 1.6 Кб
1/*
2Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com>
3This file is part of GlusterFS.
4
5This file is licensed to you under your choice of the GNU Lesser
6General Public License, version 3 or any later version (LGPLv3 or
7later), or the GNU General Public License, version 2 (GPLv2), in all
8cases as published by the Free Software Foundation.
9*/
10
11#include "glusterfs/common-utils.h"12#include "glusterfs/refcount.h"13
14void *15_gf_ref_get(gf_ref_t *ref)16{
17unsigned int cnt = GF_ATOMIC_FETCH_ADD(ref->cnt, 1);18
19/* if cnt == 0, we're in a fatal position, the object will be free'd20*
21* There is a race when two threads do a _gf_ref_get(). Only one of
22* them may get a 0 returned. That is acceptable, because one
23* _gf_ref_get() returning 0 should be handled as a fatal problem and
24* when correct usage/locking is used, it should never happen.
25*/
26GF_ASSERT(cnt != 0);27
28return cnt ? ref->data : NULL;29}
30
31unsigned int32_gf_ref_put(gf_ref_t *ref)33{
34unsigned int cnt = GF_ATOMIC_FETCH_SUB(ref->cnt, 1);35
36/* if cnt == 1, the last user just did a _gf_ref_put()37*
38* When cnt == 0, one _gf_ref_put() was done too much and there has
39* been a thread using the refcounted structure when it was not
40* supposed to.
41*/
42GF_ASSERT(cnt != 0);43
44if (cnt == 1 && ref->release)45ref->release(ref->data);46
47return (cnt != 1);48}
49
50void
51_gf_ref_init(gf_ref_t *ref, gf_ref_release_t release, void *data)52{
53GF_ASSERT(ref);54
55GF_ATOMIC_INIT(ref->cnt, 1);56ref->release = release;57ref->data = data;58}
59