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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

50.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) {
318,113,182✔
21
  OS_PARAM_CHECK(tid);
318,113,182✔
22
  OS_PARAM_CHECK(start);
318,113,182✔
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);
318,113,182✔
41
#endif
42
  if (code) {
318,113,042✔
43
    taosThreadClear(tid);
×
44
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
45
  }
46
  return code;
318,113,042✔
47
}
48

49
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) {
261,318,563✔
50
  OS_PARAM_CHECK(attr);
261,318,563✔
51
  int32_t code = pthread_attr_destroy(attr);
261,318,563✔
52
  if (code) {
261,317,841✔
53
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
54
  }
55
  return code;
261,318,645✔
56
}
57

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

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

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

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

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

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

118
int32_t taosThreadAttrInit(TdThreadAttr *attr) {
262,240,278✔
119
  OS_PARAM_CHECK(attr);
262,240,278✔
120
  int32_t code = pthread_attr_init(attr);
262,240,278✔
121
  if (code) {
262,240,278✔
122
    return (terrno = TAOS_SYSTEM_ERROR(code));
877✔
123
  }
124
  return code;
262,239,401✔
125
}
126

127
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
257,052,512✔
128
  OS_PARAM_CHECK(attr);
257,052,512✔
129
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
257,052,512✔
130
  if (code) {
257,053,206✔
131
    return (terrno = TAOS_SYSTEM_ERROR(code));
1,659✔
132
  }
133
  return code;
257,051,547✔
134
}
135

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

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

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

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

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

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

189
int32_t taosThreadCondDestroy(TdThreadCond *cond) {
1,498,969,438✔
190
  OS_PARAM_CHECK(cond);
1,498,969,438✔
191
#ifdef __USE_WIN_THREAD
192
  return 0;
193
#else
194
  int32_t code = pthread_cond_destroy(cond);
1,498,969,438✔
195
  if (code) {
1,499,104,093✔
UNCOV
196
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
197
  }
198
  return code;
1,499,110,545✔
199
#endif
200
}
201

202
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
1,498,649,971✔
203
  OS_PARAM_CHECK(cond);
1,498,649,971✔
204
#ifdef __USE_WIN_THREAD
205
  InitializeConditionVariable(cond);
206
  return 0;
207
#else
208
  int32_t code = pthread_cond_init(cond, attr);
1,498,649,971✔
209
  if (code) {
1,498,704,698✔
210
    return (terrno = TAOS_SYSTEM_ERROR(code));
183✔
211
  }
212
  return code;
1,498,724,738✔
213
#endif
214
}
215

216
int32_t taosThreadCondSignal(TdThreadCond *cond) {
207,763,905✔
217
  OS_PARAM_CHECK(cond);
207,763,905✔
218
#ifdef __USE_WIN_THREAD
219
  WakeConditionVariable(cond);
220
  return 0;
221
#else
222
  int32_t code = pthread_cond_signal(cond);
207,763,905✔
223
  if (code) {
207,796,025✔
224
    return (terrno = TAOS_SYSTEM_ERROR(code));
762✔
225
  }
226
  return code;
207,796,427✔
227
#endif
228
}
229

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

244
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
133,953,687✔
245
  OS_PARAM_CHECK(cond);
133,953,687✔
246
  OS_PARAM_CHECK(mutex);
133,953,687✔
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);
133,953,687✔
254
  if (code) {
133,954,202✔
255
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
256
  }
257
  return code;
133,954,244✔
258
#endif
259
}
260

261
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
352,482,111✔
262
  if (!abstime) return 0;
352,482,111✔
263
  OS_PARAM_CHECK(cond);
352,482,111✔
264
  OS_PARAM_CHECK(mutex);
352,482,111✔
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);
352,482,111✔
274
  if (code == ETIMEDOUT) {
352,483,986✔
275
    return TSDB_CODE_TIMEOUT_ERROR;
289,940,461✔
276
  } else if (code) {
62,543,525✔
277
    return TAOS_SYSTEM_ERROR(code);
×
278
  } else {
279
    return 0;
62,543,525✔
280
  }
281
#endif
282
}
283

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

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

313
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) {
46,342,859✔
314
#ifdef __USE_WIN_THREAD
315
  return 0;
316
#else
317
  OS_PARAM_CHECK(attr);
46,342,859✔
318
  int32_t code = pthread_condattr_init(attr);
46,342,859✔
319
  if (code) {
46,341,252✔
320
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
321
  }
322
  return code;
46,342,606✔
323
#endif
324
}
325

326
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
46,342,593✔
327
#ifdef __USE_WIN_THREAD
328
  return 0;
329
#elif defined(__APPLE__)
330
  return 0;
331
#else
332
  OS_PARAM_CHECK(attr);
46,342,593✔
333
  int32_t code = pthread_condattr_setclock(attr, clockId);
46,342,593✔
334
  if (code) {
46,340,134✔
335
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
336
  }
337
  return code;
46,341,427✔
338
#endif
339
}
340

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

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

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

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

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

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

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

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

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

405
int32_t taosThreadKill(TdThread thread, int32_t sig) {
1,142,954✔
406
  int32_t code = pthread_kill(thread, sig);
1,142,954✔
407
  if (code) {
1,142,954✔
408
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
409
  }
410
  return code;
1,142,954✔
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));
46,959✔
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));
460,843✔
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));
1,477,113✔
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) {
28,134,882✔
469
  OS_PARAM_CHECK(mutex);
28,134,882✔
470
#ifdef __USE_WIN_THREAD
471
  if (TryEnterCriticalSection(mutex)) return 0;
472
  return EBUSY;
473
#else
474
  int32_t code = pthread_mutex_trylock(mutex);
28,134,882✔
475
  if (code && code != EBUSY) {
28,134,935✔
476
    code = TAOS_SYSTEM_ERROR(code);
×
477
  }
478
  return code;
28,134,935✔
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));
5,129,737✔
491
  }
492
  return code;
2,147,483,647✔
493
#endif
494
}
495

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

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

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

556
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
×
557
#ifdef __USE_WIN_THREAD
558
  return 0;
559
#else
560
  OS_PARAM_CHECK(attr);
×
561
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
×
562
  if (code) {
×
563
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
564
  }
565
  return code;
×
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) {
5,657,977✔
574
#ifdef __USE_WIN_THREAD
575
  return 0;
576
#else
577
  OS_PARAM_CHECK(attr);
5,657,977✔
578
  int32_t code = pthread_mutexattr_settype(attr, kind);
5,657,977✔
579
  if (code) {
5,657,977✔
580
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
581
  }
582
  return code;
5,657,977✔
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));
2,683✔
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));
1✔
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✔
UNCOV
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✔
UNCOV
633
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
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) {
×
648
  OS_PARAM_CHECK(rwlock);
×
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);
×
654
  if (code) {
×
655
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
656
  }
657
  return code;
×
658
#endif
659
}
660

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

709
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
11,577,264✔
710
#ifdef __USE_WIN_THREAD
711
  return 0;
712
#else
713
  OS_PARAM_CHECK(attr);
11,577,264✔
714
  int32_t code = pthread_rwlockattr_destroy(attr);
11,577,264✔
715
  if (code) {
11,576,511✔
716
    return (terrno = TAOS_SYSTEM_ERROR(code));
267✔
717
  }
718
  return code;
11,576,244✔
719
#endif
720
}
721

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

737
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr *attr) {
11,577,264✔
738
#ifdef __USE_WIN_THREAD
739
  return 0;
740
#else
741
  OS_PARAM_CHECK(attr);
11,577,264✔
742
  int32_t code = pthread_rwlockattr_init(attr);
11,577,264✔
743
  if (code) {
11,577,264✔
744
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
745
  }
746
  return code;
11,577,995✔
747
#endif
748
}
749

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

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

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

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

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

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

799
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
1,635,492,807✔
800
  OS_PARAM_CHECK(lock);
1,635,492,807✔
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,635,492,807✔
805
  if (code) {
1,635,485,417✔
UNCOV
806
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
807
  }
808
  return code;
1,635,493,528✔
809
#endif
810
}
811

812
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
1,634,606,006✔
813
  OS_PARAM_CHECK(lock);
1,634,606,006✔
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,634,606,006✔
819
  if (code) {
1,634,534,497✔
UNCOV
820
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
821
  }
822
  return code;
1,634,691,111✔
823
#endif
824
}
825

826
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
104,671,473✔
827
  OS_PARAM_CHECK(lock);
104,671,473✔
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);
104,671,473✔
832
  if (code) {
104,687,072✔
833
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
834
  }
835
  return code;
104,687,072✔
836
#endif
837
}
838

839
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
264,455,042✔
840
  OS_PARAM_CHECK(lock);
264,455,042✔
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);
264,455,042✔
845
  if (code && code != EBUSY) {
264,494,463✔
846
    code = TAOS_SYSTEM_ERROR(code);
×
847
  }
848
  return code;
264,494,463✔
849
#endif
850
}
851

852
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
369,136,978✔
853
  OS_PARAM_CHECK(lock);
369,136,978✔
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);
369,136,978✔
858
  if (code) {
369,120,992✔
859
    return (terrno = TAOS_SYSTEM_ERROR(code));
4,772✔
860
  }
861
  return code;
369,116,495✔
862
#endif
863
}
864

865
void taosThreadTestCancel(void) { return pthread_testcancel(); }
×
866

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