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
use std::os::raw::c_void;
use std::rc::Rc;
use libc::timeval;
use crate::mainloop::api::{MainloopApi, MainloopInnerType};
use crate::time::{UnixTs, MonotonicTs, Timeval, USEC_INVALID};
use crate::callbacks::MultiUseCallback;
pub use capi::pa_time_event as TimeEventInternal;
pub struct TimeEvent<T>
where T: MainloopInnerType
{
ptr: *mut TimeEventInternal,
owner: Rc<T>,
_saved_cb: EventCb,
}
pub struct TimeEventRef<T: 'static>
where T: MainloopInnerType
{
ptr: *mut TimeEventInternal,
owner: Rc<T>,
}
pub(crate) type EventCb = MultiUseCallback<dyn FnMut(*mut TimeEventInternal),
extern "C" fn(a: *const MainloopApi, e: *mut TimeEventInternal, tv: *const timeval,
userdata: *mut c_void)>;
impl<T> TimeEvent<T>
where T: MainloopInnerType
{
#[inline]
pub(crate) fn from_raw(ptr: *mut TimeEventInternal, mainloop_inner: Rc<T>, callback: EventCb)
-> Self
{
assert_eq!(false, ptr.is_null());
Self { ptr: ptr, owner: mainloop_inner, _saved_cb: callback }
}
#[inline]
pub fn restart(&mut self, t: &UnixTs) {
let fn_ptr = (*self.owner).get_api().time_restart.unwrap();
fn_ptr(self.ptr, &(t.0).0);
}
pub fn restart_rt(&mut self, t: MonotonicTs) {
assert_ne!(t.0, USEC_INVALID);
let mut tv = Timeval::new_zero();
tv.set_rt(t.0, (*self.owner).supports_rtclock());
let fn_ptr = (*self.owner).get_api().time_restart.unwrap();
fn_ptr(self.ptr, &tv.0);
}
}
impl<T> TimeEventRef<T>
where T: MainloopInnerType
{
pub(crate) fn from_raw(ptr: *mut TimeEventInternal, mainloop_inner: Rc<T>) -> Self {
assert_eq!(false, ptr.is_null());
Self { ptr: ptr, owner: mainloop_inner }
}
#[inline]
pub fn restart(&mut self, t: &UnixTs) {
let fn_ptr = (*self.owner).get_api().time_restart.unwrap();
fn_ptr(self.ptr, &(t.0).0);
}
pub fn restart_rt(&mut self, t: MonotonicTs) {
assert_ne!(t.0, USEC_INVALID);
let mut tv = Timeval::new_zero();
tv.set_rt(t.0, (*self.owner).supports_rtclock());
let fn_ptr = (*self.owner).get_api().time_restart.unwrap();
fn_ptr(self.ptr, &tv.0);
}
}
impl<T> Drop for TimeEvent<T>
where T: MainloopInnerType
{
fn drop(&mut self) {
let fn_ptr = (*self.owner).get_api().time_free.unwrap();
fn_ptr(self.ptr);
}
}
pub(crate)
extern "C"
fn event_cb_proxy(_: *const MainloopApi, e: *mut TimeEventInternal, _: *const timeval,
userdata: *mut c_void)
{
let _ = std::panic::catch_unwind(|| {
let callback = EventCb::get_callback(userdata);
(callback)(e);
});
}