mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 07:32:44 +00:00
d29871977f
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.
58 lines
1.5 KiB
Zig
58 lines
1.5 KiB
Zig
const std = @import("../std.zig");
|
|
const RwLock = std.event.RwLock;
|
|
|
|
/// Thread-safe async/await RW 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 RwLocked(comptime T: type) type {
|
|
return struct {
|
|
lock: RwLock,
|
|
locked_data: T,
|
|
|
|
const Self = @This();
|
|
|
|
pub const HeldReadLock = struct {
|
|
value: *const T,
|
|
held: RwLock.HeldRead,
|
|
|
|
pub fn release(self: HeldReadLock) void {
|
|
self.held.release();
|
|
}
|
|
};
|
|
|
|
pub const HeldWriteLock = struct {
|
|
value: *T,
|
|
held: RwLock.HeldWrite,
|
|
|
|
pub fn release(self: HeldWriteLock) void {
|
|
self.held.release();
|
|
}
|
|
};
|
|
|
|
pub fn init(data: T) Self {
|
|
return Self{
|
|
.lock = RwLock.init(),
|
|
.locked_data = data,
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
self.lock.deinit();
|
|
}
|
|
|
|
pub fn acquireRead(self: *Self) callconv(.Async) HeldReadLock {
|
|
return HeldReadLock{
|
|
.held = self.lock.acquireRead(),
|
|
.value = &self.locked_data,
|
|
};
|
|
}
|
|
|
|
pub fn acquireWrite(self: *Self) callconv(.Async) HeldWriteLock {
|
|
return HeldWriteLock{
|
|
.held = self.lock.acquireWrite(),
|
|
.value = &self.locked_data,
|
|
};
|
|
}
|
|
};
|
|
}
|