• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.98
/source/os/src/osThread.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define ALLOW_FORBID_FUNC
17
#include <pthread.h>
18
#include "os.h"
19

20
int32_t taosThreadCreate(TdThread *tid, const TdThreadAttr *attr, void *(*start)(void *), void *arg) {
357,682,245✔
21
  OS_PARAM_CHECK(tid);
357,682,245✔
22
  OS_PARAM_CHECK(start);
357,682,139✔
23
#ifdef TD_ASTRA
24
  int32_t code = 0;
25
  if (!attr) {
26
    pthread_attr_t threadAttr;
27
    pthread_attr_init(&threadAttr);
28
    pthread_attr_setstacksize(&threadAttr, STACK_SIZE_DEFAULT);
29
    code = pthread_create(tid, &threadAttr, start, arg);
30
    pthread_attr_destroy(&threadAttr);
31
  } else {
32
    int32_t stackSize = 0;
33
    pthread_attr_getstacksize(attr, &stackSize);
34
    if (stackSize == 0) {
35
      pthread_attr_setstacksize(attr, STACK_SIZE_DEFAULT);
36
    }
37
    code = pthread_create(tid, attr, start, arg);
38
  }
39
#else
40
  int32_t code = pthread_create(tid, attr, start, arg);
357,682,033✔
41
#endif
42
  if (code) {
357,682,116✔
43
    taosThreadClear(tid);
×
44
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
45
  }
46
  return code;
357,682,116✔
47
}
48

49
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) {
294,300,976✔
50
  OS_PARAM_CHECK(attr);
294,300,976✔
51
  int32_t code = pthread_attr_destroy(attr);
294,300,870✔
52
  if (code) {
294,301,397✔
53
    return (terrno = TAOS_SYSTEM_ERROR(code));
90✔
54
  }
55
  return code;
294,301,307✔
56
}
57

58
int32_t taosThreadAttrGetDetachState(const TdThreadAttr *attr, int32_t *detachstate) {
318✔
59
  OS_PARAM_CHECK(attr);
318✔
60
  OS_PARAM_CHECK(detachstate);
212✔
61
  int32_t code = pthread_attr_getdetachstate(attr, detachstate);
106✔
62
  if (code) {
106✔
63
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
64
  }
65
  return code;
106✔
66
}
67

68
int32_t taosThreadAttrGetInheritSched(const TdThreadAttr *attr, int32_t *inheritsched) {
318✔
69
  OS_PARAM_CHECK(attr);
318✔
70
  OS_PARAM_CHECK(inheritsched);
212✔
71
  int32_t code = pthread_attr_getinheritsched(attr, inheritsched);
106✔
72
  if (code) {
106✔
73
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
74
  }
75
  return code;
106✔
76
}
77

78
int32_t taosThreadAttrGetSchedParam(const TdThreadAttr *attr, struct sched_param *param) {
318✔
79
  OS_PARAM_CHECK(attr);
318✔
80
  OS_PARAM_CHECK(param);
212✔
81
  int32_t code = pthread_attr_getschedparam(attr, param);
106✔
82
  if (code) {
106✔
83
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
84
  }
85
  return code;
106✔
86
}
87

88
int32_t taosThreadAttrGetSchedPolicy(const TdThreadAttr *attr, int32_t *policy) {
318✔
89
  OS_PARAM_CHECK(attr);
318✔
90
  OS_PARAM_CHECK(policy);
212✔
91
  int32_t code = pthread_attr_getschedpolicy(attr, policy);
106✔
92
  if (code) {
106✔
93
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
94
  }
95
  return code;
106✔
96
}
97

98
int32_t taosThreadAttrGetScope(const TdThreadAttr *attr, int32_t *contentionscope) {
318✔
99
  OS_PARAM_CHECK(attr);
318✔
100
  OS_PARAM_CHECK(contentionscope);
212✔
101
  int32_t code = pthread_attr_getscope(attr, contentionscope);
106✔
102
  if (code) {
106✔
103
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
104
  }
105
  return code;
106✔
106
}
107

108
int32_t taosThreadAttrGetStackSize(const TdThreadAttr *attr, size_t *stacksize) {
318✔
109
  OS_PARAM_CHECK(attr);
318✔
110
  OS_PARAM_CHECK(stacksize);
212✔
111
  int32_t code = pthread_attr_getstacksize(attr, stacksize);
106✔
112
  if (code) {
106✔
113
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
114
  }
115
  return code;
106✔
116
}
117

118
int32_t taosThreadAttrInit(TdThreadAttr *attr) {
295,488,735✔
119
  OS_PARAM_CHECK(attr);
295,488,735✔
120
  int32_t code = pthread_attr_init(attr);
295,488,629✔
121
  if (code) {
295,486,682✔
122
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
123
  }
124
  return code;
295,488,752✔
125
}
126

127
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
289,795,237✔
128
  OS_PARAM_CHECK(attr);
289,795,237✔
129
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
289,795,131✔
130
  if (code) {
289,793,233✔
131
    return (terrno = TAOS_SYSTEM_ERROR(code));
145✔
132
  }
133
  return code;
289,795,025✔
134
}
135

136
int32_t taosThreadAttrSetInheritSched(TdThreadAttr *attr, int32_t inheritsched) {
318✔
137
  OS_PARAM_CHECK(attr);
318✔
138
  int32_t code = pthread_attr_setinheritsched(attr, inheritsched);
212✔
139
  if (code) {
212✔
140
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
141
  }
142
  return code;
106✔
143
}
144

145
int32_t taosThreadAttrSetSchedParam(TdThreadAttr *attr, const struct sched_param *param) {
318✔
146
  OS_PARAM_CHECK(attr);
318✔
147
  int32_t code = pthread_attr_setschedparam(attr, param);
212✔
148
  if (code) {
212✔
149
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
150
  }
151
  return code;
106✔
152
}
153

154
int32_t taosThreadAttrSetSchedPolicy(TdThreadAttr *attr, int32_t policy) {
318✔
155
  OS_PARAM_CHECK(attr);
318✔
156
  int32_t code = pthread_attr_setschedpolicy(attr, policy);
212✔
157
  if (code) {
212✔
158
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
159
  }
160
  return code;
106✔
161
}
162

163
int32_t taosThreadAttrSetScope(TdThreadAttr *attr, int32_t contentionscope) {
318✔
164
  OS_PARAM_CHECK(attr);
318✔
165
  int32_t code = pthread_attr_setscope(attr, contentionscope);
212✔
166
  if (code) {
212✔
167
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
168
  }
169
  return code;
106✔
170
}
171

172
int32_t taosThreadAttrSetStackSize(TdThreadAttr *attr, size_t stacksize) {
318✔
173
  OS_PARAM_CHECK(attr);
318✔
174
  int32_t code = pthread_attr_setstacksize(attr, stacksize);
212✔
175
  if (code) {
212✔
176
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
177
  }
178
  return code;
106✔
179
}
180

181
int32_t taosThreadCancel(TdThread thread) {
212✔
182
  int32_t code = pthread_cancel(thread);
212✔
183
  if (code) {
212✔
184
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
185
  }
186
  return code;
212✔
187
}
188

189
int32_t taosThreadCondDestroy(TdThreadCond *cond) {
1,344,361,000✔
190
  OS_PARAM_CHECK(cond);
1,344,361,000✔
191
#ifdef __USE_WIN_THREAD
192
  return 0;
193
#else
194
  int32_t code = pthread_cond_destroy(cond);
1,344,360,894✔
195
  if (code) {
1,344,456,943✔
196
    return (terrno = TAOS_SYSTEM_ERROR(code));
228✔
197
  }
198
  return code;
1,344,481,536✔
199
#endif
200
}
201

202
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
1,343,886,973✔
203
  OS_PARAM_CHECK(cond);
1,343,886,973✔
204
#ifdef __USE_WIN_THREAD
205
  InitializeConditionVariable(cond);
206
  return 0;
207
#else
208
  int32_t code = pthread_cond_init(cond, attr);
1,343,886,867✔
209
  if (code) {
1,344,024,329✔
UNCOV
210
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
211
  }
212
  return code;
1,344,061,918✔
213
#endif
214
}
215

216
int32_t taosThreadCondSignal(TdThreadCond *cond) {
563,638,970✔
217
  OS_PARAM_CHECK(cond);
563,638,970✔
218
#ifdef __USE_WIN_THREAD
219
  WakeConditionVariable(cond);
220
  return 0;
221
#else
222
  int32_t code = pthread_cond_signal(cond);
563,638,864✔
223
  if (code) {
563,640,367✔
224
    return (terrno = TAOS_SYSTEM_ERROR(code));
189✔
225
  }
226
  return code;
563,640,682✔
227
#endif
228
}
229

230
int32_t taosThreadCondBroadcast(TdThreadCond *cond) {
15,373,224✔
231
  OS_PARAM_CHECK(cond);
15,373,224✔
232
#ifdef __USE_WIN_THREAD
233
  WakeAllConditionVariable(cond);
234
  return 0;
235
#else
236
  int32_t code = pthread_cond_broadcast(cond);
15,373,118✔
237
  if (code) {
15,373,118✔
238
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
239
  }
240
  return code;
15,373,118✔
241
#endif
242
}
243

244
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
141,774,959✔
245
  OS_PARAM_CHECK(cond);
141,774,959✔
246
  OS_PARAM_CHECK(mutex);
141,774,853✔
247
#ifdef __USE_WIN_THREAD
248
  if (!SleepConditionVariableCS(cond, mutex, INFINITE)) {
249
    return EINVAL;
250
  }
251
  return 0;
252
#else
253
  int32_t code = pthread_cond_wait(cond, mutex);
141,774,747✔
254
  if (code) {
141,775,144✔
255
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
256
  }
257
  return code;
141,775,215✔
258
#endif
259
}
260

261
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
850,856,122✔
262
  if (!abstime) return 0;
850,856,122✔
263
  OS_PARAM_CHECK(cond);
850,856,016✔
264
  OS_PARAM_CHECK(mutex);
850,855,910✔
265
#ifdef __USE_WIN_THREAD
266
  if (SleepConditionVariableCS(cond, mutex, (DWORD)(abstime->tv_sec * 1e3 + abstime->tv_nsec / 1e6))) return 0;
267
  DWORD error = GetLastError();
268
  if (error == ERROR_TIMEOUT) {
269
    return TSDB_CODE_TIMEOUT_ERROR;
270
  }
271
  return TAOS_SYSTEM_WINAPI_ERROR(error);
272
#else
273
  int32_t code = pthread_cond_timedwait(cond, mutex, abstime);
850,855,804✔
274
  if (code == ETIMEDOUT) {
850,853,234✔
275
    return TSDB_CODE_TIMEOUT_ERROR;
594,024,270✔
276
  } else if (code) {
256,828,964✔
277
    return TAOS_SYSTEM_ERROR(code);
×
278
  } else {
279
    return 0;
256,828,964✔
280
  }
281
#endif
282
}
283

284
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr *attr) {
70,988,122✔
285
#ifdef __USE_WIN_THREAD
286
  return 0;
287
#else
288
  OS_PARAM_CHECK(attr);
70,988,122✔
289
  int32_t code = pthread_condattr_destroy(attr);
70,988,016✔
290
  if (code) {
70,988,018✔
UNCOV
291
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
292
  }
293
  return code;
70,988,018✔
294
#endif
295
}
296

297
int32_t taosThreadCondAttrGetPshared(const TdThreadCondAttr *attr, int32_t *pshared) {
318✔
298
  OS_PARAM_CHECK(attr);
318✔
299
  OS_PARAM_CHECK(pshared);
212✔
300
#ifdef __USE_WIN_THREAD
301
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
302
  return 0;
303
#else
304
  OS_PARAM_CHECK(attr);
106✔
305
  int32_t code = pthread_condattr_getpshared(attr, pshared);
106✔
306
  if (code) {
106✔
307
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
308
  }
309
  return code;
106✔
310
#endif
311
}
312

313
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) {
70,991,743✔
314
#ifdef __USE_WIN_THREAD
315
  return 0;
316
#else
317
  OS_PARAM_CHECK(attr);
70,991,743✔
318
  int32_t code = pthread_condattr_init(attr);
70,991,637✔
319
  if (code) {
70,990,827✔
320
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
321
  }
322
  return code;
70,991,493✔
323
#endif
324
}
325

326
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
70,991,530✔
327
#ifdef __USE_WIN_THREAD
328
  return 0;
329
#elif defined(__APPLE__)
330
  return 0;
331
#else
332
  OS_PARAM_CHECK(attr);
70,991,530✔
333
  int32_t code = pthread_condattr_setclock(attr, clockId);
70,991,424✔
334
  if (code) {
70,989,740✔
335
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
336
  }
337
  return code;
70,990,588✔
338
#endif
339
}
340

341
int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr *attr, int32_t pshared) {
318✔
342
  OS_PARAM_CHECK(attr);
318✔
343
#ifdef __USE_WIN_THREAD
344
  return 0;
345
#else
346
  int32_t code = pthread_condattr_setpshared(attr, pshared);
212✔
347
  if (code) {
212✔
348
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
349
  }
350
  return code;
106✔
351
#endif
352
}
353

354
int32_t taosThreadDetach(TdThread thread) {
212✔
355
  int32_t code = pthread_detach(thread);
212✔
356
  if (code) {
212✔
357
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
358
  }
359
  return code;
212✔
360
}
361

362
int32_t taosThreadEqual(TdThread t1, TdThread t2) { return pthread_equal(t1, t2); }
106✔
363

364
void taosThreadExit(void *valuePtr) {
212✔
365
  if (valuePtr) return pthread_exit(valuePtr);
212✔
366
}
367

368
int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param) {
636✔
369
  OS_PARAM_CHECK(policy);
636✔
370
  OS_PARAM_CHECK(param);
424✔
371
  int32_t code = pthread_getschedparam(thread, policy, param);
212✔
372
  if (code) {
212✔
373
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
374
  }
375
  return code;
106✔
376
}
377

378
void *taosThreadGetSpecific(TdThreadKey key) { return pthread_getspecific(key); }
212✔
379

380
int32_t taosThreadJoin(TdThread thread, void **valuePtr) {
350,061,842✔
381
  int32_t code = pthread_join(thread, valuePtr);
350,061,842✔
382
  if (code) {
350,062,308✔
383
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
384
  }
385
  return code;
350,062,308✔
386
}
387

388
int32_t taosThreadKeyCreate(TdThreadKey *key, void (*destructor)(void *)) {
30,957✔
389
  OS_PARAM_CHECK(key);
30,957✔
390
  int32_t code = pthread_key_create(key, destructor);
30,957✔
391
  if (code) {
30,957✔
392
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
393
  }
394
  return code;
30,957✔
395
}
396

397
int32_t taosThreadKeyDelete(TdThreadKey key) {
212✔
398
  int32_t code = pthread_key_delete(key);
212✔
399
  if (code) {
212✔
400
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
401
  }
402
  return code;
212✔
403
}
404

405
int32_t taosThreadKill(TdThread thread, int32_t sig) {
1,065,265✔
406
  int32_t code = pthread_kill(thread, sig);
1,065,265✔
407
  if (code) {
1,065,265✔
408
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
409
  }
410
  return code;
1,065,265✔
411
}
412

413
// int32_t taosThreadMutexConsistent(TdThreadMutex* mutex) {
414
//   return pthread_mutex_consistent(mutex);
415
// }
416

417
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
2,147,483,647✔
418
  OS_PARAM_CHECK(mutex);
2,147,483,647✔
419
#ifdef __USE_WIN_THREAD
420
  DeleteCriticalSection(mutex);
421
  return 0;
422
#else
423
  int32_t code = pthread_mutex_destroy(mutex);
2,147,483,647✔
424
  if (code) {
2,147,483,647✔
425
    return (terrno = TAOS_SYSTEM_ERROR(code));
781✔
426
  }
427
  return code;
2,147,483,647✔
428
#endif
429
}
430

431
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
2,147,483,647✔
432
  OS_PARAM_CHECK(mutex);
2,147,483,647✔
433
#ifdef __USE_WIN_THREAD
434
  /**
435
   * Windows Server 2003 and Windows XP:  In low memory situations, InitializeCriticalSection can raise a
436
   * STATUS_NO_MEMORY exception. Starting with Windows Vista, this exception was eliminated and
437
   * InitializeCriticalSection always succeeds, even in low memory situations.
438
   */
439
  InitializeCriticalSection(mutex);
440
  return 0;
441
#else
442
  int32_t code = pthread_mutex_init(mutex, attr);
2,147,483,647✔
443
  if (code) {
2,147,483,647✔
444
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
445
  }
446
  return code;
2,147,483,647✔
447
#endif
448
}
449

450
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
2,147,483,647✔
451
  OS_PARAM_CHECK(mutex);
2,147,483,647✔
452
#ifdef __USE_WIN_THREAD
453
  EnterCriticalSection(mutex);
454
  return 0;
455
#else
456
  int32_t code = pthread_mutex_lock(mutex);
2,147,483,647✔
457
  if (code) {
2,147,483,647✔
458
    return (terrno = TAOS_SYSTEM_ERROR(code));
10✔
459
  }
460
  return code;
2,147,483,647✔
461
#endif
462
}
463

464
// int32_t taosThreadMutexTimedLock(TdThreadMutex * mutex, const struct timespec *abstime) {
465
//   return pthread_mutex_timedlock(mutex, abstime);
466
// }
467

468
int32_t taosThreadMutexTryLock(TdThreadMutex *mutex) {
39,910,987✔
469
  OS_PARAM_CHECK(mutex);
39,910,987✔
470
#ifdef __USE_WIN_THREAD
471
  if (TryEnterCriticalSection(mutex)) return 0;
472
  return EBUSY;
473
#else
474
  int32_t code = pthread_mutex_trylock(mutex);
39,910,881✔
475
  if (code && code != EBUSY) {
39,909,209✔
476
    code = TAOS_SYSTEM_ERROR(code);
×
477
  }
478
  return code;
39,909,209✔
479
#endif
480
}
481

482
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
2,147,483,647✔
483
  OS_PARAM_CHECK(mutex);
2,147,483,647✔
484
#ifdef __USE_WIN_THREAD
485
  LeaveCriticalSection(mutex);
486
  return 0;
487
#else
488
  int32_t code = pthread_mutex_unlock(mutex);
2,147,483,647✔
489
  if (code) {
2,147,483,647✔
490
    return (terrno = TAOS_SYSTEM_ERROR(code));
4✔
491
  }
492
  return code;
2,147,483,647✔
493
#endif
494
}
495

496
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr *attr) {
6,668,988✔
497
#ifdef __USE_WIN_THREAD
498
  return 0;
499
#else
500
  OS_PARAM_CHECK(attr);
6,668,988✔
501
  int32_t code = pthread_mutexattr_destroy(attr);
6,668,882✔
502
  if (code) {
6,668,882✔
503
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
504
  }
505
  return code;
6,668,882✔
506
#endif
507
}
508

509
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr *attr, int32_t *pshared) {
318✔
510
  OS_PARAM_CHECK(pshared);
318✔
511
#ifdef __USE_WIN_THREAD
512
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
513
  return 0;
514
#else
515
  OS_PARAM_CHECK(attr);
212✔
516
  int32_t code = pthread_mutexattr_getpshared(attr, pshared);
106✔
517
  if (code) {
106✔
518
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
519
  }
520
  return code;
106✔
521
#endif
522
}
523

524
// int32_t taosThreadMutexAttrGetRobust(const TdThreadMutexAttr * attr, int32_t * robust) {
525
//   return pthread_mutexattr_getrobust(attr, robust);
526
// }
527

528
int32_t taosThreadMutexAttrGetType(const TdThreadMutexAttr *attr, int32_t *kind) {
318✔
529
  OS_PARAM_CHECK(kind);
318✔
530
#ifdef __USE_WIN_THREAD
531
  if (kind) *kind = PTHREAD_MUTEX_NORMAL;
532
  return 0;
533
#else
534
  OS_PARAM_CHECK(attr);
212✔
535
  int32_t code = pthread_mutexattr_gettype(attr, kind);
106✔
536
  if (code) {
106✔
537
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
538
  }
539
  return code;
106✔
540
#endif
541
}
542

543
int32_t taosThreadMutexAttrInit(TdThreadMutexAttr *attr) {
6,681,526✔
544
#ifdef __USE_WIN_THREAD
545
  return 0;
546
#else
547
  OS_PARAM_CHECK(attr);
6,681,526✔
548
  int32_t code = pthread_mutexattr_init(attr);
6,681,420✔
549
  if (code) {
6,681,420✔
550
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
551
  }
552
  return code;
6,681,420✔
553
#endif
554
}
555

556
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
318✔
557
#ifdef __USE_WIN_THREAD
558
  return 0;
559
#else
560
  OS_PARAM_CHECK(attr);
318✔
561
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
212✔
562
  if (code) {
212✔
563
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
564
  }
565
  return code;
106✔
566
#endif
567
}
568

569
// int32_t taosThreadMutexAttrSetRobust(TdThreadMutexAttr * attr, int32_t robust) {
570
//   return pthread_mutexattr_setrobust(attr, robust);
571
// }
572

573
int32_t taosThreadMutexAttrSetType(TdThreadMutexAttr *attr, int32_t kind) {
6,681,632✔
574
#ifdef __USE_WIN_THREAD
575
  return 0;
576
#else
577
  OS_PARAM_CHECK(attr);
6,681,632✔
578
  int32_t code = pthread_mutexattr_settype(attr, kind);
6,681,526✔
579
  if (code) {
6,681,526✔
580
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
581
  }
582
  return code;
6,681,420✔
583
#endif
584
}
585

586
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
2,147,483,647✔
587
  int32_t code = pthread_once(onceControl, initRoutine);
2,147,483,647✔
588
  if (code) {
2,147,483,647✔
589
    return (terrno = TAOS_SYSTEM_ERROR(code));
47✔
590
  }
591
  return code;
2,147,483,647✔
592
}
593

594
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
2,147,483,647✔
595
#ifdef __USE_WIN_THREAD
596
  /* SRWLock does not need explicit destruction so long as there are no waiting threads
597
   * See: https://docs.microsoft.com/windows/win32/api/synchapi/nf-synchapi-initializesrwlock#remarks
598
   */
599
  return 0;
600
#else
601
  OS_PARAM_CHECK(rwlock);
2,147,483,647✔
602
  int32_t code = pthread_rwlock_destroy(rwlock);
2,147,483,647✔
603
  if (code) {
2,147,483,647✔
604
    return (terrno = TAOS_SYSTEM_ERROR(code));
114✔
605
  }
606
  return code;
2,147,483,647✔
607
#endif
608
}
609

610
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
2,147,483,647✔
611
  OS_PARAM_CHECK(rwlock);
2,147,483,647✔
612
#ifdef __USE_WIN_THREAD
613
  memset(rwlock, 0, sizeof(*rwlock));
614
  InitializeSRWLock(&rwlock->lock);
615
  return 0;
616
#else
617
  int32_t code = pthread_rwlock_init(rwlock, attr);
2,147,483,647✔
618
  if (code) {
2,147,483,647✔
619
    return (terrno = TAOS_SYSTEM_ERROR(code));
163✔
620
  }
621
  return code;
2,147,483,647✔
622
#endif
623
}
624

625
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
2,147,483,647✔
626
  OS_PARAM_CHECK(rwlock);
2,147,483,647✔
627
#ifdef __USE_WIN_THREAD
628
  AcquireSRWLockShared(&rwlock->lock);
629
  return 0;
630
#else
631
  int32_t code = pthread_rwlock_rdlock(rwlock);
2,147,483,647✔
632
  if (code) {
2,147,483,647✔
633
    return (terrno = TAOS_SYSTEM_ERROR(code));
1,120,880✔
634
  }
635
  return code;
2,147,483,647✔
636
#endif
637
}
638

639
// int32_t taosThreadRwlockTimedRdlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
640
//   return pthread_rwlock_timedrdlock(rwlock, abstime);
641
// }
642

643
// int32_t taosThreadRwlockTimedWrlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
644
//   return pthread_rwlock_timedwrlock(rwlock, abstime);
645
// }
646

647
int32_t taosThreadRwlockTryRdlock(TdThreadRwlock *rwlock) {
212✔
648
  OS_PARAM_CHECK(rwlock);
212✔
649
#ifdef __USE_WIN_THREAD
650
  if (!TryAcquireSRWLockShared(&rwlock->lock)) return EBUSY;
651
  return 0;
652
#else
653
  int32_t code = pthread_rwlock_tryrdlock(rwlock);
106✔
654
  if (code) {
106✔
655
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
656
  }
657
  return code;
106✔
658
#endif
659
}
660

661
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock *rwlock) {
212✔
662
  OS_PARAM_CHECK(rwlock);
212✔
663
#ifdef __USE_WIN_THREAD
664
  if (!TryAcquireSRWLockExclusive(&rwlock->lock)) return EBUSY;
665
  atomic_store_8(&rwlock->excl, 1);
666
  return 0;
667
#else
668
  int32_t code = pthread_rwlock_trywrlock(rwlock);
106✔
669
  if (code) {
106✔
670
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
671
  }
672
  return code;
106✔
673
#endif
674
}
675

676
int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock) {
2,147,483,647✔
677
  OS_PARAM_CHECK(rwlock);
2,147,483,647✔
678
#ifdef __USE_WIN_THREAD
679
  if (1 == atomic_val_compare_exchange_8(&rwlock->excl, 1, 0)) {
680
    ReleaseSRWLockExclusive(&rwlock->lock);
681
  } else {
682
    ReleaseSRWLockShared(&rwlock->lock);
683
  }
684
  return 0;
685
#else
686
  int32_t code = pthread_rwlock_unlock(rwlock);
2,147,483,647✔
687
  if (code) {
2,147,483,647✔
688
    return (terrno = TAOS_SYSTEM_ERROR(code));
9,620,831✔
689
  }
690
  return code;
2,147,483,647✔
691
#endif
692
}
693

694
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
2,147,483,647✔
695
  OS_PARAM_CHECK(rwlock);
2,147,483,647✔
696
#ifdef __USE_WIN_THREAD
697
  AcquireSRWLockExclusive(&rwlock->lock);
698
  atomic_store_8(&rwlock->excl, 1);
699
  return 0;
700
#else
701
  int32_t code = pthread_rwlock_wrlock(rwlock);
2,147,483,647✔
702
  if (code) {
2,147,483,647✔
703
    return (terrno = TAOS_SYSTEM_ERROR(code));
930,691✔
704
  }
705
  return code;
2,147,483,647✔
706
#endif
707
}
708

709
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
14,408,894✔
710
#ifdef __USE_WIN_THREAD
711
  return 0;
712
#else
713
  OS_PARAM_CHECK(attr);
14,408,894✔
714
  int32_t code = pthread_rwlockattr_destroy(attr);
14,408,788✔
715
  if (code) {
14,406,833✔
716
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
717
  }
718
  return code;
14,408,402✔
719
#endif
720
}
721

722
int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr *attr, int32_t *pshared) {
318✔
723
  OS_PARAM_CHECK(attr);
318✔
724
  OS_PARAM_CHECK(pshared);
212✔
725
#ifdef __USE_WIN_THREAD
726
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
727
  return 0;
728
#else
729
  int32_t code = pthread_rwlockattr_getpshared(attr, pshared);
106✔
730
  if (code) {
106✔
731
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
732
  }
733
  return code;
106✔
734
#endif
735
}
736

737
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr *attr) {
14,409,096✔
738
#ifdef __USE_WIN_THREAD
739
  return 0;
740
#else
741
  OS_PARAM_CHECK(attr);
14,409,096✔
742
  int32_t code = pthread_rwlockattr_init(attr);
14,408,990✔
743
  if (code) {
14,407,759✔
744
    return (terrno = TAOS_SYSTEM_ERROR(code));
2✔
745
  }
746
  return code;
14,408,323✔
747
#endif
748
}
749

750
int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr *attr, int32_t pshared) {
318✔
751
#ifdef __USE_WIN_THREAD
752
  return 0;
753
#else
754
  OS_PARAM_CHECK(attr);
318✔
755
  int32_t code = pthread_rwlockattr_setpshared(attr, pshared);
212✔
756
  if (code) {
212✔
757
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
758
  }
759
  return code;
106✔
760
#endif
761
}
762

763
TdThread taosThreadSelf(void) { return pthread_self(); }
730✔
764

765
int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate) {
212✔
766
  int32_t code = pthread_setcancelstate(state, oldstate);
212✔
767
  if (code) {
212✔
768
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
769
  }
770
  return code;
106✔
771
}
772

773
int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype) {
212✔
774
  int32_t code = pthread_setcanceltype(type, oldtype);
212✔
775
  if (code) {
212✔
776
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
777
  }
778
  return code;
106✔
779
}
780

781
int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sched_param *param) {
212✔
782
  OS_PARAM_CHECK(param);
212✔
783
  int32_t code = pthread_setschedparam(thread, policy, param);
212✔
784
  if (code) {
212✔
785
    return (terrno = TAOS_SYSTEM_ERROR(code));
106✔
786
  }
787
  return code;
106✔
788
}
789

790
int32_t taosThreadSetSpecific(TdThreadKey key, const void *value) {
240,784✔
791
  OS_PARAM_CHECK(value);
240,784✔
792
  int32_t code = pthread_setspecific(key, value);
240,572✔
793
  if (code) {
240,572✔
794
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
795
  }
796
  return code;
240,572✔
797
}
798

799
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
1,953,015,119✔
800
  OS_PARAM_CHECK(lock);
1,953,015,119✔
801
#ifdef TD_USE_SPINLOCK_AS_MUTEX
802
  return pthread_mutex_destroy((pthread_mutex_t *)lock);
803
#else
804
  int32_t code = pthread_spin_destroy((pthread_spinlock_t *)lock);
1,953,015,013✔
805
  if (code) {
1,952,989,848✔
806
    return (terrno = TAOS_SYSTEM_ERROR(code));
7✔
807
  }
808
  return code;
1,953,004,552✔
809
#endif
810
}
811

812
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
1,951,804,830✔
813
  OS_PARAM_CHECK(lock);
1,951,804,830✔
814
#ifdef TD_USE_SPINLOCK_AS_MUTEX
815
  if (pshared != 0) return TSDB_CODE_INVALID_PARA;
816
  return pthread_mutex_init((pthread_mutex_t *)lock, NULL);
817
#else
818
  int32_t code = pthread_spin_init((pthread_spinlock_t *)lock, pshared);
1,951,804,724✔
819
  if (code) {
1,952,053,464✔
820
    return (terrno = TAOS_SYSTEM_ERROR(code));
1,072✔
821
  }
822
  return code;
1,952,061,345✔
823
#endif
824
}
825

826
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
135,179,858✔
827
  OS_PARAM_CHECK(lock);
135,179,858✔
828
#ifdef TD_USE_SPINLOCK_AS_MUTEX
829
  return pthread_mutex_lock((pthread_mutex_t *)lock);
830
#else
831
  int32_t code = pthread_spin_lock((pthread_spinlock_t *)lock);
135,179,752✔
832
  if (code) {
135,215,997✔
833
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
834
  }
835
  return code;
135,215,997✔
836
#endif
837
}
838

839
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
381,748,381✔
840
  OS_PARAM_CHECK(lock);
381,748,381✔
841
#ifdef TD_USE_SPINLOCK_AS_MUTEX
842
  return pthread_mutex_trylock((pthread_mutex_t *)lock);
843
#else
844
  int32_t code = pthread_spin_trylock((pthread_spinlock_t *)lock);
381,748,275✔
845
  if (code && code != EBUSY) {
381,792,381✔
846
    code = TAOS_SYSTEM_ERROR(code);
×
847
  }
848
  return code;
381,792,381✔
849
#endif
850
}
851

852
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
516,978,102✔
853
  OS_PARAM_CHECK(lock);
516,978,102✔
854
#ifdef TD_USE_SPINLOCK_AS_MUTEX
855
  return pthread_mutex_unlock((pthread_mutex_t *)lock);
856
#else
857
  int32_t code = pthread_spin_unlock((pthread_spinlock_t *)lock);
516,977,996✔
858
  if (code) {
516,909,632✔
UNCOV
859
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
860
  }
861
  return code;
516,965,394✔
862
#endif
863
}
864

865
void taosThreadTestCancel(void) { return pthread_testcancel(); }
106✔
866

867
void taosThreadClear(TdThread *thread) {
282,251,736✔
868
  if (!thread) return;
282,251,736✔
869
  (void)memset(thread, 0, sizeof(TdThread));
282,251,630✔
870
}
871

872
#ifdef WINDOWS
873
bool taosThreadIsMain() {
874
  DWORD curProcessId = GetCurrentProcessId();
875
  DWORD curThreadId = GetCurrentThreadId();
876
  DWORD dwThreadId = -1;
877

878
  HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
879
  if (hThreadSnapshot == INVALID_HANDLE_VALUE) {
880
    return false;
881
  }
882

883
  THREADENTRY32 te32;
884
  te32.dwSize = sizeof(THREADENTRY32);
885

886
  if (!Thread32First(hThreadSnapshot, &te32)) {
887
    CloseHandle(hThreadSnapshot);
888
    return false;
889
  }
890

891
  do {
892
    if (te32.th32OwnerProcessID == curProcessId) {
893
      dwThreadId = te32.th32ThreadID;
894
      break;
895
    }
896
  } while (Thread32Next(hThreadSnapshot, &te32));
897

898
  CloseHandle(hThreadSnapshot);
899

900
  return curThreadId == dwThreadId;
901
}
902
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc