@@ -712,6 +712,33 @@ impl<B: WriteBackendMethods> WorkItem<B> {
712
712
}
713
713
}
714
714
}
715
+
716
+ /// Generate a short description of this work item suitable for use as a thread name.
717
+ fn short_description ( & self ) -> String {
718
+ // `pthread_setname()` on *nix is limited to 15 characters and longer names are ignored.
719
+ // Use very short descriptions in this case to maximize the space available for the module name.
720
+ // Windows does not have that limitation so use slightly more descriptive names there.
721
+ match self {
722
+ WorkItem :: Optimize ( m) => {
723
+ #[ cfg( windows) ]
724
+ return format ! ( "optimize module {}" , m. name) ;
725
+ #[ cfg( not( windows) ) ]
726
+ return format ! ( "opt {}" , m. name) ;
727
+ }
728
+ WorkItem :: CopyPostLtoArtifacts ( m) => {
729
+ #[ cfg( windows) ]
730
+ return format ! ( "copy LTO artifacts for {}" , m. name) ;
731
+ #[ cfg( not( windows) ) ]
732
+ return format ! ( "copy {}" , m. name) ;
733
+ }
734
+ WorkItem :: LTO ( m) => {
735
+ #[ cfg( windows) ]
736
+ return format ! ( "LTO module {}" , m. name( ) ) ;
737
+ #[ cfg( not( windows) ) ]
738
+ return format ! ( "LTO {}" , m. name( ) ) ;
739
+ }
740
+ }
741
+ }
715
742
}
716
743
717
744
enum WorkItemResult < B : WriteBackendMethods > {
@@ -1609,56 +1636,59 @@ fn start_executing_work<B: ExtraBackendMethods>(
1609
1636
pub struct WorkerFatalError ;
1610
1637
1611
1638
fn spawn_work < B : ExtraBackendMethods > ( cgcx : CodegenContext < B > , work : WorkItem < B > ) {
1612
- thread:: spawn ( move || {
1613
- // Set up a destructor which will fire off a message that we're done as
1614
- // we exit.
1615
- struct Bomb < B : ExtraBackendMethods > {
1616
- coordinator_send : Sender < Box < dyn Any + Send > > ,
1617
- result : Option < Result < WorkItemResult < B > , FatalError > > ,
1618
- worker_id : usize ,
1619
- }
1620
- impl < B : ExtraBackendMethods > Drop for Bomb < B > {
1621
- fn drop ( & mut self ) {
1622
- let worker_id = self . worker_id ;
1623
- let msg = match self . result . take ( ) {
1624
- Some ( Ok ( WorkItemResult :: Compiled ( m) ) ) => {
1625
- Message :: Done :: < B > { result : Ok ( m) , worker_id }
1626
- }
1627
- Some ( Ok ( WorkItemResult :: NeedsLink ( m) ) ) => {
1628
- Message :: NeedsLink :: < B > { module : m, worker_id }
1629
- }
1630
- Some ( Ok ( WorkItemResult :: NeedsFatLTO ( m) ) ) => {
1631
- Message :: NeedsFatLTO :: < B > { result : m, worker_id }
1632
- }
1633
- Some ( Ok ( WorkItemResult :: NeedsThinLTO ( name, thin_buffer) ) ) => {
1634
- Message :: NeedsThinLTO :: < B > { name, thin_buffer, worker_id }
1635
- }
1636
- Some ( Err ( FatalError ) ) => {
1637
- Message :: Done :: < B > { result : Err ( Some ( WorkerFatalError ) ) , worker_id }
1638
- }
1639
- None => Message :: Done :: < B > { result : Err ( None ) , worker_id } ,
1640
- } ;
1641
- drop ( self . coordinator_send . send ( Box :: new ( msg) ) ) ;
1639
+ let builder = thread:: Builder :: new ( ) . name ( work. short_description ( ) ) ;
1640
+ builder
1641
+ . spawn ( move || {
1642
+ // Set up a destructor which will fire off a message that we're done as
1643
+ // we exit.
1644
+ struct Bomb < B : ExtraBackendMethods > {
1645
+ coordinator_send : Sender < Box < dyn Any + Send > > ,
1646
+ result : Option < Result < WorkItemResult < B > , FatalError > > ,
1647
+ worker_id : usize ,
1648
+ }
1649
+ impl < B : ExtraBackendMethods > Drop for Bomb < B > {
1650
+ fn drop ( & mut self ) {
1651
+ let worker_id = self . worker_id ;
1652
+ let msg = match self . result . take ( ) {
1653
+ Some ( Ok ( WorkItemResult :: Compiled ( m) ) ) => {
1654
+ Message :: Done :: < B > { result : Ok ( m) , worker_id }
1655
+ }
1656
+ Some ( Ok ( WorkItemResult :: NeedsLink ( m) ) ) => {
1657
+ Message :: NeedsLink :: < B > { module : m, worker_id }
1658
+ }
1659
+ Some ( Ok ( WorkItemResult :: NeedsFatLTO ( m) ) ) => {
1660
+ Message :: NeedsFatLTO :: < B > { result : m, worker_id }
1661
+ }
1662
+ Some ( Ok ( WorkItemResult :: NeedsThinLTO ( name, thin_buffer) ) ) => {
1663
+ Message :: NeedsThinLTO :: < B > { name, thin_buffer, worker_id }
1664
+ }
1665
+ Some ( Err ( FatalError ) ) => {
1666
+ Message :: Done :: < B > { result : Err ( Some ( WorkerFatalError ) ) , worker_id }
1667
+ }
1668
+ None => Message :: Done :: < B > { result : Err ( None ) , worker_id } ,
1669
+ } ;
1670
+ drop ( self . coordinator_send . send ( Box :: new ( msg) ) ) ;
1671
+ }
1642
1672
}
1643
- }
1644
1673
1645
- let mut bomb = Bomb :: < B > {
1646
- coordinator_send : cgcx. coordinator_send . clone ( ) ,
1647
- result : None ,
1648
- worker_id : cgcx. worker ,
1649
- } ;
1674
+ let mut bomb = Bomb :: < B > {
1675
+ coordinator_send : cgcx. coordinator_send . clone ( ) ,
1676
+ result : None ,
1677
+ worker_id : cgcx. worker ,
1678
+ } ;
1650
1679
1651
- // Execute the work itself, and if it finishes successfully then flag
1652
- // ourselves as a success as well.
1653
- //
1654
- // Note that we ignore any `FatalError` coming out of `execute_work_item`,
1655
- // as a diagnostic was already sent off to the main thread - just
1656
- // surface that there was an error in this worker.
1657
- bomb. result = {
1658
- let _prof_timer = work. start_profiling ( & cgcx) ;
1659
- Some ( execute_work_item ( & cgcx, work) )
1660
- } ;
1661
- } ) ;
1680
+ // Execute the work itself, and if it finishes successfully then flag
1681
+ // ourselves as a success as well.
1682
+ //
1683
+ // Note that we ignore any `FatalError` coming out of `execute_work_item`,
1684
+ // as a diagnostic was already sent off to the main thread - just
1685
+ // surface that there was an error in this worker.
1686
+ bomb. result = {
1687
+ let _prof_timer = work. start_profiling ( & cgcx) ;
1688
+ Some ( execute_work_item ( & cgcx, work) )
1689
+ } ;
1690
+ } )
1691
+ . expect ( "failed to spawn thread" ) ;
1662
1692
}
1663
1693
1664
1694
enum SharedEmitterMessage {
0 commit comments