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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

web-flow
Merge pull request #29213 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

39.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) {
641,801✔
21
  OS_PARAM_CHECK(tid);
641,801!
22
  OS_PARAM_CHECK(start);
641,801!
23
  int32_t code = pthread_create(tid, attr, start, arg);
641,801✔
24
  if (code) {
641,799!
25
    taosThreadClear(tid);
×
26
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
27
  }
28
  return code;
641,800✔
29
}
30

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

171
int32_t taosThreadCondDestroy(TdThreadCond *cond) {
2,824,976✔
172
  OS_PARAM_CHECK(cond);
2,824,976!
173
#ifdef __USE_WIN_THREAD
174
  return 0;
175
#else
176
  int32_t code = pthread_cond_destroy(cond);
2,824,976✔
177
  if (code) {
2,826,220!
178
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
179
  }
180
  return code;
2,826,259✔
181
#endif
182
}
183

184
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
2,820,666✔
185
  OS_PARAM_CHECK(cond);
2,820,666!
186
#ifdef __USE_WIN_THREAD
187
  InitializeConditionVariable(cond);
188
  return 0;
189
#else
190
  int32_t code = pthread_cond_init(cond, attr);
2,820,666✔
191
  if (code) {
2,820,716!
UNCOV
192
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
193
  }
194
  return code;
2,820,739✔
195
#endif
196
}
197

198
int32_t taosThreadCondSignal(TdThreadCond *cond) {
710,433✔
199
  OS_PARAM_CHECK(cond);
710,433!
200
#ifdef __USE_WIN_THREAD
201
  WakeConditionVariable(cond);
202
  return 0;
203
#else
204
  int32_t code = pthread_cond_signal(cond);
710,433✔
205
  if (code) {
710,457!
UNCOV
206
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
207
  }
208
  return code;
710,458✔
209
#endif
210
}
211

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

226
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
563,156✔
227
  OS_PARAM_CHECK(cond);
563,156!
228
  OS_PARAM_CHECK(mutex);
563,156!
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);
563,156✔
236
  if (code) {
563,155!
UNCOV
237
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
238
  }
239
  return code;
563,156✔
240
#endif
241
}
242

243
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
646,092✔
244
  if (!abstime) return 0;
646,092!
245
  OS_PARAM_CHECK(cond);
646,092!
246
  OS_PARAM_CHECK(mutex);
646,092!
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);
646,092✔
256
  if (code == ETIMEDOUT) {
646,096✔
257
    return TSDB_CODE_TIMEOUT_ERROR;
517,288✔
258
  } else if (code) {
128,808!
UNCOV
259
    return TAOS_SYSTEM_ERROR(code);
×
260
  } else {
261
    return 0;
128,808✔
262
  }
263
#endif
264
}
265

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

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

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

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

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

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

UNCOV
344
int32_t taosThreadEqual(TdThread t1, TdThread t2) { return pthread_equal(t1, t2); }
×
345

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

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

UNCOV
360
void *taosThreadGetSpecific(TdThreadKey key) { return pthread_getspecific(key); }
×
361

362
int32_t taosThreadJoin(TdThread thread, void **valuePtr) {
588,730✔
363
  int32_t code = pthread_join(thread, valuePtr);
588,730✔
364
  if (code) {
588,730!
365
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
366
  }
367
  return code;
588,730✔
368
}
369

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

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

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

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

399
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
9,070,995✔
400
  OS_PARAM_CHECK(mutex);
9,070,995!
401
#ifdef __USE_WIN_THREAD
402
  DeleteCriticalSection(mutex);
403
  return 0;
404
#else
405
  int32_t code = pthread_mutex_destroy(mutex);
9,070,995✔
406
  if (code) {
9,070,923!
407
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
408
  }
409
  return code;
9,071,292✔
410
#endif
411
}
412

413
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
9,252,481✔
414
  OS_PARAM_CHECK(mutex);
9,252,481!
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);
9,252,481✔
425
  if (code) {
9,252,792!
UNCOV
426
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
427
  }
428
  return code;
9,254,436✔
429
#endif
430
}
431

432
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
301,040,438✔
433
  OS_PARAM_CHECK(mutex);
301,040,438!
434
#ifdef __USE_WIN_THREAD
435
  EnterCriticalSection(mutex);
436
  return 0;
437
#else
438
  int32_t code = pthread_mutex_lock(mutex);
301,040,438✔
439
  if (code) {
482,491,251!
UNCOV
440
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
441
  }
442
  return code;
482,515,362✔
443
#endif
444
}
445

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

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

464
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
481,624,769✔
465
  OS_PARAM_CHECK(mutex);
481,624,769!
466
#ifdef __USE_WIN_THREAD
467
  LeaveCriticalSection(mutex);
468
  return 0;
469
#else
470
  int32_t code = pthread_mutex_unlock(mutex);
481,624,769✔
471
  if (code) {
482,555,323!
UNCOV
472
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
473
  }
474
  return code;
482,574,883✔
475
#endif
476
}
477

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

UNCOV
491
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr *attr, int32_t *pshared) {
×
UNCOV
492
  OS_PARAM_CHECK(pshared);
×
493
#ifdef __USE_WIN_THREAD
494
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
495
  return 0;
496
#else
UNCOV
497
  OS_PARAM_CHECK(attr);
×
498
  int32_t code = pthread_mutexattr_getpshared(attr, pshared);
×
UNCOV
499
  if (code) {
×
UNCOV
500
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
501
  }
UNCOV
502
  return code;
×
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) {
×
511
  OS_PARAM_CHECK(kind);
×
512
#ifdef __USE_WIN_THREAD
513
  if (kind) *kind = PTHREAD_MUTEX_NORMAL;
514
  return 0;
515
#else
UNCOV
516
  OS_PARAM_CHECK(attr);
×
UNCOV
517
  int32_t code = pthread_mutexattr_gettype(attr, kind);
×
UNCOV
518
  if (code) {
×
UNCOV
519
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
520
  }
UNCOV
521
  return code;
×
522
#endif
523
}
524

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

538
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
×
539
#ifdef __USE_WIN_THREAD
540
  return 0;
541
#else
UNCOV
542
  OS_PARAM_CHECK(attr);
×
543
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
×
UNCOV
544
  if (code) {
×
UNCOV
545
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
546
  }
UNCOV
547
  return code;
×
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) {
27,266✔
556
#ifdef __USE_WIN_THREAD
557
  return 0;
558
#else
559
  OS_PARAM_CHECK(attr);
27,266!
560
  int32_t code = pthread_mutexattr_settype(attr, kind);
27,266✔
561
  if (code) {
27,264!
UNCOV
562
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
563
  }
564
  return code;
27,271✔
565
#endif
566
}
567

568
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
38,345,327✔
569
  int32_t code = pthread_once(onceControl, initRoutine);
38,345,327✔
570
  if (code) {
38,345,653!
UNCOV
571
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
572
  }
573
  return code;
38,346,382✔
574
}
575

576
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
16,219,876✔
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);
16,219,876!
584
  int32_t code = pthread_rwlock_destroy(rwlock);
16,219,876✔
585
  if (code) {
16,219,854✔
586
    return (terrno = TAOS_SYSTEM_ERROR(code));
16✔
587
  }
588
  return code;
16,219,838✔
589
#endif
590
}
591

592
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
16,225,992✔
593
  OS_PARAM_CHECK(rwlock);
16,225,992!
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);
16,225,992✔
600
  if (code) {
16,225,342!
UNCOV
601
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
602
  }
603
  return code;
16,225,484✔
604
#endif
605
}
606

607
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
167,213,330✔
608
  OS_PARAM_CHECK(rwlock);
167,213,330!
609
#ifdef __USE_WIN_THREAD
610
  AcquireSRWLockShared(&rwlock->lock);
611
  return 0;
612
#else
613
  int32_t code = pthread_rwlock_rdlock(rwlock);
167,213,330✔
614
  if (code) {
167,503,136!
UNCOV
615
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
616
  }
617
  return code;
167,506,677✔
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

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

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

658
int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock) {
727,914,189✔
659
  OS_PARAM_CHECK(rwlock);
727,914,189!
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);
727,914,189✔
669
  if (code) {
728,163,099!
UNCOV
670
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
671
  }
672
  return code;
728,167,653✔
673
#endif
674
}
675

676
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
560,492,419✔
677
  OS_PARAM_CHECK(rwlock);
560,492,419!
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);
560,492,419✔
684
  if (code) {
560,755,438!
685
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
686
  }
687
  return code;
560,756,028✔
688
#endif
689
}
690

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

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

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

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

UNCOV
745
TdThread taosThreadSelf(void) { return pthread_self(); }
×
746

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

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

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

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

781
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
3,292,045✔
782
  OS_PARAM_CHECK(lock);
3,292,045!
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);
3,292,045✔
787
  if (code) {
3,292,072!
UNCOV
788
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
789
  }
790
  return code;
3,292,087✔
791
#endif
792
}
793

794
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
3,277,339✔
795
  OS_PARAM_CHECK(lock);
3,277,339!
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);
3,277,339✔
801
  if (code) {
3,278,767!
UNCOV
802
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
803
  }
804
  return code;
3,279,156✔
805
#endif
806
}
807

808
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
287,694✔
809
  OS_PARAM_CHECK(lock);
287,694!
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);
287,694✔
814
  if (code) {
287,766!
815
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
816
  }
817
  return code;
287,766✔
818
#endif
819
}
820

821
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
914,369✔
822
  OS_PARAM_CHECK(lock);
914,369!
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);
914,369✔
827
  if (code && code != EBUSY) {
914,556!
828
    code = TAOS_SYSTEM_ERROR(code);
×
829
  }
830
  return code;
914,556✔
831
#endif
832
}
833

834
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
1,202,130✔
835
  OS_PARAM_CHECK(lock);
1,202,130!
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,202,130✔
840
  if (code) {
1,202,140!
UNCOV
841
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
842
  }
843
  return code;
1,202,185✔
844
#endif
845
}
846

UNCOV
847
void taosThreadTestCancel(void) { return pthread_testcancel(); }
×
848

849
void taosThreadClear(TdThread *thread) {
506,378✔
850
  if (!thread) return;
506,378!
851
  (void)memset(thread, 0, sizeof(TdThread));
506,378✔
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