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

taosdata / TDengine / #3564

24 Dec 2024 05:40AM UTC coverage: 62.21% (+1.2%) from 61.045%
#3564

push

travis-ci

web-flow
Merge pull request #29289 from taosdata/fix/TD-33270-2

ci(stream):add stream unit test

138331 of 285924 branches covered (48.38%)

Branch coverage included in aggregate %.

215800 of 283329 relevant lines covered (76.17%)

19198660.97 hits per line

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

85.06
/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) {
848,771✔
21
  OS_PARAM_CHECK(tid);
848,771✔
22
  OS_PARAM_CHECK(start);
848,770✔
23
  int32_t code = pthread_create(tid, attr, start, arg);
848,769✔
24
  if (code) {
848,770!
25
    taosThreadClear(tid);
×
26
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
27
  }
28
  return code;
848,770✔
29
}
30

31
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) {
689,562✔
32
  OS_PARAM_CHECK(attr);
689,562✔
33
  int32_t code = pthread_attr_destroy(attr);
689,561✔
34
  if (code) {
689,561!
35
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
36
  }
37
  return code;
689,561✔
38
}
39

40
int32_t taosThreadAttrGetDetachState(const TdThreadAttr *attr, int32_t *detachstate) {
3✔
41
  OS_PARAM_CHECK(attr);
3✔
42
  OS_PARAM_CHECK(detachstate);
2✔
43
  int32_t code = pthread_attr_getdetachstate(attr, detachstate);
1✔
44
  if (code) {
1!
45
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
46
  }
47
  return code;
1✔
48
}
49

50
int32_t taosThreadAttrGetInheritSched(const TdThreadAttr *attr, int32_t *inheritsched) {
3✔
51
  OS_PARAM_CHECK(attr);
3✔
52
  OS_PARAM_CHECK(inheritsched);
2✔
53
  int32_t code = pthread_attr_getinheritsched(attr, inheritsched);
1✔
54
  if (code) {
1!
55
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
56
  }
57
  return code;
1✔
58
}
59

60
int32_t taosThreadAttrGetSchedParam(const TdThreadAttr *attr, struct sched_param *param) {
3✔
61
  OS_PARAM_CHECK(attr);
3✔
62
  OS_PARAM_CHECK(param);
2✔
63
  int32_t code = pthread_attr_getschedparam(attr, param);
1✔
64
  if (code) {
1!
65
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
66
  }
67
  return code;
1✔
68
}
69

70
int32_t taosThreadAttrGetSchedPolicy(const TdThreadAttr *attr, int32_t *policy) {
3✔
71
  OS_PARAM_CHECK(attr);
3✔
72
  OS_PARAM_CHECK(policy);
2✔
73
  int32_t code = pthread_attr_getschedpolicy(attr, policy);
1✔
74
  if (code) {
1!
75
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
76
  }
77
  return code;
1✔
78
}
79

80
int32_t taosThreadAttrGetScope(const TdThreadAttr *attr, int32_t *contentionscope) {
3✔
81
  OS_PARAM_CHECK(attr);
3✔
82
  OS_PARAM_CHECK(contentionscope);
2✔
83
  int32_t code = pthread_attr_getscope(attr, contentionscope);
1✔
84
  if (code) {
1!
85
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
86
  }
87
  return code;
1✔
88
}
89

90
int32_t taosThreadAttrGetStackSize(const TdThreadAttr *attr, size_t *stacksize) {
3✔
91
  OS_PARAM_CHECK(attr);
3✔
92
  OS_PARAM_CHECK(stacksize);
2✔
93
  int32_t code = pthread_attr_getstacksize(attr, stacksize);
1✔
94
  if (code) {
1!
95
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
96
  }
97
  return code;
1✔
98
}
99

100
int32_t taosThreadAttrInit(TdThreadAttr *attr) {
689,892✔
101
  OS_PARAM_CHECK(attr);
689,892✔
102
  int32_t code = pthread_attr_init(attr);
689,891✔
103
  if (code) {
689,889!
104
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
105
  }
106
  return code;
689,889✔
107
}
108

109
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
677,506✔
110
  OS_PARAM_CHECK(attr);
677,506✔
111
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
677,505✔
112
  if (code) {
677,505!
113
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
114
  }
115
  return code;
677,505✔
116
}
117

118
int32_t taosThreadAttrSetInheritSched(TdThreadAttr *attr, int32_t inheritsched) {
3✔
119
  OS_PARAM_CHECK(attr);
3✔
120
  int32_t code = pthread_attr_setinheritsched(attr, inheritsched);
2✔
121
  if (code) {
2✔
122
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
123
  }
124
  return code;
1✔
125
}
126

127
int32_t taosThreadAttrSetSchedParam(TdThreadAttr *attr, const struct sched_param *param) {
3✔
128
  OS_PARAM_CHECK(attr);
3✔
129
  int32_t code = pthread_attr_setschedparam(attr, param);
2✔
130
  if (code) {
2✔
131
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
132
  }
133
  return code;
1✔
134
}
135

136
int32_t taosThreadAttrSetSchedPolicy(TdThreadAttr *attr, int32_t policy) {
3✔
137
  OS_PARAM_CHECK(attr);
3✔
138
  int32_t code = pthread_attr_setschedpolicy(attr, policy);
2✔
139
  if (code) {
2✔
140
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
141
  }
142
  return code;
1✔
143
}
144

145
int32_t taosThreadAttrSetScope(TdThreadAttr *attr, int32_t contentionscope) {
3✔
146
  OS_PARAM_CHECK(attr);
3✔
147
  int32_t code = pthread_attr_setscope(attr, contentionscope);
2✔
148
  if (code) {
2✔
149
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
150
  }
151
  return code;
1✔
152
}
153

154
int32_t taosThreadAttrSetStackSize(TdThreadAttr *attr, size_t stacksize) {
3✔
155
  OS_PARAM_CHECK(attr);
3✔
156
  int32_t code = pthread_attr_setstacksize(attr, stacksize);
2✔
157
  if (code) {
2✔
158
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
159
  }
160
  return code;
1✔
161
}
162

163
int32_t taosThreadCancel(TdThread thread) {
2✔
164
  int32_t code = pthread_cancel(thread);
2✔
165
  if (code) {
2!
166
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
167
  }
168
  return code;
2✔
169
}
170

171
int32_t taosThreadCondDestroy(TdThreadCond *cond) {
25,182,228✔
172
  OS_PARAM_CHECK(cond);
25,182,228✔
173
#ifdef __USE_WIN_THREAD
174
  return 0;
175
#else
176
  int32_t code = pthread_cond_destroy(cond);
25,182,227✔
177
  if (code) {
25,188,282!
178
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
179
  }
180
  return code;
25,188,692✔
181
#endif
182
}
183

184
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
25,147,496✔
185
  OS_PARAM_CHECK(cond);
25,147,496✔
186
#ifdef __USE_WIN_THREAD
187
  InitializeConditionVariable(cond);
188
  return 0;
189
#else
190
  int32_t code = pthread_cond_init(cond, attr);
25,147,495✔
191
  if (code) {
25,153,749!
192
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
193
  }
194
  return code;
25,154,676✔
195
#endif
196
}
197

198
int32_t taosThreadCondSignal(TdThreadCond *cond) {
2,796,579✔
199
  OS_PARAM_CHECK(cond);
2,796,579✔
200
#ifdef __USE_WIN_THREAD
201
  WakeConditionVariable(cond);
202
  return 0;
203
#else
204
  int32_t code = pthread_cond_signal(cond);
2,796,578✔
205
  if (code) {
2,796,588!
206
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
207
  }
208
  return code;
2,796,591✔
209
#endif
210
}
211

212
int32_t taosThreadCondBroadcast(TdThreadCond *cond) {
30,396✔
213
  OS_PARAM_CHECK(cond);
30,396✔
214
#ifdef __USE_WIN_THREAD
215
  WakeAllConditionVariable(cond);
216
  return 0;
217
#else
218
  int32_t code = pthread_cond_broadcast(cond);
30,395✔
219
  if (code) {
30,395!
220
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
221
  }
222
  return code;
30,395✔
223
#endif
224
}
225

226
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
2,408,676✔
227
  OS_PARAM_CHECK(cond);
2,408,676✔
228
  OS_PARAM_CHECK(mutex);
2,408,675✔
229
#ifdef __USE_WIN_THREAD
230
  if (!SleepConditionVariableCS(cond, mutex, INFINITE)) {
231
    return EINVAL;
232
  }
233
  return 0;
234
#else
235
  int32_t code = pthread_cond_wait(cond, mutex);
2,408,674✔
236
  if (code) {
2,408,671!
237
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
238
  }
239
  return code;
2,408,672✔
240
#endif
241
}
242

243
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
754,728✔
244
  if (!abstime) return 0;
754,728✔
245
  OS_PARAM_CHECK(cond);
754,727✔
246
  OS_PARAM_CHECK(mutex);
754,726✔
247
#ifdef __USE_WIN_THREAD
248
  if (SleepConditionVariableCS(cond, mutex, (DWORD)(abstime->tv_sec * 1e3 + abstime->tv_nsec / 1e6))) return 0;
249
  DWORD error = GetLastError();
250
  if (error == ERROR_TIMEOUT) {
251
    return TSDB_CODE_TIMEOUT_ERROR;
252
  }
253
  return TAOS_SYSTEM_WINAPI_ERROR(error);
254
#else
255
  int32_t code = pthread_cond_timedwait(cond, mutex, abstime);
754,725✔
256
  if (code == ETIMEDOUT) {
754,731✔
257
    return TSDB_CODE_TIMEOUT_ERROR;
598,000✔
258
  } else if (code) {
156,731!
259
    return TAOS_SYSTEM_ERROR(code);
×
260
  } else {
261
    return 0;
156,731✔
262
  }
263
#endif
264
}
265

266
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr *attr) {
214,958✔
267
#ifdef __USE_WIN_THREAD
268
  return 0;
269
#else
270
  OS_PARAM_CHECK(attr);
214,958✔
271
  int32_t code = pthread_condattr_destroy(attr);
214,957✔
272
  if (code) {
214,956!
273
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
274
  }
275
  return code;
214,956✔
276
#endif
277
}
278

279
int32_t taosThreadCondAttrGetPshared(const TdThreadCondAttr *attr, int32_t *pshared) {
3✔
280
  OS_PARAM_CHECK(attr);
3✔
281
  OS_PARAM_CHECK(pshared);
2✔
282
#ifdef __USE_WIN_THREAD
283
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
284
  return 0;
285
#else
286
  OS_PARAM_CHECK(attr);
1!
287
  int32_t code = pthread_condattr_getpshared(attr, pshared);
1✔
288
  if (code) {
1!
289
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
290
  }
291
  return code;
1✔
292
#endif
293
}
294

295
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) {
214,958✔
296
#ifdef __USE_WIN_THREAD
297
  return 0;
298
#else
299
  OS_PARAM_CHECK(attr);
214,958✔
300
  int32_t code = pthread_condattr_init(attr);
214,957✔
301
  if (code) {
214,952!
302
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
303
  }
304
  return code;
214,953✔
305
#endif
306
}
307

308
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
214,952✔
309
#ifdef __USE_WIN_THREAD
310
  return 0;
311
#elif defined(__APPLE__)
312
  return 0;
313
#else
314
  OS_PARAM_CHECK(attr);
214,952✔
315
  int32_t code = pthread_condattr_setclock(attr, clockId);
214,951✔
316
  if (code) {
214,955!
317
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
318
  }
319
  return code;
214,955✔
320
#endif
321
}
322

323
int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr *attr, int32_t pshared) {
3✔
324
  OS_PARAM_CHECK(attr);
3✔
325
#ifdef __USE_WIN_THREAD
326
  return 0;
327
#else
328
  int32_t code = pthread_condattr_setpshared(attr, pshared);
2✔
329
  if (code) {
2✔
330
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
331
  }
332
  return code;
1✔
333
#endif
334
}
335

336
int32_t taosThreadDetach(TdThread thread) {
2✔
337
  int32_t code = pthread_detach(thread);
2✔
338
  if (code) {
2!
339
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
340
  }
341
  return code;
2✔
342
}
343

344
int32_t taosThreadEqual(TdThread t1, TdThread t2) { return pthread_equal(t1, t2); }
1✔
345

346
void taosThreadExit(void *valuePtr) {
2✔
347
  if (valuePtr) return pthread_exit(valuePtr);
2✔
348
}
349

350
int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param) {
6✔
351
  OS_PARAM_CHECK(policy);
6✔
352
  OS_PARAM_CHECK(param);
4✔
353
  int32_t code = pthread_getschedparam(thread, policy, param);
2✔
354
  if (code) {
2✔
355
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
356
  }
357
  return code;
1✔
358
}
359

360
void *taosThreadGetSpecific(TdThreadKey key) { return pthread_getspecific(key); }
2✔
361

362
int32_t taosThreadJoin(TdThread thread, void **valuePtr) {
775,776✔
363
  int32_t code = pthread_join(thread, valuePtr);
775,776✔
364
  if (code) {
775,776✔
365
    return (terrno = TAOS_SYSTEM_ERROR(code));
3✔
366
  }
367
  return code;
775,773✔
368
}
369

370
int32_t taosThreadKeyCreate(TdThreadKey *key, void (*destructor)(void *)) {
20✔
371
  OS_PARAM_CHECK(key);
20!
372
  int32_t code = pthread_key_create(key, destructor);
20✔
373
  if (code) {
20!
374
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
375
  }
376
  return code;
20✔
377
}
378

379
int32_t taosThreadKeyDelete(TdThreadKey key) {
2✔
380
  int32_t code = pthread_key_delete(key);
2✔
381
  if (code) {
2!
382
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
383
  }
384
  return code;
2✔
385
}
386

387
int32_t taosThreadKill(TdThread thread, int32_t sig) {
1,062✔
388
  int32_t code = pthread_kill(thread, sig);
1,062✔
389
  if (code) {
1,062!
390
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
391
  }
392
  return code;
1,062✔
393
}
394

395
// int32_t taosThreadMutexConsistent(TdThreadMutex* mutex) {
396
//   return pthread_mutex_consistent(mutex);
397
// }
398

399
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
34,440,060✔
400
  OS_PARAM_CHECK(mutex);
34,440,060✔
401
#ifdef __USE_WIN_THREAD
402
  DeleteCriticalSection(mutex);
403
  return 0;
404
#else
405
  int32_t code = pthread_mutex_destroy(mutex);
34,440,059✔
406
  if (code) {
34,439,079!
407
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
408
  }
409
  return code;
34,441,180✔
410
#endif
411
}
412

413
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
34,726,118✔
414
  OS_PARAM_CHECK(mutex);
34,726,118✔
415
#ifdef __USE_WIN_THREAD
416
  /**
417
   * Windows Server 2003 and Windows XP:  In low memory situations, InitializeCriticalSection can raise a
418
   * STATUS_NO_MEMORY exception. Starting with Windows Vista, this exception was eliminated and
419
   * InitializeCriticalSection always succeeds, even in low memory situations.
420
   */
421
  InitializeCriticalSection(mutex);
422
  return 0;
423
#else
424
  int32_t code = pthread_mutex_init(mutex, attr);
34,726,117✔
425
  if (code) {
34,733,058!
426
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
427
  }
428
  return code;
34,742,164✔
429
#endif
430
}
431

432
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
891,463,517✔
433
  OS_PARAM_CHECK(mutex);
891,463,517✔
434
#ifdef __USE_WIN_THREAD
435
  EnterCriticalSection(mutex);
436
  return 0;
437
#else
438
  int32_t code = pthread_mutex_lock(mutex);
891,463,516✔
439
  if (code) {
1,084,475,573!
440
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
441
  }
442
  return code;
1,084,686,247✔
443
#endif
444
}
445

446
// int32_t taosThreadMutexTimedLock(TdThreadMutex * mutex, const struct timespec *abstime) {
447
//   return pthread_mutex_timedlock(mutex, abstime);
448
// }
449

450
int32_t taosThreadMutexTryLock(TdThreadMutex *mutex) {
3✔
451
  OS_PARAM_CHECK(mutex);
3✔
452
#ifdef __USE_WIN_THREAD
453
  if (TryEnterCriticalSection(mutex)) return 0;
454
  return EBUSY;
455
#else
456
  int32_t code = pthread_mutex_trylock(mutex);
2✔
457
  if (code && code != EBUSY) {
2!
458
    code = TAOS_SYSTEM_ERROR(code);
×
459
  }
460
  return code;
2✔
461
#endif
462
}
463

464
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
1,081,957,636✔
465
  OS_PARAM_CHECK(mutex);
1,081,957,636✔
466
#ifdef __USE_WIN_THREAD
467
  LeaveCriticalSection(mutex);
468
  return 0;
469
#else
470
  int32_t code = pthread_mutex_unlock(mutex);
1,081,957,635✔
471
  if (code) {
1,084,700,683!
472
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
473
  }
474
  return code;
1,085,006,542✔
475
#endif
476
}
477

478
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr *attr) {
31,368✔
479
#ifdef __USE_WIN_THREAD
480
  return 0;
481
#else
482
  OS_PARAM_CHECK(attr);
31,368✔
483
  int32_t code = pthread_mutexattr_destroy(attr);
31,367✔
484
  if (code) {
31,365!
485
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
486
  }
487
  return code;
31,369✔
488
#endif
489
}
490

491
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr *attr, int32_t *pshared) {
3✔
492
  OS_PARAM_CHECK(pshared);
3✔
493
#ifdef __USE_WIN_THREAD
494
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
495
  return 0;
496
#else
497
  OS_PARAM_CHECK(attr);
2✔
498
  int32_t code = pthread_mutexattr_getpshared(attr, pshared);
1✔
499
  if (code) {
1!
500
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
501
  }
502
  return code;
1✔
503
#endif
504
}
505

506
// int32_t taosThreadMutexAttrGetRobust(const TdThreadMutexAttr * attr, int32_t * robust) {
507
//   return pthread_mutexattr_getrobust(attr, robust);
508
// }
509

510
int32_t taosThreadMutexAttrGetType(const TdThreadMutexAttr *attr, int32_t *kind) {
3✔
511
  OS_PARAM_CHECK(kind);
3✔
512
#ifdef __USE_WIN_THREAD
513
  if (kind) *kind = PTHREAD_MUTEX_NORMAL;
514
  return 0;
515
#else
516
  OS_PARAM_CHECK(attr);
2✔
517
  int32_t code = pthread_mutexattr_gettype(attr, kind);
1✔
518
  if (code) {
1!
519
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
520
  }
521
  return code;
1✔
522
#endif
523
}
524

525
int32_t taosThreadMutexAttrInit(TdThreadMutexAttr *attr) {
31,374✔
526
#ifdef __USE_WIN_THREAD
527
  return 0;
528
#else
529
  OS_PARAM_CHECK(attr);
31,374✔
530
  int32_t code = pthread_mutexattr_init(attr);
31,373✔
531
  if (code) {
31,371!
532
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
533
  }
534
  return code;
31,376✔
535
#endif
536
}
537

538
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
3✔
539
#ifdef __USE_WIN_THREAD
540
  return 0;
541
#else
542
  OS_PARAM_CHECK(attr);
3✔
543
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
2✔
544
  if (code) {
2✔
545
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
546
  }
547
  return code;
1✔
548
#endif
549
}
550

551
// int32_t taosThreadMutexAttrSetRobust(TdThreadMutexAttr * attr, int32_t robust) {
552
//   return pthread_mutexattr_setrobust(attr, robust);
553
// }
554

555
int32_t taosThreadMutexAttrSetType(TdThreadMutexAttr *attr, int32_t kind) {
31,374✔
556
#ifdef __USE_WIN_THREAD
557
  return 0;
558
#else
559
  OS_PARAM_CHECK(attr);
31,374✔
560
  int32_t code = pthread_mutexattr_settype(attr, kind);
31,373✔
561
  if (code) {
31,373✔
562
    return (terrno = TAOS_SYSTEM_ERROR(code));
4✔
563
  }
564
  return code;
31,369✔
565
#endif
566
}
567

568
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
49,311,001✔
569
  int32_t code = pthread_once(onceControl, initRoutine);
49,311,001✔
570
  if (code) {
49,312,261!
571
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
572
  }
573
  return code;
49,314,406✔
574
}
575

576
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
31,269,271✔
577
#ifdef __USE_WIN_THREAD
578
  /* SRWLock does not need explicit destruction so long as there are no waiting threads
579
   * See: https://docs.microsoft.com/windows/win32/api/synchapi/nf-synchapi-initializesrwlock#remarks
580
   */
581
  return 0;
582
#else
583
  OS_PARAM_CHECK(rwlock);
31,269,271✔
584
  int32_t code = pthread_rwlock_destroy(rwlock);
31,269,269✔
585
  if (code) {
31,269,289✔
586
    return (terrno = TAOS_SYSTEM_ERROR(code));
13✔
587
  }
588
  return code;
31,269,276✔
589
#endif
590
}
591

592
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
31,274,848✔
593
  OS_PARAM_CHECK(rwlock);
31,274,848✔
594
#ifdef __USE_WIN_THREAD
595
  memset(rwlock, 0, sizeof(*rwlock));
596
  InitializeSRWLock(&rwlock->lock);
597
  return 0;
598
#else
599
  int32_t code = pthread_rwlock_init(rwlock, attr);
31,274,846✔
600
  if (code) {
31,273,111!
601
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
602
  }
603
  return code;
31,273,510✔
604
#endif
605
}
606

607
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
563,969,590✔
608
  OS_PARAM_CHECK(rwlock);
563,969,590✔
609
#ifdef __USE_WIN_THREAD
610
  AcquireSRWLockShared(&rwlock->lock);
611
  return 0;
612
#else
613
  int32_t code = pthread_rwlock_rdlock(rwlock);
563,969,589✔
614
  if (code) {
564,707,078!
615
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
616
  }
617
  return code;
564,716,891✔
618
#endif
619
}
620

621
// int32_t taosThreadRwlockTimedRdlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
622
//   return pthread_rwlock_timedrdlock(rwlock, abstime);
623
// }
624

625
// int32_t taosThreadRwlockTimedWrlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
626
//   return pthread_rwlock_timedwrlock(rwlock, abstime);
627
// }
628

629
int32_t taosThreadRwlockTryRdlock(TdThreadRwlock *rwlock) {
2✔
630
  OS_PARAM_CHECK(rwlock);
2✔
631
#ifdef __USE_WIN_THREAD
632
  if (!TryAcquireSRWLockShared(&rwlock->lock)) return EBUSY;
633
  return 0;
634
#else
635
  int32_t code = pthread_rwlock_tryrdlock(rwlock);
1✔
636
  if (code) {
1!
637
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
638
  }
639
  return code;
1✔
640
#endif
641
}
642

643
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock *rwlock) {
2✔
644
  OS_PARAM_CHECK(rwlock);
2✔
645
#ifdef __USE_WIN_THREAD
646
  if (!TryAcquireSRWLockExclusive(&rwlock->lock)) return EBUSY;
647
  atomic_store_8(&rwlock->excl, 1);
648
  return 0;
649
#else
650
  int32_t code = pthread_rwlock_trywrlock(rwlock);
1✔
651
  if (code) {
1!
652
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
653
  }
654
  return code;
1✔
655
#endif
656
}
657

658
int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock) {
1,419,350,675✔
659
  OS_PARAM_CHECK(rwlock);
1,419,350,675✔
660
#ifdef __USE_WIN_THREAD
661
  if (1 == atomic_val_compare_exchange_8(&rwlock->excl, 1, 0)) {
662
    ReleaseSRWLockExclusive(&rwlock->lock);
663
  } else {
664
    ReleaseSRWLockShared(&rwlock->lock);
665
  }
666
  return 0;
667
#else
668
  int32_t code = pthread_rwlock_unlock(rwlock);
1,419,350,673✔
669
  if (code) {
1,419,994,463!
670
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
671
  }
672
  return code;
1,420,014,161✔
673
#endif
674
}
675

676
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
855,053,112✔
677
  OS_PARAM_CHECK(rwlock);
855,053,112✔
678
#ifdef __USE_WIN_THREAD
679
  AcquireSRWLockExclusive(&rwlock->lock);
680
  atomic_store_8(&rwlock->excl, 1);
681
  return 0;
682
#else
683
  int32_t code = pthread_rwlock_wrlock(rwlock);
855,053,111✔
684
  if (code) {
855,385,727!
685
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
686
  }
687
  return code;
855,387,250✔
688
#endif
689
}
690

691
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
37,762✔
692
#ifdef __USE_WIN_THREAD
693
  return 0;
694
#else
695
  OS_PARAM_CHECK(attr);
37,762✔
696
  int32_t code = pthread_rwlockattr_destroy(attr);
37,761✔
697
  if (code) {
37,761!
698
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
699
  }
700
  return code;
37,768✔
701
#endif
702
}
703

704
int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr *attr, int32_t *pshared) {
3✔
705
  OS_PARAM_CHECK(attr);
3✔
706
  OS_PARAM_CHECK(pshared);
2✔
707
#ifdef __USE_WIN_THREAD
708
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
709
  return 0;
710
#else
711
  int32_t code = pthread_rwlockattr_getpshared(attr, pshared);
1✔
712
  if (code) {
1!
713
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
714
  }
715
  return code;
1✔
716
#endif
717
}
718

719
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr *attr) {
37,769✔
720
#ifdef __USE_WIN_THREAD
721
  return 0;
722
#else
723
  OS_PARAM_CHECK(attr);
37,769✔
724
  int32_t code = pthread_rwlockattr_init(attr);
37,768✔
725
  if (code) {
37,768!
726
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
727
  }
728
  return code;
37,768✔
729
#endif
730
}
731

732
int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr *attr, int32_t pshared) {
3✔
733
#ifdef __USE_WIN_THREAD
734
  return 0;
735
#else
736
  OS_PARAM_CHECK(attr);
3✔
737
  int32_t code = pthread_rwlockattr_setpshared(attr, pshared);
2✔
738
  if (code) {
2✔
739
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
740
  }
741
  return code;
1✔
742
#endif
743
}
744

745
TdThread taosThreadSelf(void) { return pthread_self(); }
3✔
746

747
int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate) {
2✔
748
  int32_t code = pthread_setcancelstate(state, oldstate);
2✔
749
  if (code) {
2✔
750
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
751
  }
752
  return code;
1✔
753
}
754

755
int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype) {
2✔
756
  int32_t code = pthread_setcanceltype(type, oldtype);
2✔
757
  if (code) {
2✔
758
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
759
  }
760
  return code;
1✔
761
}
762

763
int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sched_param *param) {
2✔
764
  OS_PARAM_CHECK(param);
2!
765
  int32_t code = pthread_setschedparam(thread, policy, param);
2✔
766
  if (code) {
2✔
767
    return (terrno = TAOS_SYSTEM_ERROR(code));
1✔
768
  }
769
  return code;
1✔
770
}
771

772
int32_t taosThreadSetSpecific(TdThreadKey key, const void *value) {
122✔
773
  OS_PARAM_CHECK(value);
122✔
774
  int32_t code = pthread_setspecific(key, value);
120✔
775
  if (code) {
120!
776
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
777
  }
778
  return code;
120✔
779
}
780

781
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
4,034,917✔
782
  OS_PARAM_CHECK(lock);
4,034,917✔
783
#ifdef TD_USE_SPINLOCK_AS_MUTEX
784
  return pthread_mutex_destroy((pthread_mutex_t *)lock);
785
#else
786
  int32_t code = pthread_spin_destroy((pthread_spinlock_t *)lock);
4,034,916✔
787
  if (code) {
4,034,939!
788
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
789
  }
790
  return code;
4,034,967✔
791
#endif
792
}
793

794
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
4,018,139✔
795
  OS_PARAM_CHECK(lock);
4,018,139✔
796
#ifdef TD_USE_SPINLOCK_AS_MUTEX
797
  if (pshared != 0) return TSDB_CODE_INVALID_PARA;
798
  return pthread_mutex_init((pthread_mutex_t *)lock, NULL);
799
#else
800
  int32_t code = pthread_spin_init((pthread_spinlock_t *)lock, pshared);
4,018,138✔
801
  if (code) {
4,020,009!
802
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
803
  }
804
  return code;
4,020,500✔
805
#endif
806
}
807

808
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
356,307✔
809
  OS_PARAM_CHECK(lock);
356,307✔
810
#ifdef TD_USE_SPINLOCK_AS_MUTEX
811
  return pthread_mutex_lock((pthread_mutex_t *)lock);
812
#else
813
  int32_t code = pthread_spin_lock((pthread_spinlock_t *)lock);
356,306✔
814
  if (code) {
356,393!
815
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
816
  }
817
  return code;
356,393✔
818
#endif
819
}
820

821
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
995,337✔
822
  OS_PARAM_CHECK(lock);
995,337✔
823
#ifdef TD_USE_SPINLOCK_AS_MUTEX
824
  return pthread_mutex_trylock((pthread_mutex_t *)lock);
825
#else
826
  int32_t code = pthread_spin_trylock((pthread_spinlock_t *)lock);
995,336✔
827
  if (code && code != EBUSY) {
995,630!
828
    code = TAOS_SYSTEM_ERROR(code);
×
829
  }
830
  return code;
995,630✔
831
#endif
832
}
833

834
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
1,351,743✔
835
  OS_PARAM_CHECK(lock);
1,351,743✔
836
#ifdef TD_USE_SPINLOCK_AS_MUTEX
837
  return pthread_mutex_unlock((pthread_mutex_t *)lock);
838
#else
839
  int32_t code = pthread_spin_unlock((pthread_spinlock_t *)lock);
1,351,742✔
840
  if (code) {
1,351,740!
841
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
842
  }
843
  return code;
1,351,829✔
844
#endif
845
}
846

847
void taosThreadTestCancel(void) { return pthread_testcancel(); }
1✔
848

849
void taosThreadClear(TdThread *thread) {
667,237✔
850
  if (!thread) return;
667,237✔
851
  (void)memset(thread, 0, sizeof(TdThread));
667,236✔
852
}
853

854
#ifdef WINDOWS
855
bool taosThreadIsMain() {
856
  DWORD curProcessId = GetCurrentProcessId();
857
  DWORD curThreadId = GetCurrentThreadId();
858
  DWORD dwThreadId = -1;
859

860
  HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
861
  if (hThreadSnapshot == INVALID_HANDLE_VALUE) {
862
    return false;
863
  }
864

865
  THREADENTRY32 te32;
866
  te32.dwSize = sizeof(THREADENTRY32);
867

868
  if (!Thread32First(hThreadSnapshot, &te32)) {
869
    CloseHandle(hThreadSnapshot);
870
    return false;
871
  }
872

873
  do {
874
    if (te32.th32OwnerProcessID == curProcessId) {
875
      dwThreadId = te32.th32ThreadID;
876
      break;
877
    }
878
  } while (Thread32Next(hThreadSnapshot, &te32));
879

880
  CloseHandle(hThreadSnapshot);
881

882
  return curThreadId == dwThreadId;
883
}
884
#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