osThread.h 13.8 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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_OS_THREAD_H_
#define _TD_OS_THREAD_H_

S
os  
Shengliang Guan 已提交
19 20
#include <pthread.h>

S
Shengliang Guan 已提交
21 22 23 24
#ifdef __cplusplus
extern "C" {
#endif

wafwerar's avatar
wafwerar 已提交
25
#ifndef WINDOWS
26
#ifndef __USE_XOPEN2K
wafwerar's avatar
wafwerar 已提交
27
#define TD_USE_SPINLOCK_AS_MUTEX
28 29
typedef pthread_mutex_t pthread_spinlock_t;
#endif
wafwerar's avatar
wafwerar 已提交
30
#endif
31

wafwerar's avatar
wafwerar 已提交
32 33 34 35 36 37 38 39 40 41
typedef pthread_t TdThread;
typedef pthread_spinlock_t TdThreadSpinlock;
typedef pthread_mutex_t TdThreadMutex;
typedef pthread_mutexattr_t TdThreadMutexAttr;
typedef pthread_rwlock_t TdThreadRwlock;
typedef pthread_attr_t TdThreadAttr;
typedef pthread_once_t TdThreadOnce;
typedef pthread_rwlockattr_t TdThreadRwlockAttr;
typedef pthread_cond_t TdThreadCond;
typedef pthread_condattr_t TdThreadCondAttr;
wafwerar's avatar
wafwerar 已提交
42
typedef pthread_key_t TdThreadKey;
wafwerar's avatar
wafwerar 已提交
43 44 45 46

#define taosThreadCleanupPush pthread_cleanup_push
#define taosThreadCleanupPop pthread_cleanup_pop

wafwerar's avatar
wafwerar 已提交
47 48 49 50 51 52 53

#ifdef WINDOWS
#define TD_PTHREAD_MUTEX_INITIALIZER (TdThreadMutex)(-1)
#else
#define TD_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#endif

wafwerar's avatar
wafwerar 已提交
54
// If the error is in a third-party library, place this header file under the third-party library header file.
wafwerar's avatar
wafwerar 已提交
55
// When you want to use this feature, you should find or add the same function in the following section.
wafwerar's avatar
wafwerar 已提交
56
#ifndef ALLOW_FORBID_FUNC
wafwerar's avatar
wafwerar 已提交
57 58 59 60 61 62 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    #define pthread_t PTHREAD_T_TYPE_TAOS_FORBID
    #define pthread_spinlock_t PTHREAD_SPINLOCK_T_TYPE_TAOS_FORBID
    #define pthread_mutex_t PTHREAD_MUTEX_T_TYPE_TAOS_FORBID
    #define pthread_mutexattr_t PTHREAD_MUTEXATTR_T_TYPE_TAOS_FORBID
    #define pthread_rwlock_t PTHREAD_RWLOCK_T_TYPE_TAOS_FORBID
    #define pthread_attr_t PTHREAD_ATTR_T_TYPE_TAOS_FORBID
    #define pthread_once_t PTHREAD_ONCE_T_TYPE_TAOS_FORBID
    #define pthread_rwlockattr_t PTHREAD_RWLOCKATTR_T_TYPE_TAOS_FORBID
    #define pthread_cond_t PTHREAD_COND_T_TYPE_TAOS_FORBID
    #define pthread_condattr_t PTHREAD_CONDATTR_T_TYPE_TAOS_FORBID
    #define pthread_key_t PTHREAD_KEY_T_TYPE_TAOS_FORBID
    #define pthread_barrier_t PTHREAD_BARRIER_T_TYPE_TAOS_FORBID
    #define pthread_barrierattr_t PTHREAD_BARRIERATTR_T_TYPE_TAOS_FORBID
    #define pthread_create PTHREAD_CREATE_FUNC_TAOS_FORBID
    #define pthread_attr_destroy PTHREAD_ATTR_DESTROY_FUNC_TAOS_FORBID
    #define pthread_attr_getdetachstate PTHREAD_ATTR_GETDETACHSTATE_FUNC_TAOS_FORBID
    #define pthread_attr_getinheritsched PTHREAD_ATTR_GETINHERITSCHED_FUNC_TAOS_FORBID
    #define pthread_attr_getschedparam PTHREAD_ATTR_GETSCHEDPARAM_FUNC_TAOS_FORBID
    #define pthread_attr_getschedpolicy PTHREAD_ATTR_GETSCHEDPOLICY_FUNC_TAOS_FORBID
    #define pthread_attr_getscope PTHREAD_ATTR_GETSCOPE_FUNC_TAOS_FORBID
    #define pthread_attr_getstacksize PTHREAD_ATTR_GETSTACKSIZE_FUNC_TAOS_FORBID
    #define pthread_attr_init PTHREAD_ATTR_INIT_FUNC_TAOS_FORBID
    #define pthread_attr_setdetachstate PTHREAD_ATTR_SETDETACHSTATE_FUNC_TAOS_FORBID
    #define pthread_attr_setinheritsched PTHREAD_ATTR_SETINHERITSCHED_FUNC_TAOS_FORBID
    #define pthread_attr_setschedparam PTHREAD_ATTR_SETSCHEDPARAM_FUNC_TAOS_FORBID
    #define pthread_attr_setschedpolicy PTHREAD_ATTR_SETSCHEDPOLICY_FUNC_TAOS_FORBID
    #define pthread_attr_setscope PTHREAD_ATTR_SETSCOPE_FUNC_TAOS_FORBID
    #define pthread_attr_setstacksize PTHREAD_ATTR_SETSTACKSIZE_FUNC_TAOS_FORBID
    #define pthread_barrier_destroy PTHREAD_BARRIER_DESTROY_FUNC_TAOS_FORBID
    #define pthread_barrier_init PTHREAD_BARRIER_INIT_FUNC_TAOS_FORBID
    #define pthread_barrier_wait PTHREAD_BARRIER_WAIT_FUNC_TAOS_FORBID
    #define pthread_barrierattr_destroy PTHREAD_BARRIERATTR_DESTROY_FUNC_TAOS_FORBID
    #define pthread_barrierattr_getpshared PTHREAD_BARRIERATTR_GETPSHARED_FUNC_TAOS_FORBID
    #define pthread_barrierattr_init PTHREAD_BARRIERATTR_INIT_FUNC_TAOS_FORBID
    #define pthread_barrierattr_setpshared PTHREAD_BARRIERATTR_SETPSHARED_FUNC_TAOS_FORBID
    #define pthread_cancel PTHREAD_CANCEL_FUNC_TAOS_FORBID
    #define pthread_cond_destroy PTHREAD_COND_DESTROY_FUNC_TAOS_FORBID
    #define pthread_cond_init PTHREAD_COND_INIT_FUNC_TAOS_FORBID
    #define pthread_cond_signal PTHREAD_COND_SIGNAL_FUNC_TAOS_FORBID
    #define pthread_cond_broadcast PTHREAD_COND_BROADCAST_FUNC_TAOS_FORBID
    #define pthread_cond_wait PTHREAD_COND_WAIT_FUNC_TAOS_FORBID
    #define pthread_cond_timedwait PTHREAD_COND_TIMEDWAIT_FUNC_TAOS_FORBID
    #define pthread_condattr_destroy PTHREAD_CONDATTR_DESTROY_FUNC_TAOS_FORBID
    #define pthread_condattr_getpshared PTHREAD_CONDATTR_GETPSHARED_FUNC_TAOS_FORBID
    #define pthread_condattr_init PTHREAD_CONDATTR_INIT_FUNC_TAOS_FORBID
    #define pthread_condattr_setpshared PTHREAD_CONDATTR_SETPSHARED_FUNC_TAOS_FORBID
    #define pthread_detach PTHREAD_DETACH_FUNC_TAOS_FORBID
    #define pthread_equal PTHREAD_EQUAL_FUNC_TAOS_FORBID
    #define pthread_exit PTHREAD_EXIT_FUNC_TAOS_FORBID
    #define pthread_getschedparam PTHREAD_GETSCHEDPARAM_FUNC_TAOS_FORBID
    #define pthread_getspecific PTHREAD_GETSPECIFIC_FUNC_TAOS_FORBID
    #define pthread_join PTHREAD_JOIN_FUNC_TAOS_FORBID
    #define pthread_key_create PTHREAD_KEY_CREATE_FUNC_TAOS_FORBID
    #define pthread_key_delete PTHREAD_KEY_DELETE_FUNC_TAOS_FORBID
    #define pthread_kill PTHREAD_KILL_FUNC_TAOS_FORBID
    #define pthread_mutex_consistent PTHREAD_MUTEX_CONSISTENT_FUNC_TAOS_FORBID
    #define pthread_mutex_destroy PTHREAD_MUTEX_DESTROY_FUNC_TAOS_FORBID
    #define pthread_mutex_init PTHREAD_MUTEX_INIT_FUNC_TAOS_FORBID
    #define pthread_mutex_lock PTHREAD_MUTEX_LOCK_FUNC_TAOS_FORBID
    #define pthread_mutex_timedlock PTHREAD_MUTEX_TIMEDLOCK_FUNC_TAOS_FORBID
    #define pthread_mutex_trylock PTHREAD_MUTEX_TRYLOCK_FUNC_TAOS_FORBID
    #define pthread_mutex_unlock PTHREAD_MUTEX_UNLOCK_FUNC_TAOS_FORBID
    #define pthread_mutexattr_destroy PTHREAD_MUTEXATTR_DESTROY_FUNC_TAOS_FORBID
    #define pthread_mutexattr_getpshared PTHREAD_MUTEXATTR_GETPSHARED_FUNC_TAOS_FORBID
    #define pthread_mutexattr_getrobust PTHREAD_MUTEXATTR_GETROBUST_FUNC_TAOS_FORBID
    #define pthread_mutexattr_gettype PTHREAD_MUTEXATTR_GETTYPE_FUNC_TAOS_FORBID
    #define pthread_mutexattr_init PTHREAD_MUTEXATTR_INIT_FUNC_TAOS_FORBID
    #define pthread_mutexattr_setpshared PTHREAD_MUTEXATTR_SETPSHARED_FUNC_TAOS_FORBID
    #define pthread_mutexattr_setrobust PTHREAD_MUTEXATTR_SETROBUST_FUNC_TAOS_FORBID
    #define pthread_mutexattr_settype PTHREAD_MUTEXATTR_SETTYPE_FUNC_TAOS_FORBID
    #define pthread_once PTHREAD_ONCE_FUNC_TAOS_FORBID
    #define pthread_rwlock_destroy PTHREAD_RWLOCK_DESTROY_FUNC_TAOS_FORBID
    #define pthread_rwlock_init PTHREAD_RWLOCK_INIT_FUNC_TAOS_FORBID
    #define pthread_rwlock_rdlock PTHREAD_RWLOCK_RDLOCK_FUNC_TAOS_FORBID
    #define pthread_rwlock_timedrdlock PTHREAD_RWLOCK_TIMEDRDLOCK_FUNC_TAOS_FORBID
    #define pthread_rwlock_timedwrlock PTHREAD_RWLOCK_TIMEDWRLOCK_FUNC_TAOS_FORBID
    #define pthread_rwlock_tryrdlock PTHREAD_RWLOCK_TRYRDLOCK_FUNC_TAOS_FORBID
    #define pthread_rwlock_trywrlock PTHREAD_RWLOCK_TRYWRLOCK_FUNC_TAOS_FORBID
    #define pthread_rwlock_unlock PTHREAD_RWLOCK_UNLOCK_FUNC_TAOS_FORBID
    #define pthread_rwlock_wrlock PTHREAD_RWLOCK_WRLOCK_FUNC_TAOS_FORBID
    #define pthread_rwlockattr_destroy PTHREAD_RWLOCKATTR_DESTROY_FUNC_TAOS_FORBID
    #define pthread_rwlockattr_getpshared PTHREAD_RWLOCKATTR_GETPSHARED_FUNC_TAOS_FORBID
    #define pthread_rwlockattr_init PTHREAD_RWLOCKATTR_INIT_FUNC_TAOS_FORBID
    #define pthread_rwlockattr_setpshared PTHREAD_RWLOCKATTR_SETPSHARED_FUNC_TAOS_FORBID
    #define pthread_self PTHREAD_SELF_FUNC_TAOS_FORBID
    #define pthread_setcancelstate PTHREAD_SETCANCELSTATE_FUNC_TAOS_FORBID
    #define pthread_setcanceltype PTHREAD_SETCANCELTYPE_FUNC_TAOS_FORBID
    #define pthread_setschedparam PTHREAD_SETSCHEDPARAM_FUNC_TAOS_FORBID
    #define pthread_setspecific PTHREAD_SETSPECIFIC_FUNC_TAOS_FORBID
    #define pthread_spin_destroy PTHREAD_SPIN_DESTROY_FUNC_TAOS_FORBID
    #define pthread_spin_init PTHREAD_SPIN_INIT_FUNC_TAOS_FORBID
    #define pthread_spin_lock PTHREAD_SPIN_LOCK_FUNC_TAOS_FORBID
    #define pthread_spin_trylock PTHREAD_SPIN_TRYLOCK_FUNC_TAOS_FORBID
    #define pthread_spin_unlock PTHREAD_SPIN_UNLOCK_FUNC_TAOS_FORBID
    #define pthread_testcancel PTHREAD_TESTCANCEL_FUNC_TAOS_FORBID
    #define pthread_sigmask PTHREAD_SIGMASK_FUNC_TAOS_FORBID
    #define sigwait SIGWAIT_FUNC_TAOS_FORBID
wafwerar's avatar
wafwerar 已提交
154 155
#endif

wafwerar's avatar
wafwerar 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
int32_t taosThreadCreate(TdThread * tid, const TdThreadAttr * attr, void *(*start)(void *), void *arg);
int32_t taosThreadAttrDestroy(TdThreadAttr * attr);
int32_t taosThreadAttrGetDetachState(const TdThreadAttr * attr, int32_t *detachstate);
int32_t taosThreadAttrGetInheritSched(const TdThreadAttr * attr, int32_t *inheritsched);
int32_t taosThreadAttrGetSchedParam(const TdThreadAttr * attr, struct sched_param *param);
int32_t taosThreadAttrGetSchedPolicy(const TdThreadAttr * attr, int32_t *policy);
int32_t taosThreadAttrGetScope(const TdThreadAttr * attr, int32_t *contentionscope);
int32_t taosThreadAttrGetStackSize(const TdThreadAttr * attr, size_t * stacksize);
int32_t taosThreadAttrInit(TdThreadAttr * attr);
int32_t taosThreadAttrSetDetachState(TdThreadAttr * attr, int32_t detachstate);
int32_t taosThreadAttrSetInheritSched(TdThreadAttr * attr, int32_t inheritsched);
int32_t taosThreadAttrSetSchedParam(TdThreadAttr * attr, const struct sched_param *param);
int32_t taosThreadAttrSetSchedPolicy(TdThreadAttr * attr, int32_t policy);
int32_t taosThreadAttrSetScope(TdThreadAttr * attr, int32_t contentionscope);
int32_t taosThreadAttrSetStackSize(TdThreadAttr * attr, size_t stacksize);
wafwerar's avatar
wafwerar 已提交
171
int32_t taosThreadCancel(TdThread thread);
wafwerar's avatar
wafwerar 已提交
172 173 174 175 176 177 178 179 180 181
int32_t taosThreadCondDestroy(TdThreadCond * cond);
int32_t taosThreadCondInit(TdThreadCond * cond, const TdThreadCondAttr * attr);
int32_t taosThreadCondSignal(TdThreadCond * cond);
int32_t taosThreadCondBroadcast(TdThreadCond * cond);
int32_t taosThreadCondWait(TdThreadCond * cond, TdThreadMutex * mutex);
int32_t taosThreadCondTimedWait(TdThreadCond * cond, TdThreadMutex * mutex, const struct timespec *abstime);
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr * attr);
int32_t taosThreadCondAttrGetPshared(const TdThreadCondAttr * attr, int32_t *pshared);
int32_t taosThreadCondAttrInit(TdThreadCondAttr * attr);
int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr * attr, int32_t pshared);
wafwerar's avatar
wafwerar 已提交
182
int32_t taosThreadDetach(TdThread thread);
wafwerar's avatar
wafwerar 已提交
183 184 185 186 187 188 189 190
int32_t taosThreadEqual(TdThread t1, TdThread t2);
void taosThreadExit(void *valuePtr);
int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param);
void *taosThreadGetSpecific(TdThreadKey key);
int32_t taosThreadJoin(TdThread thread, void **valuePtr);
int32_t taosThreadKeyCreate(TdThreadKey * key, void(*destructor)(void *));
int32_t taosThreadKeyDelete(TdThreadKey key);
int32_t taosThreadKill(TdThread thread, int32_t sig);
wafwerar's avatar
wafwerar 已提交
191
// int32_t taosThreadMutexConsistent(TdThreadMutex* mutex);
wafwerar's avatar
wafwerar 已提交
192 193 194
int32_t taosThreadMutexDestroy(TdThreadMutex * mutex);
int32_t taosThreadMutexInit(TdThreadMutex * mutex, const TdThreadMutexAttr * attr);
int32_t taosThreadMutexLock(TdThreadMutex * mutex);
wafwerar's avatar
wafwerar 已提交
195
// int32_t taosThreadMutexTimedLock(TdThreadMutex * mutex, const struct timespec *abstime);
wafwerar's avatar
wafwerar 已提交
196 197 198 199
int32_t taosThreadMutexTryLock(TdThreadMutex * mutex);
int32_t taosThreadMutexUnlock(TdThreadMutex * mutex);
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr * attr);
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr * attr, int32_t *pshared);
wafwerar's avatar
wafwerar 已提交
200
// int32_t taosThreadMutexAttrGetRobust(const TdThreadMutexAttr * attr, int32_t * robust);
wafwerar's avatar
wafwerar 已提交
201 202 203
int32_t taosThreadMutexAttrGetType(const TdThreadMutexAttr * attr, int32_t *kind);
int32_t taosThreadMutexAttrInit(TdThreadMutexAttr * attr);
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr * attr, int32_t pshared);
wafwerar's avatar
wafwerar 已提交
204
// int32_t taosThreadMutexAttrSetRobust(TdThreadMutexAttr * attr, int32_t robust);
wafwerar's avatar
wafwerar 已提交
205 206 207 208 209
int32_t taosThreadMutexAttrSetType(TdThreadMutexAttr * attr, int32_t kind);
int32_t taosThreadOnce(TdThreadOnce * onceControl, void(*initRoutine)(void));
int32_t taosThreadRwlockDestroy(TdThreadRwlock * rwlock);
int32_t taosThreadRwlockInit(TdThreadRwlock * rwlock, const TdThreadRwlockAttr * attr);
int32_t taosThreadRwlockRdlock(TdThreadRwlock * rwlock);
wafwerar's avatar
wafwerar 已提交
210 211
// int32_t taosThreadRwlockTimedRdlock(TdThreadRwlock * rwlock, const struct timespec *abstime);
// int32_t taosThreadRwlockTimedWrlock(TdThreadRwlock * rwlock, const struct timespec *abstime);
wafwerar's avatar
wafwerar 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
int32_t taosThreadRwlockTryRdlock(TdThreadRwlock * rwlock);
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock * rwlock);
int32_t taosThreadRwlockUnlock(TdThreadRwlock * rwlock);
int32_t taosThreadRwlockWrlock(TdThreadRwlock * rwlock);
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr * attr);
int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr * attr, int32_t *pshared);
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr * attr);
int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr * attr, int32_t pshared);
TdThread taosThreadSelf(void);
int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate);
int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype);
int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sched_param *param);
int32_t taosThreadSetSpecific(TdThreadKey key, const void *value);
int32_t taosThreadSpinDestroy(TdThreadSpinlock * lock);
int32_t taosThreadSpinInit(TdThreadSpinlock * lock, int32_t pshared);
int32_t taosThreadSpinLock(TdThreadSpinlock * lock);
int32_t taosThreadSpinTrylock(TdThreadSpinlock * lock);
int32_t taosThreadSpinUnlock(TdThreadSpinlock * lock);
void taosThreadTestCancel(void);
wafwerar's avatar
wafwerar 已提交
231
void taosThreadClear(TdThread *thread);
wafwerar's avatar
wafwerar 已提交
232

S
Shengliang Guan 已提交
233 234 235 236 237
#ifdef __cplusplus
}
#endif

#endif /*_TD_OS_THREAD_H_*/