aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys/unix/condvar.rs
blob: f19922cc1bb0ef2f192a5e0bd89564f59798e613 (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
// 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,
                                0,
                                0);

            mutex.lock();
        }
    }

    #[inline]
    pub fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
        ::sys_common::util::dumb_print(format_args!("condvar wait_timeout\n"));
        unimplemented!();
    }

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