Skip to content

Commit 392d1c0

Browse files
authored
More overlapped implementation (#5748)
1 parent d46bcd9 commit 392d1c0

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

stdlib/src/overlapped.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ mod _overlapped {
297297
}
298298
}
299299

300+
unsafe fn u64_to_handle(raw_ptr_value: u64) -> HANDLE {
301+
raw_ptr_value as HANDLE
302+
}
303+
300304
#[pyfunction]
301305
fn CreateIoCompletionPort(
302306
handle: isize,
@@ -354,4 +358,56 @@ mod _overlapped {
354358
]);
355359
Ok(value.into())
356360
}
361+
362+
#[pyfunction]
363+
fn CreateEvent(
364+
event_attributes: PyObjectRef,
365+
manual_reset: bool,
366+
initial_state: bool,
367+
name: Option<String>,
368+
vm: &VirtualMachine,
369+
) -> PyResult<isize> {
370+
if !vm.is_none(&event_attributes) {
371+
return Err(vm.new_value_error("EventAttributes must be None".to_owned()));
372+
}
373+
374+
let name = match name {
375+
Some(name) => {
376+
let name = widestring::WideCString::from_str(&name).unwrap();
377+
name.as_ptr()
378+
}
379+
None => std::ptr::null(),
380+
};
381+
let event = unsafe {
382+
windows_sys::Win32::System::Threading::CreateEventW(
383+
std::ptr::null(),
384+
manual_reset as _,
385+
initial_state as _,
386+
name,
387+
) as isize
388+
};
389+
if event == NULL {
390+
return Err(errno_err(vm));
391+
}
392+
Ok(event)
393+
}
394+
395+
#[pyfunction]
396+
fn SetEvent(handle: u64, vm: &VirtualMachine) -> PyResult<()> {
397+
let ret = unsafe { windows_sys::Win32::System::Threading::SetEvent(u64_to_handle(handle)) };
398+
if ret == 0 {
399+
return Err(errno_err(vm));
400+
}
401+
Ok(())
402+
}
403+
404+
#[pyfunction]
405+
fn ResetEvent(handle: u64, vm: &VirtualMachine) -> PyResult<()> {
406+
let ret =
407+
unsafe { windows_sys::Win32::System::Threading::ResetEvent(u64_to_handle(handle)) };
408+
if ret == 0 {
409+
return Err(errno_err(vm));
410+
}
411+
Ok(())
412+
}
357413
}

0 commit comments

Comments
 (0)