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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

83.62
/source/util/src/tlockfree.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 _DEFAULT_SOURCE
17
#include "tlockfree.h"
18

19
#define TD_RWLATCH_WRITE_FLAG 0x40000000
20

21
void taosInitRWLatch(SRWLatch *pLatch) { *pLatch = 0; }
2,969,957✔
22

23
void taosWLockLatch(SRWLatch *pLatch) {
141,527,712✔
24
  SRWLatch oLatch, nLatch;
25
  int32_t  nLoops = 0;
141,527,712✔
26

27
  // Set write flag
28
  while (1) {
29
    oLatch = atomic_load_32(pLatch);
2,147,483,647✔
30
    if (oLatch & TD_RWLATCH_WRITE_FLAG) {
2,147,483,647✔
31
      nLoops++;
2,147,483,647✔
32
      if (nLoops > 1000) {
2,147,483,647✔
33
        (void)sched_yield();
2,583,207✔
34
        nLoops = 0;
2,583,255✔
35
      }
36
      continue;
2,147,483,647✔
37
    }
38

39
    nLatch = oLatch | TD_RWLATCH_WRITE_FLAG;
141,520,421✔
40
    if (atomic_val_compare_exchange_32(pLatch, oLatch, nLatch) == oLatch) break;
141,520,421✔
41
  }
42

43
  // wait for all reads end
44
  nLoops = 0;
141,550,340✔
45
  while (1) {
46
    oLatch = atomic_load_32(pLatch);
343,075,244✔
47
    if (oLatch == TD_RWLATCH_WRITE_FLAG) break;
343,055,248✔
48
    nLoops++;
201,524,904✔
49
    if (nLoops > 1000) {
201,524,904✔
50
      (void)sched_yield();
191,906✔
51
      nLoops = 0;
191,906✔
52
    }
53
  }
54
}
141,530,344✔
55

56
void taosWWaitLockLatch(SRWLatch *pLatch) {
2,604✔
57
  SRWLatch oLatch, nLatch;
58
  int32_t  nLoops = 0;
2,604✔
59

60
  // Set write flag
61
  while (1) {
62
    oLatch = atomic_load_32(pLatch);
2,604✔
63
    if (oLatch & TD_RWLATCH_WRITE_FLAG) {
2,604!
64
      taosMsleep(1);
×
65
      continue;
×
66
    }
67

68
    nLatch = oLatch | TD_RWLATCH_WRITE_FLAG;
2,604✔
69
    if (atomic_val_compare_exchange_32(pLatch, oLatch, nLatch) == oLatch) break;
2,604!
70
  }
71

72
  // wait for all reads end
73
  nLoops = 0;
2,604✔
74
  while (1) {
75
    oLatch = atomic_load_32(pLatch);
2,852✔
76
    if (oLatch == TD_RWLATCH_WRITE_FLAG) break;
2,852✔
77
    taosMsleep(1);
248✔
78
  }
79
}
2,604✔
80

81
// no reentrant
82
int32_t taosWTryLockLatch(SRWLatch *pLatch) {
413✔
83
  SRWLatch oLatch, nLatch;
84
  oLatch = atomic_load_32(pLatch);
413✔
85
  if (oLatch) {
413!
86
    return -1;
×
87
  }
88

89
  nLatch = oLatch | TD_RWLATCH_WRITE_FLAG;
413✔
90
  if (atomic_val_compare_exchange_32(pLatch, oLatch, nLatch) == oLatch) {
413!
91
    return 0;
413✔
92
  }
93

94
  return -1;
×
95
}
96

97
int32_t taosWTryForceLockLatch(SRWLatch *pLatch) {
1,768✔
98
  SRWLatch oLatch, nLatch;
99

100
  while (1) {
101
    oLatch = atomic_load_32(pLatch);
1,768✔
102
    if (oLatch) {
1,768!
103
      nLatch = oLatch | TD_RWLATCH_WRITE_FLAG;
×
104
      if (atomic_val_compare_exchange_32(pLatch, oLatch, nLatch) == oLatch) {
×
105
        return -1;
×
106
      }
107
      
108
      continue;
×
109
    } else {
110
      nLatch = oLatch | TD_RWLATCH_WRITE_FLAG;
1,768✔
111
      if (atomic_val_compare_exchange_32(pLatch, oLatch, nLatch) == oLatch) {
1,768!
112
        return 0;
1,768✔
113
      }
114
      
115
      continue;
×
116
    }
117
  }
118

119
  return -1;
120
}
121

122
bool taosIsOnlyWLocked(SRWLatch *pLatch) {
159,769✔
123
  return TD_RWLATCH_WRITE_FLAG == atomic_load_32(pLatch);
159,769✔
124
}
125

126
bool taosHasRWWFlag(SRWLatch *pLatch) {
54,939✔
127
  return TD_RWLATCH_WRITE_FLAG & atomic_load_32(pLatch);
54,939✔
128
}
129

130
void taosWUnLockLatch(SRWLatch *pLatch) { atomic_store_32(pLatch, 0); }
141,517,846✔
131

132
void taosRLockLatch(SRWLatch *pLatch) {
178,267,945✔
133
  SRWLatch oLatch, nLatch;
134
  int32_t  nLoops = 0;
178,267,945✔
135

136
  while (1) {
137
    oLatch = atomic_load_32(pLatch);
249,354,021✔
138
    if (oLatch & TD_RWLATCH_WRITE_FLAG) {
249,282,112✔
139
      nLoops++;
71,011,419✔
140
      if (nLoops > 1000) {
71,011,419✔
141
        (void)sched_yield();
66,350✔
142
        nLoops = 0;
66,356✔
143
      }
144
      continue;
71,011,425✔
145
    }
146

147
    nLatch = oLatch + 1;
178,270,693✔
148
    if (atomic_val_compare_exchange_32(pLatch, oLatch, nLatch) == oLatch) break;
178,270,693✔
149
  }
150
}
178,368,386✔
151

152
// no reentrant
153
int32_t taosRTryLockLatch(SRWLatch *pLatch) {
199,763✔
154
  SRWLatch oLatch, nLatch;
155
  int32_t  nLoops = 0;
199,763✔
156

157
  while (1) {
158
    oLatch = atomic_load_32(pLatch);
199,803✔
159
    if (oLatch & TD_RWLATCH_WRITE_FLAG) {
199,771!
160
      return -1;
×
161
    }
162

163
    nLatch = oLatch + 1;
199,771✔
164
    if (atomic_val_compare_exchange_32(pLatch, oLatch, nLatch) == oLatch) {
199,771✔
165
      break;
199,816✔
166
    }
167
  }
168

169
  return 0;
199,816✔
170
}
171

172
void taosRUnLockLatch(SRWLatch *pLatch) { (void)atomic_sub_fetch_32(pLatch, 1); }
178,327,575✔
173

174
int32_t taosRUnLockLatch_r(SRWLatch *pLatch) { return atomic_sub_fetch_32(pLatch, 1); }
159,687✔
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