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

taosdata / TDengine / #5051

13 May 2026 12:00PM UTC coverage: 73.358% (-0.04%) from 73.398%
#5051

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

714 existing lines in 146 files now uncovered.

281543 of 383795 relevant lines covered (73.36%)

135448694.71 hits per line

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

90.49
/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) {
395,756,319✔
21
  OS_PARAM_CHECK(tid);
395,756,319✔
22
  OS_PARAM_CHECK(start);
395,756,115✔
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);
395,755,911✔
41
#endif
42
  if (code) {
395,756,592✔
UNCOV
43
    taosThreadClear(tid);
×
44
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
45
  }
46
  return code;
395,756,592✔
47
}
48

49
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) {
325,642,486✔
50
  OS_PARAM_CHECK(attr);
325,642,486✔
51
  int32_t code = pthread_attr_destroy(attr);
325,642,282✔
52
  if (code) {
325,642,154✔
53
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
54
  }
55
  return code;
325,642,154✔
56
}
57

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

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

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

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

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

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

118
int32_t taosThreadAttrInit(TdThreadAttr *attr) {
326,925,643✔
119
  OS_PARAM_CHECK(attr);
326,925,643✔
120
  int32_t code = pthread_attr_init(attr);
326,925,439✔
121
  if (code) {
326,923,925✔
122
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
123
  }
124
  return code;
326,924,290✔
125
}
126

127
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
320,075,196✔
128
  OS_PARAM_CHECK(attr);
320,075,196✔
129
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
320,074,992✔
130
  if (code) {
320,075,540✔
131
    return (terrno = TAOS_SYSTEM_ERROR(code));
204✔
132
  }
133
  return code;
320,075,845✔
134
}
135

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

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

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

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

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

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

189
int32_t taosThreadCondDestroy(TdThreadCond *cond) {
1,712,043,961✔
190
  OS_PARAM_CHECK(cond);
1,712,043,961✔
191
#ifdef __USE_WIN_THREAD
192
  return 0;
193
#else
194
  int32_t code = pthread_cond_destroy(cond);
1,712,043,757✔
195
  if (code) {
1,712,249,562✔
196
    return (terrno = TAOS_SYSTEM_ERROR(code));
42,307✔
197
  }
198
  return code;
1,712,207,346✔
199
#endif
200
}
201

202
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
1,711,631,032✔
203
  OS_PARAM_CHECK(cond);
1,711,631,032✔
204
#ifdef __USE_WIN_THREAD
205
  InitializeConditionVariable(cond);
206
  return 0;
207
#else
208
  int32_t code = pthread_cond_init(cond, attr);
1,711,630,828✔
209
  if (code) {
1,711,822,487✔
210
    return (terrno = TAOS_SYSTEM_ERROR(code));
18,889✔
211
  }
212
  return code;
1,711,803,874✔
213
#endif
214
}
215

216
int32_t taosThreadCondSignal(TdThreadCond *cond) {
625,809,099✔
217
  OS_PARAM_CHECK(cond);
625,809,099✔
218
#ifdef __USE_WIN_THREAD
219
  WakeConditionVariable(cond);
220
  return 0;
221
#else
222
  int32_t code = pthread_cond_signal(cond);
625,808,895✔
223
  if (code) {
625,810,222✔
224
    return (terrno = TAOS_SYSTEM_ERROR(code));
700✔
225
  }
226
  return code;
625,810,859✔
227
#endif
228
}
229

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

244
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
152,204,620✔
245
  OS_PARAM_CHECK(cond);
152,204,620✔
246
  OS_PARAM_CHECK(mutex);
152,204,416✔
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);
152,204,212✔
254
  if (code) {
152,203,829✔
255
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
256
  }
257
  return code;
152,203,829✔
258
#endif
259
}
260

261
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
910,048,762✔
262
  if (!abstime) return 0;
910,048,762✔
263
  OS_PARAM_CHECK(cond);
910,048,558✔
264
  OS_PARAM_CHECK(mutex);
910,048,354✔
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);
910,048,150✔
274
  if (code == ETIMEDOUT) {
910,032,105✔
275
    return TSDB_CODE_TIMEOUT_ERROR;
630,531,447✔
276
  } else if (code) {
279,500,658✔
277
    return TAOS_SYSTEM_ERROR(code);
×
278
  } else {
279
    return 0;
279,500,658✔
280
  }
281
#endif
282
}
283

284
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr *attr) {
85,685,203✔
285
#ifdef __USE_WIN_THREAD
286
  return 0;
287
#else
288
  OS_PARAM_CHECK(attr);
85,685,203✔
289
  int32_t code = pthread_condattr_destroy(attr);
85,684,999✔
290
  if (code) {
85,685,013✔
UNCOV
291
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
292
  }
293
  return code;
85,685,013✔
294
#endif
295
}
296

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

313
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) {
85,686,259✔
314
#ifdef __USE_WIN_THREAD
315
  return 0;
316
#else
317
  OS_PARAM_CHECK(attr);
85,686,259✔
318
  int32_t code = pthread_condattr_init(attr);
85,686,055✔
319
  if (code) {
85,685,176✔
UNCOV
320
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
321
  }
322
  return code;
85,685,203✔
323
#endif
324
}
325

326
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
85,686,362✔
327
#ifdef __USE_WIN_THREAD
328
  return 0;
329
#elif defined(__APPLE__)
330
  return 0;
331
#else
332
  OS_PARAM_CHECK(attr);
85,686,362✔
333
  int32_t code = pthread_condattr_setclock(attr, clockId);
85,686,158✔
334
  if (code) {
85,684,900✔
UNCOV
335
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
336
  }
337
  return code;
85,685,126✔
338
#endif
339
}
340

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

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

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

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

368
int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param) {
1,224✔
369
  OS_PARAM_CHECK(policy);
1,224✔
370
  OS_PARAM_CHECK(param);
816✔
371
  int32_t code = pthread_getschedparam(thread, policy, param);
408✔
372
  if (code) {
408✔
373
    return (terrno = TAOS_SYSTEM_ERROR(code));
204✔
374
  }
375
  return code;
204✔
376
}
377

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

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

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

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

405
int32_t taosThreadKill(TdThread thread, int32_t sig) {
1,204,742✔
406
  int32_t code = pthread_kill(thread, sig);
1,204,742✔
407
  if (code) {
1,204,742✔
408
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
409
  }
410
  return code;
1,204,742✔
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));
×
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));
25✔
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));
3✔
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) {
43,527,553✔
469
  OS_PARAM_CHECK(mutex);
43,527,553✔
470
#ifdef __USE_WIN_THREAD
471
  if (TryEnterCriticalSection(mutex)) return 0;
472
  return EBUSY;
473
#else
474
  int32_t code = pthread_mutex_trylock(mutex);
43,527,349✔
475
  if (code && code != EBUSY) {
43,526,025✔
476
    code = TAOS_SYSTEM_ERROR(code);
×
477
  }
478
  return code;
43,526,025✔
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✔
UNCOV
490
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
491
  }
492
  return code;
2,147,483,647✔
493
#endif
494
}
495

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

509
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr *attr, int32_t *pshared) {
612✔
510
  OS_PARAM_CHECK(pshared);
612✔
511
#ifdef __USE_WIN_THREAD
512
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
513
  return 0;
514
#else
515
  OS_PARAM_CHECK(attr);
408✔
516
  int32_t code = pthread_mutexattr_getpshared(attr, pshared);
204✔
517
  if (code) {
204✔
518
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
519
  }
520
  return code;
204✔
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) {
612✔
529
  OS_PARAM_CHECK(kind);
612✔
530
#ifdef __USE_WIN_THREAD
531
  if (kind) *kind = PTHREAD_MUTEX_NORMAL;
532
  return 0;
533
#else
534
  OS_PARAM_CHECK(attr);
408✔
535
  int32_t code = pthread_mutexattr_gettype(attr, kind);
204✔
536
  if (code) {
204✔
537
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
538
  }
539
  return code;
204✔
540
#endif
541
}
542

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

556
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
612✔
557
#ifdef __USE_WIN_THREAD
558
  return 0;
559
#else
560
  OS_PARAM_CHECK(attr);
612✔
561
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
408✔
562
  if (code) {
408✔
563
    return (terrno = TAOS_SYSTEM_ERROR(code));
204✔
564
  }
565
  return code;
204✔
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) {
7,292,499✔
574
#ifdef __USE_WIN_THREAD
575
  return 0;
576
#else
577
  OS_PARAM_CHECK(attr);
7,292,499✔
578
  int32_t code = pthread_mutexattr_settype(attr, kind);
7,292,295✔
579
  if (code) {
7,292,295✔
580
    return (terrno = TAOS_SYSTEM_ERROR(code));
204✔
581
  }
582
  return code;
7,292,091✔
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));
12✔
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));
572✔
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));
×
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,767,201✔
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) {
408✔
648
  OS_PARAM_CHECK(rwlock);
408✔
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);
204✔
654
  if (code) {
204✔
655
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
656
  }
657
  return code;
204✔
658
#endif
659
}
660

661
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock *rwlock) {
408✔
662
  OS_PARAM_CHECK(rwlock);
408✔
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);
204✔
669
  if (code) {
204✔
670
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
671
  }
672
  return code;
204✔
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,141,561✔
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));
1,067,382✔
704
  }
705
  return code;
2,147,483,647✔
706
#endif
707
}
708

709
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
15,585,511✔
710
#ifdef __USE_WIN_THREAD
711
  return 0;
712
#else
713
  OS_PARAM_CHECK(attr);
15,585,511✔
714
  int32_t code = pthread_rwlockattr_destroy(attr);
15,585,307✔
715
  if (code) {
15,584,304✔
UNCOV
716
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
717
  }
718
  return code;
15,585,863✔
719
#endif
720
}
721

722
int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr *attr, int32_t *pshared) {
612✔
723
  OS_PARAM_CHECK(attr);
612✔
724
  OS_PARAM_CHECK(pshared);
408✔
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);
204✔
730
  if (code) {
204✔
731
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
732
  }
733
  return code;
204✔
734
#endif
735
}
736

737
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr *attr) {
15,587,393✔
738
#ifdef __USE_WIN_THREAD
739
  return 0;
740
#else
741
  OS_PARAM_CHECK(attr);
15,587,393✔
742
  int32_t code = pthread_rwlockattr_init(attr);
15,587,189✔
743
  if (code) {
15,586,074✔
744
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
745
  }
746
  return code;
15,586,932✔
747
#endif
748
}
749

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

763
TdThread taosThreadSelf(void) { return pthread_self(); }
1,432✔
764

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

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

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

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

799
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
2,147,483,647✔
800
  OS_PARAM_CHECK(lock);
2,147,483,647✔
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);
2,147,483,647✔
805
  if (code) {
2,147,483,647✔
806
    return (terrno = TAOS_SYSTEM_ERROR(code));
10,661✔
807
  }
808
  return code;
2,147,483,647✔
809
#endif
810
}
811

812
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
2,147,483,647✔
813
  OS_PARAM_CHECK(lock);
2,147,483,647✔
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);
2,147,483,647✔
819
  if (code) {
2,147,483,647✔
820
    return (terrno = TAOS_SYSTEM_ERROR(code));
129✔
821
  }
822
  return code;
2,147,483,647✔
823
#endif
824
}
825

826
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
145,049,674✔
827
  OS_PARAM_CHECK(lock);
145,049,674✔
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);
145,049,470✔
832
  if (code) {
145,069,910✔
833
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
834
  }
835
  return code;
145,069,910✔
836
#endif
837
}
838

839
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
472,992,200✔
840
  OS_PARAM_CHECK(lock);
472,992,200✔
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);
472,991,996✔
845
  if (code && code != EBUSY) {
473,042,120✔
846
    code = TAOS_SYSTEM_ERROR(code);
×
847
  }
848
  return code;
473,042,120✔
849
#endif
850
}
851

852
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
618,064,494✔
853
  OS_PARAM_CHECK(lock);
618,064,494✔
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);
618,064,290✔
858
  if (code) {
618,007,987✔
859
    return (terrno = TAOS_SYSTEM_ERROR(code));
96✔
860
  }
861
  return code;
618,031,596✔
862
#endif
863
}
864

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

867
void taosThreadClear(TdThread *thread) {
311,808,260✔
868
  if (!thread) return;
311,808,260✔
869
  (void)memset(thread, 0, sizeof(TdThread));
311,808,056✔
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