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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

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

32
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) { 
439,654✔
33
  OS_PARAM_CHECK(attr);
439,654!
34
  int32_t code = pthread_attr_destroy(attr); 
439,654✔
35
  if (code) {
439,654!
36
    terrno = TAOS_SYSTEM_ERROR(code);
×
37
    return terrno;
×
38
  }
39
  return code;
439,654✔
40
}
41

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

53
int32_t taosThreadAttrGetInheritSched(const TdThreadAttr *attr, int32_t *inheritsched) {
×
54
  OS_PARAM_CHECK(attr);
×
55
  OS_PARAM_CHECK(inheritsched);
×
56
  int32_t code = pthread_attr_getinheritsched(attr, inheritsched);
×
57
  if (code) {
×
58
    terrno = TAOS_SYSTEM_ERROR(code);
×
59
    return terrno;
×
60
  }
61
  return code;
×
62
}
63

64
int32_t taosThreadAttrGetSchedParam(const TdThreadAttr *attr, struct sched_param *param) {
×
65
  OS_PARAM_CHECK(attr);
×
66
  OS_PARAM_CHECK(param);
×
67
  int32_t code = pthread_attr_getschedparam(attr, param);
×
68
  if (code) {
×
69
    terrno = TAOS_SYSTEM_ERROR(code);
×
70
    return terrno;
×
71
  }
72
  return code;
×
73
}
74

75
int32_t taosThreadAttrGetSchedPolicy(const TdThreadAttr *attr, int32_t *policy) {
×
76
  OS_PARAM_CHECK(attr);
×
77
  OS_PARAM_CHECK(policy);
×
78
  int32_t code = pthread_attr_getschedpolicy(attr, policy);
×
79
  if (code) {
×
80
    terrno = TAOS_SYSTEM_ERROR(code);
×
81
    return terrno;
×
82
  }
83
  return code;
×
84
}
85

86
int32_t taosThreadAttrGetScope(const TdThreadAttr *attr, int32_t *contentionscope) {
×
87
  OS_PARAM_CHECK(attr);
×
88
  OS_PARAM_CHECK(contentionscope);
×
89
  int32_t code = pthread_attr_getscope(attr, contentionscope);
×
90
  if (code) {
×
91
    terrno = TAOS_SYSTEM_ERROR(code);
×
92
    return terrno;
×
93
  }
94
  return code;
×
95
}
96

97
int32_t taosThreadAttrGetStackSize(const TdThreadAttr *attr, size_t *stacksize) {
×
98
  OS_PARAM_CHECK(attr);
×
99
  OS_PARAM_CHECK(stacksize);
×
100
  int32_t code = pthread_attr_getstacksize(attr, stacksize);
×
101
  if (code) {
×
102
    terrno = TAOS_SYSTEM_ERROR(code);
×
103
    return terrno;
×
104
  }
105
  return code;
×
106
}
107

108
int32_t taosThreadAttrInit(TdThreadAttr *attr) {
439,813✔
109
  OS_PARAM_CHECK(attr);
439,813!
110
  int32_t code = pthread_attr_init(attr); 
439,813✔
111
  if (code) {
439,813!
112
    terrno = TAOS_SYSTEM_ERROR(code);
×
113
    return terrno;
×
114
  }
115
  return code;
439,813✔
116
}
117

118
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
431,093✔
119
  OS_PARAM_CHECK(attr);
431,093!
120
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
431,093✔
121
  if (code) {
431,093!
122
    terrno = TAOS_SYSTEM_ERROR(code);
×
123
    return terrno;
×
124
  }
125
  return code;
431,093✔
126
}
127

128
int32_t taosThreadAttrSetInheritSched(TdThreadAttr *attr, int32_t inheritsched) {
×
129
  OS_PARAM_CHECK(attr);
×
130
  int32_t code = pthread_attr_setinheritsched(attr, inheritsched);
×
131
  if (code) {
×
132
    terrno = TAOS_SYSTEM_ERROR(code);
×
133
    return terrno;
×
134
  }
135
  return code;
×
136
}
137

138
int32_t taosThreadAttrSetSchedParam(TdThreadAttr *attr, const struct sched_param *param) {
×
139
  OS_PARAM_CHECK(attr);
×
140
  int32_t code = pthread_attr_setschedparam(attr, param);
×
141
  if (code) {
×
142
    terrno = TAOS_SYSTEM_ERROR(code);
×
143
    return terrno;
×
144
  }
145
  return code;
×
146
}
147

148
int32_t taosThreadAttrSetSchedPolicy(TdThreadAttr *attr, int32_t policy) {
×
149
  OS_PARAM_CHECK(attr);
×
150
  int32_t code = pthread_attr_setschedpolicy(attr, policy);
×
151
  if (code) {
×
152
    terrno = TAOS_SYSTEM_ERROR(code);
×
153
    return terrno;
×
154
  }
155
  return code;
×
156
}
157

158
int32_t taosThreadAttrSetScope(TdThreadAttr *attr, int32_t contentionscope) {
×
159
  OS_PARAM_CHECK(attr);
×
160
  int32_t code = pthread_attr_setscope(attr, contentionscope);
×
161
  if (code) {
×
162
    terrno = TAOS_SYSTEM_ERROR(code);
×
163
    return terrno;
×
164
  }
165
  return code;
×
166
}
167

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

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

187
int32_t taosThreadCondDestroy(TdThreadCond *cond) {
1,339,253✔
188
  OS_PARAM_CHECK(cond);
1,339,253!
189
#ifdef __USE_WIN_THREAD
190
  return 0;
191
#else
192
  int32_t code = pthread_cond_destroy(cond);
1,339,253✔
193
  if (code) {
1,339,441!
194
    terrno = TAOS_SYSTEM_ERROR(code);
×
195
    return terrno;
×
196
  }
197
  return code;
1,339,453✔
198
#endif
199
}
200

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

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

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

246
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
144,018✔
247
  OS_PARAM_CHECK(cond);
144,018!
248
  OS_PARAM_CHECK(mutex);
144,018!
249
#ifdef __USE_WIN_THREAD
250
  if (!SleepConditionVariableCS(cond, mutex, INFINITE)) {
251
    return EINVAL;
252
  }
253
  return 0;
254
#else
255
  int32_t code = pthread_cond_wait(cond, mutex);
144,018✔
256
  if (code) {
144,018!
257
    terrno = TAOS_SYSTEM_ERROR(code);
×
258
    return terrno;
×
259
  }
260
  return code;
144,018✔
261
#endif
262
}
263

264
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
601,063✔
265
  if (!abstime) return 0;
601,063!
266
  OS_PARAM_CHECK(cond);
601,063!
267
  OS_PARAM_CHECK(mutex);
601,063!
268
#ifdef __USE_WIN_THREAD
269
  if (SleepConditionVariableCS(cond, mutex, (DWORD)(abstime->tv_sec * 1e3 + abstime->tv_nsec / 1e6))) return 0;
270
  DWORD error = GetLastError();
271
  if (error == ERROR_TIMEOUT) {
272
    return TSDB_CODE_TIMEOUT_ERROR;
273
  }
274
  return TAOS_SYSTEM_WINAPI_ERROR(error);
275
#else
276
  int32_t code = pthread_cond_timedwait(cond, mutex, abstime);
601,063✔
277
  if(code == ETIMEDOUT) {
601,058✔
278
    return TSDB_CODE_TIMEOUT_ERROR;
512,197✔
279
  } else if (code) {
88,861!
280
    return TAOS_SYSTEM_ERROR(code);
×
281
  } else {
282
    return 0;
88,861✔
283
  }
284
#endif
285
}
286

287
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr *attr) {
50,515✔
288
#ifdef __USE_WIN_THREAD
289
  return 0;
290
#else
291
  OS_PARAM_CHECK(attr);
50,515!
292
  int32_t code = pthread_condattr_destroy(attr);
50,515✔
293
  if (code) {
50,515!
294
    terrno = TAOS_SYSTEM_ERROR(code);
×
295
    return terrno;
×
296
  }
297
  return code;
50,515✔
298
#endif
299
}
300

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

317
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) {
50,516✔
318
#ifdef __USE_WIN_THREAD
319
  return 0;
320
#else
321
  OS_PARAM_CHECK(attr);
50,516!
322
  int32_t code = pthread_condattr_init(attr);
50,516✔
323
  if (code) {
50,515!
324
    terrno = TAOS_SYSTEM_ERROR(code);
×
325
    return terrno;
×
326
  }
327
  return code;
50,515✔
328
#endif
329
}
330

331
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
50,515✔
332
#ifdef __USE_WIN_THREAD
333
  return 0;
334
#elif defined(__APPLE__)
335
  return 0;
336
#else
337
  OS_PARAM_CHECK(attr);
50,515!
338
  int32_t code = pthread_condattr_setclock(attr, clockId);
50,515✔
339
  if (code) {
50,515!
340
    terrno = TAOS_SYSTEM_ERROR(code);
×
341
    return terrno;
×
342
  }
343
  return code;
50,516✔
344
#endif
345
}
346

347
int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr *attr, int32_t pshared) {
×
348
  OS_PARAM_CHECK(attr);
×
349
#ifdef __USE_WIN_THREAD
350
  return 0;
351
#else
352
  int32_t code = pthread_condattr_setpshared(attr, pshared);
×
353
  if (code) {
×
354
    terrno = TAOS_SYSTEM_ERROR(code);
×
355
    return terrno;
×
356
  }
357
  return code;
×
358
#endif
359
}
360

361
int32_t taosThreadDetach(TdThread thread) { 
×
362
  int32_t code = pthread_detach(thread); 
×
363
  if (code) {
×
364
    terrno = TAOS_SYSTEM_ERROR(code);
×
365
    return terrno;
×
366
  }
367
  return code;
×
368
}
369

370
int32_t taosThreadEqual(TdThread t1, TdThread t2) { 
×
371
  return pthread_equal(t1, t2); 
×
372
}
373

374
void taosThreadExit(void *valuePtr) { 
×
375
  if(valuePtr) return pthread_exit(valuePtr); 
×
376
}
377

378
int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param) {
×
379
  OS_PARAM_CHECK(policy);
×
380
  OS_PARAM_CHECK(param);
×
381
  int32_t code = pthread_getschedparam(thread, policy, param);
×
382
  if (code) {
×
383
    terrno = TAOS_SYSTEM_ERROR(code);
×
384
    return terrno;
×
385
  }
386
  return code;
×
387
}
388

389
void *taosThreadGetSpecific(TdThreadKey key) { 
×
390
  return pthread_getspecific(key); 
×
391
}
392

393
int32_t taosThreadJoin(TdThread thread, void **valuePtr) { 
507,089✔
394
  int32_t code = pthread_join(thread, valuePtr); 
507,089✔
395
  if (code) {
507,089!
396
    terrno = TAOS_SYSTEM_ERROR(code);
×
397
    return terrno;
×
398
  }
399
  return code;
507,089✔
400
}
401

402
int32_t taosThreadKeyCreate(TdThreadKey *key, void (*destructor)(void *)) {
13✔
403
  OS_PARAM_CHECK(key);
13!
404
  int32_t code = pthread_key_create(key, destructor);
13✔
405
  if (code) {
13!
406
    terrno = TAOS_SYSTEM_ERROR(code);
×
407
    return terrno;
×
408
  }
409
  return code;
13✔
410
}
411

412
int32_t taosThreadKeyDelete(TdThreadKey key) { 
×
413
  int32_t code = pthread_key_delete(key); 
×
414
  if (code) {
×
415
    terrno = TAOS_SYSTEM_ERROR(code);
×
416
    return terrno;
×
417
  }
418
  return code;
×
419
}
420

421
int32_t taosThreadKill(TdThread thread, int32_t sig) { 
155✔
422
  int32_t code = pthread_kill(thread, sig); 
155✔
423
  if (code) {
155!
424
    terrno = TAOS_SYSTEM_ERROR(code);
×
425
    return terrno;
×
426
  }
427
  return code;
155✔
428
}
429

430
// int32_t taosThreadMutexConsistent(TdThreadMutex* mutex) {
431
//   return pthread_mutex_consistent(mutex);
432
// }
433

434
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
3,631,554✔
435
  OS_PARAM_CHECK(mutex);
3,631,554!
436
#ifdef __USE_WIN_THREAD
437
  DeleteCriticalSection(mutex);
438
  return 0;
439
#else
440
  int32_t code = pthread_mutex_destroy(mutex);
3,631,554✔
441
  if (code) {
3,631,591✔
442
    terrno = TAOS_SYSTEM_ERROR(code);
1,398✔
443
    return terrno;
1,433✔
444
  }
445
  return code;
3,630,193✔
446
#endif
447
}
448

449
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
3,681,952✔
450
  OS_PARAM_CHECK(mutex);
3,681,952!
451
#ifdef __USE_WIN_THREAD
452
  /**
453
   * Windows Server 2003 and Windows XP:  In low memory situations, InitializeCriticalSection can raise a
454
   * STATUS_NO_MEMORY exception. Starting with Windows Vista, this exception was eliminated and
455
   * InitializeCriticalSection always succeeds, even in low memory situations.
456
   */
457
  InitializeCriticalSection(mutex);
458
  return 0;
459
#else
460
  int32_t code = pthread_mutex_init(mutex, attr);
3,681,952✔
461
  if (code) {
3,682,012!
462
    terrno = TAOS_SYSTEM_ERROR(code);
×
463
    return terrno;
×
464
  }
465
  return code;
3,682,149✔
466
#endif
467
}
468

469
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
120,672,626✔
470
  OS_PARAM_CHECK(mutex);
120,672,626!
471
#ifdef __USE_WIN_THREAD
472
  EnterCriticalSection(mutex);
473
  return 0;
474
#else
475
  int32_t code = pthread_mutex_lock(mutex);
120,672,626✔
476
  if (code) {
120,733,422!
477
    terrno = TAOS_SYSTEM_ERROR(code);
×
478
    return terrno;
×
479
  }
480
  return code;
120,735,176✔
481
#endif
482
}
483

484
// int32_t taosThreadMutexTimedLock(TdThreadMutex * mutex, const struct timespec *abstime) {
485
//   return pthread_mutex_timedlock(mutex, abstime);
486
// }
487

UNCOV
488
int32_t taosThreadMutexTryLock(TdThreadMutex *mutex) {
×
UNCOV
489
  OS_PARAM_CHECK(mutex);
×
490
#ifdef __USE_WIN_THREAD
491
  if (TryEnterCriticalSection(mutex)) return 0;
492
  return EBUSY;
493
#else
UNCOV
494
  int32_t code = pthread_mutex_trylock(mutex);
×
UNCOV
495
  if (code && code != EBUSY) {
×
496
    code = TAOS_SYSTEM_ERROR(code);
×
497
  }
UNCOV
498
  return code;
×
499
#endif
500
}
501

502
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
120,693,756✔
503
  OS_PARAM_CHECK(mutex);
120,693,756!
504
#ifdef __USE_WIN_THREAD
505
  LeaveCriticalSection(mutex);
506
  return 0;
507
#else
508
  int32_t code = pthread_mutex_unlock(mutex);
120,693,756✔
509
  if (code) {
120,747,988!
510
    terrno = TAOS_SYSTEM_ERROR(code);
×
511
    return terrno;
×
512
  }
513
  return code;
120,748,272✔
514
#endif
515
}
516

517
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr *attr) {
12,811✔
518
#ifdef __USE_WIN_THREAD
519
  return 0;
520
#else
521
  OS_PARAM_CHECK(attr);
12,811!
522
  int32_t code = pthread_mutexattr_destroy(attr);
12,811✔
523
  if (code) {
12,812!
524
    terrno = TAOS_SYSTEM_ERROR(code);
×
525
    return terrno;
×
526
  }
527
  return code;
12,813✔
528
#endif
529
}
530

531
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr *attr, int32_t *pshared) {
×
532
  OS_PARAM_CHECK(pshared);
×
533
#ifdef __USE_WIN_THREAD
534
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
535
  return 0;
536
#else
537
  OS_PARAM_CHECK(attr);
×
538
  int32_t code = pthread_mutexattr_getpshared(attr, pshared);
×
539
  if (code) {
×
540
    terrno = TAOS_SYSTEM_ERROR(code);
×
541
    return terrno;
×
542
  }
543
  return code;
×
544
#endif
545
}
546

547
// int32_t taosThreadMutexAttrGetRobust(const TdThreadMutexAttr * attr, int32_t * robust) {
548
//   return pthread_mutexattr_getrobust(attr, robust);
549
// }
550

551
int32_t taosThreadMutexAttrGetType(const TdThreadMutexAttr *attr, int32_t *kind) {
×
552
  OS_PARAM_CHECK(kind);
×
553
#ifdef __USE_WIN_THREAD
554
  if (kind) *kind = PTHREAD_MUTEX_NORMAL;
555
  return 0;
556
#else
557
  OS_PARAM_CHECK(attr);
×
558
  int32_t code = pthread_mutexattr_gettype(attr, kind);
×
559
  if (code) {
×
560
    terrno = TAOS_SYSTEM_ERROR(code);
×
561
    return terrno;
×
562
  }
563
  return code;
×
564
#endif
565
}
566

567
int32_t taosThreadMutexAttrInit(TdThreadMutexAttr *attr) {
12,814✔
568
#ifdef __USE_WIN_THREAD
569
  return 0;
570
#else
571
  OS_PARAM_CHECK(attr);
12,814!
572
  int32_t code = pthread_mutexattr_init(attr);
12,814✔
573
  if (code) {
12,815!
574
    terrno = TAOS_SYSTEM_ERROR(code);
×
575
    return terrno;
×
576
  }
577
  return code;
12,815✔
578
#endif
579
}
580

581
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
×
582
#ifdef __USE_WIN_THREAD
583
  return 0;
584
#else
585
  OS_PARAM_CHECK(attr);
×
586
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
×
587
  if (code) {
×
588
    terrno = TAOS_SYSTEM_ERROR(code);
×
589
    return terrno;
×
590
  }
591
  return code;
×
592
#endif
593
}
594

595
// int32_t taosThreadMutexAttrSetRobust(TdThreadMutexAttr * attr, int32_t robust) {
596
//   return pthread_mutexattr_setrobust(attr, robust);
597
// }
598

599
int32_t taosThreadMutexAttrSetType(TdThreadMutexAttr *attr, int32_t kind) {
12,815✔
600
#ifdef __USE_WIN_THREAD
601
  return 0;
602
#else
603
  OS_PARAM_CHECK(attr);
12,815!
604
  int32_t code = pthread_mutexattr_settype(attr, kind);
12,815✔
605
  if (code) {
12,815!
606
    terrno = TAOS_SYSTEM_ERROR(code);
×
607
    return terrno;
×
608
  }
609
  return code;
12,815✔
610
#endif
611
}
612

613
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
20,563,169✔
614
  int32_t code = pthread_once(onceControl, initRoutine);
20,563,169✔
615
  if (code) {
20,563,045!
UNCOV
616
    terrno = TAOS_SYSTEM_ERROR(code);
×
617
    return terrno;
×
618
  }
619
  return code;
20,563,542✔
620
}
621

622
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
1,294,262✔
623
#ifdef __USE_WIN_THREAD
624
  /* SRWLock does not need explicit destruction so long as there are no waiting threads
625
   * See: https://docs.microsoft.com/windows/win32/api/synchapi/nf-synchapi-initializesrwlock#remarks
626
   */
627
  return 0;
628
#else
629
    OS_PARAM_CHECK(rwlock);
1,294,262!
630
  int32_t code = pthread_rwlock_destroy(rwlock);
1,294,262✔
631
  if (code) {
1,294,253✔
632
    terrno = TAOS_SYSTEM_ERROR(code);
2✔
633
    return terrno;
×
634
  }
635
  return code;
1,294,251✔
636
#endif
637
}
638

639
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
1,301,122✔
640
  OS_PARAM_CHECK(rwlock);
1,301,122!
641
#ifdef __USE_WIN_THREAD
642
  memset(rwlock, 0, sizeof(*rwlock));
643
  InitializeSRWLock(&rwlock->lock);
644
  return 0;
645
#else
646
  int32_t code = pthread_rwlock_init(rwlock, attr);
1,301,122✔
647
  if (code) {
1,300,897!
648
    terrno = TAOS_SYSTEM_ERROR(code);
×
649
    return terrno;
×
650
  }
651
  return code;
1,300,968✔
652
#endif
653
}
654

655
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
47,444,131✔
656
  OS_PARAM_CHECK(rwlock);
47,444,131!
657
#ifdef __USE_WIN_THREAD
658
  AcquireSRWLockShared(&rwlock->lock);
659
  return 0;
660
#else
661
  int32_t code = pthread_rwlock_rdlock(rwlock);
47,444,131✔
662
  if (code) {
47,459,924!
663
    terrno = TAOS_SYSTEM_ERROR(code);
×
664
    return terrno;
×
665
  }
666
  return code;
47,460,007✔
667
#endif
668
}
669

670
// int32_t taosThreadRwlockTimedRdlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
671
//   return pthread_rwlock_timedrdlock(rwlock, abstime);
672
// }
673

674
// int32_t taosThreadRwlockTimedWrlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
675
//   return pthread_rwlock_timedwrlock(rwlock, abstime);
676
// }
677

678
int32_t taosThreadRwlockTryRdlock(TdThreadRwlock *rwlock) {
×
679
  OS_PARAM_CHECK(rwlock);
×
680
#ifdef __USE_WIN_THREAD
681
  if (!TryAcquireSRWLockShared(&rwlock->lock)) return EBUSY;
682
  return 0;
683
#else
684
  int32_t code = pthread_rwlock_tryrdlock(rwlock);
×
685
  if (code) {
×
686
    terrno = TAOS_SYSTEM_ERROR(code);
×
687
    return terrno;
×
688
  }
689
  return code;
×
690
#endif
691
}
692

693
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock *rwlock) {
×
694
  OS_PARAM_CHECK(rwlock);
×
695
#ifdef __USE_WIN_THREAD
696
  if (!TryAcquireSRWLockExclusive(&rwlock->lock)) return EBUSY;
697
  atomic_store_8(&rwlock->excl, 1);
698
  return 0;
699
#else
700
  int32_t code = pthread_rwlock_trywrlock(rwlock);
×
701
  if (code) {
×
702
    terrno = TAOS_SYSTEM_ERROR(code);
×
703
    return terrno;
×
704
  }
705
  return code;
×
706
#endif
707
}
708

709
int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock) {
299,589,084✔
710
  OS_PARAM_CHECK(rwlock);
299,589,084!
711
#ifdef __USE_WIN_THREAD
712
  if (1 == atomic_val_compare_exchange_8(&rwlock->excl, 1, 0)) {
713
    ReleaseSRWLockExclusive(&rwlock->lock);
714
  } else {
715
    ReleaseSRWLockShared(&rwlock->lock);
716
  }
717
  return 0;
718
#else
719
  int32_t code = pthread_rwlock_unlock(rwlock);
299,589,084✔
720
  if (code) {
299,602,765!
721
    terrno = TAOS_SYSTEM_ERROR(code);
×
722
    return terrno;
×
723
  }
724
  return code;
299,603,365✔
725
#endif
726
}
727

728
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
252,060,645✔
729
  OS_PARAM_CHECK(rwlock);
252,060,645!
730
#ifdef __USE_WIN_THREAD
731
  AcquireSRWLockExclusive(&rwlock->lock);
732
  atomic_store_8(&rwlock->excl, 1);
733
  return 0;
734
#else
735
  int32_t code = pthread_rwlock_wrlock(rwlock);
252,060,645✔
736
  if (code) {
252,162,384!
737
    terrno = TAOS_SYSTEM_ERROR(code);
×
738
    return terrno;
×
739
  }
740
  return code;
252,162,638✔
741
#endif
742
}
743

744
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
21,166✔
745
#ifdef __USE_WIN_THREAD
746
  return 0;
747
#else
748
  OS_PARAM_CHECK(attr);
21,166!
749
  int32_t code = pthread_rwlockattr_destroy(attr);
21,166✔
750
  if (code) {
21,169!
751
    terrno = TAOS_SYSTEM_ERROR(code);
×
752
    return terrno;
×
753
  }
754
  return code;
21,173✔
755
#endif
756
}
757

758
int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr *attr, int32_t *pshared) {
×
759
  OS_PARAM_CHECK(pshared);
×
760
#ifdef __USE_WIN_THREAD
761
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
762
  return 0;
763
#else
764
  int32_t code = pthread_rwlockattr_getpshared(attr, pshared);
×
765
  if (code) {
×
766
    terrno = TAOS_SYSTEM_ERROR(code);
×
767
    return terrno;
×
768
  }
769
  return code;
×
770
#endif
771
}
772

773
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr *attr) {
21,176✔
774
#ifdef __USE_WIN_THREAD
775
  return 0;
776
#else
777
  OS_PARAM_CHECK(attr);
21,176!
778
  int32_t code = pthread_rwlockattr_init(attr);
21,176✔
779
  if (code) {
21,165!
780
    terrno = TAOS_SYSTEM_ERROR(code);
×
781
    return terrno;
×
782
  }
783
  return code;
21,169✔
784
#endif
785
}
786

787
int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr *attr, int32_t pshared) {
×
788
#ifdef __USE_WIN_THREAD
789
  return 0;
790
#else
791
  OS_PARAM_CHECK(attr);
×
792
  int32_t code = pthread_rwlockattr_setpshared(attr, pshared);
×
793
  if (code) {
×
794
    terrno = TAOS_SYSTEM_ERROR(code);
×
795
    return terrno;
×
796
  }
797
  return code;
×
798
#endif
799
}
800

801
TdThread taosThreadSelf(void) { return pthread_self(); }
×
802

803
int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate) { 
×
804
  int32_t code = pthread_setcancelstate(state, oldstate); 
×
805
  if (code) {
×
806
    terrno = TAOS_SYSTEM_ERROR(code);
×
807
    return terrno;
×
808
  }
809
  return code;
×
810
}
811

812
int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype) { 
×
813
  int32_t code = pthread_setcanceltype(type, oldtype); 
×
814
  if (code) {
×
815
    terrno = TAOS_SYSTEM_ERROR(code);
×
816
    return terrno;
×
817
  }
818
  return code;
×
819
}
820

821
int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sched_param *param) {
×
822
  OS_PARAM_CHECK(param);
×
823
  int32_t code = pthread_setschedparam(thread, policy, param);
×
824
  if (code) {
×
825
    terrno = TAOS_SYSTEM_ERROR(code);
×
826
    return terrno;
×
827
  }
828
  return code;
×
829
}
830

831
int32_t taosThreadSetSpecific(TdThreadKey key, const void *value) { 
92✔
832
  OS_PARAM_CHECK(value);
92!
833
  int32_t code = pthread_setspecific(key, value); 
92✔
834
  if (code) {
92!
835
    terrno = TAOS_SYSTEM_ERROR(code);
×
836
    return terrno;
×
837
  }
838
  return code;
92✔
839
}
840

841
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
1,802,996✔
842
  OS_PARAM_CHECK(lock);
1,802,996!
843
#ifdef TD_USE_SPINLOCK_AS_MUTEX
844
  return pthread_mutex_destroy((pthread_mutex_t *)lock);
845
#else
846
  int32_t code = pthread_spin_destroy((pthread_spinlock_t *)lock);
1,802,996✔
847
  if (code) {
1,803,017!
848
    terrno = TAOS_SYSTEM_ERROR(code);
×
849
    return terrno;
×
850
  }
851
  return code;
1,803,046✔
852
#endif
853
}
854

855
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
1,794,220✔
856
  OS_PARAM_CHECK(lock);
1,794,220!
857
#ifdef TD_USE_SPINLOCK_AS_MUTEX
858
  if (pshared != 0) return TSDB_CODE_INVALID_PARA;
859
  return pthread_mutex_init((pthread_mutex_t *)lock, NULL);
860
#else
861
  int32_t code = pthread_spin_init((pthread_spinlock_t *)lock, pshared);
1,794,220✔
862
  if (code) {
1,795,248!
863
    terrno = TAOS_SYSTEM_ERROR(code);
×
864
    return terrno;
×
865
  }
866
  return code;
1,795,988✔
867
#endif
868
}
869

870
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
102,415✔
871
  OS_PARAM_CHECK(lock);
102,415!
872
#ifdef TD_USE_SPINLOCK_AS_MUTEX
873
  return pthread_mutex_lock((pthread_mutex_t *)lock);
874
#else
875
  int32_t code = pthread_spin_lock((pthread_spinlock_t *)lock);
102,415✔
876
  if (code) {
102,415!
877
    terrno = TAOS_SYSTEM_ERROR(code);
×
878
    return terrno;
×
879
  }
880
  return code;
102,415✔
881
#endif
882
}
883

884
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
277,050✔
885
  OS_PARAM_CHECK(lock);
277,050!
886
#ifdef TD_USE_SPINLOCK_AS_MUTEX
887
  return pthread_mutex_trylock((pthread_mutex_t *)lock);
888
#else
889
  int32_t code = pthread_spin_trylock((pthread_spinlock_t *)lock);
277,050✔
890
  if (code && code != EBUSY) {
277,169!
891
    code = TAOS_SYSTEM_ERROR(code);
×
892
  }
893
  return code;
277,169✔
894
#endif
895
}
896

897
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
379,437✔
898
  OS_PARAM_CHECK(lock);
379,437!
899
#ifdef TD_USE_SPINLOCK_AS_MUTEX
900
  return pthread_mutex_unlock((pthread_mutex_t *)lock);
901
#else
902
  int32_t code = pthread_spin_unlock((pthread_spinlock_t *)lock);
379,437✔
903
  if (code) {
379,458!
904
    terrno = TAOS_SYSTEM_ERROR(code);
×
905
    return terrno;
×
906
  }
907
  return code;
379,476✔
908
#endif
909
}
910

911
void taosThreadTestCancel(void) { 
×
912
  return pthread_testcancel(); 
×
913
}
914

915
void taosThreadClear(TdThread *thread) { 
430,606✔
916
  if (!thread) return;
430,606!
917
  (void)memset(thread, 0, sizeof(TdThread)); 
430,606✔
918
}
919

920
#ifdef WINDOWS
921
bool taosThreadIsMain() {
922
  DWORD curProcessId = GetCurrentProcessId();
923
  DWORD curThreadId = GetCurrentThreadId();
924
  DWORD dwThreadId = -1;
925

926
  HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
927
  if (hThreadSnapshot == INVALID_HANDLE_VALUE) {
928
    return false;
929
  }
930

931
  THREADENTRY32 te32;
932
  te32.dwSize = sizeof(THREADENTRY32);
933

934
  if (!Thread32First(hThreadSnapshot, &te32)) {
935
    CloseHandle(hThreadSnapshot);
936
    return false;
937
  }
938

939
  do {
940
    if (te32.th32OwnerProcessID == curProcessId) {
941
      dwThreadId = te32.th32ThreadID;
942
      break;
943
    }
944
  } while (Thread32Next(hThreadSnapshot, &te32));
945

946
  CloseHandle(hThreadSnapshot);
947

948
  return curThreadId == dwThreadId;
949
}
950
#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