Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c9bb519

Browse files
committed
Misc Cleanups
1 parent e0fab63 commit c9bb519

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

example/mini_core_hello_world.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ struct pthread_attr_t {
375375
}
376376

377377
#[link(name = "pthread")]
378-
#[cfg(not(target_env="msvc"))]
378+
#[cfg(unix)]
379379
extern "C" {
380380
fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int;
381381

@@ -399,14 +399,14 @@ type LPVOID = *mut c_void;
399399
type HANDLE = *mut c_void;
400400

401401
#[link(name = "msvcrt")]
402-
#[cfg(target_env="msvc")]
402+
#[cfg(windows)]
403403
extern "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

424426
impl 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

Comments
 (0)