tlockfree.h 3.5 KB
Newer Older
H
hjxilinx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
S
Shengliang Guan 已提交
15 16 17

#ifndef _TD_UTIL_LOCK_FREE_H_
#define _TD_UTIL_LOCK_FREE_H_
H
hjxilinx 已提交
18 19 20

#include "os.h"

B
Bomin Zhang 已提交
21 22 23 24 25
#ifdef __cplusplus
extern "C" {
#endif

// reference counting
S
Shengliang Guan 已提交
26
typedef void (*_ref_fn_t)(const void *pObj);
H
hjxilinx 已提交
27 28 29

#define T_REF_DECLARE() \
  struct {              \
dengyihao's avatar
dengyihao 已提交
30
    int32_t val;        \
H
hjxilinx 已提交
31 32 33 34 35 36 37 38
  } _ref;

#define T_REF_REGISTER_FUNC(s, e) \
  struct {                        \
    _ref_fn_t start;              \
    _ref_fn_t end;                \
  } _ref_func = {.begin = (s), .end = (e)};

39 40 41 42 43 44 45 46
// set the initial reference count value
#define T_REF_INIT_VAL(x, _v)                \
  do {                                       \
    assert(_v >= 0);                         \
    atomic_store_32(&((x)->_ref.val), (_v)); \
  } while (0)

// increase the reference count by 1
dengyihao's avatar
dengyihao 已提交
47
#define T_REF_INC(x) (atomic_add_fetch_32(&((x)->_ref.val), 1))
H
hjxilinx 已提交
48 49 50 51 52 53 54 55 56

#define T_REF_INC_WITH_CB(x, p)                           \
  do {                                                    \
    int32_t v = atomic_add_fetch_32(&((x)->_ref.val), 1); \
    if (v == 1 && (p)->_ref_func.begin != NULL) {         \
      (p)->_ref_func.begin((x));                          \
    }                                                     \
  } while (0)

dengyihao's avatar
dengyihao 已提交
57
#define T_REF_DEC(x) (atomic_sub_fetch_32(&((x)->_ref.val), 1))
H
hjxilinx 已提交
58 59 60

#define T_REF_DEC_WITH_CB(x, p)                           \
  do {                                                    \
dengyihao's avatar
dengyihao 已提交
61
    int32_t v = atomic_sub_fetch_32(&((x)->_ref.val), 1); \
H
hjxilinx 已提交
62 63 64 65 66 67 68 69 70
    if (v == 0 && (p)->_ref_func.end != NULL) {           \
      (p)->_ref_func.end((x));                            \
    }                                                     \
  } while (0)

#define T_REF_VAL_CHECK(x) assert((x)->_ref.val >= 0);

#define T_REF_VAL_GET(x) (x)->_ref.val

B
Bomin Zhang 已提交
71
// single writer multiple reader lock
H
refact  
Hongze Cheng 已提交
72
typedef volatile int32_t SRWLatch;
B
Bomin Zhang 已提交
73 74 75 76 77 78

void taosInitRWLatch(SRWLatch *pLatch);
void taosWLockLatch(SRWLatch *pLatch);
void taosWUnLockLatch(SRWLatch *pLatch);
void taosRLockLatch(SRWLatch *pLatch);
void taosRUnLockLatch(SRWLatch *pLatch);
D
dapan1121 已提交
79
int32_t taosWTryLockLatch(SRWLatch *pLatch);
B
Bomin Zhang 已提交
80 81

// copy on read
S
Shengliang Guan 已提交
82 83
#define taosCorBeginRead(x)                     \
  for (uint32_t i_ = 1; 1; ++i_) {              \
dengyihao's avatar
dengyihao 已提交
84
    int32_t old_ = atomic_add_fetch_32((x), 0); \
S
Shengliang Guan 已提交
85 86 87 88 89
    if (old_ & 0x00000001) {                    \
      if (i_ % 1000 == 0) {                     \
        sched_yield();                          \
      }                                         \
      continue;                                 \
B
Bomin Zhang 已提交
90 91
    }

S
Shengliang Guan 已提交
92 93 94 95
#define taosCorEndRead(x)                    \
  if (atomic_add_fetch_32((x), 0) == old_) { \
    break;                                   \
  }                                          \
B
Bomin Zhang 已提交
96 97
  }

S
Shengliang Guan 已提交
98 99
#define taosCorBeginWrite(x) \
  taosCorBeginRead(x) if (atomic_val_compare_exchange_32((x), old_, old_ + 1) != old_) { continue; }
B
Bomin Zhang 已提交
100

S
Shengliang Guan 已提交
101 102 103
#define taosCorEndWrite(x)     \
  atomic_add_fetch_32((x), 1); \
  break;                       \
B
Bomin Zhang 已提交
104 105 106 107 108 109
  }

#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
110
#endif /*_TD_UTIL_LOCK_FREE_H_*/