Skip to content

Commit

Permalink
make dir watcher use std::thread instead of async (FuelLabs#4658)
Browse files Browse the repository at this point in the history
## Description
This PR changes how we are watching for directory changes in the
language server. Previously we were using tokio, this has now been
changed to use a regular thread and `std::mpsc::channel`. We are about
to move away from an `async` framework in the language server so this is
a small effort towards removing reliance on tokio.
  • Loading branch information
JoshuaBatty authored Jun 13, 2023
1 parent c1186df commit 060f276
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
19 changes: 14 additions & 5 deletions sway-lsp/src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ use dashmap::DashMap;
use forc_pkg as pkg;
use parking_lot::RwLock;
use pkg::{manifest::ManifestFile, Programs};
use std::{fs::File, io::Write, path::PathBuf, sync::Arc, vec};
use std::{
fs::File,
io::Write,
path::PathBuf,
sync::{atomic::Ordering, Arc},
vec,
};
use sway_core::{
decl_engine::DeclEngine,
language::{
Expand Down Expand Up @@ -99,10 +105,13 @@ impl Session {
}

pub fn shutdown(&self) {
// shutdown the thread watching the manifest file
let handle = self.sync.notify_join_handle.read();
if let Some(join_handle) = &*handle {
join_handle.abort();
// Set the should_end flag to true
self.sync.should_end.store(true, Ordering::Relaxed);

// Wait for the thread to finish
let mut join_handle_option = self.sync.notify_join_handle.write();
if let Some(join_handle) = std::mem::take(&mut *join_handle_option) {
let _ = join_handle.join();
}

// Delete the temporary directory.
Expand Down
19 changes: 12 additions & 7 deletions sway-lsp/src/core/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use std::{
fs::{self, File},
io::{Read, Write},
path::{Path, PathBuf},
sync::Arc,
sync::{atomic::AtomicBool, mpsc, Arc},
thread::JoinHandle,
time::Duration,
};
use sway_types::{SourceEngine, Span};
use tempfile::Builder;
Expand All @@ -27,7 +29,9 @@ pub enum Directory {
#[derive(Debug)]
pub struct SyncWorkspace {
pub directories: DashMap<Directory, PathBuf>,
pub notify_join_handle: RwLock<Option<tokio::task::JoinHandle<()>>>,
pub notify_join_handle: RwLock<Option<JoinHandle<()>>>,
// if we should shutdown the thread watching the manifest file
pub should_end: Arc<AtomicBool>,
}

impl SyncWorkspace {
Expand All @@ -37,6 +41,7 @@ impl SyncWorkspace {
Self {
directories: DashMap::new(),
notify_join_handle: RwLock::new(None),
should_end: Arc::new(AtomicBool::new(false)),
}
}

Expand Down Expand Up @@ -175,13 +180,13 @@ impl SyncWorkspace {
if let Some(temp_manifest_path) = self.temp_manifest_path() {
edit_manifest_dependency_paths(&manifest, &temp_manifest_path);

let handle = tokio::spawn(async move {
let (tx, mut rx) = tokio::sync::mpsc::channel(10);
let (tx, rx) = mpsc::channel();
let handle = std::thread::spawn(move || {
// Setup debouncer. No specific tickrate, max debounce time 2 seconds
let mut debouncer =
new_debouncer(std::time::Duration::from_secs(1), None, move |event| {
new_debouncer(Duration::from_secs(1), None, move |event| {
if let Ok(e) = event {
let _ = tx.blocking_send(e);
let _ = tx.send(e);
}
})
.unwrap();
Expand All @@ -191,7 +196,7 @@ impl SyncWorkspace {
.watch(manifest_dir.as_ref().path(), RecursiveMode::NonRecursive)
.unwrap();

while let Some(_events) = rx.recv().await {
while let Ok(_events) = rx.recv() {
// Rescan the Forc.toml and convert
// relative paths to absolute. Save into our temp directory.
edit_manifest_dependency_paths(&manifest, &temp_manifest_path);
Expand Down
4 changes: 2 additions & 2 deletions sway-lsp/src/utils/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,8 @@ fn extract_lit(tokens: TokenStream) -> String {
res
}

#[tokio::test]
async fn keywords_in_sync() {
#[test]
fn keywords_in_sync() {
let keyword_docs = KeywordDocs::new();
let lsp_keywords: Vec<_> = keyword_docs.keys().collect();
let compiler_keywords: Vec<_> = sway_parse::RESERVED_KEYWORDS
Expand Down

0 comments on commit 060f276

Please sign in to comment.