@@ -3,21 +3,20 @@ use std::ops::DerefMut;
3
3
use std:: sync:: Arc ;
4
4
use std:: sync:: Mutex ;
5
5
use std:: sync:: MutexGuard ;
6
+ use std:: sync:: OnceLock ;
6
7
7
8
use amalthea:: test:: dummy_frontend:: DummyFrontend ;
8
- use once_cell:: sync:: Lazy ;
9
9
10
10
use crate :: interface:: RMain ;
11
11
use crate :: interface:: SessionMode ;
12
12
13
13
// There can be only one frontend per process. Needs to be in a mutex because
14
14
// the frontend wraps zmq sockets which are unsafe to send across threads.
15
15
//
16
- // This is using `Lazy` from the `once_cell` crate instead of other standard
17
- // types because the former provides a way of checking whether it has been
18
- // initialized already.
19
- static FRONTEND : Lazy < Arc < Mutex < DummyFrontend > > > =
20
- Lazy :: new ( || Arc :: new ( Mutex :: new ( DummyArkFrontend :: init ( ) ) ) ) ;
16
+ // This is using `OnceLock` because it provides a way of checking whether the
17
+ // value has been initialized already. Also we'll need to parameterize
18
+ // initialization in the future.
19
+ static FRONTEND : OnceLock < Arc < Mutex < DummyFrontend > > > = OnceLock :: new ( ) ;
21
20
22
21
/// Wrapper around `DummyFrontend` that checks sockets are empty on drop
23
22
pub struct DummyArkFrontend {
@@ -27,12 +26,16 @@ pub struct DummyArkFrontend {
27
26
impl DummyArkFrontend {
28
27
pub fn lock ( ) -> Self {
29
28
Self {
30
- guard : FRONTEND . lock ( ) . unwrap ( ) ,
29
+ guard : Self :: get_frontend ( ) . lock ( ) . unwrap ( ) ,
31
30
}
32
31
}
33
32
33
+ fn get_frontend ( ) -> & ' static Arc < Mutex < DummyFrontend > > {
34
+ FRONTEND . get_or_init ( || Arc :: new ( Mutex :: new ( DummyArkFrontend :: init ( ) ) ) )
35
+ }
36
+
34
37
fn init ( ) -> DummyFrontend {
35
- if Lazy :: get ( & FRONTEND ) . is_some ( ) {
38
+ if FRONTEND . get ( ) . is_some ( ) {
36
39
panic ! ( "Can't spawn Ark more than once" ) ;
37
40
}
38
41
0 commit comments