| ... | ... | @@ -100,7 +100,7 @@ pub fn Queue(comptime T: type) type { |
| 100 | 100 | pub fn isEmpty(self: *Self) bool { |
| 101 | 101 | const held = self.mutex.acquire(); |
| 102 | 102 | defer held.release(); |
| 103 | | return self.head != null; |
| 103 | return self.head == null; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | pub fn dump(self: *Self) void { |
| ... | ... | @@ -172,12 +172,14 @@ test "std.atomic.Queue" { |
| 172 | 172 | }; |
| 173 | 173 | |
| 174 | 174 | if (builtin.single_threaded) { |
| 175 | expect(context.queue.isEmpty()); |
| 175 | 176 | { |
| 176 | 177 | var i: usize = 0; |
| 177 | 178 | while (i < put_thread_count) : (i += 1) { |
| 178 | 179 | expect(startPuts(&context) == 0); |
| 179 | 180 | } |
| 180 | 181 | } |
| 182 | expect(!context.queue.isEmpty()); |
| 181 | 183 | context.puts_done = 1; |
| 182 | 184 | { |
| 183 | 185 | var i: usize = 0; |
| ... | ... | @@ -185,7 +187,10 @@ test "std.atomic.Queue" { |
| 185 | 187 | expect(startGets(&context) == 0); |
| 186 | 188 | } |
| 187 | 189 | } |
| 190 | expect(context.queue.isEmpty()); |
| 188 | 191 | } else { |
| 192 | expect(context.queue.isEmpty()); |
| 193 | |
| 189 | 194 | var putters: [put_thread_count]*std.Thread = undefined; |
| 190 | 195 | for (putters) |*t| { |
| 191 | 196 | t.* = try std.Thread.spawn(&context, startPuts); |
| ... | ... | @@ -200,6 +205,8 @@ test "std.atomic.Queue" { |
| 200 | 205 | _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst); |
| 201 | 206 | for (getters) |t| |
| 202 | 207 | t.wait(); |
| 208 | |
| 209 | expect(context.queue.isEmpty()); |
| 203 | 210 | } |
| 204 | 211 | |
| 205 | 212 | if (context.put_sum != context.get_sum) { |
| ... | ... | @@ -250,6 +257,7 @@ fn startGets(ctx: *Context) u8 { |
| 250 | 257 | |
| 251 | 258 | test "std.atomic.Queue single-threaded" { |
| 252 | 259 | var queue = Queue(i32).init(); |
| 260 | expect(queue.isEmpty()); |
| 253 | 261 | |
| 254 | 262 | var node_0 = Queue(i32).Node{ |
| 255 | 263 | .data = 0, |
| ... | ... | @@ -257,6 +265,7 @@ test "std.atomic.Queue single-threaded" { |
| 257 | 265 | .prev = undefined, |
| 258 | 266 | }; |
| 259 | 267 | queue.put(&node_0); |
| 268 | expect(!queue.isEmpty()); |
| 260 | 269 | |
| 261 | 270 | var node_1 = Queue(i32).Node{ |
| 262 | 271 | .data = 1, |
| ... | ... | @@ -264,8 +273,10 @@ test "std.atomic.Queue single-threaded" { |
| 264 | 273 | .prev = undefined, |
| 265 | 274 | }; |
| 266 | 275 | queue.put(&node_1); |
| 276 | expect(!queue.isEmpty()); |
| 267 | 277 | |
| 268 | 278 | expect(queue.get().?.data == 0); |
| 279 | expect(!queue.isEmpty()); |
| 269 | 280 | |
| 270 | 281 | var node_2 = Queue(i32).Node{ |
| 271 | 282 | .data = 2, |
| ... | ... | @@ -273,6 +284,7 @@ test "std.atomic.Queue single-threaded" { |
| 273 | 284 | .prev = undefined, |
| 274 | 285 | }; |
| 275 | 286 | queue.put(&node_2); |
| 287 | expect(!queue.isEmpty()); |
| 276 | 288 | |
| 277 | 289 | var node_3 = Queue(i32).Node{ |
| 278 | 290 | .data = 3, |
| ... | ... | @@ -280,10 +292,13 @@ test "std.atomic.Queue single-threaded" { |
| 280 | 292 | .prev = undefined, |
| 281 | 293 | }; |
| 282 | 294 | queue.put(&node_3); |
| 295 | expect(!queue.isEmpty()); |
| 283 | 296 | |
| 284 | 297 | expect(queue.get().?.data == 1); |
| 298 | expect(!queue.isEmpty()); |
| 285 | 299 | |
| 286 | 300 | expect(queue.get().?.data == 2); |
| 301 | expect(!queue.isEmpty()); |
| 287 | 302 | |
| 288 | 303 | var node_4 = Queue(i32).Node{ |
| 289 | 304 | .data = 4, |
| ... | ... | @@ -291,13 +306,17 @@ test "std.atomic.Queue single-threaded" { |
| 291 | 306 | .prev = undefined, |
| 292 | 307 | }; |
| 293 | 308 | queue.put(&node_4); |
| 309 | expect(!queue.isEmpty()); |
| 294 | 310 | |
| 295 | 311 | expect(queue.get().?.data == 3); |
| 296 | 312 | node_3.next = null; |
| 313 | expect(!queue.isEmpty()); |
| 297 | 314 | |
| 298 | 315 | expect(queue.get().?.data == 4); |
| 316 | expect(queue.isEmpty()); |
| 299 | 317 | |
| 300 | 318 | expect(queue.get() == null); |
| 319 | expect(queue.isEmpty()); |
| 301 | 320 | } |
| 302 | 321 | |
| 303 | 322 | test "std.atomic.Queue dump" { |