Skip to content

Commit fbd76e2

Browse files
committed
Move r_cleanup_for_tests() to Unix file
1 parent 7f6fa3a commit fbd76e2

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

crates/ark/src/fixtures/dummy_frontend.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use amalthea::fixtures::dummy_frontend::DummyConnection;
1010
use amalthea::fixtures::dummy_frontend::DummyFrontend;
1111

1212
use crate::interface::SessionMode;
13-
use crate::interface::CLEANUP_SIGNAL;
1413
use crate::repos::DefaultRepos;
1514

1615
// There can be only one frontend per process. Needs to be in a mutex because
@@ -66,8 +65,11 @@ impl DummyArkFrontend {
6665

6766
/// Wait for R cleanup to start (indicating shutdown has been initiated).
6867
/// Panics if cleanup doesn't start within the timeout.
68+
#[cfg(unix)]
6969
#[track_caller]
7070
pub fn wait_for_cleanup() {
71+
use crate::sys::interface::CLEANUP_SIGNAL;
72+
7173
let (lock, cvar) = &CLEANUP_SIGNAL;
7274
let result = cvar
7375
.wait_timeout_while(lock.lock().unwrap(), Duration::from_secs(3), |started| {

crates/ark/src/interface.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,28 +2616,6 @@ pub unsafe extern "C-unwind" fn r_polled_events() {
26162616
};
26172617
}
26182618

2619-
// For integration tests
2620-
use std::sync::Condvar;
2621-
pub static CLEANUP_SIGNAL: (Mutex<bool>, Condvar) = (Mutex::new(false), Condvar::new());
2622-
2623-
#[no_mangle]
2624-
pub extern "C-unwind" fn r_cleanup_for_tests(_save_act: i32, _status: i32, _run_last: i32) {
2625-
// Signal that cleanup has started
2626-
let (lock, cvar) = &CLEANUP_SIGNAL;
2627-
2628-
let mut started = lock.lock().unwrap();
2629-
*started = true;
2630-
2631-
cvar.notify_all();
2632-
drop(started);
2633-
2634-
// Sleep to give tests time to complete before we panic
2635-
std::thread::sleep(std::time::Duration::from_secs(5));
2636-
2637-
// Fallthrough to R which will call `exit()`. Note that panicking from here
2638-
// would be UB, we can't panic over a C stack.
2639-
}
2640-
26412619
// This hook is called like a user onLoad hook but for every package to be
26422620
// loaded in the session
26432621
#[harp::register]

crates/ark/src/sys/unix/interface.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use std::ffi::CStr;
99
use std::os::raw::c_char;
10+
use std::sync::Condvar;
11+
use std::sync::Mutex;
1012

1113
use libr::ptr_R_Busy;
1214
use libr::ptr_R_ReadConsole;
@@ -38,6 +40,9 @@ use crate::interface::r_write_console;
3840
use crate::interface::RMain;
3941
use crate::signals::initialize_signal_handlers;
4042

43+
// For shutdown signal in integration tests
44+
pub static CLEANUP_SIGNAL: (Mutex<bool>, Condvar) = (Mutex::new(false), Condvar::new());
45+
4146
pub fn setup_r(args: &Vec<String>) {
4247
unsafe {
4348
// Before `Rf_initialize_R()`
@@ -78,10 +83,7 @@ pub fn setup_r(args: &Vec<String>) {
7883
// condition variable sends a notification, which occurs in this cleanup method
7984
// that is called during R's shutdown process.
8085
if stdext::IS_TESTING {
81-
libr::set(
82-
libr::ptr_R_CleanUp,
83-
Some(crate::interface::r_cleanup_for_tests),
84-
);
86+
libr::set(libr::ptr_R_CleanUp, Some(r_cleanup_for_tests));
8587
}
8688

8789
// In tests R may be run from various threads. This confuses R's stack
@@ -133,3 +135,21 @@ pub fn run_activity_handlers() {
133135
}
134136
}
135137
}
138+
139+
#[no_mangle]
140+
pub extern "C-unwind" fn r_cleanup_for_tests(_save_act: i32, _status: i32, _run_last: i32) {
141+
// Signal that cleanup has started
142+
let (lock, cvar) = &CLEANUP_SIGNAL;
143+
144+
let mut started = lock.lock().unwrap();
145+
*started = true;
146+
147+
cvar.notify_all();
148+
drop(started);
149+
150+
// Sleep to give tests time to complete before we panic
151+
std::thread::sleep(std::time::Duration::from_secs(5));
152+
153+
// Fallthrough to R which will call `exit()`. Note that panicking from here
154+
// would be UB, we can't panic over a C stack.
155+
}

0 commit comments

Comments
 (0)