aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys/unix/condvar.rs
blob: 51ff66545d3b8e04c2917684a04a4453df1b0ba5 (plain) (blame)
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
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// *Implementation adapted from `/sys/redox/condvar.rs`

use cell::UnsafeCell;
use intrinsics::atomic_cxchg;
use ptr;
use time::Duration;

use sys::mutex::{self, Mutex};

use libctru::synchronization::{__sync_get_arbiter, LightLock};
use libctru::svc::{svcArbitrateAddress, ArbitrationType};

pub struct Condvar {
    lock: UnsafeCell<*mut LightLock>,
}

unsafe impl Send for Condvar {}
unsafe impl Sync for Condvar {}

impl Condvar {
    pub const fn new() -> Condvar {
        Condvar {
            lock: UnsafeCell::new(ptr::null_mut()),
        }
    }

    #[inline]
    pub unsafe fn init(&self) {
        *self.lock.get() = ptr::null_mut();
    }

    #[inline]
    pub fn notify_one(&self) {
        unsafe {
            let arbiter = __sync_get_arbiter();

            svcArbitrateAddress(arbiter,
                                *self.lock.get() as u32,
                                ArbitrationType::ARBITRATION_SIGNAL,
                                1,
                                0);
        }
    }

    #[inline]
    pub fn notify_all(&self) {
        unsafe {
            let lock = self.lock.get();

            if *lock == ptr::null_mut() {
                return;
            }

            let arbiter = __sync_get_arbiter();

            svcArbitrateAddress(arbiter,
                                *self.lock.get() as u32,
                                ArbitrationType::ARBITRATION_SIGNAL,
                                -1,
                                0);
        }
    }

    #[inline]
    pub fn wait(&self, mutex: &Mutex) {
        unsafe {
            let lock = self.lock.get();

            if *lock != mutex::raw(mutex) {
                if *lock != ptr::null_mut() {
                    panic!("Condvar used with more than one Mutex");
                }

                atomic_cxchg(lock as *mut usize, 0, mutex::raw(mutex) as usize);
            }

            mutex.unlock();

            let arbiter = __sync_get_arbiter();

            svcArbitrateAddress(arbiter,
                                *self.lock.get() as u32,
                                ArbitrationType::ARBITRATION_WAIT_IF_LESS_THAN,
                                2,
                                0);

            mutex.lock();
        }
    }

    #[inline]
    pub fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
        use time::Instant;

        unsafe {
            let lock = self.lock.get();

            if *lock != mutex::raw(mutex) {
                if *lock != ptr::null_mut() {
                    panic!("Condvar used with more than one Mutex");
                }

                atomic_cxchg(lock as *mut usize, 0, mutex::raw(mutex) as usize);
            }

            let now = Instant::now();

            let nanos = dur.as_secs() * 1_000_000_000 + dur.subsec_nanos() as u64;

            mutex.unlock();

            let arbiter = __sync_get_arbiter();

            svcArbitrateAddress(arbiter,
                                *self.lock.get() as u32,
                                ArbitrationType::ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT,
                                2,
                                nanos as i64);

            mutex.lock();

            now.elapsed() < dur
        }
    }

    #[inline]
    pub unsafe fn destroy(&self) {
        *self.lock.get() = ptr::null_mut();
    }
}