|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use cell::UnsafeCell; |
| 12 | +use mem; |
| 13 | +use sync::atomic::{AtomicU32, Ordering}; |
| 14 | +use sys::cloudabi::abi; |
| 15 | +use sys::mutex::{self, Mutex}; |
| 16 | +use sys::time::dur2intervals; |
| 17 | +use time::Duration; |
| 18 | + |
| 19 | +extern "C" { |
| 20 | + #[thread_local] |
| 21 | + static __pthread_thread_id: abi::tid; |
| 22 | +} |
| 23 | + |
| 24 | +pub struct Condvar { |
| 25 | + condvar: UnsafeCell<AtomicU32>, |
| 26 | +} |
| 27 | + |
| 28 | +unsafe impl Send for Condvar {} |
| 29 | +unsafe impl Sync for Condvar {} |
| 30 | + |
| 31 | +impl Condvar { |
| 32 | + pub const fn new() -> Condvar { |
| 33 | + Condvar { |
| 34 | + condvar: UnsafeCell::new(AtomicU32::new(abi::CONDVAR_HAS_NO_WAITERS.0)), |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pub unsafe fn init(&mut self) {} |
| 39 | + |
| 40 | + pub unsafe fn notify_one(&self) { |
| 41 | + let condvar = self.condvar.get(); |
| 42 | + if (*condvar).load(Ordering::Relaxed) != abi::CONDVAR_HAS_NO_WAITERS.0 { |
| 43 | + let ret = abi::condvar_signal(condvar as *mut abi::condvar, abi::scope::PRIVATE, 1); |
| 44 | + assert_eq!( |
| 45 | + ret, |
| 46 | + abi::errno::SUCCESS, |
| 47 | + "Failed to signal on condition variable" |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + pub unsafe fn notify_all(&self) { |
| 53 | + let condvar = self.condvar.get(); |
| 54 | + if (*condvar).load(Ordering::Relaxed) != abi::CONDVAR_HAS_NO_WAITERS.0 { |
| 55 | + let ret = abi::condvar_signal( |
| 56 | + condvar as *mut abi::condvar, |
| 57 | + abi::scope::PRIVATE, |
| 58 | + abi::nthreads::max_value(), |
| 59 | + ); |
| 60 | + assert_eq!( |
| 61 | + ret, |
| 62 | + abi::errno::SUCCESS, |
| 63 | + "Failed to broadcast on condition variable" |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + pub unsafe fn wait(&self, mutex: &Mutex) { |
| 69 | + let mutex = mutex::raw(mutex); |
| 70 | + assert_eq!( |
| 71 | + (*mutex).load(Ordering::Relaxed) & !abi::LOCK_KERNEL_MANAGED.0, |
| 72 | + __pthread_thread_id.0 | abi::LOCK_WRLOCKED.0, |
| 73 | + "This lock is not write-locked by this thread" |
| 74 | + ); |
| 75 | + |
| 76 | + // Call into the kernel to wait on the condition variable. |
| 77 | + let condvar = self.condvar.get(); |
| 78 | + let subscription = abi::subscription { |
| 79 | + type_: abi::eventtype::CONDVAR, |
| 80 | + union: abi::subscription_union { |
| 81 | + condvar: abi::subscription_condvar { |
| 82 | + condvar: condvar as *mut abi::condvar, |
| 83 | + condvar_scope: abi::scope::PRIVATE, |
| 84 | + lock: mutex as *mut abi::lock, |
| 85 | + lock_scope: abi::scope::PRIVATE, |
| 86 | + }, |
| 87 | + }, |
| 88 | + ..mem::zeroed() |
| 89 | + }; |
| 90 | + let mut event: abi::event = mem::uninitialized(); |
| 91 | + let mut nevents: usize = mem::uninitialized(); |
| 92 | + let ret = abi::poll(&subscription, &mut event, 1, &mut nevents); |
| 93 | + assert_eq!( |
| 94 | + ret, |
| 95 | + abi::errno::SUCCESS, |
| 96 | + "Failed to wait on condition variable" |
| 97 | + ); |
| 98 | + assert_eq!( |
| 99 | + event.error, |
| 100 | + abi::errno::SUCCESS, |
| 101 | + "Failed to wait on condition variable" |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { |
| 106 | + let mutex = mutex::raw(mutex); |
| 107 | + assert_eq!( |
| 108 | + (*mutex).load(Ordering::Relaxed) & !abi::LOCK_KERNEL_MANAGED.0, |
| 109 | + __pthread_thread_id.0 | abi::LOCK_WRLOCKED.0, |
| 110 | + "This lock is not write-locked by this thread" |
| 111 | + ); |
| 112 | + |
| 113 | + // Call into the kernel to wait on the condition variable. |
| 114 | + let condvar = self.condvar.get(); |
| 115 | + let subscriptions = [ |
| 116 | + abi::subscription { |
| 117 | + type_: abi::eventtype::CONDVAR, |
| 118 | + union: abi::subscription_union { |
| 119 | + condvar: abi::subscription_condvar { |
| 120 | + condvar: condvar as *mut abi::condvar, |
| 121 | + condvar_scope: abi::scope::PRIVATE, |
| 122 | + lock: mutex as *mut abi::lock, |
| 123 | + lock_scope: abi::scope::PRIVATE, |
| 124 | + }, |
| 125 | + }, |
| 126 | + ..mem::zeroed() |
| 127 | + }, |
| 128 | + abi::subscription { |
| 129 | + type_: abi::eventtype::CLOCK, |
| 130 | + union: abi::subscription_union { |
| 131 | + clock: abi::subscription_clock { |
| 132 | + clock_id: abi::clockid::MONOTONIC, |
| 133 | + timeout: dur2intervals(&dur), |
| 134 | + ..mem::zeroed() |
| 135 | + }, |
| 136 | + }, |
| 137 | + ..mem::zeroed() |
| 138 | + }, |
| 139 | + ]; |
| 140 | + let mut events: [abi::event; 2] = mem::uninitialized(); |
| 141 | + let mut nevents: usize = mem::uninitialized(); |
| 142 | + let ret = abi::poll(subscriptions.as_ptr(), events.as_mut_ptr(), 2, &mut nevents); |
| 143 | + assert_eq!( |
| 144 | + ret, |
| 145 | + abi::errno::SUCCESS, |
| 146 | + "Failed to wait on condition variable" |
| 147 | + ); |
| 148 | + for i in 0..nevents { |
| 149 | + assert_eq!( |
| 150 | + events[i].error, |
| 151 | + abi::errno::SUCCESS, |
| 152 | + "Failed to wait on condition variable" |
| 153 | + ); |
| 154 | + if events[i].type_ == abi::eventtype::CONDVAR { |
| 155 | + return true; |
| 156 | + } |
| 157 | + } |
| 158 | + false |
| 159 | + } |
| 160 | + |
| 161 | + pub unsafe fn destroy(&self) { |
| 162 | + let condvar = self.condvar.get(); |
| 163 | + assert_eq!( |
| 164 | + (*condvar).load(Ordering::Relaxed), |
| 165 | + abi::CONDVAR_HAS_NO_WAITERS.0, |
| 166 | + "Attempted to destroy a condition variable with blocked threads" |
| 167 | + ); |
| 168 | + } |
| 169 | +} |
0 commit comments