authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-07 23:23:17-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-07 23:23:30-04:00
log1abc904075ee37b059777869ab144854e4db0711
tree0199fcf3a8289c6c84f4fd9af880a147372d6236
parent166402c16bddccc364b9108a9e69af3a0dd6f1ab

InternPool: start documenting new thread-safe fields


1 files changed, 12 insertions(+), 0 deletions(-)

src/InternPool.zig+12
......@@ -2,10 +2,16 @@
22//! This data structure is self-contained, with the following exceptions:
33//! * Module.Namespace has a pointer to Module.File
44
5/// One item per thread, indexed by `tid`, which is dense and unique per thread.
56locals: []Local = &.{},
7/// Length must be a power of two and represents the number of simultaneous
8/// writers that can mutate any single sharded data structure.
69shards: []Shard = &.{},
10/// Cached number of active bits in a `tid`.
711tid_width: if (single_threaded) u0 else std.math.Log2Int(u32) = 0,
12/// Cached shift amount to put a `tid` in the top bits of a 31-bit value.
813tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
14/// Cached shift amount to put a `tid` in the top bits of a 32-bit value.
915tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
1016
1117/// Rather than allocating Decl objects with an Allocator, we instead allocate
......@@ -341,7 +347,11 @@ pub const DepEntry = extern struct {
341347};
342348
343349const Local = struct {
350 /// These fields can be accessed from any thread by calling `acquire`.
351 /// They are only modified by the owning thread.
344352 shared: Shared align(std.atomic.cache_line),
353 /// This state is fully local to the owning thread and does not require any
354 /// atomic access.
345355 mutate: struct {
346356 arena: std.heap.ArenaAllocator.State,
347357 items: Mutate,
......@@ -579,6 +589,7 @@ const Local = struct {
579589 const bytes_offset = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(Elem));
580590 const View = std.MultiArrayList(Elem);
581591
592 /// Must be called when accessing from another thread.
582593 fn acquire(list: *const ListSelf) ListSelf {
583594 return .{ .bytes = @atomicLoad([*]align(@alignOf(Elem)) u8, &list.bytes, .acquire) };
584595 }
......@@ -703,6 +714,7 @@ const Shard = struct {
703714 const alignment = @max(@alignOf(Header), @alignOf(Entry));
704715 const entries_offset = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(Entry));
705716
717 /// Must be called unless the mutate mutex is locked.
706718 fn acquire(map: *const @This()) @This() {
707719 return .{ .entries = @atomicLoad([*]Entry, &map.entries, .acquire) };
708720 }