@@ -375,7 +375,7 @@ struct pthread_attr_t {
375375}
376376
377377#[ link( name = "pthread" ) ]
378- #[ cfg( not ( target_env= "msvc" ) ) ]
378+ #[ cfg( unix ) ]
379379extern "C" {
380380 fn pthread_attr_init ( attr : * mut pthread_attr_t ) -> c_int ;
381381
@@ -399,14 +399,14 @@ type LPVOID = *mut c_void;
399399type HANDLE = * mut c_void ;
400400
401401#[ link( name = "msvcrt" ) ]
402- #[ cfg( target_env= "msvc" ) ]
402+ #[ cfg( windows ) ]
403403extern "C" {
404404 fn WaitForSingleObject (
405405 hHandle : LPVOID ,
406406 dwMilliseconds : DWORD
407407 ) -> DWORD ;
408408
409- fn CreateThread (
409+ fn CreateThread (
410410 lpThreadAttributes : LPVOID , // Technically LPSECURITY_ATTRIBUTES, but we don't use it anyway
411411 dwStackSize : usize ,
412412 lpStartAddress : extern "C" fn ( _: * mut c_void ) -> * mut c_void ,
@@ -416,14 +416,16 @@ extern "C" {
416416 ) -> HANDLE ;
417417}
418418
419- enum Thread {
420- Windows ( HANDLE ) ,
421- Pthread ( pthread_t )
419+ struct Thread {
420+ #[ cfg( windows) ]
421+ handle : HANDLE ,
422+ #[ cfg( unix) ]
423+ handle : pthread_t ,
422424}
423425
424426impl Thread {
425427 unsafe fn create ( f : extern "C" fn ( _: * mut c_void ) -> * mut c_void ) -> Self {
426- #[ cfg( not ( target_env= "msvc" ) ) ]
428+ #[ cfg( unix ) ]
427429 {
428430 let mut attr: pthread_attr_t = zeroed ( ) ;
429431 let mut thread: pthread_t = 0 ;
@@ -436,35 +438,38 @@ impl Thread {
436438 assert ! ( false ) ;
437439 }
438440
439- Thread :: Pthread ( thread)
441+ Thread {
442+ handle : thread,
443+ }
440444 }
441445
442- #[ cfg( target_env= "msvc" ) ]
446+ #[ cfg( windows ) ]
443447 {
444448 let handle = CreateThread ( 0 as * mut c_void , 0 , f, 0 as * mut c_void , 0 , 0 as * mut u32 ) ;
445449
446450 if ( handle as u64 ) == 0 {
447451 assert ! ( false ) ;
448452 }
449453
450- Thread :: Windows ( handle)
454+ Thread {
455+ handle,
456+ }
451457 }
452458 }
453459
454460
455461 unsafe fn join ( self ) {
456- match self {
457- #[ cfg( not( target_env="msvc" ) ) ]
458- Thread :: Pthread ( thread) => {
459- let mut res = 0 as * mut c_void ;
460- pthread_join ( thread, & mut res) ;
461- }
462- #[ cfg( target_env="msvc" ) ]
463- Thread :: Windows ( handle) => {
464- let wait_time = 5000 ; // in milliseconds
465- assert ! ( WaitForSingleObject ( handle, wait_time) == 0 ) ;
466- }
467- _ => assert ! ( false ) ,
462+ #[ cfg( unix) ]
463+ {
464+ let mut res = 0 as * mut c_void ;
465+ pthread_join ( self . handle , & mut res) ;
466+ }
467+
468+ #[ cfg( windows) ]
469+ {
470+ // The INFINITE macro is used to signal operations that do not timeout.
471+ let infinite = 0xffffffff ;
472+ assert ! ( WaitForSingleObject ( self . handle, infinite) == 0 ) ;
468473 }
469474 }
470475}
0 commit comments