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

taosdata / TDengine / #3549

06 Dec 2024 09:44AM UTC coverage: 59.948% (+0.1%) from 59.846%
#3549

push

travis-ci

web-flow
Merge pull request #29057 from taosdata/docs/TD-33031-3.0

docs: description of user privileges

118833 of 254191 branches covered (46.75%)

Branch coverage included in aggregate %.

199893 of 277480 relevant lines covered (72.04%)

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

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

118
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
784,645✔
119
  OS_PARAM_CHECK(attr);
784,645!
120
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
784,645✔
121
  if (code) {
784,645!
122
    terrno = TAOS_SYSTEM_ERROR(code);
×
123
    return terrno;
×
124
  }
125
  return code;
784,645✔
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) {
21,141,861✔
188
  OS_PARAM_CHECK(cond);
21,141,861!
189
#ifdef __USE_WIN_THREAD
190
  return 0;
191
#else
192
  int32_t code = pthread_cond_destroy(cond);
21,141,861✔
193
  if (code) {
21,154,981!
194
    terrno = TAOS_SYSTEM_ERROR(code);
×
195
    return terrno;
×
196
  }
197
  return code;
21,155,149✔
198
#endif
199
}
200

201
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
21,105,047✔
202
  OS_PARAM_CHECK(cond);
21,105,047!
203
#ifdef __USE_WIN_THREAD
204
  InitializeConditionVariable(cond);
205
  return 0;
206
#else
207
  int32_t code = pthread_cond_init(cond, attr);
21,105,047✔
208
  if (code) {
21,108,838!
209
    terrno = TAOS_SYSTEM_ERROR(code);
×
210
    return terrno;
×
211
  }
212
  return code;
21,112,460✔
213
#endif
214
}
215

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

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

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

264
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
709,456✔
265
  if (!abstime) return 0;
709,456!
266
  OS_PARAM_CHECK(cond);
709,456!
267
  OS_PARAM_CHECK(mutex);
709,456!
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);
709,456✔
277
  if(code == ETIMEDOUT) {
709,455✔
278
    return TSDB_CODE_TIMEOUT_ERROR;
563,736✔
279
  } else if (code) {
145,719!
280
    return TAOS_SYSTEM_ERROR(code);
×
281
  } else {
282
    return 0;
145,719✔
283
  }
284
#endif
285
}
286

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

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

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

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

434
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
35,824,391✔
435
  OS_PARAM_CHECK(mutex);
35,824,391!
436
#ifdef __USE_WIN_THREAD
437
  DeleteCriticalSection(mutex);
438
  return 0;
439
#else
440
  int32_t code = pthread_mutex_destroy(mutex);
35,824,391✔
441
  if (code) {
35,821,858!
442
    terrno = TAOS_SYSTEM_ERROR(code);
×
443
    return terrno;
7✔
444
  }
445
  return code;
35,822,992✔
446
#endif
447
}
448

449
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
36,224,939✔
450
  OS_PARAM_CHECK(mutex);
36,224,939!
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,224,939✔
461
  if (code) {
36,234,030!
462
    terrno = TAOS_SYSTEM_ERROR(code);
×
463
    return terrno;
×
464
  }
465
  return code;
36,242,146✔
466
#endif
467
}
468

469
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
1,150,081,318✔
470
  OS_PARAM_CHECK(mutex);
1,150,081,318!
471
#ifdef __USE_WIN_THREAD
472
  EnterCriticalSection(mutex);
473
  return 0;
474
#else
475
  int32_t code = pthread_mutex_lock(mutex);
1,150,081,318✔
476
  if (code) {
1,154,803,650!
477
    terrno = TAOS_SYSTEM_ERROR(code);
×
478
    return terrno;
×
479
  }
480
  return code;
1,155,000,160✔
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,151,385,820✔
503
  OS_PARAM_CHECK(mutex);
1,151,385,820!
504
#ifdef __USE_WIN_THREAD
505
  LeaveCriticalSection(mutex);
506
  return 0;
507
#else
508
  int32_t code = pthread_mutex_unlock(mutex);
1,151,385,820✔
509
  if (code) {
1,154,832,761!
510
    terrno = TAOS_SYSTEM_ERROR(code);
×
511
    return terrno;
×
512
  }
513
  return code;
1,155,130,950✔
514
#endif
515
}
516

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

613
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
52,430,074✔
614
  int32_t code = pthread_once(onceControl, initRoutine);
52,430,074✔
615
  if (code) {
52,432,126✔
616
    terrno = TAOS_SYSTEM_ERROR(code);
685✔
617
    return terrno;
×
618
  }
619
  return code;
52,431,441✔
620
}
621

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

639
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
28,103,306✔
640
  OS_PARAM_CHECK(rwlock);
28,103,306!
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);
28,103,306✔
647
  if (code) {
28,100,199!
648
    terrno = TAOS_SYSTEM_ERROR(code);
×
649
    return terrno;
×
650
  }
651
  return code;
28,101,704✔
652
#endif
653
}
654

655
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
562,777,224✔
656
  OS_PARAM_CHECK(rwlock);
562,777,224!
657
#ifdef __USE_WIN_THREAD
658
  AcquireSRWLockShared(&rwlock->lock);
659
  return 0;
660
#else
661
  int32_t code = pthread_rwlock_rdlock(rwlock);
562,777,224✔
662
  if (code) {
563,487,661!
663
    terrno = TAOS_SYSTEM_ERROR(code);
×
664
    return terrno;
×
665
  }
666
  return code;
563,509,176✔
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,416,929,153✔
710
  OS_PARAM_CHECK(rwlock);
1,416,929,153!
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,416,929,153✔
720
  if (code) {
1,417,493,080!
721
    terrno = TAOS_SYSTEM_ERROR(code);
×
722
    return terrno;
×
723
  }
724
  return code;
1,417,544,671✔
725
#endif
726
}
727

728
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
853,733,199✔
729
  OS_PARAM_CHECK(rwlock);
853,733,199!
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);
853,733,199✔
736
  if (code) {
854,103,297!
737
    terrno = TAOS_SYSTEM_ERROR(code);
×
738
    return terrno;
×
739
  }
740
  return code;
854,105,345✔
741
#endif
742
}
743

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

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

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

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

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

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

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

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