22
33// Define a worker-stealing queue
44struct WorkerStealingQueue {
5- Queues : Vec < Arc < Mutex < Vec < Box < dyn Echo :: Trait :: Sequence :: Action :: Trait > > > > > ,
5+ Queues : Vec < Arc < Mutex < Vec < Box < dyn Echo :: Trait :: Sequence :: Action :: Trait > > > > > ,
66}
77
88impl WorkerStealingQueue {
9- fn New ( Force : usize ) -> Self {
10- WorkerStealingQueue { Queues : ( 0 ..Force ) . map ( |_| Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ) . collect ( ) }
9+ fn New ( Force : usize ) -> Self {
10+ WorkerStealingQueue { Queues : ( 0 ..Force ) . map ( |_| Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ) . collect ( ) }
1111 }
1212
13- async fn Assign ( & self , Identifier : usize , Action : Box < dyn Echo :: Trait :: Sequence :: Action :: Trait > ) {
13+ async fn Assign ( & self , Identifier : usize , Action : Box < dyn Echo :: Trait :: Sequence :: Action :: Trait > ) {
1414 self . Queues [ Identifier ] . lock ( ) . await . push ( Action ) ;
1515 }
1616
17- async fn Do ( & self , Worker : usize ) -> Option < Box < dyn Echo :: Trait :: Sequence :: Action :: Trait > > {
17+ async fn Do ( & self , Worker : usize ) -> Option < Box < dyn Echo :: Trait :: Sequence :: Action :: Trait > > {
1818 let mut Queue = self . Queues [ Worker ] . lock ( ) . await ;
1919
2020 if let Some ( Action ) = Queue . pop ( ) {
@@ -23,7 +23,7 @@ impl WorkerStealingQueue {
2323 // Try to steal from other queues
2424 drop ( Queue ) ;
2525
26- let mut QueuesOther : Vec < usize > = ( 0 ..self . Queues . len ( ) ) . filter ( |& i| i != Worker ) . collect ( ) ;
26+ let mut QueuesOther : Vec < usize > = ( 0 ..self . Queues . len ( ) ) . filter ( |& i| i != Worker ) . collect ( ) ;
2727
2828 QueuesOther . shuffle ( & mut rand:: thread_rng ( ) ) ;
2929
@@ -42,24 +42,20 @@ impl WorkerStealingQueue {
4242
4343// Define a worker that implements the Worker trait
4444struct StealingWorker {
45- Id : usize ,
46- Queue : Arc < WorkerStealingQueue > ,
45+ Id : usize ,
46+ Queue : Arc < WorkerStealingQueue > ,
4747}
4848
4949#[ async_trait]
5050impl Worker for StealingWorker {
51- async fn Receive (
52- & self ,
53- Action : Box < dyn Echo :: Trait :: Sequence :: Action :: Trait > ,
54- Context : & Life ,
55- ) -> Result < ( ) , Error > {
51+ async fn Receive ( & self , Action : Box < dyn Echo :: Trait :: Sequence :: Action :: Trait > , Context : & Life ) -> Result < ( ) , Error > {
5652 self . Queue . Assign ( self . Id , Action ) . await ;
5753
5854 Ok ( ( ) )
5955 }
6056}
6157
62- async fn worker_loop ( Worker : Arc < StealingWorker > , Life : Arc < Life > , Running : Arc < Mutex < bool > > ) {
58+ async fn worker_loop ( Worker : Arc < StealingWorker > , Life : Arc < Life > , Running : Arc < Mutex < bool > > ) {
6359 while * Running . lock ( ) . await {
6460 if let Some ( Action ) = Worker . Queue . Do ( Worker . Id ) . await {
6561 if let Err ( _Error) = Action . Execute ( & Life ) . await {
@@ -76,8 +72,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
7672 // Create a plan with file reading and writing actions
7773 let Plan = Arc :: new (
7874 Echo :: Struct :: Sequence :: Plan :: Struct :: New ( )
79- . WithSignature ( Signature { Name : "Read" . to_string ( ) } )
80- . WithSignature ( Signature { Name : "Write" . to_string ( ) } )
75+ . WithSignature ( Signature { Name : "Read" . to_string ( ) } )
76+ . WithSignature ( Signature { Name : "Write" . to_string ( ) } )
8177 . WithFunction ( "Read" , Common :: Read :: Fn ) ?
8278 . WithFunction ( "Write" , Common :: Write :: Fn ) ?
8379 . Build ( ) ,
@@ -90,22 +86,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
9086
9187 // Create a life context
9288 let Life = Arc :: new ( Life {
93- Span : Arc :: new ( dashmap:: DashMap :: new ( ) ) ,
94- Fate : Arc :: new ( config:: Config :: default ( ) ) ,
95- Cache : Arc :: new ( tokio:: sync:: Mutex :: new ( dashmap:: DashMap :: new ( ) ) ) ,
96- Karma : Arc :: new ( dashmap:: DashMap :: new ( ) ) ,
89+ Span : Arc :: new ( dashmap:: DashMap :: new ( ) ) ,
90+ Fate : Arc :: new ( config:: Config :: default ( ) ) ,
91+ Cache : Arc :: new ( tokio:: sync:: Mutex :: new ( dashmap:: DashMap :: new ( ) ) ) ,
92+ Karma : Arc :: new ( dashmap:: DashMap :: new ( ) ) ,
9793 } ) ;
9894
9995 // Create workers
100- let Workers : Vec < Arc < StealingWorker > > = ( 0 ..Force )
101- . map ( |Id | Arc :: new ( StealingWorker { Id , Queue : Queue . clone ( ) } ) )
96+ let Workers : Vec < Arc < StealingWorker > > = ( 0 ..Force )
97+ . map ( |Id | Arc :: new ( StealingWorker { Id , Queue : Queue . clone ( ) } ) )
10298 . collect ( ) ;
10399
104100 // Create a flag to control worker loops
105101 let Running = Arc :: new ( Mutex :: new ( true ) ) ;
106102
107103 // Spawn worker tasks
108- let Handle : Vec < _ > = Workers
104+ let Handle : Vec < _ > = Workers
109105 . iter ( )
110106 . map ( |Worker | {
111107 let Worker = Worker . clone ( ) ;
0 commit comments