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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

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

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

118
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
742,568✔
119
  OS_PARAM_CHECK(attr);
742,568!
120
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
742,568✔
121
  if (code) {
742,568!
122
    terrno = TAOS_SYSTEM_ERROR(code);
×
123
    return terrno;
×
124
  }
125
  return code;
742,568✔
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) {
7,145,934✔
188
  OS_PARAM_CHECK(cond);
7,145,934!
189
#ifdef __USE_WIN_THREAD
190
  return 0;
191
#else
192
  int32_t code = pthread_cond_destroy(cond);
7,145,934✔
193
  if (code) {
7,152,070!
194
    terrno = TAOS_SYSTEM_ERROR(code);
×
195
    return terrno;
×
196
  }
197
  return code;
7,152,184✔
198
#endif
199
}
200

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

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

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

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

264
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
693,215✔
265
  if (!abstime) return 0;
693,215!
266
  OS_PARAM_CHECK(cond);
693,215!
267
  OS_PARAM_CHECK(mutex);
693,215!
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);
693,215✔
277
  if(code == ETIMEDOUT) {
693,210✔
278
    return TSDB_CODE_TIMEOUT_ERROR;
561,810✔
279
  } else if (code) {
131,400!
280
    return TAOS_SYSTEM_ERROR(code);
×
281
  } else {
282
    return 0;
131,400✔
283
  }
284
#endif
285
}
286

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

331
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
80,343✔
332
#ifdef __USE_WIN_THREAD
333
  return 0;
334
#elif defined(__APPLE__)
335
  return 0;
336
#else
337
  OS_PARAM_CHECK(attr);
80,343!
338
  int32_t code = pthread_condattr_setclock(attr, clockId);
80,343✔
339
  if (code) {
80,343!
340
    terrno = TAOS_SYSTEM_ERROR(code);
×
341
    return terrno;
×
342
  }
343
  return code;
80,344✔
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) { 
854,566✔
394
  int32_t code = pthread_join(thread, valuePtr); 
854,566✔
395
  if (code) {
854,566!
396
    terrno = TAOS_SYSTEM_ERROR(code);
×
397
    return terrno;
×
398
  }
399
  return code;
854,566✔
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) { 
1,490✔
422
  int32_t code = pthread_kill(thread, sig); 
1,490✔
423
  if (code) {
1,490!
424
    terrno = TAOS_SYSTEM_ERROR(code);
×
425
    return terrno;
×
426
  }
427
  return code;
1,490✔
428
}
429

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

434
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
11,241,452✔
435
  OS_PARAM_CHECK(mutex);
11,241,452!
436
#ifdef __USE_WIN_THREAD
437
  DeleteCriticalSection(mutex);
438
  return 0;
439
#else
440
  int32_t code = pthread_mutex_destroy(mutex);
11,241,452✔
441
  if (code) {
11,241,271✔
442
    terrno = TAOS_SYSTEM_ERROR(code);
2,567✔
443
    return terrno;
2,701✔
444
  }
445
  return code;
11,238,704✔
446
#endif
447
}
448

449
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
11,663,763✔
450
  OS_PARAM_CHECK(mutex);
11,663,763!
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);
11,663,763✔
461
  if (code) {
11,664,711!
462
    terrno = TAOS_SYSTEM_ERROR(code);
×
463
    return terrno;
×
464
  }
465
  return code;
11,665,983✔
466
#endif
467
}
468

469
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
343,855,335✔
470
  OS_PARAM_CHECK(mutex);
343,855,335!
471
#ifdef __USE_WIN_THREAD
472
  EnterCriticalSection(mutex);
473
  return 0;
474
#else
475
  int32_t code = pthread_mutex_lock(mutex);
343,855,335✔
476
  if (code) {
344,842,798!
477
    terrno = TAOS_SYSTEM_ERROR(code);
×
478
    return terrno;
×
479
  }
480
  return code;
344,859,112✔
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) {
×
489
  OS_PARAM_CHECK(mutex);
×
490
#ifdef __USE_WIN_THREAD
491
  if (TryEnterCriticalSection(mutex)) return 0;
492
  return EBUSY;
493
#else
494
  int32_t code = pthread_mutex_trylock(mutex);
×
495
  if (code && code != EBUSY) {
×
496
    code = TAOS_SYSTEM_ERROR(code);
×
497
  }
498
  return code;
×
499
#endif
500
}
501

502
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
344,639,757✔
503
  OS_PARAM_CHECK(mutex);
344,639,757!
504
#ifdef __USE_WIN_THREAD
505
  LeaveCriticalSection(mutex);
506
  return 0;
507
#else
508
  int32_t code = pthread_mutex_unlock(mutex);
344,639,757✔
509
  if (code) {
345,273,390!
510
    terrno = TAOS_SYSTEM_ERROR(code);
×
511
    return terrno;
×
512
  }
513
  return code;
345,282,247✔
514
#endif
515
}
516

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

613
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
46,085,617✔
614
  int32_t code = pthread_once(onceControl, initRoutine);
46,085,617✔
615
  if (code) {
46,085,198✔
616
    terrno = TAOS_SYSTEM_ERROR(code);
944✔
617
    return terrno;
×
618
  }
619
  return code;
46,084,254✔
620
}
621

622
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
5,128,104✔
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);
5,128,104!
630
  int32_t code = pthread_rwlock_destroy(rwlock);
5,128,104✔
631
  if (code) {
5,128,014!
UNCOV
632
    terrno = TAOS_SYSTEM_ERROR(code);
×
633
    return terrno;
×
634
  }
635
  return code;
5,128,062✔
636
#endif
637
}
638

639
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
5,136,726✔
640
  OS_PARAM_CHECK(rwlock);
5,136,726!
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);
5,136,726✔
647
  if (code) {
5,136,383!
648
    terrno = TAOS_SYSTEM_ERROR(code);
×
649
    return terrno;
×
650
  }
651
  return code;
5,136,554✔
652
#endif
653
}
654

655
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
103,190,302✔
656
  OS_PARAM_CHECK(rwlock);
103,190,302!
657
#ifdef __USE_WIN_THREAD
658
  AcquireSRWLockShared(&rwlock->lock);
659
  return 0;
660
#else
661
  int32_t code = pthread_rwlock_rdlock(rwlock);
103,190,302✔
662
  if (code) {
103,272,914!
663
    terrno = TAOS_SYSTEM_ERROR(code);
×
664
    return terrno;
×
665
  }
666
  return code;
103,273,851✔
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) {
682,646,873✔
710
  OS_PARAM_CHECK(rwlock);
682,646,873!
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);
682,646,873✔
720
  if (code) {
682,698,818!
721
    terrno = TAOS_SYSTEM_ERROR(code);
×
722
    return terrno;
×
723
  }
724
  return code;
682,706,098✔
725
#endif
726
}
727

728
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
579,190,010✔
729
  OS_PARAM_CHECK(rwlock);
579,190,010!
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);
579,190,010✔
736
  if (code) {
579,497,331!
737
    terrno = TAOS_SYSTEM_ERROR(code);
×
738
    return terrno;
×
739
  }
740
  return code;
579,498,048✔
741
#endif
742
}
743

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

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

855
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
3,801,161✔
856
  OS_PARAM_CHECK(lock);
3,801,161!
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);
3,801,161✔
862
  if (code) {
3,800,669!
863
    terrno = TAOS_SYSTEM_ERROR(code);
×
864
    return terrno;
×
865
  }
866
  return code;
3,800,937✔
867
#endif
868
}
869

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

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

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

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

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