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

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

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

86.51
/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) {
126,394✔
21
  OS_PARAM_CHECK(tid);
126,394✔
22
  OS_PARAM_CHECK(start);
126,392✔
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);
126,390✔
41
#endif
42
  if (code) {
126,388!
43
    taosThreadClear(tid);
×
44
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
45
  }
46
  return code;
126,388✔
47
}
48

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

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

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

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

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

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

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

118
int32_t taosThreadAttrInit(TdThreadAttr *attr) {
98,230✔
119
  OS_PARAM_CHECK(attr);
98,230✔
120
  int32_t code = pthread_attr_init(attr);
98,228✔
121
  if (code) {
98,228!
122
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
123
  }
124
  return code;
98,228✔
125
}
126

127
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
95,708✔
128
  OS_PARAM_CHECK(attr);
95,708✔
129
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
95,706✔
130
  if (code) {
95,706✔
131
    return (terrno = TAOS_SYSTEM_ERROR(code));
2✔
132
  }
133
  return code;
95,704✔
134
}
135

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

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

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

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

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

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

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

202
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
233,602✔
203
  OS_PARAM_CHECK(cond);
233,602✔
204
#ifdef __USE_WIN_THREAD
205
  InitializeConditionVariable(cond);
206
  return 0;
207
#else
208
  int32_t code = pthread_cond_init(cond, attr);
233,600✔
209
  if (code) {
233,598!
210
    return (terrno = TAOS_SYSTEM_ERROR(code));
211
  }
212
  return code;
233,600✔
213
#endif
214
}
215

216
int32_t taosThreadCondSignal(TdThreadCond *cond) {
505,882✔
217
  OS_PARAM_CHECK(cond);
505,882✔
218
#ifdef __USE_WIN_THREAD
219
  WakeConditionVariable(cond);
220
  return 0;
221
#else
222
  int32_t code = pthread_cond_signal(cond);
505,880✔
223
  if (code) {
505,844!
224
    return (terrno = TAOS_SYSTEM_ERROR(code));
225
  }
226
  return code;
505,852✔
227
#endif
228
}
229

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

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

261
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
155,338✔
262
  if (!abstime) return 0;
155,338✔
263
  OS_PARAM_CHECK(cond);
155,336✔
264
  OS_PARAM_CHECK(mutex);
155,334✔
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);
155,332✔
274
  if (code == ETIMEDOUT) {
155,332✔
275
    return TSDB_CODE_TIMEOUT_ERROR;
132,032✔
276
  } else if (code) {
23,300!
277
    return TAOS_SYSTEM_ERROR(code);
×
278
  } else {
279
    return 0;
23,300✔
280
  }
281
#endif
282
}
283

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

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

313
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) {
212,550✔
314
#ifdef __USE_WIN_THREAD
315
  return 0;
316
#else
317
  OS_PARAM_CHECK(attr);
212,550✔
318
  int32_t code = pthread_condattr_init(attr);
212,548✔
319
  if (code) {
212,544!
320
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
321
  }
322
  return code;
212,544✔
323
#endif
324
}
325

326
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
212,544✔
327
#ifdef __USE_WIN_THREAD
328
  return 0;
329
#elif defined(__APPLE__)
330
  return 0;
331
#else
332
  OS_PARAM_CHECK(attr);
212,544✔
333
  int32_t code = pthread_condattr_setclock(attr, clockId);
212,542✔
334
  if (code) {
212,544!
335
    return (terrno = TAOS_SYSTEM_ERROR(code));
336
  }
337
  return code;
212,546✔
338
#endif
339
}
340

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

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

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

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

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

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

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

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

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

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

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

417
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
431,564✔
418
  OS_PARAM_CHECK(mutex);
431,564✔
419
#ifdef __USE_WIN_THREAD
420
  DeleteCriticalSection(mutex);
421
  return 0;
422
#else
423
  int32_t code = pthread_mutex_destroy(mutex);
431,562✔
424
  if (code) {
431,554!
425
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
426
  }
427
  return code;
431,554✔
428
#endif
429
}
430

431
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
478,772✔
432
  OS_PARAM_CHECK(mutex);
478,772✔
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);
478,770✔
443
  if (code) {
478,770!
444
    return (terrno = TAOS_SYSTEM_ERROR(code));
445
  }
446
  return code;
478,772✔
447
#endif
448
}
449

450
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
32,499,558✔
451
  OS_PARAM_CHECK(mutex);
32,499,558✔
452
#ifdef __USE_WIN_THREAD
453
  EnterCriticalSection(mutex);
454
  return 0;
455
#else
456
  int32_t code = pthread_mutex_lock(mutex);
32,499,556✔
457
  if (code) {
32,522,150!
458
    return (terrno = TAOS_SYSTEM_ERROR(code));
459
  }
460
  return code;
32,522,564✔
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) {
8,222✔
469
  OS_PARAM_CHECK(mutex);
8,222✔
470
#ifdef __USE_WIN_THREAD
471
  if (TryEnterCriticalSection(mutex)) return 0;
472
  return EBUSY;
473
#else
474
  int32_t code = pthread_mutex_trylock(mutex);
8,220✔
475
  if (code && code != EBUSY) {
8,220!
476
    code = TAOS_SYSTEM_ERROR(code);
×
477
  }
478
  return code;
8,220✔
479
#endif
480
}
481

482
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
32,522,628✔
483
  OS_PARAM_CHECK(mutex);
32,522,628✔
484
#ifdef __USE_WIN_THREAD
485
  LeaveCriticalSection(mutex);
486
  return 0;
487
#else
488
  int32_t code = pthread_mutex_unlock(mutex);
32,522,626✔
489
  if (code) {
32,525,410!
490
    return (terrno = TAOS_SYSTEM_ERROR(code));
491
  }
492
  return code;
32,525,950✔
493
#endif
494
}
495

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

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

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

556
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
6✔
557
#ifdef __USE_WIN_THREAD
558
  return 0;
559
#else
560
  OS_PARAM_CHECK(attr);
6✔
561
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
4✔
562
  if (code) {
4✔
563
    return (terrno = TAOS_SYSTEM_ERROR(code));
2✔
564
  }
565
  return code;
2✔
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) {
1,558✔
574
#ifdef __USE_WIN_THREAD
575
  return 0;
576
#else
577
  OS_PARAM_CHECK(attr);
1,558✔
578
  int32_t code = pthread_mutexattr_settype(attr, kind);
1,556✔
579
  if (code) {
1,556✔
580
    return (terrno = TAOS_SYSTEM_ERROR(code));
2✔
581
  }
582
  return code;
1,554✔
583
#endif
584
}
585

586
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
1,497,790✔
587
  int32_t code = pthread_once(onceControl, initRoutine);
1,497,790✔
588
  if (code) {
1,497,780!
589
    return (terrno = TAOS_SYSTEM_ERROR(code));
590
  }
591
  return code;
1,497,786✔
592
}
593

594
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
1,093,710✔
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);
1,093,710✔
602
  int32_t code = pthread_rwlock_destroy(rwlock);
1,093,706✔
603
  if (code) {
1,093,702!
604
    return (terrno = TAOS_SYSTEM_ERROR(code));
605
  }
606
  return code;
1,093,770✔
607
#endif
608
}
609

610
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
1,096,660✔
611
  OS_PARAM_CHECK(rwlock);
1,096,660✔
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);
1,096,656✔
618
  if (code) {
1,096,576!
619
    return (terrno = TAOS_SYSTEM_ERROR(code));
620
  }
621
  return code;
1,096,596✔
622
#endif
623
}
624

625
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
7,809,400✔
626
  OS_PARAM_CHECK(rwlock);
7,809,400✔
627
#ifdef __USE_WIN_THREAD
628
  AcquireSRWLockShared(&rwlock->lock);
629
  return 0;
630
#else
631
  int32_t code = pthread_rwlock_rdlock(rwlock);
7,809,398✔
632
  if (code) {
7,818,604!
633
    return (terrno = TAOS_SYSTEM_ERROR(code));
634
  }
635
  return code;
7,818,884✔
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) {
24,724✔
648
  OS_PARAM_CHECK(rwlock);
24,724✔
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);
24,722✔
654
  if (code) {
24,722!
655
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
656
  }
657
  return code;
24,722✔
658
#endif
659
}
660

661
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock *rwlock) {
4✔
662
  OS_PARAM_CHECK(rwlock);
4✔
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);
2✔
669
  if (code) {
2!
670
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
671
  }
672
  return code;
2✔
673
#endif
674
}
675

676
int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock) {
30,930,222✔
677
  OS_PARAM_CHECK(rwlock);
30,930,222✔
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);
30,930,214✔
687
  if (code) {
30,945,172!
688
    return (terrno = TAOS_SYSTEM_ERROR(code));
689
  }
690
  return code;
30,945,802✔
691
#endif
692
}
693

694
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
23,099,908✔
695
  OS_PARAM_CHECK(rwlock);
23,099,908✔
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);
23,099,906✔
702
  if (code) {
23,106,670!
703
    return (terrno = TAOS_SYSTEM_ERROR(code));
704
  }
705
  return code;
23,106,752✔
706
#endif
707
}
708

709
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
1,886✔
710
#ifdef __USE_WIN_THREAD
711
  return 0;
712
#else
713
  OS_PARAM_CHECK(attr);
1,886✔
714
  int32_t code = pthread_rwlockattr_destroy(attr);
1,884✔
715
  if (code) {
1,884!
716
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
717
  }
718
  return code;
1,884✔
719
#endif
720
}
721

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

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

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

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

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

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

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

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

799
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
1,199,372✔
800
  OS_PARAM_CHECK(lock);
1,199,372✔
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,199,370✔
805
  if (code) {
1,199,370!
806
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
807
  }
808
  return code;
1,199,370✔
809
#endif
810
}
811

812
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
1,199,572✔
813
  OS_PARAM_CHECK(lock);
1,199,572✔
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,199,570✔
819
  if (code) {
1,199,684!
820
    return (terrno = TAOS_SYSTEM_ERROR(code));
821
  }
822
  return code;
1,199,708✔
823
#endif
824
}
825

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

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

852
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
862,868✔
853
  OS_PARAM_CHECK(lock);
862,868✔
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);
862,866✔
858
  if (code) {
862,874!
859
    return (terrno = TAOS_SYSTEM_ERROR(code));
860
  }
861
  return code;
862,880✔
862
#endif
863
}
864

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

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