Skip to content

Commit 656b903

Browse files
committed
Add send_sync_in_statics test
1 parent beb8e02 commit 656b903

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
#[derive(Default)]
10+
pub struct Entry {
11+
pub name: Value<Ptr<u8>>,
12+
pub p: Value<Ptr<i32>>,
13+
}
14+
impl Clone for Entry {
15+
fn clone(&self) -> Self {
16+
let mut this = Self {
17+
name: Rc::new(RefCell::new((*self.name.borrow()).clone())),
18+
p: Rc::new(RefCell::new((*self.p.borrow()).clone())),
19+
};
20+
this
21+
}
22+
}
23+
impl ByteRepr for Entry {}
24+
thread_local!(
25+
pub static single_entry: Value<Entry> = Rc::new(RefCell::new(Entry {
26+
name: Rc::new(RefCell::new(Ptr::from_string_literal("alone"))),
27+
p: Rc::new(RefCell::new(Default::default())),
28+
}));
29+
);
30+
thread_local!(
31+
pub static entries: Value<Box<[Entry]>> = Rc::new(RefCell::new(Box::new([
32+
Entry {
33+
name: Rc::new(RefCell::new(Ptr::from_string_literal("first"))),
34+
p: Rc::new(RefCell::new(Default::default())),
35+
},
36+
Entry {
37+
name: Rc::new(RefCell::new(Ptr::from_string_literal("second"))),
38+
p: Rc::new(RefCell::new(Default::default())),
39+
},
40+
])));
41+
);
42+
pub fn main() {
43+
std::process::exit(main_0());
44+
}
45+
fn main_0() -> i32 {
46+
assert!((*(*single_entry.with(Value::clone).borrow()).p.borrow()).is_null());
47+
let i: Value<i32> = Rc::new(RefCell::new(0));
48+
'loop_: while ((*i.borrow()) < 2) {
49+
assert!(
50+
(*(*entries.with(Value::clone).borrow())[(*i.borrow()) as usize]
51+
.p
52+
.borrow())
53+
.is_null()
54+
);
55+
(*i.borrow_mut()).prefix_inc();
56+
}
57+
return 0;
58+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
#[repr(C)]
10+
#[derive(Copy, Clone, Default)]
11+
pub struct Entry {
12+
pub name: *const u8,
13+
pub p: *mut i32,
14+
}
15+
// SAFETY: preserves unsafe C semantics; thread-safety is the programmer's responsibility
16+
unsafe impl Sync for Entry {}
17+
pub static single_entry: Entry = Entry {
18+
name: b"alone\0".as_ptr(),
19+
p: std::ptr::null_mut(),
20+
};
21+
pub static entries: [Entry; 2] = [
22+
Entry {
23+
name: b"first\0".as_ptr(),
24+
p: std::ptr::null_mut(),
25+
},
26+
Entry {
27+
name: b"second\0".as_ptr(),
28+
p: std::ptr::null_mut(),
29+
},
30+
];
31+
pub fn main() {
32+
unsafe {
33+
std::process::exit(main_0() as i32);
34+
}
35+
}
36+
unsafe fn main_0() -> i32 {
37+
assert!((single_entry.p).is_null());
38+
let mut i: i32 = 0;
39+
'loop_: while ((i) < (2)) {
40+
assert!((entries[(i) as usize].p).is_null());
41+
i.prefix_inc();
42+
}
43+
return 0;
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <cassert>
2+
#include <cstddef>
3+
4+
struct Entry {
5+
const char *name;
6+
int *p;
7+
};
8+
9+
static const Entry single_entry = {"alone", nullptr};
10+
11+
static const Entry entries[2] = {
12+
{"first", nullptr},
13+
{"second", nullptr},
14+
};
15+
16+
int main() {
17+
assert(single_entry.p == nullptr);
18+
for (int i = 0; i < 2; ++i) {
19+
assert(entries[i].p == nullptr);
20+
}
21+
return 0;
22+
}

0 commit comments

Comments
 (0)