osThread.c 10.0 KB
Newer Older
wafwerar's avatar
wafwerar 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define ALLOW_FORBID_FUNC
wafwerar's avatar
wafwerar 已提交
17
#include <pthread.h>
wafwerar's avatar
wafwerar 已提交
18 19
#include "os.h"

wafwerar's avatar
wafwerar 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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
int32_t taosThreadCreate(TdThread * tid, const TdThreadAttr * attr, void *(*start)(void *), void *arg) {
  return pthread_create(tid, attr, start, arg);
}

int32_t taosThreadAttrDestroy(TdThreadAttr * attr) {
  return pthread_attr_destroy(attr);
}

int32_t taosThreadAttrGetDetachState(const TdThreadAttr * attr, int32_t *detachstate) {
  return pthread_attr_getdetachstate(attr, detachstate);
}

int32_t taosThreadAttrGetInheritSched(const TdThreadAttr * attr, int32_t *inheritsched) {
  return pthread_attr_getinheritsched(attr, inheritsched);
}

int32_t taosThreadAttrGetSchedParam(const TdThreadAttr * attr, struct sched_param *param) {
  return pthread_attr_getschedparam(attr, param);
}

int32_t taosThreadAttrGetSchedPolicy(const TdThreadAttr * attr, int32_t *policy) {
  return pthread_attr_getschedpolicy(attr, policy);
}

int32_t taosThreadAttrGetScope(const TdThreadAttr * attr, int32_t *contentionscope) {
  return pthread_attr_getscope(attr, contentionscope);
}

int32_t taosThreadAttrGetStackSize(const TdThreadAttr * attr, size_t * stacksize) {
  return pthread_attr_getstacksize(attr, stacksize);
}

int32_t taosThreadAttrInit(TdThreadAttr * attr) {
  return pthread_attr_init(attr);
}

int32_t taosThreadAttrSetDetachState(TdThreadAttr * attr, int32_t detachstate) {
  return pthread_attr_setdetachstate(attr, detachstate);
}

int32_t taosThreadAttrSetInheritSched(TdThreadAttr * attr, int32_t inheritsched) {
  return pthread_attr_setinheritsched(attr, inheritsched);
}

int32_t taosThreadAttrSetSchedParam(TdThreadAttr * attr, const struct sched_param *param) {
  return pthread_attr_setschedparam(attr, param);
}

int32_t taosThreadAttrSetSchedPolicy(TdThreadAttr * attr, int32_t policy) {
  return pthread_attr_setschedpolicy(attr, policy);
}

int32_t taosThreadAttrSetScope(TdThreadAttr * attr, int32_t contentionscope) {
  return pthread_attr_setscope(attr, contentionscope);
}

int32_t taosThreadAttrSetStackSize(TdThreadAttr * attr, size_t stacksize) {
  return pthread_attr_setstacksize(attr, stacksize);
}

int32_t taosThreadCancel(TdThread thread) {
  return pthread_cancel(thread);
}

int32_t taosThreadCondDestroy(TdThreadCond * cond) {
  return pthread_cond_destroy(cond);
}

int32_t taosThreadCondInit(TdThreadCond * cond, const TdThreadCondAttr * attr) {
  return pthread_cond_init(cond, attr);
}

int32_t taosThreadCondSignal(TdThreadCond * cond) {
  return pthread_cond_signal(cond);
}

int32_t taosThreadCondBroadcast(TdThreadCond * cond) {
  return pthread_cond_broadcast(cond);
}

int32_t taosThreadCondWait(TdThreadCond * cond, TdThreadMutex * mutex) {
  return pthread_cond_wait(cond, mutex);
}

int32_t taosThreadCondTimedWait(TdThreadCond * cond, TdThreadMutex * mutex, const struct timespec *abstime) {
  return pthread_cond_timedwait(cond, mutex, abstime);
}

int32_t taosThreadCondAttrDestroy(TdThreadCondAttr * attr) {
  return pthread_condattr_destroy(attr);
}

int32_t taosThreadCondAttrGetPshared(const TdThreadCondAttr * attr, int32_t *pshared) {
  return pthread_condattr_getpshared(attr, pshared);
}

int32_t taosThreadCondAttrInit(TdThreadCondAttr * attr) {
  return pthread_condattr_init(attr);
}

int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr * attr, int32_t pshared) {
  return pthread_condattr_setpshared(attr, pshared);
}

int32_t taosThreadDetach(TdThread thread) {
  return pthread_detach(thread);
}
wafwerar's avatar
wafwerar 已提交
127

wafwerar's avatar
wafwerar 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141
int32_t taosThreadEqual(TdThread t1, TdThread t2) {
  return pthread_equal(t1, t2);
}

void taosThreadExit(void *valuePtr) {
  return pthread_exit(valuePtr);
}

int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param) {
  return pthread_getschedparam(thread, policy, param);
}

void *taosThreadGetSpecific(TdThreadKey key) {
  return pthread_getspecific(key);
wafwerar's avatar
wafwerar 已提交
142 143
}

wafwerar's avatar
wafwerar 已提交
144 145
int32_t taosThreadJoin(TdThread thread, void **valuePtr) {
  return pthread_join(thread, valuePtr);
wafwerar's avatar
wafwerar 已提交
146 147
}

wafwerar's avatar
wafwerar 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161
int32_t taosThreadKeyCreate(TdThreadKey * key, void(*destructor)(void *)) {
  return pthread_key_create(key, destructor);
}

int32_t taosThreadKeyDelete(TdThreadKey key) {
  return pthread_key_delete(key);
}

int32_t taosThreadKill(TdThread thread, int32_t sig) {
  return pthread_kill(thread, sig);
}

int32_t taosThreadMutexConsistent(TdThreadMutex* mutex) {
  return pthread_mutex_consistent(mutex);
wafwerar's avatar
wafwerar 已提交
162 163 164
}

int32_t taosThreadMutexDestroy(TdThreadMutex * mutex) {
wafwerar's avatar
wafwerar 已提交
165
  return pthread_mutex_destroy(mutex);
wafwerar's avatar
wafwerar 已提交
166 167
}

wafwerar's avatar
wafwerar 已提交
168 169
int32_t taosThreadMutexInit(TdThreadMutex * mutex, const TdThreadMutexAttr * attr) {
  return pthread_mutex_init(mutex, attr);
wafwerar's avatar
wafwerar 已提交
170 171
}

wafwerar's avatar
wafwerar 已提交
172 173
int32_t taosThreadMutexLock(TdThreadMutex * mutex) {
  return pthread_mutex_lock(mutex);
wafwerar's avatar
wafwerar 已提交
174 175
}

wafwerar's avatar
wafwerar 已提交
176 177
int32_t taosThreadMutexTimedLock(TdThreadMutex * mutex, const struct timespec *abstime) {
  return pthread_mutex_timedlock(mutex, abstime);
wafwerar's avatar
wafwerar 已提交
178 179
}

wafwerar's avatar
wafwerar 已提交
180 181
int32_t taosThreadMutexTryLock(TdThreadMutex * mutex) {
  return pthread_mutex_trylock(mutex);
wafwerar's avatar
wafwerar 已提交
182 183
}

wafwerar's avatar
wafwerar 已提交
184 185
int32_t taosThreadMutexUnlock(TdThreadMutex * mutex) {
  return pthread_mutex_unlock(mutex);
wafwerar's avatar
wafwerar 已提交
186 187
}

wafwerar's avatar
wafwerar 已提交
188 189
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr * attr) {
  return pthread_mutexattr_destroy(attr);
wafwerar's avatar
wafwerar 已提交
190 191
}

wafwerar's avatar
wafwerar 已提交
192 193
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr * attr, int32_t *pshared) {
  return pthread_mutexattr_getpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
194 195
}

wafwerar's avatar
wafwerar 已提交
196 197
int32_t taosThreadMutexAttrGetRobust(const TdThreadMutexAttr * attr, int32_t * robust) {
  return pthread_mutexattr_getrobust(attr, robust);
wafwerar's avatar
wafwerar 已提交
198 199
}

wafwerar's avatar
wafwerar 已提交
200 201
int32_t taosThreadMutexAttrGetType(const TdThreadMutexAttr * attr, int32_t *kind) {
  return pthread_mutexattr_gettype(attr, kind);
wafwerar's avatar
wafwerar 已提交
202 203
}

wafwerar's avatar
wafwerar 已提交
204 205
int32_t taosThreadMutexAttrInit(TdThreadMutexAttr * attr) {
  return pthread_mutexattr_init(attr);
wafwerar's avatar
wafwerar 已提交
206 207
}

wafwerar's avatar
wafwerar 已提交
208 209
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr * attr, int32_t pshared) {
  return pthread_mutexattr_setpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
210 211
}

wafwerar's avatar
wafwerar 已提交
212 213
int32_t taosThreadMutexAttrSetRobust(TdThreadMutexAttr * attr, int32_t robust) {
  return pthread_mutexattr_setrobust(attr, robust);
wafwerar's avatar
wafwerar 已提交
214 215
}

wafwerar's avatar
wafwerar 已提交
216 217
int32_t taosThreadMutexAttrSetType(TdThreadMutexAttr * attr, int32_t kind) {
  return pthread_mutexattr_settype(attr, kind);
wafwerar's avatar
wafwerar 已提交
218 219
}

wafwerar's avatar
wafwerar 已提交
220 221
int32_t taosThreadOnce(TdThreadOnce * onceControl, void(*initRoutine)(void)) {
  return pthread_once(onceControl, initRoutine);
wafwerar's avatar
wafwerar 已提交
222 223
}

wafwerar's avatar
wafwerar 已提交
224 225
int32_t taosThreadRwlockDestroy(TdThreadRwlock * rwlock) {
  return pthread_rwlock_destroy(rwlock);
wafwerar's avatar
wafwerar 已提交
226 227
}

wafwerar's avatar
wafwerar 已提交
228 229
int32_t taosThreadRwlockInit(TdThreadRwlock * rwlock, const TdThreadRwlockAttr * attr) {
  return pthread_rwlock_init(rwlock, attr);
wafwerar's avatar
wafwerar 已提交
230 231
}

wafwerar's avatar
wafwerar 已提交
232 233
int32_t taosThreadRwlockRdlock(TdThreadRwlock * rwlock) {
  return pthread_rwlock_rdlock(rwlock);
wafwerar's avatar
wafwerar 已提交
234 235
}

wafwerar's avatar
wafwerar 已提交
236 237
int32_t taosThreadRwlockTimedRdlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
  return pthread_rwlock_timedrdlock(rwlock, abstime);
wafwerar's avatar
wafwerar 已提交
238 239
}

wafwerar's avatar
wafwerar 已提交
240 241
int32_t taosThreadRwlockTimedWrlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
  return pthread_rwlock_timedwrlock(rwlock, abstime);
wafwerar's avatar
wafwerar 已提交
242 243
}

wafwerar's avatar
wafwerar 已提交
244 245
int32_t taosThreadRwlockTryRdlock(TdThreadRwlock * rwlock) {
  return pthread_rwlock_tryrdlock(rwlock);
wafwerar's avatar
wafwerar 已提交
246 247
}

wafwerar's avatar
wafwerar 已提交
248 249
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock * rwlock) {
  return pthread_rwlock_trywrlock(rwlock);
wafwerar's avatar
wafwerar 已提交
250 251
}

wafwerar's avatar
wafwerar 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
int32_t taosThreadRwlockUnlock(TdThreadRwlock * rwlock) {
  return pthread_rwlock_unlock(rwlock);
}

int32_t taosThreadRwlockWrlock(TdThreadRwlock * rwlock) {
  return pthread_rwlock_wrlock(rwlock);
}

int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr * attr) {
  return pthread_rwlockattr_destroy(attr);
}

int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr * attr, int32_t *pshared) {
  return pthread_rwlockattr_getpshared(attr, pshared);
}

int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr * attr) {
  return pthread_rwlockattr_init(attr);
}

int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr * attr, int32_t pshared) {
  return pthread_rwlockattr_setpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
274 275 276
}

TdThread taosThreadSelf(void) {
wafwerar's avatar
wafwerar 已提交
277
  return pthread_self();
wafwerar's avatar
wafwerar 已提交
278 279
}

wafwerar's avatar
wafwerar 已提交
280 281 282
int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate) {
  return pthread_setcancelstate(state, oldstate);
}
wafwerar's avatar
wafwerar 已提交
283

wafwerar's avatar
wafwerar 已提交
284 285
int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype) {
  return pthread_setcanceltype(type, oldtype);
wafwerar's avatar
wafwerar 已提交
286 287
}

wafwerar's avatar
wafwerar 已提交
288 289
int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sched_param *param) {
  return pthread_setschedparam(thread, policy, param);
wafwerar's avatar
wafwerar 已提交
290 291
}

wafwerar's avatar
wafwerar 已提交
292 293
int32_t taosThreadSetSpecific(TdThreadKey key, const void *value) {
  return pthread_setspecific(key, value);
wafwerar's avatar
wafwerar 已提交
294 295
}

wafwerar's avatar
wafwerar 已提交
296
int32_t taosThreadSpinDestroy(TdThreadSpinlock * lock) {
wafwerar's avatar
wafwerar 已提交
297
#ifdef TD_USE_SPINLOCK_AS_MUTEX
298 299 300 301
  return pthread_mutex_destroy((pthread_mutex_t*)lock);
#else
  return pthread_spin_destroy((pthread_spinlock_t*)lock);
#endif
wafwerar's avatar
wafwerar 已提交
302 303
}

wafwerar's avatar
wafwerar 已提交
304
int32_t taosThreadSpinInit(TdThreadSpinlock * lock, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
305 306 307
#ifdef TD_USE_SPINLOCK_AS_MUTEX
  assert(pshared == NULL);
  return pthread_mutex_init((pthread_mutex_t*)lock, NULL);
308 309 310
#else
  return pthread_spin_init((pthread_spinlock_t*)lock, pshared);
#endif
wafwerar's avatar
wafwerar 已提交
311 312 313
}

int32_t taosThreadSpinLock(TdThreadSpinlock * lock) {
wafwerar's avatar
wafwerar 已提交
314
#ifdef TD_USE_SPINLOCK_AS_MUTEX
315 316 317 318
  return pthread_mutex_lock((pthread_mutex_t*)lock);
#else
  return pthread_spin_lock((pthread_spinlock_t*)lock);
#endif
wafwerar's avatar
wafwerar 已提交
319 320 321
}

int32_t taosThreadSpinTrylock(TdThreadSpinlock * lock) {
wafwerar's avatar
wafwerar 已提交
322
#ifdef TD_USE_SPINLOCK_AS_MUTEX
323 324 325 326
  return pthread_mutex_trylock((pthread_mutex_t*)lock);
#else
  return pthread_spin_trylock((pthread_spinlock_t*)lock);
#endif
wafwerar's avatar
wafwerar 已提交
327 328 329
}

int32_t taosThreadSpinUnlock(TdThreadSpinlock * lock) {
wafwerar's avatar
wafwerar 已提交
330
#ifdef TD_USE_SPINLOCK_AS_MUTEX
331 332 333 334
  return pthread_mutex_unlock((pthread_mutex_t*)lock);
#else
  return pthread_spin_unlock((pthread_spinlock_t*)lock);
#endif
wafwerar's avatar
wafwerar 已提交
335 336 337 338 339 340
}

void taosThreadTestCancel(void) {
  return pthread_testcancel();
}

wafwerar's avatar
wafwerar 已提交
341 342
void taosThreadClear(TdThread *thread) {
  memset(thread, 0, sizeof(TdThread));
wafwerar's avatar
wafwerar 已提交
343
}