summaryrefslogtreecommitdiffstats
path: root/third_party/rust/pulse/src/threaded_mainloop.rs
blob: 74d2410d637c5d41ed925945171874657d9baed0 (plain)
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
// Copyright © 2017 Mozilla Foundation
//
// This program is made available under an ISC-style license.  See the
// accompanying file LICENSE for details.

use ffi;
use mainloop_api;
use mainloop_api::MainloopApi;
use ErrorCode;
use Result;

#[derive(Debug)]
pub struct ThreadedMainloop(*mut ffi::pa_threaded_mainloop);

impl ThreadedMainloop {
    pub unsafe fn from_raw_ptr(raw: *mut ffi::pa_threaded_mainloop) -> Self {
        ThreadedMainloop(raw)
    }

    pub fn new() -> Self {
        unsafe { ThreadedMainloop::from_raw_ptr(ffi::pa_threaded_mainloop_new()) }
    }

    pub fn raw_mut(&self) -> &mut ffi::pa_threaded_mainloop {
        unsafe { &mut *self.0 }
    }

    pub fn is_null(&self) -> bool {
        self.0.is_null()
    }

    pub fn start(&self) -> Result<()> {
        match unsafe { ffi::pa_threaded_mainloop_start(self.raw_mut()) } {
            0 => Ok(()),
            _ => Err(ErrorCode::from_error_code(ffi::PA_ERR_UNKNOWN)),
        }
    }

    pub fn stop(&self) {
        unsafe {
            ffi::pa_threaded_mainloop_stop(self.raw_mut());
        }
    }

    pub fn lock(&self) {
        unsafe {
            ffi::pa_threaded_mainloop_lock(self.raw_mut());
        }
    }

    pub fn unlock(&self) {
        unsafe {
            ffi::pa_threaded_mainloop_unlock(self.raw_mut());
        }
    }

    pub fn wait(&self) {
        unsafe {
            ffi::pa_threaded_mainloop_wait(self.raw_mut());
        }
    }

    pub fn signal(&self) {
        unsafe {
            ffi::pa_threaded_mainloop_signal(self.raw_mut(), 0);
        }
    }

    pub fn get_api(&self) -> MainloopApi {
        unsafe { mainloop_api::from_raw_ptr(ffi::pa_threaded_mainloop_get_api(self.raw_mut())) }
    }

    pub fn in_thread(&self) -> bool {
        unsafe { ffi::pa_threaded_mainloop_in_thread(self.raw_mut()) != 0 }
    }
}

impl ::std::default::Default for ThreadedMainloop {
    fn default() -> Self {
        ThreadedMainloop(::std::ptr::null_mut())
    }
}

impl ::std::ops::Drop for ThreadedMainloop {
    fn drop(&mut self) {
        if !self.is_null() {
            unsafe {
                ffi::pa_threaded_mainloop_free(self.raw_mut());
            }
        }
    }
}