Skip to content

Commit

Permalink
remove dependency of DocumenStore on Server
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Jan 29, 2024
1 parent 17befd1 commit 8f0a621
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
25 changes: 15 additions & 10 deletions src/DocumentStore.zig
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ config: *const Config,
/// the DocumentStore assumes that `runtime_zig_version` is not modified while calling one of its functions.
runtime_zig_version: *const ?ZigVersionWrapper,
lock: std.Thread.RwLock = .{},
thread_pool: if (builtin.single_threaded) void else *std.Thread.Pool,
handles: std.StringArrayHashMapUnmanaged(*Handle) = .{},
build_files: std.StringArrayHashMapUnmanaged(*BuildFile) = .{},
cimports: std.AutoArrayHashMapUnmanaged(Hash, translate_c.Result) = .{},
Expand Down Expand Up @@ -671,6 +672,19 @@ pub fn invalidateBuildFile(self: *DocumentStore, build_file_uri: Uri) error{OutO
if (self.config.build_runner_path == null) return;
if (self.config.global_cache_path == null) return;

const uri = try self.allocator.dupe(u8, build_file_uri);
errdefer self.allocator.free(uri);

Check warning on line 676 in src/DocumentStore.zig

View check run for this annotation

Codecov / codecov/patch

src/DocumentStore.zig#L675-L676

Added lines #L675 - L676 were not covered by tests

if (builtin.single_threaded) {
self.invalidateBuildFileWorker(uri);
} else {
try self.thread_pool.spawn(invalidateBuildFileWorker, .{ self, uri });

Check warning on line 681 in src/DocumentStore.zig

View check run for this annotation

Codecov / codecov/patch

src/DocumentStore.zig#L681

Added line #L681 was not covered by tests
}
}

fn invalidateBuildFileWorker(self: *DocumentStore, build_file_uri: Uri) void {
defer self.allocator.free(build_file_uri);

Check warning on line 686 in src/DocumentStore.zig

View check run for this annotation

Codecov / codecov/patch

src/DocumentStore.zig#L685-L686

Added lines #L685 - L686 were not covered by tests

const build_config = loadBuildConfiguration(self, build_file_uri) catch |err| {
log.err("Failed to load build configuration for {s} (error: {})", .{ build_file_uri, err });
return;
Expand Down Expand Up @@ -1006,16 +1020,7 @@ fn createBuildFile(self: *DocumentStore, uri: Uri) error{OutOfMemory}!BuildFile
}

if (std.process.can_spawn) {
const Server = @import("Server.zig");
const server = @fieldParentPtr(Server, "document_store", self);

server.job_queue_lock.lock();
defer server.job_queue_lock.unlock();

try server.job_queue.ensureUnusedCapacity(1);
server.job_queue.writeItemAssumeCapacity(.{
.load_build_configuration = try server.allocator.dupe(u8, build_file.uri),
});
try self.invalidateBuildFile(build_file.uri);

Check warning on line 1023 in src/DocumentStore.zig

View check run for this annotation

Codecov / codecov/patch

src/DocumentStore.zig#L1023

Added line #L1023 was not covered by tests
}

return build_file;
Expand Down
42 changes: 10 additions & 32 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,12 @@ pub const Status = enum {
const Job = union(enum) {
incoming_message: std.json.Parsed(Message),
generate_diagnostics: DocumentStore.Uri,
load_build_configuration: DocumentStore.Uri,
run_build_on_save,

fn deinit(self: Job, allocator: std.mem.Allocator) void {
switch (self) {
.incoming_message => |parsed_message| parsed_message.deinit(),
.generate_diagnostics => |uri| allocator.free(uri),
.load_build_configuration => |uri| allocator.free(uri),
.run_build_on_save => {},
}
}
Expand All @@ -169,9 +167,7 @@ const Job = union(enum) {
return switch (self) {
.incoming_message => |parsed_message| if (parsed_message.value.isBlocking()) .exclusive else .shared,
.generate_diagnostics => .shared,
.load_build_configuration,
.run_build_on_save,
=> .atomic,
.run_build_on_save => .atomic,
};
}
};
Expand Down Expand Up @@ -669,23 +665,6 @@ fn registerCapability(server: *Server, method: []const u8) Error!void {
server.allocator.free(json_message);
}

fn invalidateAllBuildFiles(server: *Server) error{OutOfMemory}!void {
if (!std.process.can_spawn) return;

server.document_store.lock.lockShared();
defer server.document_store.lock.unlockShared();

server.job_queue_lock.lock();
defer server.job_queue_lock.unlock();

try server.job_queue.ensureUnusedCapacity(server.document_store.build_files.count());
for (server.document_store.build_files.keys()) |build_file_uri| {
server.job_queue.writeItemAssumeCapacity(.{
.load_build_configuration = try server.allocator.dupe(u8, build_file_uri),
});
}
}

fn requestConfiguration(server: *Server) Error!void {
if (server.recording_enabled) {
log.info("workspace/configuration are disabled during a recording session!", .{});
Expand Down Expand Up @@ -883,8 +862,12 @@ pub fn updateConfiguration(server: *Server, new_config: configuration.Configurat
server.runtime_zig_version = null;
}

if (new_zig_exe_path or new_build_runner_path) {
try server.invalidateAllBuildFiles();
if (new_zig_exe_path or new_build_runner_path) blk: {
if (!std.process.can_spawn) break :blk;

for (server.document_store.build_files.keys()) |build_file_uri| {
try server.document_store.invalidateBuildFile(build_file_uri);
}
}

if (new_zig_exe_path or new_zig_lib_path) {
Expand Down Expand Up @@ -1194,9 +1177,7 @@ fn saveDocumentHandler(server: *Server, arena: std.mem.Allocator, notification:
const uri = notification.textDocument.uri;

if (std.process.can_spawn and DocumentStore.isBuildFile(uri)) {
try server.pushJob(.{
.load_build_configuration = try server.allocator.dupe(u8, uri),
});
try server.document_store.invalidateBuildFile(uri);
}

if (std.process.can_spawn and server.config.enable_build_on_save) {
Expand Down Expand Up @@ -1768,6 +1749,7 @@ pub fn create(allocator: std.mem.Allocator) !*Server {
.allocator = allocator,
.config = &server.config,
.runtime_zig_version = &server.runtime_zig_version,
.thread_pool = if (zig_builtin.single_threaded) {} else undefined, // set below
},
.job_queue = std.fifo.LinearFifo(Job, .Dynamic).init(allocator),
.thread_pool = undefined, // set below
Expand All @@ -1781,6 +1763,7 @@ pub fn create(allocator: std.mem.Allocator) !*Server {
.allocator = allocator,
.n_jobs = 4, // what is a good value here?
});
server.document_store.thread_pool = &server.thread_pool;
}

server.ip = try InternPool.init(allocator);
Expand Down Expand Up @@ -2031,11 +2014,6 @@ fn processJob(server: *Server, job: Job, wait_group: ?*std.Thread.WaitGroup) voi
const json_message = server.sendToClientNotification("textDocument/publishDiagnostics", diagnostics) catch return;
server.allocator.free(json_message);
},
.load_build_configuration => |build_file_uri| {
std.debug.assert(std.process.can_spawn);
if (!std.process.can_spawn) return;
server.document_store.invalidateBuildFile(build_file_uri) catch return;
},
.run_build_on_save => {
std.debug.assert(std.process.can_spawn);
if (!std.process.can_spawn) return;
Expand Down

0 comments on commit 8f0a621

Please sign in to comment.