@@ -11,6 +11,7 @@ use crossbeam_utils::thread::scope;
1111use once_cell:: unsync:: OnceCell ;
1212
1313use crate :: rt:: Reactor ;
14+ use crate :: sync:: Spinlock ;
1415use crate :: task:: Runnable ;
1516use crate :: utils:: { abort_on_panic, random} ;
1617
@@ -58,7 +59,7 @@ impl Runtime {
5859
5960 let stealers = machines
6061 . iter ( )
61- . map ( |m| m. processor . worker . stealer ( ) )
62+ . map ( |m| m. processor . lock ( ) . worker . stealer ( ) )
6263 . collect ( ) ;
6364
6465 Runtime {
@@ -138,33 +139,32 @@ impl Runtime {
138139/// A thread running a processor.
139140struct Machine {
140141 /// Holds the processor until it gets stolen.
141- processor : Processor ,
142+ processor : Spinlock < Processor > ,
142143}
143144
144- unsafe impl Send for Machine { }
145- unsafe impl Sync for Machine { }
146-
147145impl Machine {
148146 /// Creates a new machine running a processor.
149147 fn new ( p : Processor ) -> Machine {
150- Machine { processor : p }
148+ Machine {
149+ processor : Spinlock :: new ( p) ,
150+ }
151151 }
152152
153153 /// Schedules a task onto the machine.
154154 fn schedule ( & self , rt : & Runtime , task : Runnable ) {
155- self . processor . schedule ( rt, task) ;
155+ self . processor . lock ( ) . schedule ( rt, task) ;
156156 }
157157
158158 /// Finds the next runnable task.
159159 fn find_task ( & self , rt : & Runtime ) -> Steal < Runnable > {
160160 let mut retry = false ;
161161
162162 // First try finding a task in the local queue or in the global queue.
163- if let Some ( task) = self . processor . pop_task ( ) {
163+ if let Some ( task) = self . processor . lock ( ) . pop_task ( ) {
164164 return Steal :: Success ( task) ;
165165 }
166166
167- match self . processor . steal_from_global ( rt) {
167+ match self . processor . lock ( ) . steal_from_global ( rt) {
168168 Steal :: Empty => { }
169169 Steal :: Retry => retry = true ,
170170 Steal :: Success ( task) => return Steal :: Success ( task) ,
@@ -176,12 +176,12 @@ impl Machine {
176176 // Try finding a task in the local queue, which might hold tasks woken by the reactor. If
177177 // the local queue is still empty, try stealing from other processors.
178178 if progress {
179- if let Some ( task) = self . processor . pop_task ( ) {
179+ if let Some ( task) = self . processor . lock ( ) . pop_task ( ) {
180180 return Steal :: Success ( task) ;
181181 }
182182 }
183183
184- match self . processor . steal_from_others ( rt) {
184+ match self . processor . lock ( ) . steal_from_others ( rt) {
185185 Steal :: Empty => { }
186186 Steal :: Retry => retry = true ,
187187 Steal :: Success ( task) => return Steal :: Success ( task) ,
@@ -208,7 +208,7 @@ impl Machine {
208208 // Check if `task::yield_now()` was invoked and flush the slot if so.
209209 YIELD_NOW . with ( |flag| {
210210 if flag. replace ( false ) {
211- self . processor . flush_slot ( rt) ;
211+ self . processor . lock ( ) . flush_slot ( rt) ;
212212 }
213213 } ) ;
214214
@@ -219,11 +219,12 @@ impl Machine {
219219 runs = 0 ;
220220 rt. quick_poll ( ) . unwrap ( ) ;
221221
222- if let Steal :: Success ( task) = self . processor . steal_from_global ( rt) {
223- self . processor . schedule ( rt, task) ;
222+ let p = self . processor . lock ( ) ;
223+ if let Steal :: Success ( task) = p. steal_from_global ( rt) {
224+ p. schedule ( rt, task) ;
224225 }
225226
226- self . processor . flush_slot ( rt) ;
227+ p . flush_slot ( rt) ;
227228 }
228229
229230 // Try to find a runnable task.
0 commit comments