Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion percpu.x
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SECTIONS
. = ALIGN(4K);
_percpu_start = .;
_percpu_end = _percpu_start + SIZEOF(.percpu);
.percpu 0x0 (NOLOAD) : AT(_percpu_start) {
.percpu (NOLOAD) : AT(_percpu_start) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it also fix issues like arceos-org/percpu#5 and arceos-org/percpu#12? If so, you may also contribute it to percpu.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change would make the load address no longer 0x0, which causes the assertions in the tests to fail. The corresponding tests would also need to be adjusted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. You may adjust the test codes accordingly.

_percpu_load_start = .;
*(.percpu .percpu.*)
_percpu_load_end = .;
Expand Down
101 changes: 100 additions & 1 deletion tests/scope_local.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{panic, sync::Arc, thread};

use ctor::ctor;
use scope_local::{ActiveScope, Scope, scope_local};
Expand Down Expand Up @@ -54,4 +54,103 @@ fn shared() {
}

assert_eq!(Arc::strong_count(&SHARED), 1);

let panic = panic::catch_unwind(|| {
let mut scope = Scope::new();
*SHARED.scope_mut(&mut scope) = SHARED.clone();
panic!("panic");
});
assert!(panic.is_err());

assert_eq!(Arc::strong_count(&SHARED), 1);
}

scope_local! {
static T_SHARED: Arc<String> = Arc::new("qwq".to_string());
}

#[test]
fn threads_shared() {
assert_eq!(Arc::strong_count(&SHARED), 1);

let handles: Vec<_> = (0..10)
.map(|_| {
thread::spawn(move || {
let mut scope = Scope::new();
*T_SHARED.scope_mut(&mut scope) = T_SHARED.clone();
assert!(Arc::strong_count(&T_SHARED) >= 2);
assert_eq!(*T_SHARED, Arc::new("qwq".to_string()));
})
})
.collect();

for h in handles {
h.join().unwrap();
}

assert_eq!(Arc::strong_count(&T_SHARED), 1);

{
let mut scope = Scope::new();
*T_SHARED.scope_mut(&mut scope) = T_SHARED.clone();
let scope = Arc::new(scope);

let handles: Vec<_> = (0..10)
.map(|_| {
let scope = scope.clone();
thread::spawn(move || {
unsafe { ActiveScope::set(&scope) };
assert_eq!(Arc::strong_count(&T_SHARED), 2);
assert_eq!(*T_SHARED, Arc::new("qwq".to_string()));
ActiveScope::set_global();
})
})
.collect();

for h in handles {
h.join().unwrap();
}
}

assert_eq!(Arc::strong_count(&T_SHARED), 1);
}

#[test]
fn isolation() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test that multiple threads share one scope?

let handles: Vec<_> = (0..10)
.map(|i| {
thread::spawn(move || {
let mut scope = Scope::new();
*DATA.scope_mut(&mut scope) = i;

unsafe { ActiveScope::set(&scope) };
assert_eq!(*DATA, i);

ActiveScope::set_global();
Comment on lines +128 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line? I remember percpu delegates to thread local if target_os = "linux"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this line will cause a SIGSEGV

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this line will cause a SIGSEGV

That's strange, and may indicate a bug in our design.

})
})
.collect();

for h in handles {
h.join().unwrap();
}

assert_eq!(*DATA, 0);
}

#[test]
fn nested() {
let mut outer = Scope::new();
unsafe { ActiveScope::set(&outer) };
*DATA.scope_mut(&mut outer) = 1;

let mut inner = Scope::new();
unsafe { ActiveScope::set(&inner) };
*DATA.scope_mut(&mut inner) = 2;
assert_eq!(*DATA, 2);

unsafe { ActiveScope::set(&outer) };
assert_eq!(*DATA, 1);

ActiveScope::set_global();
}