Skip to content

Commit 5122412

Browse files
Moved std::unstable::mutex to std::rt.
This is to cleanout the std::unstable module and use the appropriate unstable attribute. This mutex module is **not** supposed to be used outside of the compiler/std. This change is according to rust-lang#1457 and comment rust-lang#1457 (comment) [breaking-change]
1 parent 746d086 commit 5122412

22 files changed

+180
-27
lines changed

src/libgreen/sched.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::rt::rtio::{RemoteCallback, PausableIdleCallback, Callback, EventLoop};
1414
use std::rt::task::BlockedTask;
1515
use std::rt::task::Task;
1616
use std::sync::deque;
17-
use std::unstable::mutex::NativeMutex;
17+
use std::rt::mutex::NativeMutex;
1818
use std::raw;
1919

2020
use rand::{XorShiftRng, Rng, Rand};
@@ -1474,7 +1474,7 @@ mod test {
14741474

14751475
#[test]
14761476
fn test_spawn_sched_blocking() {
1477-
use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
1477+
use std::rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
14781478
static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
14791479

14801480
// Testing that a task in one scheduler can block in foreign code

src/libgreen/simple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::rt::local::Local;
1818
use std::rt::rtio;
1919
use std::rt::task::{Task, BlockedTask};
2020
use std::task::TaskOpts;
21-
use std::unstable::mutex::NativeMutex;
21+
use std::rt::mutex::NativeMutex;
2222

2323
struct SimpleTask {
2424
lock: NativeMutex,

src/libgreen/task.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::rt::rtio;
2828
use std::rt::stack;
2929
use std::rt::task::{Task, BlockedTask, SendMessage};
3030
use std::task::TaskOpts;
31-
use std::unstable::mutex::NativeMutex;
31+
use std::rt::mutex::NativeMutex;
3232

3333
use context::Context;
3434
use coroutine::Coroutine;

src/libnative/io/net.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use std::io::net::ip;
1414
use std::io;
1515
use std::mem;
1616
use std::rt::rtio;
17-
use std::unstable::mutex;
17+
use std::sync::arc::UnsafeArc;
18+
use std::rt::mutex;
1819

1920
use super::{IoResult, retry, keep_going};
2021
use super::c;
@@ -215,7 +216,7 @@ pub fn init() {}
215216
pub fn init() {
216217

217218
unsafe {
218-
use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
219+
use std::rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
219220
static mut INITIALIZED: bool = false;
220221
static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
221222

src/libnative/io/pipe_unix.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use std::intrinsics;
1515
use std::io;
1616
use std::mem;
1717
use std::rt::rtio;
18-
use std::unstable::mutex;
18+
use std::sync::arc::UnsafeArc;
19+
use std::rt::mutex;
1920

2021
use super::{IoResult, retry};
2122
use super::net;

src/libnative/io/pipe_win32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use std::os;
9494
use std::ptr;
9595
use std::rt::rtio;
9696
use std::sync::atomics;
97-
use std::unstable::mutex;
97+
use std::rt::mutex;
9898

9999
use super::IoResult;
100100
use super::c;

src/libnative/io/timer_helper.rs

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright 2013-2014 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+
//! Implementation of the helper thread for the timer module
12+
//!
13+
//! This module contains the management necessary for the timer worker thread.
14+
//! This thread is responsible for performing the send()s on channels for timers
15+
//! that are using channels instead of a blocking call.
16+
//!
17+
//! The timer thread is lazily initialized, and it's shut down via the
18+
//! `shutdown` function provided. It must be maintained as an invariant that
19+
//! `shutdown` is only called when the entire program is finished. No new timers
20+
//! can be created in the future and there must be no active timers at that
21+
//! time.
22+
23+
use std::mem;
24+
use std::rt::bookkeeping;
25+
use std::rt;
26+
use std::rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
27+
28+
use io::timer::{Req, Shutdown};
29+
use task;
30+
31+
// You'll note that these variables are *not* protected by a lock. These
32+
// variables are initialized with a Once before any Timer is created and are
33+
// only torn down after everything else has exited. This means that these
34+
// variables are read-only during use (after initialization) and both of which
35+
// are safe to use concurrently.
36+
static mut HELPER_CHAN: *mut Sender<Req> = 0 as *mut Sender<Req>;
37+
static mut HELPER_SIGNAL: imp::signal = 0 as imp::signal;
38+
39+
static mut TIMER_HELPER_EXIT: StaticNativeMutex = NATIVE_MUTEX_INIT;
40+
41+
pub fn boot(helper: fn(imp::signal, Receiver<Req>)) {
42+
static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
43+
static mut INITIALIZED: bool = false;
44+
45+
unsafe {
46+
let mut _guard = LOCK.lock();
47+
if !INITIALIZED {
48+
let (tx, rx) = channel();
49+
// promote this to a shared channel
50+
drop(tx.clone());
51+
HELPER_CHAN = mem::transmute(box tx);
52+
let (receive, send) = imp::new();
53+
HELPER_SIGNAL = send;
54+
55+
task::spawn(proc() {
56+
bookkeeping::decrement();
57+
helper(receive, rx);
58+
TIMER_HELPER_EXIT.lock().signal()
59+
});
60+
61+
rt::at_exit(proc() { shutdown() });
62+
INITIALIZED = true;
63+
}
64+
}
65+
}
66+
67+
pub fn send(req: Req) {
68+
unsafe {
69+
assert!(!HELPER_CHAN.is_null());
70+
(*HELPER_CHAN).send(req);
71+
imp::signal(HELPER_SIGNAL);
72+
}
73+
}
74+
75+
fn shutdown() {
76+
// Request a shutdown, and then wait for the task to exit
77+
unsafe {
78+
let guard = TIMER_HELPER_EXIT.lock();
79+
send(Shutdown);
80+
guard.wait();
81+
drop(guard);
82+
TIMER_HELPER_EXIT.destroy();
83+
}
84+
85+
86+
// Clean up after ther helper thread
87+
unsafe {
88+
imp::close(HELPER_SIGNAL);
89+
let _chan: Box<Sender<Req>> = mem::transmute(HELPER_CHAN);
90+
HELPER_CHAN = 0 as *mut Sender<Req>;
91+
HELPER_SIGNAL = 0 as imp::signal;
92+
}
93+
}
94+
95+
#[cfg(unix)]
96+
mod imp {
97+
use libc;
98+
use std::os;
99+
100+
use io::file::FileDesc;
101+
102+
pub type signal = libc::c_int;
103+
104+
pub fn new() -> (signal, signal) {
105+
let pipe = os::pipe();
106+
(pipe.input, pipe.out)
107+
}
108+
109+
pub fn signal(fd: libc::c_int) {
110+
FileDesc::new(fd, false).inner_write([0]).unwrap();
111+
}
112+
113+
pub fn close(fd: libc::c_int) {
114+
let _fd = FileDesc::new(fd, true);
115+
}
116+
}
117+
118+
#[cfg(windows)]
119+
mod imp {
120+
use libc::{BOOL, LPCSTR, HANDLE, LPSECURITY_ATTRIBUTES, CloseHandle};
121+
use std::ptr;
122+
use libc;
123+
124+
pub type signal = HANDLE;
125+
126+
pub fn new() -> (HANDLE, HANDLE) {
127+
unsafe {
128+
let handle = CreateEventA(ptr::mut_null(), libc::FALSE, libc::FALSE,
129+
ptr::null());
130+
(handle, handle)
131+
}
132+
}
133+
134+
pub fn signal(handle: HANDLE) {
135+
assert!(unsafe { SetEvent(handle) != 0 });
136+
}
137+
138+
pub fn close(handle: HANDLE) {
139+
assert!(unsafe { CloseHandle(handle) != 0 });
140+
}
141+
142+
extern "system" {
143+
fn CreateEventA(lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
144+
bManualReset: BOOL,
145+
bInitialState: BOOL,
146+
lpName: LPCSTR) -> HANDLE;
147+
fn SetEvent(hEvent: HANDLE) -> BOOL;
148+
}
149+
}

src/libnative/task.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::rt::task::{Task, BlockedTask, SendMessage};
2525
use std::rt::thread::Thread;
2626
use std::rt;
2727
use std::task::TaskOpts;
28-
use std::unstable::mutex::NativeMutex;
28+
use std::rt::mutex::NativeMutex;
2929

3030
use io;
3131
use task;

src/librustuv/queue.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use alloc::arc::Arc;
2424
use libc::c_void;
2525
use std::mem;
2626
use std::rt::task::BlockedTask;
27-
use std::unstable::mutex::NativeMutex;
27+
use std::sync::arc::UnsafeArc;
28+
use std::rt::mutex::NativeMutex;
2829
use mpsc = std::sync::mpsc_queue;
2930

3031
use async::AsyncWatcher;

src/libstd/comm/shared.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rt::local::Local;
3030
use rt::task::{Task, BlockedTask};
3131
use rt::thread::Thread;
3232
use sync::atomics;
33-
use unstable::mutex::NativeMutex;
33+
use rt::mutex::NativeMutex;
3434

3535
use mpsc = sync::mpsc_queue;
3636

src/libstd/comm/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rt::local::Local;
4646
use rt::task::{Task, BlockedTask};
4747
use sync::atomics;
4848
use ty::Unsafe;
49-
use unstable::mutex::{NativeMutex, LockGuard};
49+
use rt::mutex::{NativeMutex, LockGuard};
5050
use vec::Vec;
5151

5252
pub struct Packet<T> {

src/libstd/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Accessing environment variables is not generally threadsafe.
161161
Serialize access through a global lock.
162162
*/
163163
fn with_env_lock<T>(f: || -> T) -> T {
164-
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
164+
use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
165165

166166
static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
167167

src/libstd/rt/args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mod imp {
4848
use iter::Iterator;
4949
use option::{Option, Some, None};
5050
use owned::Box;
51-
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
51+
use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
5252
use mem;
5353
use vec::Vec;
5454
use ptr::RawPtr;

src/libstd/rt/backtrace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ mod imp {
242242
use mem;
243243
use option::{Some, None, Option};
244244
use result::{Ok, Err};
245-
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
245+
use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
246246
use uw = rt::libunwind;
247247

248248
struct Context<'a> {
@@ -515,7 +515,7 @@ mod imp {
515515
use str::StrSlice;
516516
use unstable::dynamic_lib::DynamicLibrary;
517517
use intrinsics;
518-
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
518+
use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
519519
use slice::ImmutableVector;
520520

521521
extern "system" {

src/libstd/rt/bookkeeping.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#![doc(hidden)]
2323

2424
use sync::atomics;
25-
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
25+
use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
2626

2727
static mut TASK_COUNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
2828
static mut TASK_LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;

src/libstd/rt/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ pub mod shouldnt_be_public {
9292
// Internal macros used by the runtime.
9393
mod macros;
9494

95-
/// Implementations of language-critical runtime features like @.
9695
pub mod task;
9796

97+
pub mod mutex;
98+
9899
// The EventLoop and internal synchronous I/O interface.
99100
pub mod rtio;
100101

src/libstd/unstable/mutex.rs renamed to src/libstd/rt/mutex.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//! # Example
3434
//!
3535
//! ```rust
36-
//! use std::unstable::mutex::{NativeMutex, StaticNativeMutex, NATIVE_MUTEX_INIT};
36+
//! use std::rt::mutex::{NativeMutex, StaticNativeMutex, NATIVE_MUTEX_INIT};
3737
//!
3838
//! // Use a statically initialized mutex
3939
//! static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
@@ -109,7 +109,7 @@ impl StaticNativeMutex {
109109
/// # Example
110110
///
111111
/// ```rust
112-
/// use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
112+
/// use std::rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
113113
/// static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
114114
/// unsafe {
115115
/// let _guard = LOCK.lock();
@@ -183,7 +183,7 @@ impl NativeMutex {
183183
///
184184
/// # Example
185185
/// ```rust
186-
/// use std::unstable::mutex::NativeMutex;
186+
/// use std::rt::mutex::NativeMutex;
187187
/// unsafe {
188188
/// let mut lock = NativeMutex::new();
189189
///

src/libstd/unstable/dynamic_lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ pub mod dl {
223223
dlopen(ptr::null(), Lazy as libc::c_int) as *u8
224224
}
225225

226-
pub fn check_for_errors_in<T>(f: || -> T) -> Result<T, String> {
227-
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
226+
pub fn check_for_errors_in<T>(f: || -> T) -> Result<T, ~str> {
227+
use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
228228
static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
229229
unsafe {
230230
// dlerror isn't thread safe, so we need to lock around this entire

src/libstd/unstable/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@
1313
pub mod dynamic_lib;
1414

1515
pub mod sync;
16-
pub mod mutex;
1716

src/libstd/unstable/sync.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use alloc::arc::Arc;
1313
use clone::Clone;
1414
use kinds::Send;
1515
use ty::Unsafe;
16-
use unstable::mutex::NativeMutex;
16+
use sync::arc::UnsafeArc;
17+
use rt::mutex::NativeMutex;
1718

1819
struct ExData<T> {
1920
lock: NativeMutex,

src/libsync/mutex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use std::rt::task::{BlockedTask, Task};
6464
use std::rt::thread::Thread;
6565
use std::sync::atomics;
6666
use std::ty::Unsafe;
67-
use std::unstable::mutex;
67+
use std::rt::mutex;
6868

6969
use q = mpsc_intrusive;
7070

src/llvm

Submodule llvm updated 2770 files

0 commit comments

Comments
 (0)