vibeio 0.2.8

A high-performance, cross-platform asynchronous runtime for Rust
Documentation
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
use std::cell::RefCell;
use std::ffi::c_void;
use std::io::{self, ErrorKind};
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle, RawHandle};
use std::ptr;
use std::sync::Arc;
use std::task::Waker;
use std::time::Duration;

use mio::{Interest, Token};
use slab::Slab;
use windows_sys::Wdk::Foundation::OBJECT_ATTRIBUTES;
use windows_sys::Wdk::Storage::FileSystem::{
    FileReplaceCompletionInformation, NtCancelIoFileEx, NtCreateFile, NtSetInformationFile,
    FILE_COMPLETION_INFORMATION, FILE_OPEN,
};
use windows_sys::Wdk::System::IO::NtDeviceIoControlFile;
use windows_sys::Win32::Foundation::{
    RtlNtStatusToDosError, ERROR_ABANDONED_WAIT_0, HANDLE, INVALID_HANDLE_VALUE, NTSTATUS,
    OBJ_CASE_INSENSITIVE, UNICODE_STRING, WAIT_TIMEOUT,
};
use windows_sys::Win32::Networking::WinSock::{
    self as WinSock, INVALID_SOCKET, SIO_BASE_HANDLE, SIO_BSP_HANDLE_POLL, SOCKET, SOCKET_ERROR,
};
use windows_sys::Win32::Storage::FileSystem::{
    SetFileCompletionNotificationModes, FILE_SHARE_READ, FILE_SHARE_WRITE,
};
use windows_sys::Win32::System::IO::{
    CreateIoCompletionPort, GetQueuedCompletionStatusEx, PostQueuedCompletionStatus,
    IO_STATUS_BLOCK, OVERLAPPED, OVERLAPPED_ENTRY,
};

use crate::driver::{CompletionIoResult, Interruptor};
use crate::{
    driver::{Driver, RegistrationMode},
    fd_inner::{InnerRawHandle, RawOsHandle},
    op::Op,
};

const INTERRUPT_KEY: usize = usize::MAX;
const AFD_POLL_COMPLETION_KEY: usize = usize::MAX - 1;
const IOCP_BATCH_SIZE: usize = 128;

const STATUS_PENDING: NTSTATUS = 0x0000_0103;
const IOCTL_AFD_POLL: u32 = 0x0001_2024;
const FILE_SKIP_SET_EVENT_ON_HANDLE: u8 = 0x02;
const SYNCHRONIZE_ACCESS: u32 = 0x0010_0000;

const AFD_POLL_RECEIVE: u32 = 0x0001;
const AFD_POLL_RECEIVE_EXPEDITED: u32 = 0x0002;
const AFD_POLL_SEND: u32 = 0x0004;
const AFD_POLL_DISCONNECT: u32 = 0x0008;
const AFD_POLL_ABORT: u32 = 0x0010;
const AFD_POLL_LOCAL_CLOSE: u32 = 0x0020;
const AFD_POLL_CONNECT: u32 = 0x0040;
const AFD_POLL_ACCEPT: u32 = 0x0080;
const AFD_POLL_CONNECT_FAIL: u32 = 0x0100;

#[repr(C)]
#[derive(Clone, Copy, Default)]
struct AfdPollHandleInfo {
    handle: HANDLE,
    events: u32,
    status: NTSTATUS,
}

#[repr(C)]
#[derive(Clone, Copy)]
struct AfdPollInfo {
    timeout: i64,
    number_of_handles: u32,
    exclusive: u32,
    handles: [AfdPollHandleInfo; 1],
}

impl AfdPollInfo {
    #[inline]
    fn new(socket: SOCKET, events: u32) -> Self {
        Self {
            timeout: i64::MAX,
            number_of_handles: 1,
            exclusive: 0,
            handles: [AfdPollHandleInfo {
                handle: socket as HANDLE,
                events,
                status: 0,
            }],
        }
    }
}

pub struct IocpInterruptor {
    port: std::sync::Weak<OwnedHandle>,
}

impl Interruptor for IocpInterruptor {
    #[inline]
    fn interrupt(&self) {
        if let Some(port) = self.port.upgrade() {
            let _ = unsafe {
                PostQueuedCompletionStatus(
                    port.as_raw_handle() as HANDLE,
                    0,
                    INTERRUPT_KEY,
                    ptr::null_mut(),
                )
            };
        }
    }
}

struct Completion {
    waiter: Option<Waker>,
    completed: Option<i32>,
    overlapped: Option<Box<OverlappedCtx>>,
    ignored_data: Option<Box<dyn std::any::Any>>,
}

#[repr(C)]
struct OverlappedCtx {
    overlapped: OVERLAPPED,
    token: usize,
}

#[repr(C)]
struct AfdIoStatusCtx {
    io_status: IO_STATUS_BLOCK,
    token: usize,
}

struct PollOp {
    registration_token: usize,
    io_status: Box<AfdIoStatusCtx>,
    _input: Box<AfdPollInfo>,
    _output: Box<AfdPollInfo>,
}

struct PollRegistration {
    socket: SOCKET,
    waiter: Option<Waker>,
    interest: Interest,
    poll_token: Option<usize>,
}

enum HandleRegistration {
    Completion,
    Poll(PollRegistration),
}

struct DriverState {
    registrations: Slab<HandleRegistration>,
    completions: Slab<Completion>,
    poll_ops: Slab<PollOp>,
}

pub struct IocpDriver {
    port: Arc<OwnedHandle>,
    afd: RefCell<Option<OwnedHandle>>,
    state: RefCell<DriverState>,
}

impl IocpDriver {
    #[inline]
    pub(crate) fn new() -> Result<Self, io::Error> {
        let port = unsafe {
            CreateIoCompletionPort(INVALID_HANDLE_VALUE as HANDLE, ptr::null_mut(), 0, 0)
        };
        if port.is_null() {
            return Err(io::Error::last_os_error());
        }

        Ok(Self {
            port: Arc::new(unsafe { OwnedHandle::from_raw_handle(port as RawHandle) }),
            afd: RefCell::new(None),
            state: RefCell::new(DriverState {
                registrations: Slab::with_capacity(1024),
                completions: Slab::with_capacity(1024),
                poll_ops: Slab::with_capacity(1024),
            }),
        })
    }

    #[inline]
    fn update_waiter(waiter_slot: &mut Option<Waker>, waker: Waker) {
        if !waiter_slot
            .as_ref()
            .is_some_and(|waiter| waiter.will_wake(&waker))
        {
            *waiter_slot = Some(waker);
        }
    }

    #[inline]
    fn iocp_handle(&self) -> HANDLE {
        self.port.as_raw_handle() as HANDLE
    }

    #[inline]
    fn status_is_success(status: NTSTATUS) -> bool {
        status >= 0
    }

    #[inline]
    fn ntstatus_to_io_error(status: NTSTATUS) -> io::Error {
        let mapped = unsafe { RtlNtStatusToDosError(status) } as i32;
        if mapped != 0 {
            io::Error::from_raw_os_error(mapped)
        } else {
            io::Error::from_raw_os_error(status)
        }
    }

    #[inline]
    fn raw_os_handle_to_windows_handle(handle: RawOsHandle) -> HANDLE {
        match handle {
            RawOsHandle::Socket(socket) => socket as HANDLE,
            RawOsHandle::Handle(handle) => handle as HANDLE,
        }
    }

    #[inline]
    fn raw_os_handle_to_socket(handle: RawOsHandle) -> Result<SOCKET, io::Error> {
        match handle {
            RawOsHandle::Socket(socket) => Ok(socket as SOCKET),
            RawOsHandle::Handle(_) => Err(io::Error::new(
                ErrorKind::Unsupported,
                "poll mode currently supports sockets only on Windows",
            )),
        }
    }

    #[inline]
    fn duration_to_timeout_ms(timeout: Option<Duration>) -> u32 {
        match timeout {
            Some(timeout) => timeout.as_millis().min(u32::MAX as u128) as u32,
            None => u32::MAX,
        }
    }

    #[inline]
    fn completion_result_from_entry(entry: &OVERLAPPED_ENTRY) -> i32 {
        if entry.Internal == 0 {
            return entry.dwNumberOfBytesTransferred as i32;
        }

        let ntstatus = entry.Internal as i32;
        let win32_error = unsafe { RtlNtStatusToDosError(ntstatus) } as i32;
        let mapped_error = if win32_error == 0 {
            ntstatus
        } else {
            win32_error
        };
        -mapped_error
    }

    #[inline]
    fn interest_to_afd_events(interest: Interest) -> u32 {
        let mut events = AFD_POLL_DISCONNECT | AFD_POLL_ABORT | AFD_POLL_LOCAL_CLOSE;

        if interest.is_readable() {
            events |= AFD_POLL_RECEIVE | AFD_POLL_RECEIVE_EXPEDITED | AFD_POLL_ACCEPT;
        }
        if interest.is_writable() {
            events |= AFD_POLL_SEND | AFD_POLL_CONNECT | AFD_POLL_CONNECT_FAIL;
        }

        if events == (AFD_POLL_DISCONNECT | AFD_POLL_ABORT | AFD_POLL_LOCAL_CLOSE) {
            events |= AFD_POLL_RECEIVE | AFD_POLL_SEND | AFD_POLL_ACCEPT | AFD_POLL_CONNECT;
        }

        events
    }

    #[inline]
    fn get_base_socket(socket: SOCKET, ioctl: u32) -> Result<SOCKET, io::Error> {
        let mut base_socket: SOCKET = INVALID_SOCKET;
        let mut bytes: u32 = 0;
        let result = unsafe {
            WinSock::WSAIoctl(
                socket,
                ioctl,
                ptr::null_mut(),
                0,
                (&mut base_socket as *mut SOCKET).cast(),
                std::mem::size_of::<SOCKET>() as u32,
                &mut bytes,
                ptr::null_mut(),
                None,
            )
        };

        if result == SOCKET_ERROR {
            let err = unsafe { WinSock::WSAGetLastError() };
            return Err(io::Error::from_raw_os_error(err));
        }

        Ok(base_socket)
    }

    #[inline]
    fn resolve_base_socket(mut socket: SOCKET) -> Result<SOCKET, io::Error> {
        loop {
            if let Ok(base_socket) = Self::get_base_socket(socket, SIO_BASE_HANDLE) {
                return Ok(base_socket);
            }

            match Self::get_base_socket(socket, SIO_BSP_HANDLE_POLL) {
                Ok(base_socket) if base_socket != INVALID_SOCKET && base_socket != socket => {
                    socket = base_socket;
                }
                Ok(_) => {
                    return Err(io::Error::new(
                        ErrorKind::Other,
                        "failed to resolve base socket for AFD polling",
                    ))
                }
                Err(err) => return Err(err),
            }
        }
    }

    #[inline]
    fn open_afd_handle() -> Result<OwnedHandle, io::Error> {
        let device_name = format!("\\Device\\Afd\\vibeio-{}", std::process::id());
        let mut device_name_utf16: Vec<u16> = device_name.encode_utf16().collect();
        let name_len = (device_name_utf16.len() * std::mem::size_of::<u16>()) as u16;

        let object_name = UNICODE_STRING {
            Length: name_len,
            MaximumLength: name_len,
            Buffer: device_name_utf16.as_mut_ptr(),
        };
        let object_attributes = OBJECT_ATTRIBUTES {
            Length: std::mem::size_of::<OBJECT_ATTRIBUTES>() as u32,
            RootDirectory: ptr::null_mut(),
            ObjectName: &object_name,
            Attributes: OBJ_CASE_INSENSITIVE,
            SecurityDescriptor: ptr::null(),
            SecurityQualityOfService: ptr::null(),
        };

        let mut afd_handle: HANDLE = ptr::null_mut();
        let mut create_status = IO_STATUS_BLOCK::default();
        let status = unsafe {
            NtCreateFile(
                &mut afd_handle,
                SYNCHRONIZE_ACCESS,
                &object_attributes,
                &mut create_status,
                ptr::null(),
                0,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                FILE_OPEN,
                0,
                ptr::null(),
                0,
            )
        };

        if !Self::status_is_success(status) {
            return Err(Self::ntstatus_to_io_error(status));
        }
        if afd_handle.is_null() {
            return Err(io::Error::new(
                ErrorKind::Other,
                "NtCreateFile returned a null AFD handle",
            ));
        }

        Ok(unsafe { OwnedHandle::from_raw_handle(afd_handle as RawHandle) })
    }

    #[inline]
    fn ensure_afd_handle(&self) -> Result<HANDLE, io::Error> {
        {
            let afd = self.afd.borrow();
            if let Some(afd) = afd.as_ref() {
                return Ok(afd.as_raw_handle() as HANDLE);
            }
        }

        let mut afd_slot = self.afd.borrow_mut();
        if afd_slot.is_none() {
            let afd = Self::open_afd_handle()?;
            let afd_handle = afd.as_raw_handle() as HANDLE;

            let completion_port = unsafe {
                CreateIoCompletionPort(afd_handle, self.iocp_handle(), AFD_POLL_COMPLETION_KEY, 0)
            };
            if completion_port.is_null() {
                return Err(io::Error::last_os_error());
            }

            if unsafe {
                SetFileCompletionNotificationModes(afd_handle, FILE_SKIP_SET_EVENT_ON_HANDLE)
            } == 0
            {
                return Err(io::Error::last_os_error());
            }

            *afd_slot = Some(afd);
        }

        Ok(afd_slot
            .as_ref()
            .expect("AFD handle must be initialized")
            .as_raw_handle() as HANDLE)
    }

    #[inline]
    fn arm_poll_operation(
        &self,
        registration_token: usize,
        socket: SOCKET,
        interest: Interest,
    ) -> Result<(), io::Error> {
        let afd_handle = self.ensure_afd_handle()?;
        let poll_events = Self::interest_to_afd_events(interest);

        let (poll_token, io_status_ptr, input_ptr, output_ptr) = {
            let mut state = self.state.borrow_mut();

            match state.registrations.get(registration_token) {
                Some(HandleRegistration::Poll(registration)) => {
                    if registration.poll_token.is_some() {
                        return Ok(());
                    }
                }
                Some(HandleRegistration::Completion) => {
                    return Err(io::Error::new(
                        ErrorKind::Unsupported,
                        format!(
                            "I/O token {} is registered for completion mode, not poll mode",
                            registration_token
                        ),
                    ));
                }
                None => {
                    return Err(io::Error::new(
                        ErrorKind::NotFound,
                        format!(
                            "I/O token {} is not registered with this driver",
                            registration_token
                        ),
                    ));
                }
            }

            let mut io_status = Box::new(unsafe { std::mem::zeroed::<AfdIoStatusCtx>() });
            let mut input = Box::new(AfdPollInfo::new(socket, poll_events));
            let mut output = Box::new(AfdPollInfo::new(socket, 0));

            let poll_entry = state.poll_ops.vacant_entry();
            let poll_token = poll_entry.key();
            io_status.token = poll_token;

            let io_status_ptr = &mut io_status.io_status as *mut IO_STATUS_BLOCK;
            let input_ptr = (&mut *input as *mut AfdPollInfo).cast();
            let output_ptr = (&mut *output as *mut AfdPollInfo).cast();

            poll_entry.insert(PollOp {
                registration_token,
                io_status,
                _input: input,
                _output: output,
            });
            if let Some(HandleRegistration::Poll(registration)) =
                state.registrations.get_mut(registration_token)
            {
                registration.poll_token = Some(poll_token);
            }

            (poll_token, io_status_ptr, input_ptr, output_ptr)
        };

        let status = unsafe {
            NtDeviceIoControlFile(
                afd_handle,
                ptr::null_mut(),
                None,
                io_status_ptr as *const c_void,
                io_status_ptr,
                IOCTL_AFD_POLL,
                input_ptr,
                std::mem::size_of::<AfdPollInfo>() as u32,
                output_ptr,
                std::mem::size_of::<AfdPollInfo>() as u32,
            )
        };

        if Self::status_is_success(status) || status == STATUS_PENDING {
            return Ok(());
        }

        let mut state = self.state.borrow_mut();
        let _ = state.poll_ops.try_remove(poll_token);
        if let Some(HandleRegistration::Poll(registration)) =
            state.registrations.get_mut(registration_token)
        {
            if registration.poll_token == Some(poll_token) {
                registration.poll_token = None;
            }
        }

        Err(Self::ntstatus_to_io_error(status))
    }

    #[inline]
    fn cancel_poll_operation(&self, poll_token: usize) {
        let afd_handle = {
            let afd = self.afd.borrow();
            let Some(afd) = afd.as_ref() else {
                return;
            };
            afd.as_raw_handle() as HANDLE
        };

        let io_status_ptr = {
            let state = self.state.borrow();
            let Some(poll_op) = state.poll_ops.get(poll_token) else {
                return;
            };
            &poll_op.io_status.io_status as *const IO_STATUS_BLOCK
        };

        let mut cancel_status = IO_STATUS_BLOCK::default();
        let _ = unsafe { NtCancelIoFileEx(afd_handle, io_status_ptr, &mut cancel_status) };
    }

    #[inline]
    fn disassociate_iocp_handle(&self, handle: &InnerRawHandle) {
        let windows_handle = Self::raw_os_handle_to_windows_handle(handle.handle);
        let info: FILE_COMPLETION_INFORMATION = FILE_COMPLETION_INFORMATION {
            Port: std::ptr::null_mut(),
            Key: std::ptr::null_mut(),
        };
        let mut status = IO_STATUS_BLOCK::default();
        let _ = unsafe {
            NtSetInformationFile(
                windows_handle,
                &mut status,
                &info as *const FILE_COMPLETION_INFORMATION as *const c_void,
                std::mem::size_of::<FILE_COMPLETION_INFORMATION>() as u32,
                FileReplaceCompletionInformation,
            )
        };
    }

    #[inline]
    fn process_entries(&self, entries: &[OVERLAPPED_ENTRY]) {
        let mut state = self.state.borrow_mut();
        for entry in entries {
            if entry.lpCompletionKey == INTERRUPT_KEY {
                continue;
            }

            if entry.lpCompletionKey == AFD_POLL_COMPLETION_KEY {
                if entry.lpOverlapped.is_null() {
                    continue;
                }

                // SAFETY: every AFD poll submission passes a pointer to AfdIoStatusCtx::io_status,
                // and AfdIoStatusCtx is repr(C) with io_status as its first field.
                let poll_token = unsafe { (*entry.lpOverlapped.cast::<AfdIoStatusCtx>()).token };
                let registration_token = state
                    .poll_ops
                    .get(poll_token)
                    .map(|poll| poll.registration_token);
                let _ = state.poll_ops.try_remove(poll_token);

                if let Some(registration_token) = registration_token {
                    if let Some(HandleRegistration::Poll(registration)) =
                        state.registrations.get_mut(registration_token)
                    {
                        if registration.poll_token == Some(poll_token) {
                            registration.poll_token = None;
                        }
                        if let Some(waiter) = registration.waiter.take() {
                            waiter.wake();
                        }
                    }
                }
                continue;
            }

            if entry.lpOverlapped.is_null() {
                continue;
            }

            // SAFETY: every OVERLAPPED pointer submitted by completion operations points to the
            // first field of OverlappedCtx (repr(C), first field), and lives in Completion::overlapped
            // until consumed here.
            let completion_token = unsafe { (*entry.lpOverlapped.cast::<OverlappedCtx>()).token };
            if let Some(completion) = state.completions.get_mut(completion_token) {
                completion.completed = Some(Self::completion_result_from_entry(entry));
                completion.overlapped = None;
                if let Some(waiter) = completion.waiter.take() {
                    waiter.wake();
                }
                if completion.ignored_data.is_some() {
                    state.completions.remove(completion_token);
                }
            }
        }
    }

    #[inline]
    fn process_batch(&self, timeout_ms: u32) -> Result<usize, io::Error> {
        let mut entries = [OVERLAPPED_ENTRY::default(); IOCP_BATCH_SIZE];
        let mut entries_removed: u32 = 0;

        let success = unsafe {
            GetQueuedCompletionStatusEx(
                self.iocp_handle(),
                entries.as_mut_ptr(),
                entries.len() as u32,
                &mut entries_removed,
                timeout_ms,
                0,
            )
        } != 0;

        if !success {
            let err = io::Error::last_os_error();
            if matches!(
                err.raw_os_error(),
                Some(code) if code == WAIT_TIMEOUT as i32 || code == ERROR_ABANDONED_WAIT_0 as i32
            ) {
                return Ok(0);
            }
            return Err(err);
        }

        if entries_removed == 0 {
            return Ok(0);
        }

        self.process_entries(&entries[..entries_removed as usize]);

        Ok(entries_removed as usize)
    }

    #[inline]
    fn process_ready_completions(&self) -> Result<(), io::Error> {
        while self.process_batch(0)? > 0 {}
        Ok(())
    }
}

impl Driver for IocpDriver {
    type Interruptor = IocpInterruptor;

    #[inline]
    fn flush(&self) {
        match self.process_ready_completions() {
            Ok(_) => {}
            Err(err) if err.kind() == io::ErrorKind::Interrupted => {}
            Err(err) => panic!("iocp flush failed while processing completions: {err}"),
        }
    }

    #[inline]
    fn wait(&self, timeout: Option<Duration>) {
        let timeout_ms = Self::duration_to_timeout_ms(timeout);
        match self.process_batch(timeout_ms) {
            Ok(processed) if processed > 0 => {
                if let Err(err) = self.process_ready_completions() {
                    panic!("iocp drain failed while waiting for I/O: {err}");
                }
            }
            Ok(_) => {}
            Err(err) if err.kind() == io::ErrorKind::Interrupted => {}
            Err(err) => panic!("iocp wait failed while waiting for I/O: {err}"),
        }
    }

    #[inline]
    fn register_handle(
        &self,
        handle: &InnerRawHandle,
        interest: Interest,
    ) -> Result<Token, io::Error> {
        self.register_handle_with_mode(handle, interest, RegistrationMode::Completion)
    }

    #[inline]
    fn register_handle_with_mode(
        &self,
        handle: &InnerRawHandle,
        interest: Interest,
        mode: RegistrationMode,
    ) -> Result<Token, io::Error> {
        match mode {
            RegistrationMode::Completion => {
                let token = {
                    let mut state = self.state.borrow_mut();
                    let entry = state.registrations.vacant_entry();
                    let token = Token(entry.key());
                    entry.insert(HandleRegistration::Completion);
                    token
                };

                let source_handle = Self::raw_os_handle_to_windows_handle(handle.handle);
                let completion_port = unsafe {
                    CreateIoCompletionPort(source_handle, self.iocp_handle(), token.0, 0)
                };
                if completion_port.is_null() {
                    let mut state = self.state.borrow_mut();
                    let _ = state.registrations.try_remove(token.0);
                    return Err(io::Error::last_os_error());
                }

                Ok(token)
            }
            RegistrationMode::Poll => {
                let socket = Self::raw_os_handle_to_socket(handle.handle)?;
                let base_socket = Self::resolve_base_socket(socket)?;
                self.ensure_afd_handle()?;

                let mut state = self.state.borrow_mut();
                let entry = state.registrations.vacant_entry();
                let token = Token(entry.key());
                entry.insert(HandleRegistration::Poll(PollRegistration {
                    socket: base_socket,
                    waiter: None,
                    interest,
                    poll_token: None,
                }));
                Ok(token)
            }
        }
    }

    #[inline]
    fn reregister_handle(
        &self,
        handle: &InnerRawHandle,
        interest: Interest,
    ) -> Result<(), io::Error> {
        let mut state = self.state.borrow_mut();
        match state.registrations.get_mut(handle.token.0) {
            Some(HandleRegistration::Completion) => Ok(()),
            Some(HandleRegistration::Poll(registration)) => {
                registration.interest = interest;
                Ok(())
            }
            None => Err(io::Error::new(
                ErrorKind::NotFound,
                format!(
                    "I/O token {} is not registered with this driver",
                    handle.token.0
                ),
            )),
        }
    }

    #[inline]
    fn deregister_handle(&self, handle: &InnerRawHandle) -> Result<(), io::Error> {
        let poll_token = {
            let mut state = self.state.borrow_mut();
            match state.registrations.try_remove(handle.token.0) {
                Some(HandleRegistration::Completion) => None,
                Some(HandleRegistration::Poll(registration)) => registration.poll_token,
                None => {
                    return Err(io::Error::new(
                        ErrorKind::NotFound,
                        format!(
                            "I/O token {} is not registered with this driver",
                            handle.token.0
                        ),
                    ))
                }
            }
        };

        if let Some(poll_token) = poll_token {
            // Poll-based I/O
            self.cancel_poll_operation(poll_token);
        } else {
            // Completion-based I/O
            self.disassociate_iocp_handle(handle);
        }

        Ok(())
    }

    #[inline]
    fn supports_completion(&self) -> bool {
        true
    }

    #[inline]
    fn submit_completion<O>(&self, op: &mut O, waker: Waker) -> CompletionIoResult
    where
        O: Op,
    {
        let (completion_token, overlapped_ptr) = {
            let mut state = self.state.borrow_mut();
            let vacant_completion = state.completions.vacant_entry();
            let completion_token = vacant_completion.key();

            let mut overlapped = Box::new(unsafe { std::mem::zeroed::<OverlappedCtx>() });
            overlapped.token = completion_token;
            let overlapped_ptr: *mut OVERLAPPED = &mut overlapped.overlapped;

            vacant_completion.insert(Completion {
                waiter: Some(waker),
                completed: None,
                overlapped: Some(overlapped),
                ignored_data: None,
            });

            (completion_token, overlapped_ptr)
        };

        if let Err(err) = op.submit_windows(overlapped_ptr) {
            let mut state = self.state.borrow_mut();
            let _ = state.completions.try_remove(completion_token);
            return CompletionIoResult::SubmitErr(err);
        }

        CompletionIoResult::Retry(completion_token)
    }

    #[inline]
    fn submit_poll(
        &self,
        handle: &InnerRawHandle,
        waker: Waker,
        interest: Interest,
    ) -> Result<(), io::Error> {
        let (socket, should_arm) = {
            let mut state = self.state.borrow_mut();
            let registration = state.registrations.get_mut(handle.token.0).ok_or_else(|| {
                io::Error::new(
                    ErrorKind::NotFound,
                    format!(
                        "I/O token {} is not registered with this driver",
                        handle.token.0
                    ),
                )
            })?;

            let HandleRegistration::Poll(registration) = registration else {
                return Err(io::Error::new(
                    ErrorKind::Unsupported,
                    format!(
                        "I/O token {} is registered for completion mode, not poll mode",
                        handle.token.0
                    ),
                ));
            };

            registration.interest = interest;
            Self::update_waiter(&mut registration.waiter, waker);
            (registration.socket, registration.poll_token.is_none())
        };

        if should_arm {
            self.arm_poll_operation(handle.token.0, socket, interest)?;
        }

        Ok(())
    }

    #[inline]
    fn get_completion_result(&self, token: usize) -> Option<i32> {
        let mut state = self.state.borrow_mut();
        let completed = state
            .completions
            .get(token)
            .and_then(|completion| completion.completed);
        if completed.is_some() {
            state.completions.remove(token);
        }
        completed
    }

    #[inline]
    fn set_completion_waker(&self, token: usize, waker: Waker) {
        let mut state = self.state.borrow_mut();
        if let Some(completion) = state.completions.get_mut(token) {
            Self::update_waiter(&mut completion.waiter, waker);
        }
    }

    #[inline]
    fn get_interruptor(&self) -> Self::Interruptor {
        IocpInterruptor {
            port: Arc::downgrade(&self.port),
        }
    }

    #[inline]
    fn ignore_completion(&self, token: usize, data: Box<dyn std::any::Any>) {
        let mut state = self.state.borrow_mut();
        if let Some(c) = state.completions.get_mut(token) {
            c.ignored_data = Some(data);
        }
    }
}