tlockfree.h 2.9 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/>.
 */
B
Bomin Zhang 已提交
15 16
#ifndef __TD_LOCK_FREE_H__
#define __TD_LOCK_FREE_H__
H
hjxilinx 已提交
17 18 19

#include "os.h"

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


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

#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)};

dengyihao's avatar
dengyihao 已提交
39
#define T_REF_INC(x) (atomic_add_fetch_32(&((x)->_ref.val), 1))
H
hjxilinx 已提交
40 41 42 43 44 45 46 47 48

#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 已提交
49
#define T_REF_DEC(x) (atomic_sub_fetch_32(&((x)->_ref.val), 1))
H
hjxilinx 已提交
50 51 52

#define T_REF_DEC_WITH_CB(x, p)                           \
  do {                                                    \
dengyihao's avatar
dengyihao 已提交
53
    int32_t v = atomic_sub_fetch_32(&((x)->_ref.val), 1); \
H
hjxilinx 已提交
54 55 56 57 58 59 60 61 62
    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 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106


// single writer multiple reader lock
typedef int32_t SRWLatch;

void taosInitRWLatch(SRWLatch *pLatch);
void taosWLockLatch(SRWLatch *pLatch);
void taosWUnLockLatch(SRWLatch *pLatch);
void taosRLockLatch(SRWLatch *pLatch);
void taosRUnLockLatch(SRWLatch *pLatch);



// copy on read
#define taosCorBeginRead(x) for (uint32_t i_ = 1; 1; ++i_) { \
    int32_t old_ = atomic_load_32(x); \
    if (old_ & 0x00000001) { \
      if (i_ % 1000 == 0) { \
        sched_yield(); \
      } \
      continue; \
    }

#define taosCorEndRead(x) \
    if (atomic_load_32(x) == old_) { \
      break; \
    } \
  }

#define taosCorBeginWrite(x) taosCorBeginRead(x) \
    if (atomic_val_compare_exchange_32((x), old_, old_ + 1) != old_) { \
        continue; \
    }

#define taosCorEndWrite(x) atomic_add_fetch_32((x), 1); \
    break; \
  }


#ifdef __cplusplus
}
#endif

#endif