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

H
Hongze Cheng 已提交
20
int32_t taosThreadCreate(TdThread *tid, const TdThreadAttr *attr, void *(*start)(void *), void *arg) {
wafwerar's avatar
wafwerar 已提交
21 22 23
  return pthread_create(tid, attr, start, arg);
}

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

H
Hongze Cheng 已提交
26
int32_t taosThreadAttrGetDetachState(const TdThreadAttr *attr, int32_t *detachstate) {
wafwerar's avatar
wafwerar 已提交
27 28 29
  return pthread_attr_getdetachstate(attr, detachstate);
}

H
Hongze Cheng 已提交
30
int32_t taosThreadAttrGetInheritSched(const TdThreadAttr *attr, int32_t *inheritsched) {
wafwerar's avatar
wafwerar 已提交
31 32 33
  return pthread_attr_getinheritsched(attr, inheritsched);
}

H
Hongze Cheng 已提交
34
int32_t taosThreadAttrGetSchedParam(const TdThreadAttr *attr, struct sched_param *param) {
wafwerar's avatar
wafwerar 已提交
35 36 37
  return pthread_attr_getschedparam(attr, param);
}

H
Hongze Cheng 已提交
38
int32_t taosThreadAttrGetSchedPolicy(const TdThreadAttr *attr, int32_t *policy) {
wafwerar's avatar
wafwerar 已提交
39 40 41
  return pthread_attr_getschedpolicy(attr, policy);
}

H
Hongze Cheng 已提交
42
int32_t taosThreadAttrGetScope(const TdThreadAttr *attr, int32_t *contentionscope) {
wafwerar's avatar
wafwerar 已提交
43 44 45
  return pthread_attr_getscope(attr, contentionscope);
}

H
Hongze Cheng 已提交
46
int32_t taosThreadAttrGetStackSize(const TdThreadAttr *attr, size_t *stacksize) {
wafwerar's avatar
wafwerar 已提交
47 48 49
  return pthread_attr_getstacksize(attr, stacksize);
}

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

H
Hongze Cheng 已提交
52
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
wafwerar's avatar
wafwerar 已提交
53 54 55
  return pthread_attr_setdetachstate(attr, detachstate);
}

H
Hongze Cheng 已提交
56
int32_t taosThreadAttrSetInheritSched(TdThreadAttr *attr, int32_t inheritsched) {
wafwerar's avatar
wafwerar 已提交
57 58 59
  return pthread_attr_setinheritsched(attr, inheritsched);
}

H
Hongze Cheng 已提交
60
int32_t taosThreadAttrSetSchedParam(TdThreadAttr *attr, const struct sched_param *param) {
wafwerar's avatar
wafwerar 已提交
61 62 63
  return pthread_attr_setschedparam(attr, param);
}

H
Hongze Cheng 已提交
64
int32_t taosThreadAttrSetSchedPolicy(TdThreadAttr *attr, int32_t policy) {
wafwerar's avatar
wafwerar 已提交
65 66 67
  return pthread_attr_setschedpolicy(attr, policy);
}

H
Hongze Cheng 已提交
68
int32_t taosThreadAttrSetScope(TdThreadAttr *attr, int32_t contentionscope) {
wafwerar's avatar
wafwerar 已提交
69 70 71
  return pthread_attr_setscope(attr, contentionscope);
}

H
Hongze Cheng 已提交
72
int32_t taosThreadAttrSetStackSize(TdThreadAttr *attr, size_t stacksize) {
wafwerar's avatar
wafwerar 已提交
73 74 75
  return pthread_attr_setstacksize(attr, stacksize);
}

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

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

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

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

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

H
Hongze Cheng 已提交
86
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) { return pthread_cond_wait(cond, mutex); }
wafwerar's avatar
wafwerar 已提交
87

H
Hongze Cheng 已提交
88
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
wafwerar's avatar
wafwerar 已提交
89 90 91
  return pthread_cond_timedwait(cond, mutex, abstime);
}

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

H
Hongze Cheng 已提交
94
int32_t taosThreadCondAttrGetPshared(const TdThreadCondAttr *attr, int32_t *pshared) {
wafwerar's avatar
wafwerar 已提交
95 96 97
  return pthread_condattr_getpshared(attr, pshared);
}

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

H
Hongze Cheng 已提交
100
int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr *attr, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
101 102 103
  return pthread_condattr_setpshared(attr, pshared);
}

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

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

H
Hongze Cheng 已提交
108
void taosThreadExit(void *valuePtr) { return pthread_exit(valuePtr); }
wafwerar's avatar
wafwerar 已提交
109 110 111 112 113

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

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

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

H
Hongze Cheng 已提交
118
int32_t taosThreadKeyCreate(TdThreadKey *key, void (*destructor)(void *)) {
wafwerar's avatar
wafwerar 已提交
119 120 121
  return pthread_key_create(key, destructor);
}

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

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

wafwerar's avatar
wafwerar 已提交
126 127 128
// int32_t taosThreadMutexConsistent(TdThreadMutex* mutex) {
//   return pthread_mutex_consistent(mutex);
// }
wafwerar's avatar
wafwerar 已提交
129

H
Hongze Cheng 已提交
130
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) { return pthread_mutex_destroy(mutex); }
wafwerar's avatar
wafwerar 已提交
131

H
Hongze Cheng 已提交
132
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
wafwerar's avatar
wafwerar 已提交
133
  return pthread_mutex_init(mutex, attr);
wafwerar's avatar
wafwerar 已提交
134 135
}

H
Hongze Cheng 已提交
136
int32_t taosThreadMutexLock(TdThreadMutex *mutex) { return pthread_mutex_lock(mutex); }
wafwerar's avatar
wafwerar 已提交
137

wafwerar's avatar
wafwerar 已提交
138 139 140
// int32_t taosThreadMutexTimedLock(TdThreadMutex * mutex, const struct timespec *abstime) {
//   return pthread_mutex_timedlock(mutex, abstime);
// }
wafwerar's avatar
wafwerar 已提交
141

H
Hongze Cheng 已提交
142
int32_t taosThreadMutexTryLock(TdThreadMutex *mutex) { return pthread_mutex_trylock(mutex); }
wafwerar's avatar
wafwerar 已提交
143

H
Hongze Cheng 已提交
144
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) { return pthread_mutex_unlock(mutex); }
wafwerar's avatar
wafwerar 已提交
145

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

H
Hongze Cheng 已提交
148
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr *attr, int32_t *pshared) {
wafwerar's avatar
wafwerar 已提交
149
  return pthread_mutexattr_getpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
150 151
}

wafwerar's avatar
wafwerar 已提交
152 153 154
// int32_t taosThreadMutexAttrGetRobust(const TdThreadMutexAttr * attr, int32_t * robust) {
//   return pthread_mutexattr_getrobust(attr, robust);
// }
wafwerar's avatar
wafwerar 已提交
155

H
Hongze Cheng 已提交
156
int32_t taosThreadMutexAttrGetType(const TdThreadMutexAttr *attr, int32_t *kind) {
wafwerar's avatar
wafwerar 已提交
157
  return pthread_mutexattr_gettype(attr, kind);
wafwerar's avatar
wafwerar 已提交
158 159
}

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

H
Hongze Cheng 已提交
162
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
163
  return pthread_mutexattr_setpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
164 165
}

wafwerar's avatar
wafwerar 已提交
166 167 168
// int32_t taosThreadMutexAttrSetRobust(TdThreadMutexAttr * attr, int32_t robust) {
//   return pthread_mutexattr_setrobust(attr, robust);
// }
wafwerar's avatar
wafwerar 已提交
169

H
Hongze Cheng 已提交
170
int32_t taosThreadMutexAttrSetType(TdThreadMutexAttr *attr, int32_t kind) {
wafwerar's avatar
wafwerar 已提交
171
  return pthread_mutexattr_settype(attr, kind);
wafwerar's avatar
wafwerar 已提交
172 173
}

H
Hongze Cheng 已提交
174
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
wafwerar's avatar
wafwerar 已提交
175
  return pthread_once(onceControl, initRoutine);
wafwerar's avatar
wafwerar 已提交
176 177
}

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

H
Hongze Cheng 已提交
180
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
wafwerar's avatar
wafwerar 已提交
181
  return pthread_rwlock_init(rwlock, attr);
wafwerar's avatar
wafwerar 已提交
182 183
}

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

wafwerar's avatar
wafwerar 已提交
186 187 188
// int32_t taosThreadRwlockTimedRdlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
//   return pthread_rwlock_timedrdlock(rwlock, abstime);
// }
wafwerar's avatar
wafwerar 已提交
189

wafwerar's avatar
wafwerar 已提交
190 191 192
// int32_t taosThreadRwlockTimedWrlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
//   return pthread_rwlock_timedwrlock(rwlock, abstime);
// }
wafwerar's avatar
wafwerar 已提交
193

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

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

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

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

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

H
Hongze Cheng 已提交
204
int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr *attr, int32_t *pshared) {
wafwerar's avatar
wafwerar 已提交
205 206 207
  return pthread_rwlockattr_getpshared(attr, pshared);
}

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

H
Hongze Cheng 已提交
210
int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr *attr, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
211
  return pthread_rwlockattr_setpshared(attr, pshared);
wafwerar's avatar
wafwerar 已提交
212 213
}

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

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

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

wafwerar's avatar
wafwerar 已提交
220 221
int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sched_param *param) {
  return pthread_setschedparam(thread, policy, param);
wafwerar's avatar
wafwerar 已提交
222 223
}

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

H
Hongze Cheng 已提交
226
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
wafwerar's avatar
wafwerar 已提交
227
#ifdef TD_USE_SPINLOCK_AS_MUTEX
H
Hongze Cheng 已提交
228
  return pthread_mutex_destroy((pthread_mutex_t *)lock);
229
#else
H
Hongze Cheng 已提交
230
  return pthread_spin_destroy((pthread_spinlock_t *)lock);
231
#endif
wafwerar's avatar
wafwerar 已提交
232 233
}

H
Hongze Cheng 已提交
234
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
wafwerar's avatar
wafwerar 已提交
235
#ifdef TD_USE_SPINLOCK_AS_MUTEX
X
xinsheng Ren 已提交
236 237
  ASSERT(pshared == 0);
  if (pshared != 0) return -1;
H
Hongze Cheng 已提交
238
  return pthread_mutex_init((pthread_mutex_t *)lock, NULL);
239
#else
H
Hongze Cheng 已提交
240
  return pthread_spin_init((pthread_spinlock_t *)lock, pshared);
241
#endif
wafwerar's avatar
wafwerar 已提交
242 243
}

H
Hongze Cheng 已提交
244
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
wafwerar's avatar
wafwerar 已提交
245
#ifdef TD_USE_SPINLOCK_AS_MUTEX
H
Hongze Cheng 已提交
246
  return pthread_mutex_lock((pthread_mutex_t *)lock);
247
#else
H
Hongze Cheng 已提交
248
  return pthread_spin_lock((pthread_spinlock_t *)lock);
249
#endif
wafwerar's avatar
wafwerar 已提交
250 251
}

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

H
Hongze Cheng 已提交
260
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
wafwerar's avatar
wafwerar 已提交
261
#ifdef TD_USE_SPINLOCK_AS_MUTEX
H
Hongze Cheng 已提交
262
  return pthread_mutex_unlock((pthread_mutex_t *)lock);
263
#else
H
Hongze Cheng 已提交
264
  return pthread_spin_unlock((pthread_spinlock_t *)lock);
265
#endif
wafwerar's avatar
wafwerar 已提交
266 267
}

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

H
Hongze Cheng 已提交
270
void taosThreadClear(TdThread *thread) { memset(thread, 0, sizeof(TdThread)); }