@@ -17,7 +17,7 @@ use std::collections::HashMap;
1717use std:: hash:: { DefaultHasher , Hash , Hasher } ;
1818use std:: path:: { Path , PathBuf } ;
1919use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
20- use std:: sync:: { Arc , Mutex , MutexGuard } ;
20+ use std:: sync:: { Arc , Condvar , Mutex , MutexGuard } ;
2121
2222use crate :: loader:: approval:: { self , Artifact } ;
2323
@@ -31,6 +31,8 @@ pub(crate) struct Trees {
3131 /// The bytes of plugin files the walks and copies read.
3232 hashed : Arc < AtomicU64 > ,
3333 wake : Arc < dyn Fn ( ) + Send + Sync > ,
34+ /// While set, a copy waits before it reads anything ([`Trees::pause_stages`]).
35+ paused : Arc < ( Mutex < bool > , Condvar ) > ,
3436}
3537
3638/// Each copy by the tree and its hash: the tree's [`Entry::changes`] when it began, and its result until it is taken.
@@ -59,7 +61,13 @@ struct Entry {
5961impl Trees {
6062 /// `wake` runs on a walk's thread when a walk found something new, and on a copy's when it ended.
6163 pub ( crate ) fn new ( wake : impl Fn ( ) + Send + Sync + ' static ) -> Self {
62- Self { state : Arc :: default ( ) , stages : Arc :: default ( ) , hashed : Arc :: default ( ) , wake : Arc :: new ( wake) }
64+ Self {
65+ state : Arc :: default ( ) ,
66+ stages : Arc :: default ( ) ,
67+ hashed : Arc :: default ( ) ,
68+ wake : Arc :: new ( wake) ,
69+ paused : Arc :: default ( ) ,
70+ }
6371 }
6472
6573 /// The build of the global plugin `dir` as the last walk found it, or `None` before the first walk ended.
@@ -101,8 +109,10 @@ impl Trees {
101109 stages. insert ( key, ( changes, None ) ) ;
102110 drop ( stages) ;
103111 let ( stages, hashed, wake) = ( self . stages . clone ( ) , self . hashed . clone ( ) , self . wake . clone ( ) ) ;
104- let ( owned, to) = ( artifact. clone ( ) , root. to_owned ( ) ) ;
112+ let ( owned, to, paused ) = ( artifact. clone ( ) , root. to_owned ( ) , self . paused . clone ( ) ) ;
105113 let spawned = std:: thread:: Builder :: new ( ) . name ( "plugin-stage" . to_owned ( ) ) . spawn ( move || {
114+ let ( flag, resumed) = & * paused;
115+ drop ( resumed. wait_while ( lock ( flag) , |paused| * paused) . unwrap_or_else ( |poisoned| poisoned. into_inner ( ) ) ) ;
106116 stage ( & stages, & hashed, & owned, & to, changes) ;
107117 wake ( ) ;
108118 } ) ;
@@ -114,6 +124,14 @@ impl Trees {
114124 None
115125 }
116126
127+ /// Holds every copy of an approved tree before it reads anything while `paused`, and lets them go on when not: a
128+ /// test saves a file between the approval and the copy.
129+ pub ( crate ) fn pause_stages ( & self , paused : bool ) {
130+ let ( flag, resumed) = & * self . paused ;
131+ * lock ( flag) = paused;
132+ resumed. notify_all ( ) ;
133+ }
134+
117135 /// The bytes of plugin files the walks and copies read so far.
118136 pub ( crate ) fn hashed ( & self ) -> u64 {
119137 self . hashed . load ( Ordering :: Relaxed )
0 commit comments