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

taosdata / TDengine / #3525

10 Nov 2024 03:50AM UTC coverage: 60.818% (-0.08%) from 60.898%
#3525

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

118634 of 249004 branches covered (47.64%)

Branch coverage included in aggregate %.

136 of 169 new or added lines in 23 files covered. (80.47%)

542 existing lines in 129 files now uncovered.

199071 of 273386 relevant lines covered (72.82%)

15691647.46 hits per line

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

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

30
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) { 
836,006✔
31
  int32_t code = pthread_attr_destroy(attr); 
836,006✔
32
  if (code) {
836,004!
33
    terrno = TAOS_SYSTEM_ERROR(code);
×
34
    return terrno;
×
35
  }
36
  return code;
836,005✔
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) { 
836,276✔
94
  int32_t code = pthread_attr_init(attr); 
836,276✔
95
  if (code) {
836,276!
96
    terrno = TAOS_SYSTEM_ERROR(code);
×
97
    return terrno;
×
98
  }
99
  return code;
836,276✔
100
}
101

102
int32_t taosThreadAttrSetDetachState(TdThreadAttr *attr, int32_t detachstate) {
820,816✔
103
  int32_t code = pthread_attr_setdetachstate(attr, detachstate);
820,816✔
104
  if (code) {
820,815!
105
    terrno = TAOS_SYSTEM_ERROR(code);
×
106
    return terrno;
×
107
  }
108
  return code;
820,815✔
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) {
21,398,995✔
166
#ifdef __USE_WIN_THREAD
167
  return 0;
168
#else
169
  int32_t code = pthread_cond_destroy(cond);
21,398,995✔
170
  if (code) {
21,412,620!
UNCOV
171
    terrno = TAOS_SYSTEM_ERROR(code);
×
172
    return terrno;
×
173
  }
174
  return code;
21,412,942✔
175
#endif
176
}
177

178
int32_t taosThreadCondInit(TdThreadCond *cond, const TdThreadCondAttr *attr) {
21,369,236✔
179
#ifdef __USE_WIN_THREAD
180
  InitializeConditionVariable(cond);
181
  return 0;
182
#else
183
  int32_t code = pthread_cond_init(cond, attr);
21,369,236✔
184
  if (code) {
21,374,447!
185
    terrno = TAOS_SYSTEM_ERROR(code);
×
186
    return terrno;
×
187
  }
188
  return code;
21,377,845✔
189
#endif
190
}
191

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

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

220
int32_t taosThreadCondWait(TdThreadCond *cond, TdThreadMutex *mutex) {
2,233,828✔
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,233,828✔
228
  if (code) {
2,233,827!
229
    terrno = TAOS_SYSTEM_ERROR(code);
×
230
    return terrno;
×
231
  }
232
  return code;
2,233,827✔
233
#endif
234
}
235

236
int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const struct timespec *abstime) {
742,586✔
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);
742,586✔
247
  if(code == ETIMEDOUT) {
742,564✔
248
    return TSDB_CODE_TIMEOUT_ERROR;
580,931✔
249
  } else if (code) {
161,633!
250
    return TAOS_SYSTEM_ERROR(code);
×
251
  } else {
252
    return 0;
161,633✔
253
  }
254
#endif
255
}
256

257
int32_t taosThreadCondAttrDestroy(TdThreadCondAttr *attr) {
112,505✔
258
#ifdef __USE_WIN_THREAD
259
  return 0;
260
#else
261
  int32_t code = pthread_condattr_destroy(attr);
112,505✔
262
  if (code) {
112,505!
263
    terrno = TAOS_SYSTEM_ERROR(code);
×
264
    return terrno;
×
265
  }
266
  return code;
112,505✔
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,505✔
285
#ifdef __USE_WIN_THREAD
286
  return 0;
287
#else
288
  int32_t code = pthread_condattr_init(attr);
112,505✔
289
  if (code) {
112,505!
290
    terrno = TAOS_SYSTEM_ERROR(code);
×
291
    return terrno;
×
292
  }
293
  return code;
112,505✔
294
#endif
295
}
296

297
int32_t taosThreadCondAttrSetclock(TdThreadCondAttr *attr, int clockId) {
112,505✔
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,505✔
304
  if (code) {
112,505!
305
    terrno = TAOS_SYSTEM_ERROR(code);
×
306
    return terrno;
×
307
  }
308
  return code;
112,505✔
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,682✔
356
  int32_t code = pthread_join(thread, valuePtr); 
933,682✔
357
  if (code) {
933,683!
358
    terrno = TAOS_SYSTEM_ERROR(code);
×
359
    return terrno;
×
360
  }
361
  return code;
933,683✔
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,231✔
383
  int32_t code = pthread_kill(thread, sig); 
2,231✔
384
  if (code) {
2,231!
385
    terrno = TAOS_SYSTEM_ERROR(code);
×
386
    return terrno;
×
387
  }
388
  return code;
2,231✔
389
}
390

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

395
int32_t taosThreadMutexDestroy(TdThreadMutex *mutex) {
34,217,337✔
396
#ifdef __USE_WIN_THREAD
397
  DeleteCriticalSection(mutex);
398
  return 0;
399
#else
400
  int32_t code = pthread_mutex_destroy(mutex);
34,217,337✔
401
  if (code) {
34,218,607✔
402
    terrno = TAOS_SYSTEM_ERROR(code);
1,458✔
403
    return terrno;
3✔
404
  }
405
  return code;
34,217,149✔
406
#endif
407
}
408

409
int32_t taosThreadMutexInit(TdThreadMutex *mutex, const TdThreadMutexAttr *attr) {
34,639,580✔
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,639,580✔
420
  if (code) {
34,654,857✔
421
    terrno = TAOS_SYSTEM_ERROR(code);
1,959✔
422
    return terrno;
×
423
  }
424
  return code;
34,652,898✔
425
#endif
426
}
427

428
int32_t taosThreadMutexLock(TdThreadMutex *mutex) {
1,117,630,493✔
429
#ifdef __USE_WIN_THREAD
430
  EnterCriticalSection(mutex);
431
  return 0;
432
#else
433
  int32_t code = pthread_mutex_lock(mutex);
1,117,630,493✔
434
  if (code) {
1,121,114,728✔
435
    terrno = TAOS_SYSTEM_ERROR(code);
537,191✔
436
    return terrno;
×
437
  }
438
  return code;
1,120,577,537✔
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,118,444,442✔
460
#ifdef __USE_WIN_THREAD
461
  LeaveCriticalSection(mutex);
462
  return 0;
463
#else
464
  int32_t code = pthread_mutex_unlock(mutex);
1,118,444,442✔
465
  if (code) {
1,121,721,428!
UNCOV
466
    terrno = TAOS_SYSTEM_ERROR(code);
×
467
    return terrno;
×
468
  }
469
  return code;
1,121,744,909✔
470
#endif
471
}
472

473
int32_t taosThreadMutexAttrDestroy(TdThreadMutexAttr *attr) {
33,795✔
474
#ifdef __USE_WIN_THREAD
475
  return 0;
476
#else
477
  int32_t code = pthread_mutexattr_destroy(attr);
33,795✔
478
  if (code) {
33,804!
UNCOV
479
    terrno = TAOS_SYSTEM_ERROR(code);
×
480
    return terrno;
×
481
  }
482
  return code;
33,807✔
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) {
33,804✔
519
#ifdef __USE_WIN_THREAD
520
  return 0;
521
#else
522
  int32_t code = pthread_mutexattr_init(attr);
33,804✔
523
  if (code) {
33,806!
524
    terrno = TAOS_SYSTEM_ERROR(code);
×
525
    return terrno;
×
526
  }
527
  return code;
33,810✔
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) {
33,802✔
549
#ifdef __USE_WIN_THREAD
550
  return 0;
551
#else
552
  int32_t code = pthread_mutexattr_settype(attr, kind);
33,802✔
553
  if (code) {
33,798✔
554
    terrno = TAOS_SYSTEM_ERROR(code);
1✔
555
    return terrno;
×
556
  }
557
  return code;
33,797✔
558
#endif
559
}
560

561
int32_t taosThreadOnce(TdThreadOnce *onceControl, void (*initRoutine)(void)) {
57,408,973✔
562
  int32_t code = pthread_once(onceControl, initRoutine);
57,408,973✔
563
  if (code) {
57,409,503!
UNCOV
564
    terrno = TAOS_SYSTEM_ERROR(code);
×
565
    return terrno;
×
566
  }
567
  return code;
57,410,917✔
568
}
569

570
int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
22,230,237✔
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);
22,230,237✔
578
  if (code) {
22,230,252!
UNCOV
579
    terrno = TAOS_SYSTEM_ERROR(code);
×
580
    return terrno;
×
581
  }
582
  return code;
22,230,254✔
583
#endif
584
}
585

586
int32_t taosThreadRwlockInit(TdThreadRwlock *rwlock, const TdThreadRwlockAttr *attr) {
22,232,088✔
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);
22,232,088✔
593
  if (code) {
22,229,514!
UNCOV
594
    terrno = TAOS_SYSTEM_ERROR(code);
×
595
    return terrno;
×
596
  }
597
  return code;
22,230,682✔
598
#endif
599
}
600

601
int32_t taosThreadRwlockRdlock(TdThreadRwlock *rwlock) {
621,743,717✔
602
#ifdef __USE_WIN_THREAD
603
  AcquireSRWLockShared(&rwlock->lock);
604
  return 0;
605
#else
606
  int32_t code = pthread_rwlock_rdlock(rwlock);
621,743,717✔
607
  if (code) {
622,471,837!
UNCOV
608
    terrno = TAOS_SYSTEM_ERROR(code);
×
609
    return terrno;
×
610
  }
611
  return code;
622,477,848✔
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,536,539,188✔
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,536,539,188✔
662
  if (code) {
1,537,195,009✔
663
    terrno = TAOS_SYSTEM_ERROR(code);
86,871✔
664
    return terrno;
×
665
  }
666
  return code;
1,537,108,138✔
667
#endif
668
}
669

670
int32_t taosThreadRwlockWrlock(TdThreadRwlock *rwlock) {
914,449,300✔
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);
914,449,300✔
677
  if (code) {
914,830,880!
UNCOV
678
    terrno = TAOS_SYSTEM_ERROR(code);
×
679
    return terrno;
×
680
  }
681
  return code;
914,832,547✔
682
#endif
683
}
684

685
int32_t taosThreadRwlockAttrDestroy(TdThreadRwlockAttr *attr) {
42,596✔
686
#ifdef __USE_WIN_THREAD
687
  return 0;
688
#else
689
  int32_t code = pthread_rwlockattr_destroy(attr);
42,596✔
690
  if (code) {
42,600!
UNCOV
691
    terrno = TAOS_SYSTEM_ERROR(code);
×
692
    return terrno;
×
693
  }
694
  return code;
42,604✔
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,608✔
713
#ifdef __USE_WIN_THREAD
714
  return 0;
715
#else
716
  int32_t code = pthread_rwlockattr_init(attr);
42,608✔
717
  if (code) {
42,612!
UNCOV
718
    terrno = TAOS_SYSTEM_ERROR(code);
×
719
    return terrno;
×
720
  }
721
  return code;
42,612✔
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) { 
195✔
768
  int32_t code = pthread_setspecific(key, value); 
195✔
769
  if (code) {
195!
770
    terrno = TAOS_SYSTEM_ERROR(code);
×
771
    return terrno;
×
772
  }
773
  return code;
195✔
774
}
775

776
int32_t taosThreadSpinDestroy(TdThreadSpinlock *lock) {
4,436,879✔
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,436,879✔
781
  if (code) {
4,436,953!
UNCOV
782
    terrno = TAOS_SYSTEM_ERROR(code);
×
783
    return terrno;
×
784
  }
785
  return code;
4,437,008✔
786
#endif
787
}
788

789
int32_t taosThreadSpinInit(TdThreadSpinlock *lock, int32_t pshared) {
4,415,415✔
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,415,415✔
795
  if (code) {
4,417,233!
796
    terrno = TAOS_SYSTEM_ERROR(code);
×
797
    return terrno;
×
798
  }
799
  return code;
4,418,059✔
800
#endif
801
}
802

803
int32_t taosThreadSpinLock(TdThreadSpinlock *lock) {
371,099✔
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,099✔
808
  if (code) {
371,221!
809
    terrno = TAOS_SYSTEM_ERROR(code);
×
810
    return terrno;
×
811
  }
812
  return code;
371,221✔
813
#endif
814
}
815

816
int32_t taosThreadSpinTrylock(TdThreadSpinlock *lock) {
1,046,727✔
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,046,727✔
821
  if (code && code != EBUSY) {
1,047,111!
822
    code = TAOS_SYSTEM_ERROR(code);
×
823
  }
824
  return code;
1,047,111✔
825
#endif
826
}
827

828
int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
1,418,004✔
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,418,004✔
833
  if (code) {
1,418,043✔
834
    terrno = TAOS_SYSTEM_ERROR(code);
7✔
835
    return terrno;
×
836
  }
837
  return code;
1,418,036✔
838
#endif
839
}
840

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

845
void taosThreadClear(TdThread *thread) { 
815,786✔
846
  (void)memset(thread, 0, sizeof(TdThread)); 
815,786✔
847
}
815,786✔
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