mirror of
https://github.com/ziglang/zig.git
synced 2025-12-07 14:53:08 +00:00
We already have a LICENSE file that covers the Zig Standard Library. We no longer need to remind everyone that the license is MIT in every single file. Previously this was introduced to clarify the situation for a fork of Zig that made Zig's LICENSE file harder to find, and replaced it with their own license that required annual payments to their company. However that fork now appears to be dead. So there is no need to reinforce the copyright notice in every single file.
43 lines
1.1 KiB
Zig
43 lines
1.1 KiB
Zig
const std = @import("../std.zig");
|
|
const Lock = std.event.Lock;
|
|
|
|
/// Thread-safe async/await lock that protects one piece of data.
|
|
/// Functions which are waiting for the lock are suspended, and
|
|
/// are resumed when the lock is released, in order.
|
|
pub fn Locked(comptime T: type) type {
|
|
return struct {
|
|
lock: Lock,
|
|
private_data: T,
|
|
|
|
const Self = @This();
|
|
|
|
pub const HeldLock = struct {
|
|
value: *T,
|
|
held: Lock.Held,
|
|
|
|
pub fn release(self: HeldLock) void {
|
|
self.held.release();
|
|
}
|
|
};
|
|
|
|
pub fn init(data: T) Self {
|
|
return Self{
|
|
.lock = Lock.init(),
|
|
.private_data = data,
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
self.lock.deinit();
|
|
}
|
|
|
|
pub fn acquire(self: *Self) callconv(.Async) HeldLock {
|
|
return HeldLock{
|
|
// TODO guaranteed allocation elision
|
|
.held = self.lock.acquire(),
|
|
.value = &self.private_data,
|
|
};
|
|
}
|
|
};
|
|
}
|