mirror of
https://github.com/ziglang/zig.git
synced 2025-12-26 16:13:07 +00:00
* split std.ResetEvent into:
- ResetEvent - requires init() at runtime and it can fail. Also
requires deinit().
- StaticResetEvent - can be statically initialized and requires no
deinitialization. Initialization cannot fail.
* the POSIX sem_t implementation can in fact fail on initialization
because it is allowed to be implemented as a file descriptor.
* Completely define, clarify, and explain in detail the semantics of
these APIs. Remove the `isSet` function.
* `ResetEvent.timedWait` returns an enum instead of a possible error.
* `ResetEvent.init` takes a pointer to the ResetEvent instead of
returning a copy.
* On Darwin, `ResetEvent` is implemented using Grand Central Dispatch,
which is exposed by libSystem.
stage2 changes:
* ThreadPool: use a single, pre-initialized `ResetEvent` per worker.
* WaitGroup: now requires init() and deinit() and init() can fail.
- Add a `reset` function.
- Compilation initializes one for the work queue in creation and
re-uses it for every update.
- Rename `stop` to `finish`.
- Simplify the implementation based on the usage pattern.
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.Mutex = .{},
|
|
counter: usize = 0,
|
|
event: std.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();
|
|
}
|