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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

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

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

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

32
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) { 
847,965✔
33
  OS_PARAM_CHECK(attr);
847,965!
34
  int32_t code = pthread_attr_destroy(attr); 
847,965✔
35
  if (code) {
847,965!
UNCOV
36
    terrno = TAOS_SYSTEM_ERROR(code);
×
37
    return terrno;
×
38
  }
39
  return code;
847,965✔
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) {
848,233✔
109
  OS_PARAM_CHECK(attr);
848,233!
110
  int32_t code = pthread_attr_init(attr); 
848,233✔
111
  if (code) {
848,233!
112
    terrno = TAOS_SYSTEM_ERROR(code);
×
113
    return terrno;
×
114
  }
115
  return code;
848,233✔
116
}
117

118
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
832,543✔
119
  OS_PARAM_CHECK(attr);
832,543!
120
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
832,543✔
121
  if (code) {
832,542!
122
    terrno = TAOS_SYSTEM_ERROR(code);
×
123
    return terrno;
×
124
  }
125
  return code;
832,543✔
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) {
18,779,767✔
188
  OS_PARAM_CHECK(cond);
18,779,767!
189
#ifdef __USE_WIN_THREAD
190
  return 0;
191
#else
192
  int32_t code = pthread_cond_destroy(cond);
18,779,767✔
193
  if (code) {
18,793,843!
194
    terrno = TAOS_SYSTEM_ERROR(code);
×
195
    return terrno;
×
196
  }
197
  return code;
18,794,023✔
198
#endif
199
}
200

201
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
18,755,568✔
202
  OS_PARAM_CHECK(cond);
18,755,568!
203
#ifdef __USE_WIN_THREAD
204
  InitializeConditionVariable(cond);
205
  return 0;
206
#else
207
  int32_t code = pthread_cond_init(cond, attr);
18,755,568✔
208
  if (code) {
18,759,439!
209
    terrno = TAOS_SYSTEM_ERROR(code);
×
210
    return terrno;
×
211
  }
212
  return code;
18,761,117✔
213
#endif
214
}
215

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

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

246
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
2,483,632✔
247
  OS_PARAM_CHECK(cond);
2,483,632!
248
  OS_PARAM_CHECK(mutex);
2,483,632!
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);
2,483,632✔
256
  if (code) {
2,483,632!
257
    terrno = TAOS_SYSTEM_ERROR(code);
×
258
    return terrno;
×
259
  }
260
  return code;
2,483,632✔
261
#endif
262
}
263

264
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
775,975✔
265
  if (!abstime) return 0;
775,975!
266
  OS_PARAM_CHECK(cond);
775,975!
267
  OS_PARAM_CHECK(mutex);
775,975!
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);
775,975✔
277
  if(code == ETIMEDOUT) {
775,963✔
278
    return TSDB_CODE_TIMEOUT_ERROR;
609,807✔
279
  } else if (code) {
166,156!
280
    return TAOS_SYSTEM_ERROR(code);
×
281
  } else {
282
    return 0;
166,156✔
283
  }
284
#endif
285
}
286

287
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr *attr) {
114,671✔
288
#ifdef __USE_WIN_THREAD
289
  return 0;
290
#else
291
  OS_PARAM_CHECK(attr);
114,671!
292
  int32_t code = pthread_condattr_destroy(attr);
114,671✔
293
  if (code) {
114,671!
294
    terrno = TAOS_SYSTEM_ERROR(code);
×
295
    return terrno;
×
296
  }
297
  return code;
114,671✔
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) {
114,669✔
318
#ifdef __USE_WIN_THREAD
319
  return 0;
320
#else
321
  OS_PARAM_CHECK(attr);
114,669!
322
  int32_t code = pthread_condattr_init(attr);
114,669✔
323
  if (code) {
114,668!
324
    terrno = TAOS_SYSTEM_ERROR(code);
×
325
    return terrno;
×
326
  }
327
  return code;
114,669✔
328
#endif
329
}
330

331
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
114,668✔
332
#ifdef __USE_WIN_THREAD
333
  return 0;
334
#elif defined(__APPLE__)
335
  return 0;
336
#else
337
  OS_PARAM_CHECK(attr);
114,668!
338
  int32_t code = pthread_condattr_setclock(attr, clockId);
114,668✔
339
  if (code) {
114,668!
340
    terrno = TAOS_SYSTEM_ERROR(code);
×
341
    return terrno;
×
342
  }
343
  return code;
114,670✔
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) { 
947,129✔
394
  int32_t code = pthread_join(thread, valuePtr); 
947,129✔
395
  if (code) {
947,129!
396
    terrno = TAOS_SYSTEM_ERROR(code);
×
397
    return terrno;
×
398
  }
399
  return code;
947,129✔
400
}
401

402
int32_t taosThreadKeyCreate(TdThreadKey *key, void (*destructor)(void *)) {
19✔
403
  OS_PARAM_CHECK(key);
19!
404
  int32_t code = pthread_key_create(key, destructor);
19✔
405
  if (code) {
19!
406
    terrno = TAOS_SYSTEM_ERROR(code);
×
407
    return terrno;
×
408
  }
409
  return code;
19✔
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) { 
2,253✔
422
  int32_t code = pthread_kill(thread, sig); 
2,253✔
423
  if (code) {
2,253!
424
    terrno = TAOS_SYSTEM_ERROR(code);
×
425
    return terrno;
×
426
  }
427
  return code;
2,253✔
428
}
429

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

434
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
35,665,126✔
435
  OS_PARAM_CHECK(mutex);
35,665,126!
436
#ifdef __USE_WIN_THREAD
437
  DeleteCriticalSection(mutex);
438
  return 0;
439
#else
440
  int32_t code = pthread_mutex_destroy(mutex);
35,665,126✔
441
  if (code) {
35,663,274!
442
    terrno = TAOS_SYSTEM_ERROR(code);
×
443
    return terrno;
6✔
444
  }
445
  return code;
35,664,471✔
446
#endif
447
}
448

449
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
36,070,100✔
450
  OS_PARAM_CHECK(mutex);
36,070,100!
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);
36,070,100✔
461
  if (code) {
36,079,371!
462
    terrno = TAOS_SYSTEM_ERROR(code);
×
463
    return terrno;
×
464
  }
465
  return code;
36,088,411✔
466
#endif
467
}
468

469
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
1,127,969,044✔
470
  OS_PARAM_CHECK(mutex);
1,127,969,044!
471
#ifdef __USE_WIN_THREAD
472
  EnterCriticalSection(mutex);
473
  return 0;
474
#else
475
  int32_t code = pthread_mutex_lock(mutex);
1,127,969,044✔
476
  if (code) {
1,132,306,128!
477
    terrno = TAOS_SYSTEM_ERROR(code);
×
478
    return terrno;
×
479
  }
480
  return code;
1,132,485,902✔
481
#endif
482
}
483

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

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

502
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
1,129,208,558✔
503
  OS_PARAM_CHECK(mutex);
1,129,208,558!
504
#ifdef __USE_WIN_THREAD
505
  LeaveCriticalSection(mutex);
506
  return 0;
507
#else
508
  int32_t code = pthread_mutex_unlock(mutex);
1,129,208,558✔
509
  if (code) {
1,132,805,711!
510
    terrno = TAOS_SYSTEM_ERROR(code);
×
511
    return terrno;
×
512
  }
513
  return code;
1,133,053,337✔
514
#endif
515
}
516

517
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr *attr) {
34,399✔
518
#ifdef __USE_WIN_THREAD
519
  return 0;
520
#else
521
  OS_PARAM_CHECK(attr);
34,399!
522
  int32_t code = pthread_mutexattr_destroy(attr);
34,399✔
523
  if (code) {
34,397!
524
    terrno = TAOS_SYSTEM_ERROR(code);
×
525
    return terrno;
×
526
  }
527
  return code;
34,398✔
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) {
34,411✔
568
#ifdef __USE_WIN_THREAD
569
  return 0;
570
#else
571
  OS_PARAM_CHECK(attr);
34,411!
572
  int32_t code = pthread_mutexattr_init(attr);
34,411✔
573
  if (code) {
34,409!
574
    terrno = TAOS_SYSTEM_ERROR(code);
×
575
    return terrno;
×
576
  }
577
  return code;
34,410✔
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) {
34,410✔
600
#ifdef __USE_WIN_THREAD
601
  return 0;
602
#else
603
  OS_PARAM_CHECK(attr);
34,410!
604
  int32_t code = pthread_mutexattr_settype(attr, kind);
34,410✔
605
  if (code) {
34,403!
606
    terrno = TAOS_SYSTEM_ERROR(code);
×
607
    return terrno;
×
608
  }
609
  return code;
34,409✔
610
#endif
611
}
612

613
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
47,311,099✔
614
  int32_t code = pthread_once(onceControl, initRoutine);
47,311,099✔
615
  if (code) {
47,312,687✔
616
    terrno = TAOS_SYSTEM_ERROR(code);
909✔
617
    return terrno;
×
618
  }
619
  return code;
47,311,778✔
620
}
621

622
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
20,107,608✔
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);
20,107,608!
630
  int32_t code = pthread_rwlock_destroy(rwlock);
20,107,608✔
631
  if (code) {
20,107,475!
632
    terrno = TAOS_SYSTEM_ERROR(code);
×
633
    return terrno;
×
634
  }
635
  return code;
20,107,529✔
636
#endif
637
}
638

639
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
20,109,485✔
640
  OS_PARAM_CHECK(rwlock);
20,109,485!
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);
20,109,485✔
647
  if (code) {
20,106,932!
648
    terrno = TAOS_SYSTEM_ERROR(code);
×
649
    return terrno;
×
650
  }
651
  return code;
20,108,020✔
652
#endif
653
}
654

655
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
567,356,048✔
656
  OS_PARAM_CHECK(rwlock);
567,356,048!
657
#ifdef __USE_WIN_THREAD
658
  AcquireSRWLockShared(&rwlock->lock);
659
  return 0;
660
#else
661
  int32_t code = pthread_rwlock_rdlock(rwlock);
567,356,048✔
662
  if (code) {
568,026,229!
663
    terrno = TAOS_SYSTEM_ERROR(code);
×
664
    return terrno;
×
665
  }
666
  return code;
568,042,910✔
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) {
1,404,014,674✔
710
  OS_PARAM_CHECK(rwlock);
1,404,014,674!
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);
1,404,014,674✔
720
  if (code) {
1,404,603,580!
721
    terrno = TAOS_SYSTEM_ERROR(code);
×
722
    return terrno;
×
723
  }
724
  return code;
1,404,672,539✔
725
#endif
726
}
727

728
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
836,419,519✔
729
  OS_PARAM_CHECK(rwlock);
836,419,519!
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);
836,419,519✔
736
  if (code) {
836,842,126!
737
    terrno = TAOS_SYSTEM_ERROR(code);
×
738
    return terrno;
×
739
  }
740
  return code;
836,843,939✔
741
#endif
742
}
743

744
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
43,719✔
745
#ifdef __USE_WIN_THREAD
746
  return 0;
747
#else
748
  OS_PARAM_CHECK(attr);
43,719!
749
  int32_t code = pthread_rwlockattr_destroy(attr);
43,719✔
750
  if (code) {
43,720!
751
    terrno = TAOS_SYSTEM_ERROR(code);
×
752
    return terrno;
×
753
  }
754
  return code;
43,722✔
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) {
43,724✔
774
#ifdef __USE_WIN_THREAD
775
  return 0;
776
#else
777
  OS_PARAM_CHECK(attr);
43,724!
778
  int32_t code = pthread_rwlockattr_init(attr);
43,724✔
779
  if (code) {
43,722!
780
    terrno = TAOS_SYSTEM_ERROR(code);
×
781
    return terrno;
×
782
  }
783
  return code;
43,727✔
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) { 
199✔
832
  OS_PARAM_CHECK(value);
199!
833
  int32_t code = pthread_setspecific(key, value); 
199✔
834
  if (code) {
199!
835
    terrno = TAOS_SYSTEM_ERROR(code);
×
836
    return terrno;
×
837
  }
838
  return code;
199✔
839
}
840

841
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
4,487,503✔
842
  OS_PARAM_CHECK(lock);
4,487,503!
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);
4,487,503✔
847
  if (code) {
4,487,478!
848
    terrno = TAOS_SYSTEM_ERROR(code);
×
849
    return terrno;
×
850
  }
851
  return code;
4,487,527✔
852
#endif
853
}
854

855
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
4,460,714✔
856
  OS_PARAM_CHECK(lock);
4,460,714!
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);
4,460,714✔
862
  if (code) {
4,460,447!
863
    terrno = TAOS_SYSTEM_ERROR(code);
×
864
    return terrno;
×
865
  }
866
  return code;
4,460,694✔
867
#endif
868
}
869

870
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
376,584✔
871
  OS_PARAM_CHECK(lock);
376,584!
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);
376,584✔
876
  if (code) {
376,686!
877
    terrno = TAOS_SYSTEM_ERROR(code);
×
878
    return terrno;
×
879
  }
880
  return code;
376,686✔
881
#endif
882
}
883

884
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
1,137,205✔
885
  OS_PARAM_CHECK(lock);
1,137,205!
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);
1,137,205✔
890
  if (code && code != EBUSY) {
1,137,460!
891
    code = TAOS_SYSTEM_ERROR(code);
×
892
  }
893
  return code;
1,137,460✔
894
#endif
895
}
896

897
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
1,513,934✔
898
  OS_PARAM_CHECK(lock);
1,513,934!
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);
1,513,934✔
903
  if (code) {
1,513,882!
904
    terrno = TAOS_SYSTEM_ERROR(code);
×
905
    return terrno;
×
906
  }
907
  return code;
1,513,924✔
908
#endif
909
}
910

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

915
void taosThreadClear(TdThread *thread) { 
827,474✔
916
  if (!thread) return;
827,474!
917
  (void)memset(thread, 0, sizeof(TdThread)); 
827,474✔
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

© 2025 Coveralls, Inc