mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 15:13:08 +00:00
* move concurrency primitives that always operate on kernel threads to the std.Thread namespace * remove std.SpinLock. Nobody should use this in a non-freestanding environment; the other primitives are always preferable. In freestanding, it will be necessary to put custom spin logic in there, so there are no use cases for a std lib version. * move some std lib files to the top level fields convention * add std.Thread.spinLoopHint * add std.Thread.Condition * add std.Thread.Semaphore * new implementation of std.Thread.Mutex for Windows and non-pthreads Linux * add std.Thread.RwLock Implementations provided by @kprotty
62 lines
1.3 KiB
Zig
62 lines
1.3 KiB
Zig
// SPDX-License-Identifier: MIT
|
|
// Copyright (c) 2015-2020 Zig Contributors
|
|
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
|
|
// The MIT license requires this copyright notice to be included in all copies
|
|
// and substantial portions of the software.
|
|
const std = @import("std");
|
|
const WaitGroup = @This();
|
|
|
|
lock: std.Thread.Mutex = .{},
|
|
counter: usize = 0,
|
|
event: std.Thread.ResetEvent,
|
|
|
|
pub fn init(self: *WaitGroup) !void {
|
|
self.* = .{
|
|
.lock = .{},
|
|
.counter = 0,
|
|
.event = undefined,
|
|
};
|
|
try self.event.init();
|
|
}
|
|
|
|
pub fn deinit(self: *WaitGroup) void {
|
|
self.event.deinit();
|
|
self.* = undefined;
|
|
}
|
|
|
|
pub fn start(self: *WaitGroup) void {
|
|
const held = self.lock.acquire();
|
|
defer held.release();
|
|
|
|
self.counter += 1;
|
|
}
|
|
|
|
pub fn finish(self: *WaitGroup) void {
|
|
const held = self.lock.acquire();
|
|
defer held.release();
|
|
|
|
self.counter -= 1;
|
|
|
|
if (self.counter == 0) {
|
|
self.event.set();
|
|
}
|
|
}
|
|
|
|
pub fn wait(self: *WaitGroup) void {
|
|
while (true) {
|
|
const held = self.lock.acquire();
|
|
|
|
if (self.counter == 0) {
|
|
held.release();
|
|
return;
|
|
}
|
|
|
|
held.release();
|
|
self.event.wait();
|
|
}
|
|
}
|
|
|
|
pub fn reset(self: *WaitGroup) void {
|
|
self.event.reset();
|
|
}
|