td-928: thread-safe 'copy on read'
also merge 'reference counting' and 'rwlatch' into 'lock-free'
This commit is contained in:
parent
7533b69064
commit
470cf1f668
|
@ -24,7 +24,7 @@
|
||||||
#include "qtsbuf.h"
|
#include "qtsbuf.h"
|
||||||
#include "taosdef.h"
|
#include "taosdef.h"
|
||||||
#include "tarray.h"
|
#include "tarray.h"
|
||||||
#include "tref.h"
|
#include "tlockfree.h"
|
||||||
#include "tsdb.h"
|
#include "tsdb.h"
|
||||||
#include "tsqlfunction.h"
|
#include "tsqlfunction.h"
|
||||||
#include "query.h"
|
#include "query.h"
|
||||||
|
|
|
@ -21,11 +21,10 @@
|
||||||
#include "tkvstore.h"
|
#include "tkvstore.h"
|
||||||
#include "tlist.h"
|
#include "tlist.h"
|
||||||
#include "tlog.h"
|
#include "tlog.h"
|
||||||
#include "tref.h"
|
#include "tlockfree.h"
|
||||||
#include "tsdb.h"
|
#include "tsdb.h"
|
||||||
#include "tskiplist.h"
|
#include "tskiplist.h"
|
||||||
#include "tutil.h"
|
#include "tutil.h"
|
||||||
#include "trwlatch.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
|
@ -21,7 +21,7 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
#include "tref.h"
|
#include "tlockfree.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
|
||||||
typedef void (*__cache_free_fn_t)(void*);
|
typedef void (*__cache_free_fn_t)(void*);
|
||||||
|
|
|
@ -12,12 +12,17 @@
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* 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/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
#ifndef __TD_LOCK_FREE_H__
|
||||||
#ifndef TDENGINE_TREF_H
|
#define __TD_LOCK_FREE_H__
|
||||||
#define TDENGINE_TREF_H
|
|
||||||
|
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// reference counting
|
||||||
typedef void (*_ref_fn_t)(const void* pObj);
|
typedef void (*_ref_fn_t)(const void* pObj);
|
||||||
|
|
||||||
#define T_REF_DECLARE() \
|
#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
|
#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
|
|
@ -1,36 +0,0 @@
|
||||||
/*
|
|
||||||
* 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
|
// #define _GNU_SOURCE
|
||||||
// #include <pthread.h>
|
// #include <pthread.h>
|
||||||
|
|
||||||
#include "trwlatch.h"
|
#include "tlockfree.h"
|
||||||
#include "os.h"
|
|
||||||
|
|
||||||
#define TD_RWLATCH_WRITE_FLAG 0x40000000
|
#define TD_RWLATCH_WRITE_FLAG 0x40000000
|
||||||
|
|
Loading…
Reference in New Issue