Skip to content

Commit f8a1839

Browse files
committed
---
yaml --- r: 146873 b: refs/heads/try2 c: 2cf3d8a h: refs/heads/master i: 146871: dd32239 v: v3
1 parent 4be3e83 commit f8a1839

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 1795ae4e8a472ce500660fa59abf61114f5ef8c9
8+
refs/heads/try2: 2cf3d8adf2cbdf760063a247162a558e68fe35fd
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/rt/local_ptr.rs

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,35 @@
1717
1818
use libc::c_void;
1919
use cast;
20+
#[cfg(stage0)]
21+
#[cfg(windows)]
2022
use ptr;
2123
use cell::Cell;
2224
use option::{Option, Some, None};
2325
use unstable::finally::Finally;
26+
#[cfg(stage0)]
27+
#[cfg(windows)]
2428
use unstable::mutex::{Mutex, MUTEX_INIT};
29+
#[cfg(stage0)]
30+
#[cfg(windows)]
2531
use tls = rt::thread_local_storage;
2632

33+
#[cfg(not(stage0), not(windows), test)]
34+
#[thread_local]
35+
pub use realstd::rt::shouldnt_be_public::RT_TLS_PTR;
36+
37+
#[cfg(not(stage0), not(windows), not(test))]
38+
#[thread_local]
39+
pub static mut RT_TLS_PTR: *mut c_void = 0 as *mut c_void;
40+
41+
#[cfg(stage0)]
42+
#[cfg(windows)]
2743
static mut RT_TLS_KEY: tls::Key = -1;
2844

2945
/// Initialize the TLS key. Other ops will fail if this isn't executed first.
46+
#[inline(never)]
47+
#[cfg(stage0)]
48+
#[cfg(windows)]
3049
pub fn init_tls_key() {
3150
static mut lock: Mutex = MUTEX_INIT;
3251
static mut initialized: bool = false;
@@ -41,24 +60,42 @@ pub fn init_tls_key() {
4160
}
4261
}
4362

63+
#[cfg(not(stage0), not(windows))]
64+
pub fn init_tls_key() {}
65+
4466
/// Give a pointer to thread-local storage.
4567
///
4668
/// # Safety note
4769
///
4870
/// Does not validate the pointer type.
4971
#[inline]
72+
#[cfg(stage0)]
73+
#[cfg(windows)]
5074
pub unsafe fn put<T>(sched: ~T) {
5175
let key = tls_key();
5276
let void_ptr: *mut c_void = cast::transmute(sched);
5377
tls::set(key, void_ptr);
5478
}
5579

80+
/// Give a pointer to thread-local storage.
81+
///
82+
/// # Safety note
83+
///
84+
/// Does not validate the pointer type.
85+
#[inline]
86+
#[cfg(not(stage0), not(windows))]
87+
pub unsafe fn put<T>(sched: ~T) {
88+
RT_TLS_PTR = cast::transmute(sched)
89+
}
90+
5691
/// Take ownership of a pointer from thread-local storage.
5792
///
5893
/// # Safety note
5994
///
6095
/// Does not validate the pointer type.
6196
#[inline]
97+
#[cfg(stage0)]
98+
#[cfg(windows)]
6299
pub unsafe fn take<T>() -> ~T {
63100
let key = tls_key();
64101
let void_ptr: *mut c_void = tls::get(key);
@@ -70,13 +107,28 @@ pub unsafe fn take<T>() -> ~T {
70107
return ptr;
71108
}
72109

110+
/// Take ownership of a pointer from thread-local storage.
111+
///
112+
/// # Safety note
113+
///
114+
/// Does not validate the pointer type.
115+
#[inline]
116+
#[cfg(not(stage0), not(windows))]
117+
pub unsafe fn take<T>() -> ~T {
118+
let ptr: ~T = cast::transmute(RT_TLS_PTR);
119+
RT_TLS_PTR = cast::transmute(0); // can't use `as`, due to type not matching with `cfg(test)`
120+
ptr
121+
}
122+
73123
/// Take ownership of a pointer from thread-local storage.
74124
///
75125
/// # Safety note
76126
///
77127
/// Does not validate the pointer type.
78128
/// Leaves the old pointer in TLS for speed.
79129
#[inline]
130+
#[cfg(stage0)]
131+
#[cfg(windows)]
80132
pub unsafe fn unsafe_take<T>() -> ~T {
81133
let key = tls_key();
82134
let void_ptr: *mut c_void = tls::get(key);
@@ -87,7 +139,21 @@ pub unsafe fn unsafe_take<T>() -> ~T {
87139
return ptr;
88140
}
89141

142+
/// Take ownership of a pointer from thread-local storage.
143+
///
144+
/// # Safety note
145+
///
146+
/// Does not validate the pointer type.
147+
/// Leaves the old pointer in TLS for speed.
148+
#[inline]
149+
#[cfg(not(stage0), not(windows))]
150+
pub unsafe fn unsafe_take<T>() -> ~T {
151+
cast::transmute(RT_TLS_PTR)
152+
}
153+
90154
/// Check whether there is a thread-local pointer installed.
155+
#[cfg(stage0)]
156+
#[cfg(windows)]
91157
pub fn exists() -> bool {
92158
unsafe {
93159
match maybe_tls_key() {
@@ -97,6 +163,14 @@ pub fn exists() -> bool {
97163
}
98164
}
99165

166+
/// Check whether there is a thread-local pointer installed.
167+
#[cfg(not(stage0), not(windows))]
168+
pub fn exists() -> bool {
169+
unsafe {
170+
RT_TLS_PTR.is_not_null()
171+
}
172+
}
173+
100174
/// Borrow the thread-local value from thread-local storage.
101175
/// While the value is borrowed it is not available in TLS.
102176
///
@@ -123,6 +197,8 @@ pub unsafe fn borrow<T>(f: |&mut T|) {
123197
///
124198
/// Because this leaves the value in thread-local storage it is possible
125199
/// For the Scheduler pointer to be aliased
200+
#[cfg(stage0)]
201+
#[cfg(windows)]
126202
pub unsafe fn unsafe_borrow<T>() -> *mut T {
127203
let key = tls_key();
128204
let void_ptr = tls::get(key);
@@ -132,6 +208,16 @@ pub unsafe fn unsafe_borrow<T>() -> *mut T {
132208
void_ptr as *mut T
133209
}
134210

211+
#[cfg(not(stage0), not(windows))]
212+
pub unsafe fn unsafe_borrow<T>() -> *mut T {
213+
if RT_TLS_PTR.is_null() {
214+
rtabort!("thread-local pointer is null. bogus!");
215+
}
216+
RT_TLS_PTR as *mut T
217+
}
218+
219+
#[cfg(stage0)]
220+
#[cfg(windows)]
135221
pub unsafe fn try_unsafe_borrow<T>() -> Option<*mut T> {
136222
match maybe_tls_key() {
137223
Some(key) => {
@@ -146,7 +232,18 @@ pub unsafe fn try_unsafe_borrow<T>() -> Option<*mut T> {
146232
}
147233
}
148234

235+
#[cfg(not(stage0), not(windows))]
236+
pub unsafe fn try_unsafe_borrow<T>() -> Option<*mut T> {
237+
if RT_TLS_PTR.is_null() {
238+
None
239+
} else {
240+
Some(RT_TLS_PTR as *mut T)
241+
}
242+
}
243+
149244
#[inline]
245+
#[cfg(stage0)]
246+
#[cfg(windows)]
150247
fn tls_key() -> tls::Key {
151248
match maybe_tls_key() {
152249
Some(key) => key,
@@ -155,7 +252,8 @@ fn tls_key() -> tls::Key {
155252
}
156253

157254
#[inline]
158-
#[cfg(not(test))]
255+
#[cfg(not(test), stage0)]
256+
#[cfg(not(test), windows)]
159257
pub fn maybe_tls_key() -> Option<tls::Key> {
160258
unsafe {
161259
// NB: This is a little racy because, while the key is
@@ -176,11 +274,9 @@ pub fn maybe_tls_key() -> Option<tls::Key> {
176274
}
177275
}
178276

179-
// XXX: The boundary between the running runtime and the testing runtime
180-
// seems to be fuzzy at the moment, and trying to use two different keys
181-
// results in disaster. This should not be necessary.
182277
#[inline]
183-
#[cfg(test)]
278+
#[cfg(test, stage0)]
279+
#[cfg(test, windows)]
184280
pub fn maybe_tls_key() -> Option<tls::Key> {
185281
unsafe { ::cast::transmute(::realstd::rt::shouldnt_be_public::maybe_tls_key()) }
186282
}

branches/try2/src/libstd/rt/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ pub use self::kill::BlockedTask;
9595
pub mod shouldnt_be_public {
9696
pub use super::select::SelectInner;
9797
pub use super::select::{SelectInner, SelectPortInner};
98+
#[cfg(stage0)]
99+
#[cfg(windows)]
98100
pub use super::local_ptr::maybe_tls_key;
101+
#[cfg(not(stage0), not(windows))]
102+
pub use super::local_ptr::RT_TLS_PTR;
99103
}
100104

101105
// Internal macros used by the runtime.

0 commit comments

Comments
 (0)