Skip to content

Declare Windows imports jobserver depends on #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/windows.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!

As a bonus this is mildly more efficient.

Out of curiosity, how does it get more efficient with this change?

Copy link
Member Author

@ChrisDenton ChrisDenton Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a bit about something similar on zulip: https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Link.20attribute.20on.20Windows/near/470532686

It's a super minor thing and almost certainly doesn't affect the performance of jobserver. In short, an extern block defaults to static linkage whereas #[link] defaults to dynamic linkage. Since this function is from a DLL we want dynamic linkage because the location of a function is stored at an address in the import table rather than a static function you can call directly.

Static linkage wouldn't work at all except import libraries add a "jump stub". That is a kinda of dummy function that just jumps to the real function. It's the equivalent to this:

fn CloseHandle(handle: HANDLE) -> BOOL {
    (*__imp_CloseHandle)(handle)
}

So there's an extra level of indirection. Like, I said it's super minor and only rarely matters.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const WAIT_FAILED: DWORD = 4294967295u32;
const WAIT_OBJECT_0: DWORD = 0u32;
const WAIT_TIMEOUT: DWORD = 258u32;

#[link(name = "kernel32")]
extern "system" {
fn CloseHandle(handle: HANDLE) -> BOOL;
fn SetEvent(hEvent: HANDLE) -> BOOL;
Expand Down Expand Up @@ -64,6 +65,10 @@ extern "system" {
) -> HANDLE;
fn OpenSemaphoreA(dwDesiredAccess: DWORD, bInheritHandle: BOOL, lpName: *const i8) -> HANDLE;
fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
}

#[link(name = "advapi32")]
extern "system" {
#[link_name = "SystemFunction036"]
fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> u8;
}
Expand Down