glusterfs

Форк
0
58 строк · 1.6 Кб
1
/*
2
  Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com>
3
  This file is part of GlusterFS.
4

5
  This file is licensed to you under your choice of the GNU Lesser
6
  General Public License, version 3 or any later version (LGPLv3 or
7
  later), or the GNU General Public License, version 2 (GPLv2), in all
8
  cases as published by the Free Software Foundation.
9
*/
10

11
#include "glusterfs/common-utils.h"
12
#include "glusterfs/refcount.h"
13

14
void *
15
_gf_ref_get(gf_ref_t *ref)
16
{
17
    unsigned 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'd
20
     *
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
     */
26
    GF_ASSERT(cnt != 0);
27

28
    return cnt ? ref->data : NULL;
29
}
30

31
unsigned int
32
_gf_ref_put(gf_ref_t *ref)
33
{
34
    unsigned 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
     */
42
    GF_ASSERT(cnt != 0);
43

44
    if (cnt == 1 && ref->release)
45
        ref->release(ref->data);
46

47
    return (cnt != 1);
48
}
49

50
void
51
_gf_ref_init(gf_ref_t *ref, gf_ref_release_t release, void *data)
52
{
53
    GF_ASSERT(ref);
54

55
    GF_ATOMIC_INIT(ref->cnt, 1);
56
    ref->release = release;
57
    ref->data = data;
58
}
59

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.