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

taosdata / TDengine / #3527

12 Nov 2024 08:50PM UTC coverage: 60.819% (+0.6%) from 60.225%
#3527

push

travis-ci

GitHub
Merge pull request #28730 from taosdata/doc/analysis

118574 of 249004 branches covered (47.62%)

Branch coverage included in aggregate %.

199137 of 273386 relevant lines covered (72.84%)

15792295.89 hits per line

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

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

30
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) { 
835,495✔
31
  int32_t code = pthread_attr_destroy(attr); 
835,495✔
32
  if (code) {
835,494!
33
    terrno = TAOS_SYSTEM_ERROR(code);
×
34
    return terrno;
×
35
  }
36
  return code;
835,495✔
37
}
38

39
int32_t taosThreadAttrGetDetachState(const TdThreadAttr *attr, int32_t *detachstate) {
×
40
  int32_t code = pthread_attr_getdetachstate(attr, detachstate);
×
41
  if (code) {
×
42
    terrno = TAOS_SYSTEM_ERROR(code);
×
43
    return terrno;
×
44
  }
45
  return code;
×
46
}
47

48
int32_t taosThreadAttrGetInheritSched(const TdThreadAttr *attr, int32_t *inheritsched) {
×
49
  int32_t code = pthread_attr_getinheritsched(attr, inheritsched);
×
50
  if (code) {
×
51
    terrno = TAOS_SYSTEM_ERROR(code);
×
52
    return terrno;
×
53
  }
54
  return code;
×
55
}
56

57
int32_t taosThreadAttrGetSchedParam(const TdThreadAttr *attr, struct sched_param *param) {
×
58
  int32_t code = pthread_attr_getschedparam(attr, param);
×
59
  if (code) {
×
60
    terrno = TAOS_SYSTEM_ERROR(code);
×
61
    return terrno;
×
62
  }
63
  return code;
×
64
}
65

66
int32_t taosThreadAttrGetSchedPolicy(const TdThreadAttr *attr, int32_t *policy) {
×
67
  int32_t code = pthread_attr_getschedpolicy(attr, policy);
×
68
  if (code) {
×
69
    terrno = TAOS_SYSTEM_ERROR(code);
×
70
    return terrno;
×
71
  }
72
  return code;
×
73
}
74

75
int32_t taosThreadAttrGetScope(const TdThreadAttr *attr, int32_t *contentionscope) {
×
76
  int32_t code = pthread_attr_getscope(attr, contentionscope);
×
77
  if (code) {
×
78
    terrno = TAOS_SYSTEM_ERROR(code);
×
79
    return terrno;
×
80
  }
81
  return code;
×
82
}
83

84
int32_t taosThreadAttrGetStackSize(const TdThreadAttr *attr, size_t *stacksize) {
×
85
  int32_t code = pthread_attr_getstacksize(attr, stacksize);
×
86
  if (code) {
×
87
    terrno = TAOS_SYSTEM_ERROR(code);
×
88
    return terrno;
×
89
  }
90
  return code;
×
91
}
92

93
int32_t taosThreadAttrInit(TdThreadAttr *attr) { 
835,763✔
94
  int32_t code = pthread_attr_init(attr); 
835,763✔
95
  if (code) {
835,763!
96
    terrno = TAOS_SYSTEM_ERROR(code);
×
97
    return terrno;
×
98
  }
99
  return code;
835,763✔
100
}
101

102
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
820,298✔
103
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
820,298✔
104
  if (code) {
820,298!
105
    terrno = TAOS_SYSTEM_ERROR(code);
×
106
    return terrno;
×
107
  }
108
  return code;
820,298✔
109
}
110

111
int32_t taosThreadAttrSetInheritSched(TdThreadAttr *attr, int32_t inheritsched) {
×
112
  int32_t code = pthread_attr_setinheritsched(attr, inheritsched);
×
113
  if (code) {
×
114
    terrno = TAOS_SYSTEM_ERROR(code);
×
115
    return terrno;
×
116
  }
117
  return code;
×
118
}
119

120
int32_t taosThreadAttrSetSchedParam(TdThreadAttr *attr, const struct sched_param *param) {
×
121
  int32_t code = pthread_attr_setschedparam(attr, param);
×
122
  if (code) {
×
123
    terrno = TAOS_SYSTEM_ERROR(code);
×
124
    return terrno;
×
125
  }
126
  return code;
×
127
}
128

129
int32_t taosThreadAttrSetSchedPolicy(TdThreadAttr *attr, int32_t policy) {
×
130
  int32_t code = pthread_attr_setschedpolicy(attr, policy);
×
131
  if (code) {
×
132
    terrno = TAOS_SYSTEM_ERROR(code);
×
133
    return terrno;
×
134
  }
135
  return code;
×
136
}
137

138
int32_t taosThreadAttrSetScope(TdThreadAttr *attr, int32_t contentionscope) {
×
139
  int32_t code = pthread_attr_setscope(attr, contentionscope);
×
140
  if (code) {
×
141
    terrno = TAOS_SYSTEM_ERROR(code);
×
142
    return terrno;
×
143
  }
144
  return code;
×
145
}
146

147
int32_t taosThreadAttrSetStackSize(TdThreadAttr *attr, size_t stacksize) {
×
148
  int32_t code = pthread_attr_setstacksize(attr, stacksize);
×
149
  if (code) {
×
150
    terrno = TAOS_SYSTEM_ERROR(code);
×
151
    return terrno;
×
152
  }
153
  return code;
×
154
}
155

156
int32_t taosThreadCancel(TdThread thread) { 
×
157
  int32_t code = pthread_cancel(thread); 
×
158
  if (code) {
×
159
    terrno = TAOS_SYSTEM_ERROR(code);
×
160
    return terrno;
×
161
  }
162
  return code;
×
163
}
164

165
int32_t taosThreadCondDestroy(TdThreadCond *cond) {
23,657,840✔
166
#ifdef __USE_WIN_THREAD
167
  return 0;
168
#else
169
  int32_t code = pthread_cond_destroy(cond);
23,657,840✔
170
  if (code) {
23,669,753!
171
    terrno = TAOS_SYSTEM_ERROR(code);
×
172
    return terrno;
×
173
  }
174
  return code;
23,670,065✔
175
#endif
176
}
177

178
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
23,632,462✔
179
#ifdef __USE_WIN_THREAD
180
  InitializeConditionVariable(cond);
181
  return 0;
182
#else
183
  int32_t code = pthread_cond_init(cond, attr);
23,632,462✔
184
  if (code) {
23,636,710!
185
    terrno = TAOS_SYSTEM_ERROR(code);
×
186
    return terrno;
×
187
  }
188
  return code;
23,639,237✔
189
#endif
190
}
191

192
int32_t taosThreadCondSignal(TdThreadCond *cond) {
2,460,159✔
193
#ifdef __USE_WIN_THREAD
194
  WakeConditionVariable(cond);
195
  return 0;
196
#else
197
  int32_t code = pthread_cond_signal(cond);
2,460,159✔
198
  if (code) {
2,460,188!
199
    terrno = TAOS_SYSTEM_ERROR(code);
×
200
    return terrno;
×
201
  }
202
  return code;
2,460,189✔
203
#endif
204
}
205

206
int32_t taosThreadCondBroadcast(TdThreadCond *cond) {
31,576✔
207
#ifdef __USE_WIN_THREAD
208
  WakeAllConditionVariable(cond);
209
  return 0;
210
#else
211
  int32_t code = pthread_cond_broadcast(cond);
31,576✔
212
  if (code) {
31,576!
213
    terrno = TAOS_SYSTEM_ERROR(code);
×
214
    return terrno;
×
215
  }
216
  return code;
31,576✔
217
#endif
218
}
219

220
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
2,263,474✔
221
#ifdef __USE_WIN_THREAD
222
  if (!SleepConditionVariableCS(cond, mutex, INFINITE)) {
223
    return EINVAL;
224
  }
225
  return 0;
226
#else
227
  int32_t code = pthread_cond_wait(cond, mutex);
2,263,474✔
228
  if (code) {
2,263,474!
229
    terrno = TAOS_SYSTEM_ERROR(code);
×
230
    return terrno;
×
231
  }
232
  return code;
2,263,474✔
233
#endif
234
}
235

236
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
751,603✔
237
#ifdef __USE_WIN_THREAD
238
  if (!abstime) return 0;
239
  if (SleepConditionVariableCS(cond, mutex, (DWORD)(abstime->tv_sec * 1e3 + abstime->tv_nsec / 1e6))) return 0;
240
  DWORD error = GetLastError();
241
  if (error == ERROR_TIMEOUT) {
242
    return TSDB_CODE_TIMEOUT_ERROR;
243
  }
244
  return TAOS_SYSTEM_WINAPI_ERROR(error);
245
#else
246
  int32_t code = pthread_cond_timedwait(cond, mutex, abstime);
751,603✔
247
  if(code == ETIMEDOUT) {
751,594✔
248
    return TSDB_CODE_TIMEOUT_ERROR;
590,892✔
249
  } else if (code) {
160,702!
250
    return TAOS_SYSTEM_ERROR(code);
×
251
  } else {
252
    return 0;
160,702✔
253
  }
254
#endif
255
}
256

257
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr *attr) {
112,929✔
258
#ifdef __USE_WIN_THREAD
259
  return 0;
260
#else
261
  int32_t code = pthread_condattr_destroy(attr);
112,929✔
262
  if (code) {
112,929!
263
    terrno = TAOS_SYSTEM_ERROR(code);
×
264
    return terrno;
×
265
  }
266
  return code;
112,929✔
267
#endif
268
}
269

270
int32_t taosThreadCondAttrGetPshared(const TdThreadCondAttr *attr, int32_t *pshared) {
×
271
#ifdef __USE_WIN_THREAD
272
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
273
  return 0;
274
#else
275
  int32_t code = pthread_condattr_getpshared(attr, pshared);
×
276
  if (code) {
×
277
    terrno = TAOS_SYSTEM_ERROR(code);
×
278
    return terrno;
×
279
  }
280
  return code;
×
281
#endif
282
}
283

284
int32_t taosThreadCondAttrInit(TdThreadCondAttr *attr) {
112,923✔
285
#ifdef __USE_WIN_THREAD
286
  return 0;
287
#else
288
  int32_t code = pthread_condattr_init(attr);
112,923✔
289
  if (code) {
112,924!
290
    terrno = TAOS_SYSTEM_ERROR(code);
×
291
    return terrno;
×
292
  }
293
  return code;
112,924✔
294
#endif
295
}
296

297
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
112,923✔
298
#ifdef __USE_WIN_THREAD
299
  return 0;
300
#elif defined(__APPLE__)
301
  return 0;
302
#else
303
  int32_t code = pthread_condattr_setclock(attr, clockId);
112,923✔
304
  if (code) {
112,924!
305
    terrno = TAOS_SYSTEM_ERROR(code);
×
306
    return terrno;
×
307
  }
308
  return code;
112,925✔
309
#endif
310
}
311

312
int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr *attr, int32_t pshared) {
×
313
#ifdef __USE_WIN_THREAD
314
  return 0;
315
#else
316
  int32_t code = pthread_condattr_setpshared(attr, pshared);
×
317
  if (code) {
×
318
    terrno = TAOS_SYSTEM_ERROR(code);
×
319
    return terrno;
×
320
  }
321
  return code;
×
322
#endif
323
}
324

325
int32_t taosThreadDetach(TdThread thread) { 
×
326
  int32_t code = pthread_detach(thread); 
×
327
  if (code) {
×
328
    terrno = TAOS_SYSTEM_ERROR(code);
×
329
    return terrno;
×
330
  }
331
  return code;
×
332
}
333

334
int32_t taosThreadEqual(TdThread t1, TdThread t2) { 
×
335
  return pthread_equal(t1, t2); 
×
336
}
337

338
void taosThreadExit(void *valuePtr) { 
×
339
  return pthread_exit(valuePtr); 
×
340
}
341

342
int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param) {
×
343
  int32_t code = pthread_getschedparam(thread, policy, param);
×
344
  if (code) {
×
345
    terrno = TAOS_SYSTEM_ERROR(code);
×
346
    return terrno;
×
347
  }
348
  return code;
×
349
}
350

351
void *taosThreadGetSpecific(TdThreadKey key) { 
×
352
  return pthread_getspecific(key); 
×
353
}
354

355
int32_t taosThreadJoin(TdThread thread, void **valuePtr) { 
933,018✔
356
  int32_t code = pthread_join(thread, valuePtr); 
933,018✔
357
  if (code) {
933,019!
358
    terrno = TAOS_SYSTEM_ERROR(code);
×
359
    return terrno;
×
360
  }
361
  return code;
933,019✔
362
}
363

364
int32_t taosThreadKeyCreate(TdThreadKey *key, void (*destructor)(void *)) {
19✔
365
  int32_t code = pthread_key_create(key, destructor);
19✔
366
  if (code) {
19!
367
    terrno = TAOS_SYSTEM_ERROR(code);
×
368
    return terrno;
×
369
  }
370
  return code;
19✔
371
}
372

373
int32_t taosThreadKeyDelete(TdThreadKey key) { 
×
374
  int32_t code = pthread_key_delete(key); 
×
375
  if (code) {
×
376
    terrno = TAOS_SYSTEM_ERROR(code);
×
377
    return terrno;
×
378
  }
379
  return code;
×
380
}
381

382
int32_t taosThreadKill(TdThread thread, int32_t sig) { 
2,230✔
383
  int32_t code = pthread_kill(thread, sig); 
2,230✔
384
  if (code) {
2,230!
385
    terrno = TAOS_SYSTEM_ERROR(code);
×
386
    return terrno;
×
387
  }
388
  return code;
2,230✔
389
}
390

391
// int32_t taosThreadMutexConsistent(TdThreadMutex* mutex) {
392
//   return pthread_mutex_consistent(mutex);
393
// }
394

395
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
34,445,063✔
396
#ifdef __USE_WIN_THREAD
397
  DeleteCriticalSection(mutex);
398
  return 0;
399
#else
400
  int32_t code = pthread_mutex_destroy(mutex);
34,445,063✔
401
  if (code) {
34,446,382✔
402
    terrno = TAOS_SYSTEM_ERROR(code);
1,444✔
403
    return terrno;
3✔
404
  }
405
  return code;
34,444,938✔
406
#endif
407
}
408

409
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
34,876,116✔
410
#ifdef __USE_WIN_THREAD
411
  /**
412
   * Windows Server 2003 and Windows XP:  In low memory situations, InitializeCriticalSection can raise a
413
   * STATUS_NO_MEMORY exception. Starting with Windows Vista, this exception was eliminated and
414
   * InitializeCriticalSection always succeeds, even in low memory situations.
415
   */
416
  InitializeCriticalSection(mutex);
417
  return 0;
418
#else
419
  int32_t code = pthread_mutex_init(mutex, attr);
34,876,116✔
420
  if (code) {
34,889,928✔
421
    terrno = TAOS_SYSTEM_ERROR(code);
1,845✔
422
    return terrno;
×
423
  }
424
  return code;
34,888,083✔
425
#endif
426
}
427

428
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
1,110,850,264✔
429
#ifdef __USE_WIN_THREAD
430
  EnterCriticalSection(mutex);
431
  return 0;
432
#else
433
  int32_t code = pthread_mutex_lock(mutex);
1,110,850,264✔
434
  if (code) {
1,113,994,857✔
435
    terrno = TAOS_SYSTEM_ERROR(code);
460,677✔
436
    return terrno;
×
437
  }
438
  return code;
1,113,534,180✔
439
#endif
440
}
441

442
// int32_t taosThreadMutexTimedLock(TdThreadMutex * mutex, const struct timespec *abstime) {
443
//   return pthread_mutex_timedlock(mutex, abstime);
444
// }
445

446
int32_t taosThreadMutexTryLock(TdThreadMutex *mutex) {
2✔
447
#ifdef __USE_WIN_THREAD
448
  if (TryEnterCriticalSection(mutex)) return 0;
449
  return EBUSY;
450
#else
451
  int32_t code = pthread_mutex_trylock(mutex);
2✔
452
  if (code && code != EBUSY) {
2!
453
    code = TAOS_SYSTEM_ERROR(code);
×
454
  }
455
  return code;
2✔
456
#endif
457
}
458

459
int32_t taosThreadMutexUnlock(TdThreadMutex *mutex) {
1,111,517,983✔
460
#ifdef __USE_WIN_THREAD
461
  LeaveCriticalSection(mutex);
462
  return 0;
463
#else
464
  int32_t code = pthread_mutex_unlock(mutex);
1,111,517,983✔
465
  if (code) {
1,114,529,566!
466
    terrno = TAOS_SYSTEM_ERROR(code);
×
467
    return terrno;
×
468
  }
469
  return code;
1,114,547,864✔
470
#endif
471
}
472

473
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr *attr) {
32,577✔
474
#ifdef __USE_WIN_THREAD
475
  return 0;
476
#else
477
  int32_t code = pthread_mutexattr_destroy(attr);
32,577✔
478
  if (code) {
32,584!
479
    terrno = TAOS_SYSTEM_ERROR(code);
×
480
    return terrno;
×
481
  }
482
  return code;
32,586✔
483
#endif
484
}
485

486
int32_t taosThreadMutexAttrGetPshared(const TdThreadMutexAttr *attr, int32_t *pshared) {
×
487
#ifdef __USE_WIN_THREAD
488
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
489
  return 0;
490
#else
491
  int32_t code = pthread_mutexattr_getpshared(attr, pshared);
×
492
  if (code) {
×
493
    terrno = TAOS_SYSTEM_ERROR(code);
×
494
    return terrno;
×
495
  }
496
  return code;
×
497
#endif
498
}
499

500
// int32_t taosThreadMutexAttrGetRobust(const TdThreadMutexAttr * attr, int32_t * robust) {
501
//   return pthread_mutexattr_getrobust(attr, robust);
502
// }
503

504
int32_t taosThreadMutexAttrGetType(const TdThreadMutexAttr *attr, int32_t *kind) {
×
505
#ifdef __USE_WIN_THREAD
506
  if (kind) *kind = PTHREAD_MUTEX_NORMAL;
507
  return 0;
508
#else
509
  int32_t code = pthread_mutexattr_gettype(attr, kind);
×
510
  if (code) {
×
511
    terrno = TAOS_SYSTEM_ERROR(code);
×
512
    return terrno;
×
513
  }
514
  return code;
×
515
#endif
516
}
517

518
int32_t taosThreadMutexAttrInit(TdThreadMutexAttr *attr) {
32,586✔
519
#ifdef __USE_WIN_THREAD
520
  return 0;
521
#else
522
  int32_t code = pthread_mutexattr_init(attr);
32,586✔
523
  if (code) {
32,587!
524
    terrno = TAOS_SYSTEM_ERROR(code);
×
525
    return terrno;
×
526
  }
527
  return code;
32,590✔
528
#endif
529
}
530

531
int32_t taosThreadMutexAttrSetPshared(TdThreadMutexAttr *attr, int32_t pshared) {
×
532
#ifdef __USE_WIN_THREAD
533
  return 0;
534
#else
535
  int32_t code = pthread_mutexattr_setpshared(attr, pshared);
×
536
  if (code) {
×
537
    terrno = TAOS_SYSTEM_ERROR(code);
×
538
    return terrno;
×
539
  }
540
  return code;
×
541
#endif
542
}
543

544
// int32_t taosThreadMutexAttrSetRobust(TdThreadMutexAttr * attr, int32_t robust) {
545
//   return pthread_mutexattr_setrobust(attr, robust);
546
// }
547

548
int32_t taosThreadMutexAttrSetType(TdThreadMutexAttr *attr, int32_t kind) {
32,585✔
549
#ifdef __USE_WIN_THREAD
550
  return 0;
551
#else
552
  int32_t code = pthread_mutexattr_settype(attr, kind);
32,585✔
553
  if (code) {
32,584!
554
    terrno = TAOS_SYSTEM_ERROR(code);
×
555
    return terrno;
×
556
  }
557
  return code;
32,585✔
558
#endif
559
}
560

561
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
56,936,607✔
562
  int32_t code = pthread_once(onceControl, initRoutine);
56,936,607✔
563
  if (code) {
56,936,868!
564
    terrno = TAOS_SYSTEM_ERROR(code);
×
565
    return terrno;
×
566
  }
567
  return code;
56,938,282✔
568
}
569

570
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
21,913,719✔
571
#ifdef __USE_WIN_THREAD
572
  /* SRWLock does not need explicit destruction so long as there are no waiting threads
573
   * See: https://docs.microsoft.com/windows/win32/api/synchapi/nf-synchapi-initializesrwlock#remarks
574
   */
575
  return 0;
576
#else
577
  int32_t code = pthread_rwlock_destroy(rwlock);
21,913,719✔
578
  if (code) {
21,913,738!
579
    terrno = TAOS_SYSTEM_ERROR(code);
×
580
    return terrno;
×
581
  }
582
  return code;
21,913,762✔
583
#endif
584
}
585

586
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
21,912,831✔
587
#ifdef __USE_WIN_THREAD
588
  memset(rwlock, 0, sizeof(*rwlock));
589
  InitializeSRWLock(&rwlock->lock);
590
  return 0;
591
#else
592
  int32_t code = pthread_rwlock_init(rwlock, attr);
21,912,831✔
593
  if (code) {
21,911,021!
594
    terrno = TAOS_SYSTEM_ERROR(code);
×
595
    return terrno;
×
596
  }
597
  return code;
21,912,384✔
598
#endif
599
}
600

601
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
630,195,059✔
602
#ifdef __USE_WIN_THREAD
603
  AcquireSRWLockShared(&rwlock->lock);
604
  return 0;
605
#else
606
  int32_t code = pthread_rwlock_rdlock(rwlock);
630,195,059✔
607
  if (code) {
630,878,639!
608
    terrno = TAOS_SYSTEM_ERROR(code);
×
609
    return terrno;
×
610
  }
611
  return code;
630,887,009✔
612
#endif
613
}
614

615
// int32_t taosThreadRwlockTimedRdlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
616
//   return pthread_rwlock_timedrdlock(rwlock, abstime);
617
// }
618

619
// int32_t taosThreadRwlockTimedWrlock(TdThreadRwlock * rwlock, const struct timespec *abstime) {
620
//   return pthread_rwlock_timedwrlock(rwlock, abstime);
621
// }
622

623
int32_t taosThreadRwlockTryRdlock(TdThreadRwlock *rwlock) {
×
624
#ifdef __USE_WIN_THREAD
625
  if (!TryAcquireSRWLockShared(&rwlock->lock)) return EBUSY;
626
  return 0;
627
#else
628
  int32_t code = pthread_rwlock_tryrdlock(rwlock);
×
629
  if (code) {
×
630
    terrno = TAOS_SYSTEM_ERROR(code);
×
631
    return terrno;
×
632
  }
633
  return code;
×
634
#endif
635
}
636

637
int32_t taosThreadRwlockTryWrlock(TdThreadRwlock *rwlock) {
×
638
#ifdef __USE_WIN_THREAD
639
  if (!TryAcquireSRWLockExclusive(&rwlock->lock)) return EBUSY;
640
  atomic_store_8(&rwlock->excl, 1);
641
  return 0;
642
#else
643
  int32_t code = pthread_rwlock_trywrlock(rwlock);
×
644
  if (code) {
×
645
    terrno = TAOS_SYSTEM_ERROR(code);
×
646
    return terrno;
×
647
  }
648
  return code;
×
649
#endif
650
}
651

652
int32_t taosThreadRwlockUnlock(TdThreadRwlock *rwlock) {
1,546,944,575✔
653
#ifdef __USE_WIN_THREAD
654
  if (1 == atomic_val_compare_exchange_8(&rwlock->excl, 1, 0)) {
655
    ReleaseSRWLockExclusive(&rwlock->lock);
656
  } else {
657
    ReleaseSRWLockShared(&rwlock->lock);
658
  }
659
  return 0;
660
#else
661
  int32_t code = pthread_rwlock_unlock(rwlock);
1,546,944,575✔
662
  if (code) {
1,547,596,116✔
663
    terrno = TAOS_SYSTEM_ERROR(code);
95,897✔
664
    return terrno;
×
665
  }
666
  return code;
1,547,500,219✔
667
#endif
668
}
669

670
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
916,477,852✔
671
#ifdef __USE_WIN_THREAD
672
  AcquireSRWLockExclusive(&rwlock->lock);
673
  atomic_store_8(&rwlock->excl, 1);
674
  return 0;
675
#else
676
  int32_t code = pthread_rwlock_wrlock(rwlock);
916,477,852✔
677
  if (code) {
916,850,891!
678
    terrno = TAOS_SYSTEM_ERROR(code);
×
679
    return terrno;
×
680
  }
681
  return code;
916,852,578✔
682
#endif
683
}
684

685
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
42,450✔
686
#ifdef __USE_WIN_THREAD
687
  return 0;
688
#else
689
  int32_t code = pthread_rwlockattr_destroy(attr);
42,450✔
690
  if (code) {
42,451!
691
    terrno = TAOS_SYSTEM_ERROR(code);
×
692
    return terrno;
×
693
  }
694
  return code;
42,455✔
695
#endif
696
}
697

698
int32_t taosThreadRwlockAttrGetPshared(const TdThreadRwlockAttr *attr, int32_t *pshared) {
×
699
#ifdef __USE_WIN_THREAD
700
  if (pshared) *pshared = PTHREAD_PROCESS_PRIVATE;
701
  return 0;
702
#else
703
  int32_t code = pthread_rwlockattr_getpshared(attr, pshared);
×
704
  if (code) {
×
705
    terrno = TAOS_SYSTEM_ERROR(code);
×
706
    return terrno;
×
707
  }
708
  return code;
×
709
#endif
710
}
711

712
int32_t taosThreadRwlockAttrInit(TdThreadRwlockAttr *attr) {
42,447✔
713
#ifdef __USE_WIN_THREAD
714
  return 0;
715
#else
716
  int32_t code = pthread_rwlockattr_init(attr);
42,447✔
717
  if (code) {
42,452✔
718
    terrno = TAOS_SYSTEM_ERROR(code);
2✔
719
    return terrno;
×
720
  }
721
  return code;
42,450✔
722
#endif
723
}
724

725
int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr *attr, int32_t pshared) {
×
726
#ifdef __USE_WIN_THREAD
727
  return 0;
728
#else
729
  int32_t code = pthread_rwlockattr_setpshared(attr, pshared);
×
730
  if (code) {
×
731
    terrno = TAOS_SYSTEM_ERROR(code);
×
732
    return terrno;
×
733
  }
734
  return code;
×
735
#endif
736
}
737

738
TdThread taosThreadSelf(void) { return pthread_self(); }
×
739

740
int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate) { 
×
741
  int32_t code = pthread_setcancelstate(state, oldstate); 
×
742
  if (code) {
×
743
    terrno = TAOS_SYSTEM_ERROR(code);
×
744
    return terrno;
×
745
  }
746
  return code;
×
747
}
748

749
int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype) { 
×
750
  int32_t code = pthread_setcanceltype(type, oldtype); 
×
751
  if (code) {
×
752
    terrno = TAOS_SYSTEM_ERROR(code);
×
753
    return terrno;
×
754
  }
755
  return code;
×
756
}
757

758
int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sched_param *param) {
×
759
  int32_t code = pthread_setschedparam(thread, policy, param);
×
760
  if (code) {
×
761
    terrno = TAOS_SYSTEM_ERROR(code);
×
762
    return terrno;
×
763
  }
764
  return code;
×
765
}
766

767
int32_t taosThreadSetSpecific(TdThreadKey key, const void *value) { 
197✔
768
  int32_t code = pthread_setspecific(key, value); 
197✔
769
  if (code) {
197!
770
    terrno = TAOS_SYSTEM_ERROR(code);
×
771
    return terrno;
×
772
  }
773
  return code;
197✔
774
}
775

776
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
4,384,124✔
777
#ifdef TD_USE_SPINLOCK_AS_MUTEX
778
  return pthread_mutex_destroy((pthread_mutex_t *)lock);
779
#else
780
  int32_t code = pthread_spin_destroy((pthread_spinlock_t *)lock);
4,384,124✔
781
  if (code) {
4,384,172!
782
    terrno = TAOS_SYSTEM_ERROR(code);
×
783
    return terrno;
×
784
  }
785
  return code;
4,384,217✔
786
#endif
787
}
788

789
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
4,364,331✔
790
#ifdef TD_USE_SPINLOCK_AS_MUTEX
791
  if (pshared != 0) return TSDB_CODE_INVALID_PARA;
792
  return pthread_mutex_init((pthread_mutex_t *)lock, NULL);
793
#else
794
  int32_t code = pthread_spin_init((pthread_spinlock_t *)lock, pshared);
4,364,331✔
795
  if (code) {
4,366,069!
796
    terrno = TAOS_SYSTEM_ERROR(code);
×
797
    return terrno;
×
798
  }
799
  return code;
4,366,881✔
800
#endif
801
}
802

803
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
371,102✔
804
#ifdef TD_USE_SPINLOCK_AS_MUTEX
805
  return pthread_mutex_lock((pthread_mutex_t *)lock);
806
#else
807
  int32_t code = pthread_spin_lock((pthread_spinlock_t *)lock);
371,102✔
808
  if (code) {
371,264!
809
    terrno = TAOS_SYSTEM_ERROR(code);
×
810
    return terrno;
×
811
  }
812
  return code;
371,264✔
813
#endif
814
}
815

816
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
1,031,590✔
817
#ifdef TD_USE_SPINLOCK_AS_MUTEX
818
  return pthread_mutex_trylock((pthread_mutex_t *)lock);
819
#else
820
  int32_t code = pthread_spin_trylock((pthread_spinlock_t *)lock);
1,031,590✔
821
  if (code && code != EBUSY) {
1,032,010!
822
    code = TAOS_SYSTEM_ERROR(code);
×
823
  }
824
  return code;
1,032,010✔
825
#endif
826
}
827

828
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
1,402,981✔
829
#ifdef TD_USE_SPINLOCK_AS_MUTEX
830
  return pthread_mutex_unlock((pthread_mutex_t *)lock);
831
#else
832
  int32_t code = pthread_spin_unlock((pthread_spinlock_t *)lock);
1,402,981✔
833
  if (code) {
1,403,009!
834
    terrno = TAOS_SYSTEM_ERROR(code);
×
835
    return terrno;
×
836
  }
837
  return code;
1,403,011✔
838
#endif
839
}
840

841
void taosThreadTestCancel(void) { 
×
842
  return pthread_testcancel(); 
×
843
}
844

845
void taosThreadClear(TdThread *thread) { 
815,265✔
846
  (void)memset(thread, 0, sizeof(TdThread)); 
815,265✔
847
}
815,265✔
848

849
#ifdef WINDOWS
850
bool taosThreadIsMain() {
851
  DWORD curProcessId = GetCurrentProcessId();
852
  DWORD curThreadId = GetCurrentThreadId();
853
  DWORD dwThreadId = -1;
854

855
  HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
856
  if (hThreadSnapshot == INVALID_HANDLE_VALUE) {
857
    return false;
858
  }
859

860
  THREADENTRY32 te32;
861
  te32.dwSize = sizeof(THREADENTRY32);
862

863
  if (!Thread32First(hThreadSnapshot, &te32)) {
864
    CloseHandle(hThreadSnapshot);
865
    return false;
866
  }
867

868
  do {
869
    if (te32.th32OwnerProcessID == curProcessId) {
870
      dwThreadId = te32.th32ThreadID;
871
      break;
872
    }
873
  } while (Thread32Next(hThreadSnapshot, &te32));
874

875
  CloseHandle(hThreadSnapshot);
876

877
  return curThreadId == dwThreadId;
878
}
879
#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