1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use paste::paste;
use crate::thread::ThreadId;
use crate::monitor_types;
macro_rules! mk_monitor {
($t:tt) => {
paste! { [< start_monitor_ $t >]($t) }
};
($t:tt, $($e:expr),* $(,)?) => {
paste! { [< start_monitor_ $t >](<$t>::new($($e),* )) }
};
}
#[cfg(test)]
mod test {
use super::*;
use crate::msg::Message;
use crate::thread::{self, JoinHandle, ThreadId};
use crate::Config;
use crate::ConsType;
use std::collections::BTreeMap;
const TEST_RUNS: u32 = 1000;
#[monitor(M1, M2, Foo)]
#[derive(Clone, Debug)]
pub struct MyMonitor {
value: u32,
}
#[monitor(M1)]
#[derive(Clone, Debug)]
pub struct MyOtherMonitor;
#[derive(Debug, Clone, PartialEq)]
pub struct M1;
#[derive(Debug, Clone, PartialEq)]
pub struct M2;
#[derive(Debug, Clone, PartialEq)]
pub struct Foo;
#[derive(Debug, Clone, PartialEq)]
pub struct M4;
impl MyMonitor {
pub fn new(_f: u8) -> Self {
// _g: u16) -> Self {
Self { value: 0 }
}
}
impl Monitor for MyMonitor {}
impl Observer<M1> for MyMonitor {
fn notify(&mut self, who: ThreadId, whom: ThreadId, what: &M1) -> MonitorResult {
//println!("Monitor: {:?} == {:?} => {:?}", who, what, whom);
Ok(())
}
}
impl Observer<M2> for MyMonitor {}
impl Observer<Foo> for MyMonitor {}
impl Monitor for MyOtherMonitor {}
impl Observer<M1> for MyOtherMonitor {}
/*
pub fn monitor_code() {
let mut mon = MyMonitor;
let (who, whom, m): (ThreadId, ThreadId, MyMonitorMsg) = crate::recv_msg_block();
println!("Who = {:?}, Whom = {:?}, what = {:?}", who, whom, &m);
let _ = (&mut mon as &mut dyn Observer<MyMonitorMsg>).notify(who, whom, &m);
}
pub fn send_monitor(t: ThreadId, m: Box<dyn Message>) {
let msg = m.clone();
if let Ok(msg) = msg.as_any().downcast::<(ThreadId, ThreadId, M1)>() {
println!("M1");
let msg = *msg;
let tid1 = msg.0;
let tid2 = msg.1;
let msg = msg.2;
let m: MyMonitorMsg = <M1 as Into<MyMonitorMsg>>::into(msg);
println!("HERE M1");
crate::send_msg(t, (tid1, tid2, m));
return;
}
let msg = m.clone();
if let Ok(msg) = msg.as_any().downcast::<M2>() {
println!("M2");
let m: MyMonitorMsg = <M2 as Into<MyMonitorMsg>>::into(*msg);
crate::send_msg(t, m);
return;
}
panic!("Unknown message type");
}
pub fn send_monitor2(t: ThreadId, msg: Box<dyn Message>) {
let msg1 = msg.clone();
if let Ok(msg) = msg1.as_any().downcast::<(ThreadId, ThreadId, M1)>() {
println!("HERE");
let msg = *msg;
let tid1 = msg.0;
let tid2 = msg.1;
let msg = msg.2;
let m: MyMonitorMsg = <M1 as Into<MyMonitorMsg>>::into(msg);
println!("HERE2");
crate::send_msg(t, (tid1, tid2, m));
return;
}
let msg1 = msg.clone();
if let Ok(msg) = msg1.as_any().downcast::<(ThreadId, ThreadId, M2)>() {
let msg = *msg;
let tid1 = msg.0;
let tid2 = msg.1;
let msg = msg.2;
let m: MyMonitorMsg = <M2 as Into<MyMonitorMsg>>::into(msg);
crate::send_msg(t, (tid1, tid2, m));
return;
}
panic!("Unknown message type");
}
*/
#[test]
fn run() {
for _ in 0..TEST_RUNS {
let mut creators = BTreeMap::new();
creators.insert(
thread::ThreadId { task_id: 3.into() },
create_msg_for_monitor_MyMonitor
as fn(Val) -> Option<Val>,
);
creators.insert(
thread::ThreadId { task_id: 4.into() },
create_msg_for_monitor_MyOtherMonitor
as fn(Val) -> Option<Val>,
);
let stats = crate::verify(
Config::builder()
.with_policy(crate::SchedulePolicy::Arbitrary)
.with_cons_type(ConsType::WB)
.with_monitor_info(crate::MonitorInfo {
is_present: true,
create_msgs: creators,
})
.build(),
|| {
let tid1 = thread::spawn(|| {
crate::send_msg(thread::ThreadId { task_id: 2.into() }, M1);
let _: Foo = crate::recv_msg_block();
crate::send_msg(thread::ThreadId { task_id: 2.into() }, Foo);
crate::send_msg(thread::ThreadId { task_id: 2.into() }, M4);
});
let tid2 = thread::spawn(|| {
crate::send_msg(thread::ThreadId { task_id: 1.into() }, Foo);
});
let mtid1: JoinHandle<MonitorResult> = mk_monitor!(MyMonitor, 0); //start_monitor_MyMonitor(MyMonitor {value : 0}); // mk_monitor!(MyMonitor);
let mtid2: JoinHandle<MonitorResult> = mk_monitor!(MyOtherMonitor); // start_monitor_MyOtherMonitor(MyOtherMonitor);
let tid1 = tid1.thread().id();
let tid2 = tid2.thread().id();
//send_monitor_MyMonitor(mtid1.thread().id(),Box::new((Some(tid1), Some(tid2), m.clone())));
//send_monitor_MyOtherMonitor(mtid2.thread().id(),Box::new((Some(tid1), Some(tid2), m.clone())));
//println!("return value {:?}",create_msg_for_monitor_MyMonitor(Box::new(m.clone())));
//create_msg_for_monitor_MyMonitor(Box::new(mp.clone()));
//println!("return value {:?}",create_msg_for_monitor_MyMonitor(Box::new(mpp.clone())));
terminate_monitor_MyMonitor(mtid1.thread().id());
terminate_monitor_MyOtherMonitor(mtid2.thread().id());
/*
// This is what the subscription list will look like
let q: Vec<(
ThreadId,
fn(ThreadId, Box<dyn Message>) -> (),
fn(ThreadId) -> (),
)> = vec![
(
mtid1.thread().id(),
send_monitor_MyMonitor,
terminate_monitor_MyMonitor,
),
(
mtid2.thread().id(),
send_monitor_MyOtherMonitor,
terminate_monitor_MyOtherMonitor,
),
];
for (tid, f, _) in &q {
f(*tid, Box::new((Some(tid1), Some(tid2), m.clone())));
}
// terminate_monitor_MyMonitor(mtid1.thread().id());
for (tid, _, kill) in &q {
// send terminate
kill(*tid);
}
*/
let ok1: MonitorResult = mtid1.join().unwrap();
let ok2: MonitorResult = mtid2.join().unwrap();
assert!(ok1.is_ok() && ok2.is_ok());
},
);
println!("Total number of executions {}", stats.execs);
assert_eq!(stats.execs, 18);
}
}
}