@@ -18,25 +18,16 @@ use cell::Cell;
18
18
19
19
use rt:: sched:: Scheduler ;
20
20
use rt:: rtio:: { EventLoop , IoFactoryObject } ;
21
- use tls = rt:: thread_local_storage;
22
21
use unstable:: finally:: Finally ;
22
+ use rt:: local_ptr;
23
+ use tls = rt:: thread_local_storage;
23
24
24
25
#[ cfg( test) ] use rt:: uv:: uvio:: UvEventLoop ;
25
26
26
- /// Initialize the TLS key. Other ops will fail if this isn't executed first.
27
- pub fn init_tls_key ( ) {
28
- unsafe {
29
- rust_initialize_rt_tls_key ( ) ;
30
- extern {
31
- fn rust_initialize_rt_tls_key ( ) ;
32
- }
33
- }
34
- }
35
-
36
27
/// Give the Scheduler to thread-local storage
37
28
pub fn put ( sched : ~Scheduler ) {
38
29
unsafe {
39
- let key = tls_key ( ) ;
30
+ let key = local_ptr :: tls_key ( ) ;
40
31
let void_sched: * mut c_void = cast:: transmute ( sched) ;
41
32
tls:: set ( key, void_sched) ;
42
33
}
@@ -45,7 +36,7 @@ pub fn put(sched: ~Scheduler) {
45
36
/// Take ownership of the Scheduler from thread-local storage
46
37
pub fn take ( ) -> ~Scheduler {
47
38
unsafe {
48
- let key = tls_key ( ) ;
39
+ let key = local_ptr :: tls_key ( ) ;
49
40
let void_sched: * mut c_void = tls:: get ( key) ;
50
41
rtassert ! ( void_sched. is_not_null( ) ) ;
51
42
let sched: ~Scheduler = cast:: transmute ( void_sched) ;
@@ -57,7 +48,7 @@ pub fn take() -> ~Scheduler {
57
48
/// Check whether there is a thread-local Scheduler attached to the running thread
58
49
pub fn exists ( ) -> bool {
59
50
unsafe {
60
- match maybe_tls_key ( ) {
51
+ match local_ptr :: maybe_tls_key ( ) {
61
52
Some ( key) => tls:: get ( key) . is_not_null ( ) ,
62
53
None => false
63
54
}
@@ -89,7 +80,7 @@ pub fn borrow(f: &fn(&mut Scheduler)) {
89
80
/// Because this leaves the Scheduler in thread-local storage it is possible
90
81
/// For the Scheduler pointer to be aliased
91
82
pub unsafe fn unsafe_borrow ( ) -> * mut Scheduler {
92
- let key = tls_key ( ) ;
83
+ let key = local_ptr :: tls_key ( ) ;
93
84
let mut void_sched: * mut c_void = tls:: get ( key) ;
94
85
rtassert ! ( void_sched. is_not_null( ) ) ;
95
86
{
@@ -106,43 +97,6 @@ pub unsafe fn unsafe_borrow_io() -> *mut IoFactoryObject {
106
97
return io;
107
98
}
108
99
109
- fn tls_key ( ) -> tls:: Key {
110
- match maybe_tls_key ( ) {
111
- Some ( key) => key,
112
- None => abort ! ( "runtime tls key not initialized" )
113
- }
114
- }
115
-
116
- fn maybe_tls_key ( ) -> Option < tls:: Key > {
117
- unsafe {
118
- let key: * mut c_void = rust_get_rt_tls_key ( ) ;
119
- let key: & mut tls:: Key = cast:: transmute ( key) ;
120
- let key = * key;
121
- // Check that the key has been initialized.
122
-
123
- // NB: This is a little racy because, while the key is
124
- // initalized under a mutex and it's assumed to be initalized
125
- // in the Scheduler ctor by any thread that needs to use it,
126
- // we are not accessing the key under a mutex. Threads that
127
- // are not using the new Scheduler but still *want to check*
128
- // whether they are running under a new Scheduler may see a 0
129
- // value here that is in the process of being initialized in
130
- // another thread. I think this is fine since the only action
131
- // they could take if it was initialized would be to check the
132
- // thread-local value and see that it's not set.
133
- if key != -1 {
134
- return Some ( key) ;
135
- } else {
136
- return None ;
137
- }
138
- }
139
- }
140
-
141
- extern {
142
- #[ fast_ffi]
143
- fn rust_get_rt_tls_key ( ) -> * mut c_void ;
144
- }
145
-
146
100
#[ test]
147
101
fn thread_local_scheduler_smoke_test ( ) {
148
102
let scheduler = ~UvEventLoop :: new_scheduler ( ) ;
0 commit comments