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 @@...@@ -2,10 +2,16 @@
2//! This data structure is self-contained, with the following exceptions:2//! This data structure is self-contained, with the following exceptions:
3//! * Module.Namespace has a pointer to Module.File3//! * Module.Namespace has a pointer to Module.File
44
5/// One item per thread, indexed by `tid`, which is dense and unique per thread.
5locals: []Local = &.{},6locals: []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.
6shards: []Shard = &.{},9shards: []Shard = &.{},
10/// Cached number of active bits in a `tid`.
7tid_width: if (single_threaded) u0 else std.math.Log2Int(u32) = 0,11tid_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.
8tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,13tid_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.
9tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,15tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
1016
11/// Rather than allocating Decl objects with an Allocator, we instead allocate17/// Rather than allocating Decl objects with an Allocator, we instead allocate
...@@ -341,7 +347,11 @@ pub const DepEntry = extern struct {...@@ -341,7 +347,11 @@ pub const DepEntry = extern struct {
341};347};
342348
343const Local = struct {349const Local = struct {
350 /// These fields can be accessed from any thread by calling `acquire`.
351 /// They are only modified by the owning thread.
344 shared: Shared align(std.atomic.cache_line),352 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.
345 mutate: struct {355 mutate: struct {
346 arena: std.heap.ArenaAllocator.State,356 arena: std.heap.ArenaAllocator.State,
347 items: Mutate,357 items: Mutate,
...@@ -579,6 +589,7 @@ const Local = struct {...@@ -579,6 +589,7 @@ const Local = struct {
579 const bytes_offset = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(Elem));589 const bytes_offset = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(Elem));
580 const View = std.MultiArrayList(Elem);590 const View = std.MultiArrayList(Elem);
581591
592 /// Must be called when accessing from another thread.
582 fn acquire(list: *const ListSelf) ListSelf {593 fn acquire(list: *const ListSelf) ListSelf {
583 return .{ .bytes = @atomicLoad([*]align(@alignOf(Elem)) u8, &list.bytes, .acquire) };594 return .{ .bytes = @atomicLoad([*]align(@alignOf(Elem)) u8, &list.bytes, .acquire) };
584 }595 }
...@@ -703,6 +714,7 @@ const Shard = struct {...@@ -703,6 +714,7 @@ const Shard = struct {
703 const alignment = @max(@alignOf(Header), @alignOf(Entry));714 const alignment = @max(@alignOf(Header), @alignOf(Entry));
704 const entries_offset = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(Entry));715 const entries_offset = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(Entry));
705716
717 /// Must be called unless the mutate mutex is locked.
706 fn acquire(map: *const @This()) @This() {718 fn acquire(map: *const @This()) @This() {
707 return .{ .entries = @atomicLoad([*]Entry, &map.entries, .acquire) };719 return .{ .entries = @atomicLoad([*]Entry, &map.entries, .acquire) };
708 }720 }