authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-13 22:54:16-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-13 22:54:16-07:00
loge5aef96293336fa25e6f094bf82d189176e8d1a7
tree197612b613650d003c226b7e772cd470d5953f8e
parent97ea5da18d0a26d55447082d37f6a0945da42d1f

stage2: CRT files retain locks on the build artifacts


4 files changed, 42 insertions(+), 25 deletions(-)

src-self-hosted/Compilation.zig+14-3
......@@ -70,13 +70,24 @@ libc_static_lib: ?[]const u8 = null,
7070/// For example `Scrt1.o` and `libc.so.6`. These are populated after building libc from source,
7171/// The set of needed CRT (C runtime) files differs depending on the target and compilation settings.
7272/// The key is the basename, and the value is the absolute path to the completed build artifact.
73crt_files: std.StringHashMapUnmanaged([]const u8) = .{},
73crt_files: std.StringHashMapUnmanaged(CRTFile) = .{},
7474
7575/// Keeping track of this possibly open resource so we can close it later.
7676owned_link_dir: ?std.fs.Dir,
7777
7878pub const InnerError = Module.InnerError;
7979
80pub const CRTFile = struct {
81 lock: std.cache_hash.Lock,
82 full_object_path: []const u8,
83
84 fn deinit(self: *CRTFile, gpa: *Allocator) void {
85 self.lock.release();
86 gpa.free(self.full_object_path);
87 self.* = undefined;
88 }
89};
90
8091/// For passing to a C compiler.
8192pub const CSourceFile = struct {
8293 src_path: []const u8,
......@@ -625,7 +636,7 @@ pub fn destroy(self: *Compilation) void {
625636 var it = self.crt_files.iterator();
626637 while (it.next()) |entry| {
627638 gpa.free(entry.key);
628 gpa.free(entry.value);
639 entry.value.deinit(gpa);
629640 }
630641 self.crt_files.deinit(gpa);
631642 }
......@@ -1512,7 +1523,7 @@ fn detectLibCFromLibCInstallation(arena: *Allocator, target: Target, lci: *const
15121523
15131524pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []const u8) ![]const u8 {
15141525 if (comp.wantBuildGLibCFromSource()) {
1515 return comp.crt_files.get(basename).?;
1526 return comp.crt_files.get(basename).?.full_object_path;
15161527 }
15171528 const lci = comp.bin_file.options.libc_installation orelse return error.LibCInstallationNotAvailable;
15181529 const crt_dir_path = lci.crt_dir orelse return error.LibCInstallationMissingCRTDir;
src-self-hosted/glibc.zig+4-1
......@@ -648,5 +648,8 @@ fn build_libc_object(comp: *Compilation, basename: []const u8, c_source_file: Co
648648 try comp.gpa.dupe(u8, basename);
649649
650650 // TODO obtain a lock on the artifact and put that in crt_files as well.
651 comp.crt_files.putAssumeCapacityNoClobber(basename, artifact_path);
651 comp.crt_files.putAssumeCapacityNoClobber(basename, .{
652 .full_object_path = artifact_path,
653 .lock = sub_compilation.bin_file.toOwnedLock(),
654 });
652655}
src-self-hosted/link.zig+18
......@@ -90,6 +90,10 @@ pub const File = struct {
9090 /// this location, and then this path can be placed on the LLD linker line.
9191 intermediary_basename: ?[]const u8 = null,
9292
93 /// Prevents other processes from clobbering files in the output directory
94 /// of this linking operation.
95 lock: ?std.cache_hash.Lock = null,
96
9397 pub const LinkBlock = union {
9498 elf: Elf.TextBlock,
9599 coff: Coff.TextBlock,
......@@ -228,7 +232,21 @@ pub const File = struct {
228232 }
229233 }
230234
235 pub fn releaseLock(self: *File) void {
236 if (self.lock) |*lock| {
237 lock.release();
238 self.lock = null;
239 }
240 }
241
242 pub fn toOwnedLock(self: *File) std.cache_hash.Lock {
243 const lock = self.lock.?;
244 self.lock = null;
245 return lock;
246 }
247
231248 pub fn destroy(base: *File) void {
249 base.releaseLock();
232250 if (base.file) |f| f.close();
233251 if (base.intermediary_basename) |sub_path| base.allocator.free(sub_path);
234252 switch (base.tag) {
src-self-hosted/link/Elf.zig+6-21
......@@ -123,9 +123,6 @@ dbg_info_decl_free_list: std.AutoHashMapUnmanaged(*TextBlock, void) = .{},
123123dbg_info_decl_first: ?*TextBlock = null,
124124dbg_info_decl_last: ?*TextBlock = null,
125125
126/// Prevents other processes from clobbering the output file this is linking.
127lock: ?std.cache_hash.Lock = null,
128
129126/// `alloc_num / alloc_den` is the factor of padding when allocating.
130127const alloc_num = 4;
131128const alloc_den = 3;
......@@ -290,21 +287,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Elf {
290287 return self;
291288}
292289
293pub fn releaseLock(self: *Elf) void {
294 if (self.lock) |*lock| {
295 lock.release();
296 self.lock = null;
297 }
298}
299
300pub fn toOwnedLock(self: *Elf) std.cache_hash.Lock {
301 const lock = self.lock.?;
302 self.lock = null;
303 return lock;
304}
305
306290pub fn deinit(self: *Elf) void {
307 self.releaseLock();
308291 self.sections.deinit(self.base.allocator);
309292 self.program_headers.deinit(self.base.allocator);
310293 self.shstrtab.deinit(self.base.allocator);
......@@ -1253,7 +1236,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
12531236 const id_symlink_basename = "id.txt";
12541237
12551238 // We are about to obtain this lock, so here we give other processes a chance first.
1256 self.releaseLock();
1239 self.base.releaseLock();
12571240
12581241 var ch = comp.cache_parent.obtain();
12591242 defer ch.deinit();
......@@ -1302,14 +1285,16 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13021285 const digest = ch.final();
13031286
13041287 var prev_digest_buf: [digest.len]u8 = undefined;
1305 const prev_digest: []u8 = directory.handle.readLink(id_symlink_basename, &prev_digest_buf) catch blk: {
1288 const prev_digest: []u8 = directory.handle.readLink(id_symlink_basename, &prev_digest_buf) catch |err| blk: {
1289 log.debug("ELF LLD new_digest={} readlink error: {}", .{digest, @errorName(err)});
13061290 // Handle this as a cache miss.
13071291 mem.set(u8, &prev_digest_buf, 0);
13081292 break :blk &prev_digest_buf;
13091293 };
1294 log.debug("ELF LLD prev_digest={} new_digest={}", .{prev_digest, digest});
13101295 if (mem.eql(u8, prev_digest, &digest)) {
13111296 // Hot diggity dog! The output binary is already there.
1312 self.lock = ch.toOwnedLock();
1297 self.base.lock = ch.toOwnedLock();
13131298 return;
13141299 }
13151300
......@@ -1611,7 +1596,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16111596 };
16121597 // We hang on to this lock so that the output file path can be used without
16131598 // other processes clobbering it.
1614 self.lock = ch.toOwnedLock();
1599 self.base.lock = ch.toOwnedLock();
16151600}
16161601
16171602fn append_diagnostic(context: usize, ptr: [*]const u8, len: usize) callconv(.C) void {