Skip to content

Commit

Permalink
Improve windows::Client::new impl (#39)
Browse files Browse the repository at this point in the history
 - Use `u128` for the random int in the name for named semaphore in
 `windows::Client::new` to minimize possibility of collision.
 - Format the int as hex in `windows::Client::new` to reduce size of
   the name for the named semaphore.
 - Replace `String` with `Box<str>` in `windows::Client`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu committed Mar 1, 2023
1 parent 4a6e368 commit b27ad7e
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const WAIT_OBJECT_1: u32 = WAIT_OBJECT_0 + 1;
#[derive(Debug)]
pub struct Client {
sem: Handle,
name: String,
name: Box<str>,
}

#[derive(Debug)]
Expand All @@ -58,13 +58,20 @@ impl Client {
// but don't try for too long.
let prefix = "__rust_jobslot_semaphore_";

let mut name = prefix.to_string();
let mut name = String::with_capacity(
prefix.len() +
// 32B for the max size of u128
32 +
// 1B for the null byte
1,
);
name.push_str(prefix);

for _ in 0..100 {
let mut bytes = [0; 4];
let mut bytes = [0; 16];
getrandom(&mut bytes)?;

write!(&mut name, "{}\0", u32::from_ne_bytes(bytes)).unwrap();
write!(&mut name, "{}\0", u128::from_ne_bytes(bytes)).unwrap();

let res = unsafe {
Handle::new_or_err(CreateSemaphoreA(
Expand All @@ -78,7 +85,10 @@ impl Client {
match res {
Ok(sem) => {
name.pop(); // chop off the trailing nul
let client = Client { sem, name };
let client = Client {
sem,
name: name.into_boxed_str(),
};
if create_limit != limit {
client.acquire()?;
}
Expand Down Expand Up @@ -120,7 +130,7 @@ impl Client {
);
Handle::new(sem).map(|sem| Client {
sem,
name: s.to_string(),
name: s.into(),
})
}

Expand Down

0 comments on commit b27ad7e

Please sign in to comment.