| ... | @@ -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.File | 3 | //! * Module.Namespace has a pointer to Module.File |
| 4 | | 4 | |
| | 5 | /// One item per thread, indexed by `tid`, which is dense and unique per thread. |
| 5 | locals: []Local = &.{}, | 6 | locals: []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. |
| 6 | shards: []Shard = &.{}, | 9 | shards: []Shard = &.{}, |
| | 10 | /// Cached number of active bits in a `tid`. |
| 7 | tid_width: if (single_threaded) u0 else std.math.Log2Int(u32) = 0, | 11 | tid_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. |
| 8 | tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31, | 13 | tid_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. |
| 9 | tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31, | 15 | tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31, |
| 10 | | 16 | |
| 11 | /// Rather than allocating Decl objects with an Allocator, we instead allocate | 17 | /// 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 | }; |
| 342 | | 348 | |
| 343 | const Local = struct { | 349 | const 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); |
| 581 | | 591 | |
| | 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)); |
| 705 | | 716 | |
| | 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 | } |