2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2018-07-09 19:22:44 -07:00
|
|
|
const Lock = std.event.Lock;
|
|
|
|
|
|
|
|
/// Thread-safe async/await lock that protects one piece of data.
|
2019-08-13 11:14:19 -07:00
|
|
|
/// Functions which are waiting for the lock are suspended, and
|
2018-07-09 19:22:44 -07:00
|
|
|
/// are resumed when the lock is released, in order.
|
|
|
|
pub fn Locked(comptime T: type) type {
|
2018-11-13 05:08:37 -08:00
|
|
|
return struct {
|
2018-07-09 19:22:44 -07:00
|
|
|
lock: Lock,
|
|
|
|
private_data: T,
|
|
|
|
|
2018-09-13 13:34:33 -07:00
|
|
|
const Self = @This();
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const HeldLock = struct {
|
2018-07-09 19:22:44 -07:00
|
|
|
value: *T,
|
|
|
|
held: Lock.Held,
|
|
|
|
|
|
|
|
pub fn release(self: HeldLock) void {
|
|
|
|
self.held.release();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-06 11:41:35 -08:00
|
|
|
pub fn init(data: T) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2019-11-06 11:41:35 -08:00
|
|
|
.lock = Lock.init(),
|
2018-07-09 19:22:44 -07:00
|
|
|
.private_data = data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
self.lock.deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn acquire(self: *Self) HeldLock {
|
2018-11-13 05:08:37 -08:00
|
|
|
return HeldLock{
|
2019-05-12 09:56:01 -07:00
|
|
|
// TODO guaranteed allocation elision
|
2019-11-06 11:41:35 -08:00
|
|
|
.held = self.lock.acquire(),
|
2018-07-09 19:22:44 -07:00
|
|
|
.value = &self.private_data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|