osThread.c 10.4 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"

20 21 22 23 24 25 26 27 28
#ifdef WINDOWS
#define THREAD_PTR_CHECK(p)        \
  do {                             \
    if (!(p) || !(*(p))) return 0; \
  } while (0);
#else
#define THREAD_PTR_CHECK(p)
#endif

H
Hongze Cheng 已提交
29
int32_t taosThreadCreate(TdThread *tid, const TdThreadAttr *attr, void *(*start)(void *), void *arg) {
wafwerar's avatar
wafwerar 已提交
30 31 32
  return pthread_create(tid, attr, start, arg);
}

H
Hongze Cheng 已提交
33
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) { return pthread_attr_destroy(attr); }
wafwerar's avatar
wafwerar 已提交
34

H
Hongze Cheng 已提交
35
int32_t taosThreadAttrGetDetachState(const TdThreadAttr *attr, int32_t *detachstate) {
wafwerar's avatar
wafwerar 已提交
36 37 38
  return pthread_attr_getdetachstate(attr, detachstate);
}

H
Hongze Cheng 已提交
39
int32_t taosThreadAttrGetInheritSched(const TdThreadAttr *attr, int32_t *inheritsched) {
wafwerar's avatar
wafwerar 已提交
40 41 42
  return pthread_attr_getinheritsched(attr, inheritsched);
}

H
Hongze Cheng 已提交
43
int32_t taosThreadAttrGetSchedParam(const TdThreadAttr *attr, struct sched_param *param) {
wafwerar's avatar
wafwerar 已提交
44 45 46
  return pthread_attr_getschedparam(attr, param);
}

H
Hongze Cheng 已提交
47
int32_t taosThreadAttrGetSchedPolicy(const TdThreadAttr *attr, int32_t *policy) {
wafwerar's avatar
wafwerar 已提交
48 49 50
  return pthread_attr_getschedpolicy(attr, policy);
}

H
Hongze Cheng 已提交
51
int32_t taosThreadAttrGetScope(const TdThreadAttr *attr, int32_t *contentionscope) {
wafwerar's avatar
wafwerar 已提交
52 53 54
  return pthread_attr_getscope(attr, contentionscope);
}

H
Hongze Cheng 已提交
55
int32_t taosThreadAttrGetStackSize(const TdThreadAttr *attr, size_t *stacksize) {
wafwerar's avatar
wafwerar 已提交
56 57 58
  return pthread_attr_getstacksize(attr, stacksize);
}

H
Hongze Cheng 已提交
59
int32_t taosThreadAttrInit(TdThreadAttr *attr) { return pthread_attr_init(attr); }
wafwerar's avatar
wafwerar 已提交
60

H
Hongze Cheng 已提交
61
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
wafwerar's avatar
wafwerar 已提交
62 63 64
  return pthread_attr_setdetachstate(attr, detachstate);
}

H
Hongze Cheng 已提交
65
int32_t taosThreadAttrSetInheritSched(TdThreadAttr *attr, int32_t inheritsched) {
wafwerar's avatar
wafwerar 已提交
66 67 68
  return pthread_attr_setinheritsched(attr, inheritsched);
}

H
Hongze Cheng 已提交
69
int32_t taosThreadAttrSetSchedParam(TdThreadAttr *attr, const struct sched_param *param) {
wafwerar's avatar
wafwerar 已提交
70 71 72
  return pthread_attr_setschedparam(attr, param);
}

H
Hongze Cheng 已提交
73
int32_t taosThreadAttrSetSchedPolicy(TdThreadAttr *attr, int32_t policy) {
wafwerar's avatar
wafwerar 已提交
74 75 76
  return pthread_attr_setschedpolicy(attr, policy);
}

H
Hongze Cheng 已提交
77
int32_t taosThreadAttrSetScope(TdThreadAttr *attr, int32_t contentionscope) {
wafwerar's avatar
wafwerar 已提交
78 79 80
  return pthread_attr_setscope(attr, contentionscope);
}

H
Hongze Cheng 已提交
81
int32_t taosThreadAttrSetStackSize(TdThreadAttr *attr, size_t stacksize) {
wafwerar's avatar
wafwerar 已提交
82 83 84
  return pthread_attr_setstacksize(attr, stacksize);
}

H
Hongze Cheng 已提交
85
int32_t taosThreadCancel(TdThread thread) { return pthread_cancel(thread); }
wafwerar's avatar
wafwerar 已提交
86

H
Hongze Cheng 已提交
87
int32_t taosThreadCondDestroy(TdThreadCond *cond) { return pthread_cond_destroy(cond); }
wafwerar's avatar
wafwerar 已提交
88

H
Hongze Cheng 已提交
89
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) { return pthread_cond_init(cond, attr); }
wafwerar's avatar
wafwerar 已提交
90

H
Hongze Cheng 已提交
91
int32_t taosThreadCondSignal(TdThreadCond *cond) { return pthread_cond_signal(cond); }
wafwerar's avatar
wafwerar 已提交
92

H
Hongze Cheng 已提交
93
int32_t taosThreadCondBroadcast(TdThreadCond *cond) { return pthread_cond_broadcast(cond); }
wafwerar's avatar
wafwerar 已提交
94

95 96 97 98
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
  THREAD_PTR_CHECK(mutex)
  return pthread_cond_wait(cond, mutex);
}
wafwerar's avatar
wafwerar 已提交
99

H
Hongze Cheng 已提交
100
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
101
  THREAD_PTR_CHECK(mutex)
wafwerar's avatar
wafwerar 已提交
102 103 104
  return pthread_cond_timedwait(cond, mutex, abstime);
}

H
Hongze Cheng 已提交
105
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr *attr) { return pthread_condattr_destroy(attr); }
wafwerar's avatar
wafwerar 已提交
106

H
Hongze Cheng 已提交
107
int32_t taosThreadCondAttrGetPshared(const TdThreadCondAttr *attr, int32_t *pshared) {
wafwerar's avatar
wafwerar 已提交
108 109 110
  return pthread_condattr_getpshared(attr, pshared);
}

H
Hongze Cheng 已提交
111
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) { return pthread_condattr_init(attr); }
wafwerar's avatar
wafwerar 已提交
112

H
Hongze Cheng 已提交
113
int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr *attr, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
114 115 116
  return pthread_condattr_setpshared(attr, pshared);
}

H
Hongze Cheng 已提交
117
int32_t taosThreadDetach(TdThread thread) { return pthread_detach(thread); }
wafwerar's avatar
wafwerar 已提交
118

H
Hongze Cheng 已提交
119
int32_t taosThreadEqual(TdThread t1, TdThread t2) { return pthread_equal(t1, t2); }
wafwerar's avatar
wafwerar 已提交
120

H
Hongze Cheng 已提交
121
void taosThreadExit(void *valuePtr) { return pthread_exit(valuePtr); }
wafwerar's avatar
wafwerar 已提交
122 123 124 125 126

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

H
Hongze Cheng 已提交
127
void *taosThreadGetSpecific(TdThreadKey key) { return pthread_getspecific(key); }
wafwerar's avatar
wafwerar 已提交
128

H
Hongze Cheng 已提交
129
int32_t taosThreadJoin(TdThread thread, void **valuePtr) { return pthread_join(thread, valuePtr); }
wafwerar's avatar
wafwerar 已提交
130

H
Hongze Cheng 已提交
131
int32_t taosThreadKeyCreate(TdThreadKey *key, void (*destructor)(void *)) {
wafwerar's avatar
wafwerar 已提交
132 133 134
  return pthread_key_create(key, destructor);
}

H
Hongze Cheng 已提交
135
int32_t taosThreadKeyDelete(TdThreadKey key) { return pthread_key_delete(key); }
wafwerar's avatar
wafwerar 已提交
136

H
Hongze Cheng 已提交
137
int32_t taosThreadKill(TdThread thread, int32_t sig) { return pthread_kill(thread, sig); }
wafwerar's avatar
wafwerar 已提交
138

wafwerar's avatar
wafwerar 已提交
139
// int32_t taosThreadMutexConsistent(TdThreadMutex* mutex) {
140
//   THREAD_PTR_CHECK(mutex)
wafwerar's avatar
wafwerar 已提交
141 142
//   return pthread_mutex_consistent(mutex);
// }
wafwerar's avatar
wafwerar 已提交
143

144 145 146 147
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
  THREAD_PTR_CHECK(mutex)
  return pthread_mutex_destroy(mutex);
}
wafwerar's avatar
wafwerar 已提交
148

H
Hongze Cheng 已提交
149
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
wafwerar's avatar
wafwerar 已提交
150
  return pthread_mutex_init(mutex, attr);
wafwerar's avatar
wafwerar 已提交
151 152
}

153 154 155 156
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
  THREAD_PTR_CHECK(mutex)
  return pthread_mutex_lock(mutex);
}
wafwerar's avatar
wafwerar 已提交
157

wafwerar's avatar
wafwerar 已提交
158 159 160
// int32_t taosThreadMutexTimedLock(TdThreadMutex * mutex, const struct timespec *abstime) {
//   return pthread_mutex_timedlock(mutex, abstime);
// }
wafwerar's avatar
wafwerar 已提交
161

162 163 164 165
int32_t taosThreadMutexTryLock(TdThreadMutex *mutex) {
   THREAD_PTR_CHECK(mutex)
   return pthread_mutex_trylock(mutex);
}
wafwerar's avatar
wafwerar 已提交
166

167 168 169 170
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
  THREAD_PTR_CHECK(mutex)
  return pthread_mutex_unlock(mutex);
}
wafwerar's avatar
wafwerar 已提交
171

H
Hongze Cheng 已提交
172
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr *attr) { return pthread_mutexattr_destroy(attr); }
wafwerar's avatar
wafwerar 已提交
173

H
Hongze Cheng 已提交
174
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr *attr, int32_t *pshared) {
wafwerar's avatar
wafwerar 已提交
175
  return pthread_mutexattr_getpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
176 177
}

wafwerar's avatar
wafwerar 已提交
178 179 180
// int32_t taosThreadMutexAttrGetRobust(const TdThreadMutexAttr * attr, int32_t * robust) {
//   return pthread_mutexattr_getrobust(attr, robust);
// }
wafwerar's avatar
wafwerar 已提交
181

H
Hongze Cheng 已提交
182
int32_t taosThreadMutexAttrGetType(const TdThreadMutexAttr *attr, int32_t *kind) {
wafwerar's avatar
wafwerar 已提交
183
  return pthread_mutexattr_gettype(attr, kind);
wafwerar's avatar
wafwerar 已提交
184 185
}

H
Hongze Cheng 已提交
186
int32_t taosThreadMutexAttrInit(TdThreadMutexAttr *attr) { return pthread_mutexattr_init(attr); }
wafwerar's avatar
wafwerar 已提交
187

H
Hongze Cheng 已提交
188
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
189
  return pthread_mutexattr_setpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
190 191
}

wafwerar's avatar
wafwerar 已提交
192 193 194
// int32_t taosThreadMutexAttrSetRobust(TdThreadMutexAttr * attr, int32_t robust) {
//   return pthread_mutexattr_setrobust(attr, robust);
// }
wafwerar's avatar
wafwerar 已提交
195

H
Hongze Cheng 已提交
196
int32_t taosThreadMutexAttrSetType(TdThreadMutexAttr *attr, int32_t kind) {
wafwerar's avatar
wafwerar 已提交
197
  return pthread_mutexattr_settype(attr, kind);
wafwerar's avatar
wafwerar 已提交
198 199
}

H
Hongze Cheng 已提交
200
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
wafwerar's avatar
wafwerar 已提交
201
  return pthread_once(onceControl, initRoutine);
wafwerar's avatar
wafwerar 已提交
202 203
}

H
Hongze Cheng 已提交
204
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) { return pthread_rwlock_destroy(rwlock); }
wafwerar's avatar
wafwerar 已提交
205

H
Hongze Cheng 已提交
206
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
wafwerar's avatar
wafwerar 已提交
207
  return pthread_rwlock_init(rwlock, attr);
wafwerar's avatar
wafwerar 已提交
208 209
}

H
Hongze Cheng 已提交
210
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) { return pthread_rwlock_rdlock(rwlock); }
wafwerar's avatar
wafwerar 已提交
211

wafwerar's avatar
wafwerar 已提交
212 213 214
// int32_t taosThreadRwlockTimedRdlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
//   return pthread_rwlock_timedrdlock(rwlock, abstime);
// }
wafwerar's avatar
wafwerar 已提交
215

wafwerar's avatar
wafwerar 已提交
216 217 218
// int32_t taosThreadRwlockTimedWrlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
//   return pthread_rwlock_timedwrlock(rwlock, abstime);
// }
wafwerar's avatar
wafwerar 已提交
219

H
Hongze Cheng 已提交
220
int32_t taosThreadRwlockTryRdlock(TdThreadRwlock *rwlock) { return pthread_rwlock_tryrdlock(rwlock); }
wafwerar's avatar
wafwerar 已提交
221

H
Hongze Cheng 已提交
222
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock *rwlock) { return pthread_rwlock_trywrlock(rwlock); }
wafwerar's avatar
wafwerar 已提交
223

H
Hongze Cheng 已提交
224
int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock) { return pthread_rwlock_unlock(rwlock); }
wafwerar's avatar
wafwerar 已提交
225

H
Hongze Cheng 已提交
226
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) { return pthread_rwlock_wrlock(rwlock); }
wafwerar's avatar
wafwerar 已提交
227

H
Hongze Cheng 已提交
228
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) { return pthread_rwlockattr_destroy(attr); }
wafwerar's avatar
wafwerar 已提交
229

H
Hongze Cheng 已提交
230
int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr *attr, int32_t *pshared) {
wafwerar's avatar
wafwerar 已提交
231 232 233
  return pthread_rwlockattr_getpshared(attr, pshared);
}

H
Hongze Cheng 已提交
234
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr *attr) { return pthread_rwlockattr_init(attr); }
wafwerar's avatar
wafwerar 已提交
235

H
Hongze Cheng 已提交
236
int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr *attr, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
237
  return pthread_rwlockattr_setpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
238 239
}

H
Hongze Cheng 已提交
240
TdThread taosThreadSelf(void) { return pthread_self(); }
wafwerar's avatar
wafwerar 已提交
241

H
Hongze Cheng 已提交
242
int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate) { return pthread_setcancelstate(state, oldstate); }
wafwerar's avatar
wafwerar 已提交
243

H
Hongze Cheng 已提交
244
int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype) { return pthread_setcanceltype(type, oldtype); }
wafwerar's avatar
wafwerar 已提交
245

wafwerar's avatar
wafwerar 已提交
246 247
int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sched_param *param) {
  return pthread_setschedparam(thread, policy, param);
wafwerar's avatar
wafwerar 已提交
248 249
}

H
Hongze Cheng 已提交
250
int32_t taosThreadSetSpecific(TdThreadKey key, const void *value) { return pthread_setspecific(key, value); }
wafwerar's avatar
wafwerar 已提交
251

H
Hongze Cheng 已提交
252
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
253
  THREAD_PTR_CHECK(lock)
wafwerar's avatar
wafwerar 已提交
254
#ifdef TD_USE_SPINLOCK_AS_MUTEX
H
Hongze Cheng 已提交
255
  return pthread_mutex_destroy((pthread_mutex_t *)lock);
256
#else
H
Hongze Cheng 已提交
257
  return pthread_spin_destroy((pthread_spinlock_t *)lock);
258
#endif
wafwerar's avatar
wafwerar 已提交
259 260
}

H
Hongze Cheng 已提交
261
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
262
#ifdef TD_USE_SPINLOCK_AS_MUTEX
X
xinsheng Ren 已提交
263 264
  ASSERT(pshared == 0);
  if (pshared != 0) return -1;
H
Hongze Cheng 已提交
265
  return pthread_mutex_init((pthread_mutex_t *)lock, NULL);
266
#else
H
Hongze Cheng 已提交
267
  return pthread_spin_init((pthread_spinlock_t *)lock, pshared);
268
#endif
wafwerar's avatar
wafwerar 已提交
269 270
}

H
Hongze Cheng 已提交
271
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
272
  THREAD_PTR_CHECK(lock)
wafwerar's avatar
wafwerar 已提交
273
#ifdef TD_USE_SPINLOCK_AS_MUTEX
H
Hongze Cheng 已提交
274
  return pthread_mutex_lock((pthread_mutex_t *)lock);
275
#else
H
Hongze Cheng 已提交
276
  return pthread_spin_lock((pthread_spinlock_t *)lock);
277
#endif
wafwerar's avatar
wafwerar 已提交
278 279
}

H
Hongze Cheng 已提交
280
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
281
  THREAD_PTR_CHECK(lock)
wafwerar's avatar
wafwerar 已提交
282
#ifdef TD_USE_SPINLOCK_AS_MUTEX
H
Hongze Cheng 已提交
283
  return pthread_mutex_trylock((pthread_mutex_t *)lock);
284
#else
H
Hongze Cheng 已提交
285
  return pthread_spin_trylock((pthread_spinlock_t *)lock);
286
#endif
wafwerar's avatar
wafwerar 已提交
287 288
}

H
Hongze Cheng 已提交
289
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
290
  THREAD_PTR_CHECK(lock)
wafwerar's avatar
wafwerar 已提交
291
#ifdef TD_USE_SPINLOCK_AS_MUTEX
H
Hongze Cheng 已提交
292
  return pthread_mutex_unlock((pthread_mutex_t *)lock);
293
#else
H
Hongze Cheng 已提交
294
  return pthread_spin_unlock((pthread_spinlock_t *)lock);
295
#endif
wafwerar's avatar
wafwerar 已提交
296 297
}

H
Hongze Cheng 已提交
298
void taosThreadTestCancel(void) { return pthread_testcancel(); }
wafwerar's avatar
wafwerar 已提交
299

K
kailixu 已提交
300
void taosThreadClear(TdThread *thread) { memset(thread, 0, sizeof(TdThread)); }