1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::{io, ptr};
use winapi::um::synchapi::{CreateSemaphoreW, ReleaseSemaphore};
use crate::{nonnull_handle_result, result, Handle};
#[derive(Clone, Debug)]
pub struct Semaphore(Handle);
impl Semaphore {
pub fn new() -> io::Result<Self> {
let handle = nonnull_handle_result(unsafe {
CreateSemaphoreW(ptr::null_mut(), 0, 1, ptr::null_mut())
})?;
let handle = unsafe { Handle::from_raw(handle) };
Ok(Self(handle))
}
pub fn release(&self) -> io::Result<()> {
result(unsafe { ReleaseSemaphore(*self.0, 1, ptr::null_mut()) })
}
pub fn handle(&self) -> &Handle {
&self.0
}
}
unsafe impl Send for Semaphore {}
unsafe impl Sync for Semaphore {}