@@ -16,7 +16,7 @@ use cmp;
16
16
use fmt;
17
17
use io:: lazy:: Lazy ;
18
18
use io:: { self , BufReader , LineWriter } ;
19
- use sync:: { Arc , Mutex , MutexGuard } ;
19
+ use sync:: { Arc , ReentrantMutex , ReentrantMutexGuard } ;
20
20
use sys:: stdio;
21
21
22
22
/// Stdout used by print! and println! macros
@@ -96,7 +96,7 @@ impl Write for StderrRaw {
96
96
/// of `Stdin` must be executed with care.
97
97
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
98
98
pub struct Stdin {
99
- inner : Arc < Mutex < BufReader < StdinRaw > > > ,
99
+ inner : Arc < ReentrantMutex < BufReader < StdinRaw > > > ,
100
100
}
101
101
102
102
/// A locked reference to the a `Stdin` handle.
@@ -105,7 +105,7 @@ pub struct Stdin {
105
105
/// constructed via the `lock` method on `Stdin`.
106
106
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
107
107
pub struct StdinLock < ' a > {
108
- inner : MutexGuard < ' a , BufReader < StdinRaw > > ,
108
+ inner : ReentrantMutexGuard < ' a , BufReader < StdinRaw > > ,
109
109
}
110
110
111
111
/// Create a new handle to the global standard input stream of this process.
@@ -119,17 +119,17 @@ pub struct StdinLock<'a> {
119
119
/// locked version, `StdinLock`, implements both `Read` and `BufRead`, however.
120
120
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
121
121
pub fn stdin ( ) -> Stdin {
122
- static INSTANCE : Lazy < Mutex < BufReader < StdinRaw > > > = lazy_init ! ( stdin_init) ;
122
+ static INSTANCE : Lazy < ReentrantMutex < BufReader < StdinRaw > > > = lazy_init ! ( stdin_init) ;
123
123
return Stdin {
124
124
inner : INSTANCE . get ( ) . expect ( "cannot access stdin during shutdown" ) ,
125
125
} ;
126
126
127
- fn stdin_init ( ) -> Arc < Mutex < BufReader < StdinRaw > > > {
127
+ fn stdin_init ( ) -> Arc < ReentrantMutex < BufReader < StdinRaw > > > {
128
128
// The default buffer capacity is 64k, but apparently windows
129
129
// doesn't like 64k reads on stdin. See #13304 for details, but the
130
130
// idea is that on windows we use a slightly smaller buffer that's
131
131
// been seen to be acceptable.
132
- Arc :: new ( Mutex :: new ( if cfg ! ( windows) {
132
+ Arc :: new ( ReentrantMutex :: new ( if cfg ! ( windows) {
133
133
BufReader :: with_capacity ( 8 * 1024 , stdin_raw ( ) )
134
134
} else {
135
135
BufReader :: new ( stdin_raw ( ) )
@@ -210,7 +210,7 @@ pub struct Stdout {
210
210
// FIXME: this should be LineWriter or BufWriter depending on the state of
211
211
// stdout (tty or not). Note that if this is not line buffered it
212
212
// should also flush-on-panic or some form of flush-on-abort.
213
- inner : Arc < Mutex < LineWriter < StdoutRaw > > > ,
213
+ inner : Arc < ReentrantMutex < LineWriter < StdoutRaw > > > ,
214
214
}
215
215
216
216
/// A locked reference to the a `Stdout` handle.
@@ -219,7 +219,7 @@ pub struct Stdout {
219
219
/// method on `Stdout`.
220
220
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
221
221
pub struct StdoutLock < ' a > {
222
- inner : MutexGuard < ' a , LineWriter < StdoutRaw > > ,
222
+ inner : ReentrantMutexGuard < ' a , LineWriter < StdoutRaw > > ,
223
223
}
224
224
225
225
/// Constructs a new reference to the standard output of the current process.
@@ -231,13 +231,13 @@ pub struct StdoutLock<'a> {
231
231
/// The returned handle implements the `Write` trait.
232
232
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
233
233
pub fn stdout ( ) -> Stdout {
234
- static INSTANCE : Lazy < Mutex < LineWriter < StdoutRaw > > > = lazy_init ! ( stdout_init) ;
234
+ static INSTANCE : Lazy < ReentrantMutex < LineWriter < StdoutRaw > > > = lazy_init ! ( stdout_init) ;
235
235
return Stdout {
236
236
inner : INSTANCE . get ( ) . expect ( "cannot access stdout during shutdown" ) ,
237
237
} ;
238
238
239
- fn stdout_init ( ) -> Arc < Mutex < LineWriter < StdoutRaw > > > {
240
- Arc :: new ( Mutex :: new ( LineWriter :: new ( stdout_raw ( ) ) ) )
239
+ fn stdout_init ( ) -> Arc < ReentrantMutex < LineWriter < StdoutRaw > > > {
240
+ Arc :: new ( ReentrantMutex :: new ( LineWriter :: new ( stdout_raw ( ) ) ) )
241
241
}
242
242
}
243
243
@@ -264,8 +264,9 @@ impl Write for Stdout {
264
264
fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
265
265
self . lock ( ) . write_all ( buf)
266
266
}
267
- // Don't override write_fmt as it's possible to run arbitrary code during a
268
- // write_fmt, allowing the possibility of a recursive lock (aka deadlock)
267
+ fn write_fmt ( & mut self , args : fmt:: Arguments ) -> io:: Result < ( ) > {
268
+ self . lock ( ) . write_fmt ( args)
269
+ }
269
270
}
270
271
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
271
272
impl < ' a > Write for StdoutLock < ' a > {
@@ -280,7 +281,7 @@ impl<'a> Write for StdoutLock<'a> {
280
281
/// For more information, see `stderr`
281
282
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
282
283
pub struct Stderr {
283
- inner : Arc < Mutex < StderrRaw > > ,
284
+ inner : Arc < ReentrantMutex < StderrRaw > > ,
284
285
}
285
286
286
287
/// A locked reference to the a `Stderr` handle.
@@ -289,7 +290,7 @@ pub struct Stderr {
289
290
/// method on `Stderr`.
290
291
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
291
292
pub struct StderrLock < ' a > {
292
- inner : MutexGuard < ' a , StderrRaw > ,
293
+ inner : ReentrantMutexGuard < ' a , StderrRaw > ,
293
294
}
294
295
295
296
/// Constructs a new reference to the standard error stream of a process.
@@ -300,13 +301,13 @@ pub struct StderrLock<'a> {
300
301
/// The returned handle implements the `Write` trait.
301
302
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
302
303
pub fn stderr ( ) -> Stderr {
303
- static INSTANCE : Lazy < Mutex < StderrRaw > > = lazy_init ! ( stderr_init) ;
304
+ static INSTANCE : Lazy < ReentrantMutex < StderrRaw > > = lazy_init ! ( stderr_init) ;
304
305
return Stderr {
305
306
inner : INSTANCE . get ( ) . expect ( "cannot access stderr during shutdown" ) ,
306
307
} ;
307
308
308
- fn stderr_init ( ) -> Arc < Mutex < StderrRaw > > {
309
- Arc :: new ( Mutex :: new ( stderr_raw ( ) ) )
309
+ fn stderr_init ( ) -> Arc < ReentrantMutex < StderrRaw > > {
310
+ Arc :: new ( ReentrantMutex :: new ( stderr_raw ( ) ) )
310
311
}
311
312
}
312
313
@@ -333,7 +334,9 @@ impl Write for Stderr {
333
334
fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
334
335
self . lock ( ) . write_all ( buf)
335
336
}
336
- // Don't override write_fmt for the same reasons as Stdout
337
+ fn write_fmt ( & mut self , args : fmt:: Arguments ) -> io:: Result < ( ) > {
338
+ self . lock ( ) . write_fmt ( args)
339
+ }
337
340
}
338
341
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
339
342
impl < ' a > Write for StderrLock < ' a > {
0 commit comments