@@ -16,6 +16,7 @@ use core::{
1616 ptr:: NonNull ,
1717 slice,
1818} ;
19+ use hashbrown:: HashMap ;
1920
2021use arrayvec:: ArrayVec ;
2122use smallvec:: SmallVec ;
@@ -37,6 +38,8 @@ use crate::{
3738} ;
3839use crate :: { dispatch:: DispatchAdapter , util:: Mutex } ;
3940
41+ mod thread_id;
42+
4043#[ derive( Clone ) ]
4144pub struct ContextWgpuCore ( Arc < wgc:: global:: Global > ) ;
4245
@@ -627,14 +630,14 @@ struct ErrorScope {
627630}
628631
629632struct ErrorSinkRaw {
630- scopes : Vec < ErrorScope > ,
633+ scopes : HashMap < thread_id :: ThreadId , Vec < ErrorScope > > ,
631634 uncaptured_handler : Option < Arc < dyn crate :: UncapturedErrorHandler > > ,
632635}
633636
634637impl ErrorSinkRaw {
635638 fn new ( ) -> ErrorSinkRaw {
636639 ErrorSinkRaw {
637- scopes : Vec :: new ( ) ,
640+ scopes : HashMap :: new ( ) ,
638641 uncaptured_handler : None ,
639642 }
640643 }
@@ -656,12 +659,9 @@ impl ErrorSinkRaw {
656659 crate :: Error :: Validation { .. } => crate :: ErrorFilter :: Validation ,
657660 crate :: Error :: Internal { .. } => crate :: ErrorFilter :: Internal ,
658661 } ;
659- match self
660- . scopes
661- . iter_mut ( )
662- . rev ( )
663- . find ( |scope| scope. filter == filter)
664- {
662+ let thread_id = thread_id:: ThreadId :: current ( ) ;
663+ let scopes = self . scopes . entry ( thread_id) . or_default ( ) ;
664+ match scopes. iter_mut ( ) . rev ( ) . find ( |scope| scope. filter == filter) {
665665 Some ( scope) => {
666666 if scope. error . is_none ( ) {
667667 scope. error = Some ( err) ;
@@ -1805,15 +1805,20 @@ impl dispatch::DeviceInterface for CoreDevice {
18051805
18061806 fn push_error_scope ( & self , filter : crate :: ErrorFilter ) {
18071807 let mut error_sink = self . error_sink . lock ( ) ;
1808- error_sink. scopes . push ( ErrorScope {
1808+ let thread_id = thread_id:: ThreadId :: current ( ) ;
1809+ let scopes = error_sink. scopes . entry ( thread_id) . or_default ( ) ;
1810+ scopes. push ( ErrorScope {
18091811 error : None ,
18101812 filter,
18111813 } ) ;
18121814 }
18131815
18141816 fn pop_error_scope ( & self ) -> Pin < Box < dyn dispatch:: PopErrorScopeFuture > > {
18151817 let mut error_sink = self . error_sink . lock ( ) ;
1816- let scope = error_sink. scopes . pop ( ) . unwrap ( ) ;
1818+ let thread_id = thread_id:: ThreadId :: current ( ) ;
1819+ let err = "Mismatched pop_error_scope call: no error scope for this thread. Error scopes are thread-local." ;
1820+ let scopes = error_sink. scopes . get_mut ( & thread_id) . expect ( err) ;
1821+ let scope = scopes. pop ( ) . expect ( err) ;
18171822 Box :: pin ( ready ( scope. error ) )
18181823 }
18191824
0 commit comments