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

taosdata / TDengine / #4806

17 Oct 2025 09:36AM UTC coverage: 61.094% (-0.2%) from 61.259%
#4806

push

travis-ci

web-flow
Merge da5cd734f into 21184b20f

155401 of 324487 branches covered (47.89%)

Branch coverage included in aggregate %.

72 of 84 new or added lines in 6 files covered. (85.71%)

778 existing lines in 110 files now uncovered.

207559 of 269610 relevant lines covered (76.98%)

127253104.64 hits per line

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

76.95
/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) {
363,909,475✔
21
  OS_PARAM_CHECK(tid);
363,909,475✔
22
  OS_PARAM_CHECK(start);
363,909,464✔
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);
363,909,453!
41
#endif
42
  if (code) {
363,910,539!
43
    taosThreadClear(tid);
×
44
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
45
  }
46
  return code;
363,910,539✔
47
}
48

49
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) {
299,307,845✔
50
  OS_PARAM_CHECK(attr);
299,307,845✔
51
  int32_t code = pthread_attr_destroy(attr);
299,307,834!
52
  if (code) {
299,307,834!
53
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
54
  }
55
  return code;
299,307,834✔
56
}
57

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

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

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

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

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

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

118
int32_t taosThreadAttrInit(TdThreadAttr *attr) {
300,346,412✔
119
  OS_PARAM_CHECK(attr);
300,346,412✔
120
  int32_t code = pthread_attr_init(attr);
300,346,401!
121
  if (code) {
300,346,309!
122
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
123
  }
124
  return code;
300,346,309✔
125
}
126

127
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
295,057,009✔
128
  OS_PARAM_CHECK(attr);
295,057,009✔
129
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
295,056,998!
130
  if (code) {
295,056,046✔
131
    return (terrno = TAOS_SYSTEM_ERROR(code));
169✔
132
  }
133
  return code;
295,056,085✔
134
}
135

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

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

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

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

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

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

189
int32_t taosThreadCondDestroy(TdThreadCond *cond) {
869,772,189✔
190
  OS_PARAM_CHECK(cond);
869,772,189✔
191
#ifdef __USE_WIN_THREAD
192
  return 0;
193
#else
194
  int32_t code = pthread_cond_destroy(cond);
869,772,178!
195
  if (code) {
869,813,245!
196
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
197
  }
198
  return code;
869,820,624✔
199
#endif
200
}
201

202
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
869,561,022✔
203
  OS_PARAM_CHECK(cond);
869,561,022✔
204
#ifdef __USE_WIN_THREAD
205
  InitializeConditionVariable(cond);
206
  return 0;
207
#else
208
  int32_t code = pthread_cond_init(cond, attr);
869,561,011!
209
  if (code) {
869,559,752!
210
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
211
  }
212
  return code;
869,575,945✔
213
#endif
214
}
215

216
int32_t taosThreadCondSignal(TdThreadCond *cond) {
135,287,640✔
217
  OS_PARAM_CHECK(cond);
135,287,640✔
218
#ifdef __USE_WIN_THREAD
219
  WakeConditionVariable(cond);
220
  return 0;
221
#else
222
  int32_t code = pthread_cond_signal(cond);
135,287,629!
223
  if (code) {
135,292,579✔
224
    return (terrno = TAOS_SYSTEM_ERROR(code));
806✔
225
  }
226
  return code;
135,291,773✔
227
#endif
228
}
229

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

244
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
69,236,005✔
245
  OS_PARAM_CHECK(cond);
69,236,005✔
246
  OS_PARAM_CHECK(mutex);
69,235,994✔
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);
69,235,983!
254
  if (code) {
69,236,043!
255
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
256
  }
257
  return code;
69,236,043✔
258
#endif
259
}
260

261
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
340,926,839✔
262
  if (!abstime) return 0;
340,926,839✔
263
  OS_PARAM_CHECK(cond);
340,926,828✔
264
  OS_PARAM_CHECK(mutex);
340,926,817✔
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);
340,926,806!
274
  if (code == ETIMEDOUT) {
340,926,922✔
275
    return TSDB_CODE_TIMEOUT_ERROR;
293,061,676✔
276
  } else if (code) {
47,865,246!
277
    return TAOS_SYSTEM_ERROR(code);
×
278
  } else {
279
    return 0;
47,865,246✔
280
  }
281
#endif
282
}
283

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

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

313
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) {
46,496,979✔
314
#ifdef __USE_WIN_THREAD
315
  return 0;
316
#else
317
  OS_PARAM_CHECK(attr);
46,496,979✔
318
  int32_t code = pthread_condattr_init(attr);
46,496,968!
319
  if (code) {
46,496,529✔
320
    return (terrno = TAOS_SYSTEM_ERROR(code));
658✔
321
  }
322
  return code;
46,495,871✔
323
#endif
324
}
325

326
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
46,496,968✔
327
#ifdef __USE_WIN_THREAD
328
  return 0;
329
#elif defined(__APPLE__)
330
  return 0;
331
#else
332
  OS_PARAM_CHECK(attr);
46,496,968✔
333
  int32_t code = pthread_condattr_setclock(attr, clockId);
46,496,957!
334
  if (code) {
46,495,347!
UNCOV
335
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
336
  }
337
  return code;
46,496,526✔
338
#endif
339
}
340

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

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

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

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

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

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

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

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

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

405
int32_t taosThreadKill(TdThread thread, int32_t sig) {
916,550✔
406
  int32_t code = pthread_kill(thread, sig);
916,550✔
407
  if (code) {
916,550!
408
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
409
  }
410
  return code;
916,550✔
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));
22,479✔
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));
123,796✔
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));
2,033,587✔
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) {
26,494,544✔
469
  OS_PARAM_CHECK(mutex);
26,494,544✔
470
#ifdef __USE_WIN_THREAD
471
  if (TryEnterCriticalSection(mutex)) return 0;
472
  return EBUSY;
473
#else
474
  int32_t code = pthread_mutex_trylock(mutex);
26,494,533!
475
  if (code && code != EBUSY) {
26,495,203!
476
    code = TAOS_SYSTEM_ERROR(code);
×
477
  }
478
  return code;
26,495,203✔
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,446,395✔
491
  }
492
  return code;
2,147,483,647✔
493
#endif
494
}
495

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

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

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

556
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
33✔
557
#ifdef __USE_WIN_THREAD
558
  return 0;
559
#else
560
  OS_PARAM_CHECK(attr);
33✔
561
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
22!
562
  if (code) {
22✔
563
    return (terrno = TAOS_SYSTEM_ERROR(code));
11✔
564
  }
565
  return code;
11✔
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,185,805✔
574
#ifdef __USE_WIN_THREAD
575
  return 0;
576
#else
577
  OS_PARAM_CHECK(attr);
6,185,805✔
578
  int32_t code = pthread_mutexattr_settype(attr, kind);
6,185,794!
579
  if (code) {
6,185,794✔
580
    return (terrno = TAOS_SYSTEM_ERROR(code));
11✔
581
  }
582
  return code;
6,185,783✔
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));
25,129✔
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));
×
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));
399,573✔
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));
684✔
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) {
22✔
648
  OS_PARAM_CHECK(rwlock);
22✔
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);
11!
654
  if (code) {
11!
655
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
656
  }
657
  return code;
11✔
658
#endif
659
}
660

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

709
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
12,989,305✔
710
#ifdef __USE_WIN_THREAD
711
  return 0;
712
#else
713
  OS_PARAM_CHECK(attr);
12,989,305✔
714
  int32_t code = pthread_rwlockattr_destroy(attr);
12,989,294!
715
  if (code) {
12,988,313!
UNCOV
716
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
717
  }
718
  return code;
12,989,042✔
719
#endif
720
}
721

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

737
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr *attr) {
12,990,556✔
738
#ifdef __USE_WIN_THREAD
739
  return 0;
740
#else
741
  OS_PARAM_CHECK(attr);
12,990,556✔
742
  int32_t code = pthread_rwlockattr_init(attr);
12,990,545!
743
  if (code) {
12,990,288✔
744
    return (terrno = TAOS_SYSTEM_ERROR(code));
757✔
745
  }
746
  return code;
12,989,531✔
747
#endif
748
}
749

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

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

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

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

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

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

799
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
1,638,055,767✔
800
  OS_PARAM_CHECK(lock);
1,638,055,767✔
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,638,055,756!
805
  if (code) {
1,638,062,542!
806
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
807
  }
808
  return code;
1,638,073,535✔
809
#endif
810
}
811

812
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
1,637,046,624✔
813
  OS_PARAM_CHECK(lock);
1,637,046,624✔
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,637,046,613!
819
  if (code) {
1,637,134,409!
820
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
821
  }
822
  return code;
1,637,152,342✔
823
#endif
824
}
825

826
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
109,804,433✔
827
  OS_PARAM_CHECK(lock);
109,804,433✔
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);
109,804,422!
832
  if (code) {
109,839,015!
833
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
834
  }
835
  return code;
109,839,015✔
836
#endif
837
}
838

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

852
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
366,702,304✔
853
  OS_PARAM_CHECK(lock);
366,702,304✔
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);
366,702,293!
858
  if (code) {
366,687,281!
859
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
860
  }
861
  return code;
366,700,851✔
862
#endif
863
}
864

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

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