Debian Codegraph @

third_party/rust/pulse/src/threaded_mainloop.rs

raw
1 // Copyright © 2017 Mozilla Foundation
2 //
3 // This program is made available under an ISC-style license. See the
4 // accompanying file LICENSE for details.
5
6 use ffi;
7 use mainloop_api;
8 use mainloop_api::MainloopApi;
9 use ErrorCode;
10 use Result;
11
12 #[derive(Debug)]
13 pub struct ThreadedMainloop(*mut ffi::pa_threaded_mainloop);
14
15 impl ThreadedMainloop {
16 pub unsafe fn from_raw_ptr(raw: *mut ffi::pa_threaded_mainloop) -> Self {
17 ThreadedMainloop(raw)
18 }
19
20 pub fn new() -> Self {
21 unsafe { ThreadedMainloop::from_raw_ptr(ffi::pa_threaded_mainloop_new()) }
22 }
23
24 pub fn raw_mut(&self) -> &mut ffi::pa_threaded_mainloop {
25 unsafe { &mut *self.0 }
26 }
27
28 pub fn is_null(&self) -> bool {
29 self.0.is_null()
30 }
31
32 pub fn start(&self) -> Result<()> {
33 match unsafe { ffi::pa_threaded_mainloop_start(self.raw_mut()) } {
34 0 => Ok(()),
35 _ => Err(ErrorCode::from_error_code(ffi::PA_ERR_UNKNOWN)),
36 }
37 }
38
39 pub fn stop(&self) {
40 unsafe {
41 ffi::pa_threaded_mainloop_stop(self.raw_mut());
42 }
43 }
44
45 pub fn lock(&self) {
46 unsafe {
47 ffi::pa_threaded_mainloop_lock(self.raw_mut());
48 }
49 }
50
51 pub fn unlock(&self) {
52 unsafe {
53 ffi::pa_threaded_mainloop_unlock(self.raw_mut());
54 }
55 }
56
57 pub fn wait(&self) {
58 unsafe {
59 ffi::pa_threaded_mainloop_wait(self.raw_mut());
60 }
61 }
62
63 pub fn signal(&self) {
64 unsafe {
65 ffi::pa_threaded_mainloop_signal(self.raw_mut(), 0);
66 }
67 }
68
69 pub fn get_api(&self) -> MainloopApi {
70 unsafe { mainloop_api::from_raw_ptr(ffi::pa_threaded_mainloop_get_api(self.raw_mut())) }
71 }
72
73 pub fn in_thread(&self) -> bool {
74 unsafe { ffi::pa_threaded_mainloop_in_thread(self.raw_mut()) != 0 }
75 }
76 }
77
78 impl ::std::default::Default for ThreadedMainloop {
79 fn default() -> Self {
80 ThreadedMainloop(::std::ptr::null_mut())
81 }
82 }
83
84 impl ::std::ops::Drop for ThreadedMainloop {
85 fn drop(&mut self) {
86 if !self.is_null() {
87 unsafe {
88 ffi::pa_threaded_mainloop_free(self.raw_mut());
89 }
90 }
91 }
92 }