Skip to content

Commit d8f9d56

Browse files
committed
Remove remaining r_lock! contexts
1 parent 82a992b commit d8f9d56

File tree

5 files changed

+13
-198
lines changed

5 files changed

+13
-198
lines changed

crates/ark/src/interface.rs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use std::sync::Arc;
1818
use std::sync::Mutex;
1919
use std::sync::Once;
2020
use std::time::Duration;
21-
use std::time::SystemTime;
2221

2322
use amalthea::events::BusyEvent;
2423
use amalthea::events::PositronEvent;
@@ -49,8 +48,6 @@ use harp::exec::r_source;
4948
use harp::exec::RFunction;
5049
use harp::exec::RFunctionExt;
5150
use harp::interrupts::RInterruptsSuspendedScope;
52-
use harp::lock::R_RUNTIME_LOCK;
53-
use harp::lock::R_RUNTIME_LOCK_COUNT;
5451
use harp::object::RObject;
5552
use harp::r_safely;
5653
use harp::r_symbol;
@@ -61,7 +58,6 @@ use harp::utils::r_is_data_frame;
6158
use libR_sys::*;
6259
use log::*;
6360
use nix::sys::signal::*;
64-
use parking_lot::ReentrantMutexGuard;
6561
use serde_json::json;
6662
use stdext::result::ResultOrLog;
6763
use stdext::*;
@@ -267,12 +263,10 @@ pub struct RMain {
267263
stderr: String,
268264
banner: String,
269265

270-
/// A lock guard, used to manage access to the R runtime. The main
271-
/// thread holds the lock by default, but releases it at opportune
272-
/// times to allow the LSP to access the R runtime where appropriate.
273-
runtime_lock_guard: Option<ReentrantMutexGuard<'static, ()>>,
266+
/// The ID of the R thread
274267
pub thread_id: std::thread::ThreadId,
275268

269+
/// Channel to receive tasks from `r_task()`
276270
pub tasks_tx: Sender<RTaskMain>,
277271
tasks_rx: Receiver<RTaskMain>,
278272

@@ -355,10 +349,6 @@ impl RMain {
355349
kernel_init_tx: Bus<KernelInfo>,
356350
dap: Arc<Mutex<Dap>>,
357351
) -> Self {
358-
// The main thread owns the R runtime lock by default, but releases
359-
// it when appropriate to give other threads a chance to execute.
360-
let lock_guard = unsafe { R_RUNTIME_LOCK.lock() };
361-
362352
// Channel to receive tasks from auxiliary threads via `r_task()`
363353
let (tasks_tx, tasks_rx) = unbounded::<RTaskMain>();
364354

@@ -373,7 +363,6 @@ impl RMain {
373363
stdout: String::new(),
374364
stderr: String::new(),
375365
banner: String::new(),
376-
runtime_lock_guard: Some(lock_guard),
377366
kernel,
378367
error_occurred: false,
379368
error_message: String::new(),
@@ -597,8 +586,7 @@ impl RMain {
597586
// descriptors that R has open and select() on those for
598587
// available data?
599588
loop {
600-
// Release the R runtime lock while we're waiting for input.
601-
self.runtime_lock_guard = None;
589+
// Yield to auxiliary threads.
602590
self.run_one_task();
603591

604592
// FIXME: Race between interrupt and new code request. To fix
@@ -640,9 +628,6 @@ impl RMain {
640628
},
641629
};
642630

643-
// Take back the lock after we've received some console input.
644-
unsafe { self.runtime_lock_guard = Some(R_RUNTIME_LOCK.lock()) };
645-
646631
if Self::process_interrupts(&info) {
647632
return (0, true);
648633
}
@@ -670,8 +655,6 @@ impl RMain {
670655
}
671656
},
672657
Err(err) => {
673-
unsafe { self.runtime_lock_guard = Some(R_RUNTIME_LOCK.lock()) };
674-
675658
use RecvTimeoutError::*;
676659
match err {
677660
Timeout => {
@@ -879,27 +862,6 @@ impl RMain {
879862
fn polled_events(&mut self) {
880863
// Check for pending tasks.
881864
self.run_one_task();
882-
883-
let count = R_RUNTIME_LOCK_COUNT.load(std::sync::atomic::Ordering::Acquire);
884-
if count == 0 {
885-
return;
886-
}
887-
888-
info!(
889-
"{} thread(s) are waiting; the main thread is releasing the R runtime lock.",
890-
count
891-
);
892-
let now = SystemTime::now();
893-
894-
// `bump()` does a fair unlock, giving other threads
895-
// waiting for the lock a chance to acquire it, and then
896-
// relocks it.
897-
ReentrantMutexGuard::bump(self.runtime_lock_guard.as_mut().unwrap());
898-
899-
info!(
900-
"The main thread re-acquired the R runtime lock after {} milliseconds.",
901-
now.elapsed().unwrap().as_millis()
902-
);
903865
}
904866

905867
unsafe fn process_events() {

crates/harp/src/exec.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use libR_sys::*;
1515

1616
use crate::error::Error;
1717
use crate::error::Result;
18-
use crate::lock::r_polled_events_disabled;
19-
use crate::lock::R_PolledEvents;
2018
use crate::object::RObject;
2119
use crate::protect::RProtect;
2220
use crate::r_string;
@@ -27,6 +25,13 @@ use crate::utils::r_typeof;
2725
use crate::vector::CharacterVector;
2826
use crate::vector::Vector;
2927

28+
extern "C" {
29+
pub static mut R_PolledEvents: Option<unsafe extern "C" fn()>;
30+
}
31+
32+
#[no_mangle]
33+
pub extern "C" fn r_polled_events_disabled() {}
34+
3035
extern "C" {
3136
pub static R_ParseError: c_int;
3237
pub static R_ParseErrorMsg: [c_char; 256usize];

crates/harp/src/lib.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub mod eval;
1111
pub mod exec;
1212
pub mod external_ptr;
1313
pub mod interrupts;
14-
pub mod lock;
1514
pub mod object;
1615
pub mod protect;
1716
pub mod r_version;
@@ -26,21 +25,7 @@ pub mod vector;
2625

2726
pub use harp_macros::register;
2827

29-
pub fn initialize() {
30-
lock::initialize();
31-
}
32-
33-
#[macro_export]
34-
macro_rules! r_lock {
35-
36-
($($expr:tt)*) => {{
37-
#[allow(unused_unsafe)]
38-
$crate::lock::with_r_lock(|| {
39-
unsafe { $($expr)* } }
40-
)
41-
}}
42-
43-
}
28+
pub fn initialize() {}
4429

4530
#[macro_export]
4631
macro_rules! r_safely {

crates/harp/src/lock.rs

Lines changed: 0 additions & 120 deletions
This file was deleted.

crates/harp/src/test.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::sync::Once;
2020
use libR_sys::*;
2121
use stdext::cargs;
2222

23-
use crate::lock::with_r_lock;
23+
use crate::r_safely;
2424

2525
// Escape hatch for unit tests
2626
pub static mut R_TASK_BYPASS: bool = false;
@@ -64,30 +64,13 @@ pub fn start_r() {
6464

6565
pub fn r_test_impl<F: FnMut()>(f: F) {
6666
start_r();
67-
with_r_lock(f);
68-
}
69-
70-
pub fn r_test_unlocked_impl<F: FnMut()>(mut f: F) {
71-
start_r();
72-
f();
67+
r_safely!(f);
7368
}
7469

7570
#[macro_export]
7671
macro_rules! r_test {
77-
7872
($($expr:tt)*) => {
7973
#[allow(unused_unsafe)]
8074
$crate::test::r_test_impl(|| unsafe { $($expr)* })
8175
}
82-
83-
}
84-
85-
#[macro_export]
86-
macro_rules! r_test_unlocked {
87-
88-
($($expr:tt)*) => {
89-
#[allow(unused_unsafe)]
90-
$crate::test::r_test_unlocked_impl(|| unsafe { $($expr)* })
91-
}
92-
9376
}

0 commit comments

Comments
 (0)