-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cndrv): 实现 context、mem、queue、notifier
Signed-off-by: YdrMaster <ydrml@hotmail.com>
- Loading branch information
Showing
6 changed files
with
414 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::{bindings as cn, impl_spore, AsRaw, Queue}; | ||
use std::{marker::PhantomData, ptr::null_mut, time::Duration}; | ||
|
||
impl_spore!(Notifier and NotifierSpore by cn::CNnotifier); | ||
|
||
impl<'ctx> Queue<'ctx> { | ||
pub fn record(&self) -> Notifier<'ctx> { | ||
let mut event = null_mut(); | ||
cndrv!(cnCreateNotifier( | ||
&mut event, | ||
CNNotifierFlags::CN_NOTIFIER_DEFAULT as _ | ||
)); | ||
cndrv!(cnPlaceNotifier(event, self.as_raw())); | ||
Notifier(unsafe { self.ctx().wrap_resource(event) }, PhantomData) | ||
} | ||
} | ||
|
||
impl Drop for Notifier<'_> { | ||
#[inline] | ||
fn drop(&mut self) { | ||
cndrv!(cnDestroyNotifier(self.0.res)); | ||
} | ||
} | ||
|
||
impl AsRaw for Notifier<'_> { | ||
type Raw = cn::CNnotifier; | ||
#[inline] | ||
unsafe fn as_raw(&self) -> Self::Raw { | ||
self.0.res | ||
} | ||
} | ||
|
||
impl Queue<'_> { | ||
pub fn bench(&self, f: impl Fn(usize, &Self), times: usize, warm_up: usize) -> Duration { | ||
for i in 0..warm_up { | ||
f(i, self); | ||
} | ||
let start = self.record(); | ||
for i in 0..times { | ||
f(i, self); | ||
} | ||
let end = self.record(); | ||
end.synchronize(); | ||
end.elapse_from(&start).div_f32(times as _) | ||
} | ||
} | ||
|
||
impl Notifier<'_> { | ||
#[inline] | ||
pub fn synchronize(&self) { | ||
cndrv!(cnWaitNotifier(self.0.res)); | ||
} | ||
|
||
#[inline] | ||
pub fn elapse_from(&self, start: &Self) -> Duration { | ||
let mut ms = 0.0; | ||
cndrv!(cnNotifierElapsedTime(&mut ms, start.0.res, self.0.res)); | ||
Duration::from_secs_f32(ms * 1e-3) | ||
} | ||
} |
Oops, something went wrong.