未验证 提交 5fa90830 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #2668 from taosdata/feature/td-928

td-928: thread-safe 'copy on read'
......@@ -24,7 +24,7 @@
#include "qtsbuf.h"
#include "taosdef.h"
#include "tarray.h"
#include "tref.h"
#include "tlockfree.h"
#include "tsdb.h"
#include "tsqlfunction.h"
#include "query.h"
......
......@@ -21,11 +21,10 @@
#include "tkvstore.h"
#include "tlist.h"
#include "tlog.h"
#include "tref.h"
#include "tlockfree.h"
#include "tsdb.h"
#include "tskiplist.h"
#include "tutil.h"
#include "trwlatch.h"
#ifdef __cplusplus
extern "C" {
......
......@@ -21,7 +21,7 @@ extern "C" {
#endif
#include "os.h"
#include "tref.h"
#include "tlockfree.h"
#include "hash.h"
typedef void (*__cache_free_fn_t)(void*);
......
......@@ -12,12 +12,17 @@
* 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/>.
*/
#ifndef TDENGINE_TREF_H
#define TDENGINE_TREF_H
#ifndef __TD_LOCK_FREE_H__
#define __TD_LOCK_FREE_H__
#include "os.h"
#ifdef __cplusplus
extern "C" {
#endif
// reference counting
typedef void (*_ref_fn_t)(const void* pObj);
#define T_REF_DECLARE() \
......@@ -55,4 +60,47 @@ typedef void (*_ref_fn_t)(const void* pObj);
#define T_REF_VAL_GET(x) (x)->_ref.val
#endif // TDENGINE_TREF_H
// 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
/*
* 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/>.
*/
#ifndef __TD_RWLATCH_H__
#define __TD_RWLATCH_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
typedef int32_t SRWLatch;
void taosInitRWLatch(SRWLatch *pLatch);
void taosWLockLatch(SRWLatch *pLatch);
void taosWUnLockLatch(SRWLatch *pLatch);
void taosRLockLatch(SRWLatch *pLatch);
void taosRUnLockLatch(SRWLatch *pLatch);
#ifdef __cplusplus
}
#endif
#endif
......@@ -15,8 +15,7 @@
// #define _GNU_SOURCE
// #include <pthread.h>
#include "trwlatch.h"
#include "os.h"
#include "tlockfree.h"
#define TD_RWLATCH_WRITE_FLAG 0x40000000
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册