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

taosdata / TDengine / #3542

27 Nov 2024 02:52AM UTC coverage: 60.819% (+0.04%) from 60.776%
#3542

push

travis-ci

web-flow
Merge pull request #28931 from taosdata/enh/jdbc-demo-3.0

update jdbc demo, and version history

120305 of 252779 branches covered (47.59%)

Branch coverage included in aggregate %.

201010 of 275538 relevant lines covered (72.95%)

19989893.51 hits per line

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

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

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

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

201
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
20,074,397✔
202
  OS_PARAM_CHECK(cond);
20,074,397!
203
#ifdef __USE_WIN_THREAD
204
  InitializeConditionVariable(cond);
205
  return 0;
206
#else
207
  int32_t code = pthread_cond_init(cond, attr);
20,074,397✔
208
  if (code) {
20,080,583!
209
    terrno = TAOS_SYSTEM_ERROR(code);
×
210
    return terrno;
×
211
  }
212
  return code;
20,083,110✔
213
#endif
214
}
215

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

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

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

264
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
783,339✔
265
  if (!abstime) return 0;
783,339!
266
  OS_PARAM_CHECK(cond);
783,339!
267
  OS_PARAM_CHECK(mutex);
783,339!
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);
783,339✔
277
  if(code == ETIMEDOUT) {
783,336✔
278
    return TSDB_CODE_TIMEOUT_ERROR;
616,333✔
279
  } else if (code) {
167,003!
280
    return TAOS_SYSTEM_ERROR(code);
×
281
  } else {
282
    return 0;
167,003✔
283
  }
284
#endif
285
}
286

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

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

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

434
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
35,612,486✔
435
  OS_PARAM_CHECK(mutex);
35,612,486!
436
#ifdef __USE_WIN_THREAD
437
  DeleteCriticalSection(mutex);
438
  return 0;
439
#else
440
  int32_t code = pthread_mutex_destroy(mutex);
35,612,486✔
441
  if (code) {
35,611,212!
442
    terrno = TAOS_SYSTEM_ERROR(code);
×
443
    return terrno;
8✔
444
  }
445
  return code;
35,612,344✔
446
#endif
447
}
448

449
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
36,033,299✔
450
  OS_PARAM_CHECK(mutex);
36,033,299!
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,033,299✔
461
  if (code) {
36,037,855!
462
    terrno = TAOS_SYSTEM_ERROR(code);
×
463
    return terrno;
×
464
  }
465
  return code;
36,047,432✔
466
#endif
467
}
468

469
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
1,122,452,429✔
470
  OS_PARAM_CHECK(mutex);
1,122,452,429!
471
#ifdef __USE_WIN_THREAD
472
  EnterCriticalSection(mutex);
473
  return 0;
474
#else
475
  int32_t code = pthread_mutex_lock(mutex);
1,122,452,429✔
476
  if (code) {
1,125,908,578!
477
    terrno = TAOS_SYSTEM_ERROR(code);
×
478
    return terrno;
×
479
  }
480
  return code;
1,126,128,621✔
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,123,696,750✔
503
  OS_PARAM_CHECK(mutex);
1,123,696,750!
504
#ifdef __USE_WIN_THREAD
505
  LeaveCriticalSection(mutex);
506
  return 0;
507
#else
508
  int32_t code = pthread_mutex_unlock(mutex);
1,123,696,750✔
509
  if (code) {
1,126,136,871!
510
    terrno = TAOS_SYSTEM_ERROR(code);
×
511
    return terrno;
×
512
  }
513
  return code;
1,126,388,817✔
514
#endif
515
}
516

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

613
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
47,252,246✔
614
  int32_t code = pthread_once(onceControl, initRoutine);
47,252,246✔
615
  if (code) {
47,252,984✔
616
    terrno = TAOS_SYSTEM_ERROR(code);
1,197✔
617
    return terrno;
×
618
  }
619
  return code;
47,251,787✔
620
}
621

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

639
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
21,517,954✔
640
  OS_PARAM_CHECK(rwlock);
21,517,954!
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);
21,517,954✔
647
  if (code) {
21,513,409!
648
    terrno = TAOS_SYSTEM_ERROR(code);
×
649
    return terrno;
×
650
  }
651
  return code;
21,515,711✔
652
#endif
653
}
654

655
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
588,579,062✔
656
  OS_PARAM_CHECK(rwlock);
588,579,062!
657
#ifdef __USE_WIN_THREAD
658
  AcquireSRWLockShared(&rwlock->lock);
659
  return 0;
660
#else
661
  int32_t code = pthread_rwlock_rdlock(rwlock);
588,579,062✔
662
  if (code) {
589,245,626!
663
    terrno = TAOS_SYSTEM_ERROR(code);
×
664
    return terrno;
×
665
  }
666
  return code;
589,266,502✔
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,428,237,170✔
710
  OS_PARAM_CHECK(rwlock);
1,428,237,170!
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,428,237,170✔
720
  if (code) {
1,428,771,444!
721
    terrno = TAOS_SYSTEM_ERROR(code);
×
722
    return terrno;
×
723
  }
724
  return code;
1,428,847,451✔
725
#endif
726
}
727

728
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
839,363,578✔
729
  OS_PARAM_CHECK(rwlock);
839,363,578!
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);
839,363,578✔
736
  if (code) {
839,720,010!
737
    terrno = TAOS_SYSTEM_ERROR(code);
×
738
    return terrno;
×
739
  }
740
  return code;
839,723,161✔
741
#endif
742
}
743

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

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

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

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

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

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

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

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