tlockfree.h 3.2 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)};

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 72 73


// single writer multiple reader lock
H
refact  
Hongze Cheng 已提交
74
typedef volatile int32_t SRWLatch;
B
Bomin Zhang 已提交
75 76 77 78 79 80 81 82 83 84 85

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_) { \
dengyihao's avatar
dengyihao 已提交
86
    int32_t old_ = atomic_add_fetch_32((x), 0); \
B
Bomin Zhang 已提交
87 88 89 90 91 92 93 94
    if (old_ & 0x00000001) { \
      if (i_ % 1000 == 0) { \
        sched_yield(); \
      } \
      continue; \
    }

#define taosCorEndRead(x) \
dengyihao's avatar
dengyihao 已提交
95
    if (atomic_add_fetch_32((x), 0) == old_) { \
B
Bomin Zhang 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
      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