-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext.rs
More file actions
72 lines (64 loc) · 2.11 KB
/
context.rs
File metadata and controls
72 lines (64 loc) · 2.11 KB
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
use std::sync::atomic::AtomicBool;
use crate::{AsyncHandler, CompressionProofSystem, SnarkWrapperProofSystem, SnarkWrapperStep};
pub(crate) trait ContextManagerInterface {
fn init_compression_context<P>(&self, config: P::ContextConfig) -> AsyncHandler<P::Context>
where
P: CompressionProofSystem;
fn init_snark_context<S>(&self, crs: AsyncHandler<S::CRS>) -> AsyncHandler<S::Context>
where
S: SnarkWrapperStep;
}
pub(crate) struct SimpleContextManager {
context_status: std::sync::Arc<AtomicBool>,
}
impl SimpleContextManager {
pub fn new() -> Self {
Self {
context_status: std::sync::Arc::new(AtomicBool::new(false)),
}
}
}
impl ContextManagerInterface for SimpleContextManager {
fn init_compression_context<P>(&self, config: P::ContextConfig) -> AsyncHandler<P::Context>
where
P: CompressionProofSystem,
{
assert!(
self.context_status
.load(std::sync::atomic::Ordering::Relaxed)
== false
);
let flag = self.context_status.clone();
let f = move || {
let (sender, receiver) = std::sync::mpsc::channel();
let context = P::init_context(config);
sender.send(context).unwrap();
flag.store(false, std::sync::atomic::Ordering::Relaxed);
receiver
};
AsyncHandler::spawn(f)
}
fn init_snark_context<S>(
&self,
compact_raw_crs: AsyncHandler<S::CRS>,
) -> AsyncHandler<S::Context>
where
S: SnarkWrapperProofSystem,
{
assert!(
self.context_status
.load(std::sync::atomic::Ordering::Relaxed)
== false
);
// load next context
let flag = self.context_status.clone();
let f = move || {
let (sender, receiver) = std::sync::mpsc::channel();
let context = S::init_context(compact_raw_crs);
sender.send(context).unwrap();
flag.store(false, std::sync::atomic::Ordering::Relaxed);
receiver
};
AsyncHandler::spawn(f)
}
}