Skip to content

Commit f1b5f59

Browse files
committed
std: adjust requested stack size for thread-local storage.
If there is a lot of data in thread-local storage some implementations of pthreads (e.g. glibc) fail if you don't request a stack large enough -- by adjusting for the minimum size we guarantee that our stacks are always large enough. Issue #6233.
1 parent 27ce4d3 commit f1b5f59

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/libstd/rt/thread.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ mod imp {
202202
let mut native: libc::pthread_t = intrinsics::uninit();
203203
let mut attr: libc::pthread_attr_t = intrinsics::uninit();
204204
assert_eq!(pthread_attr_init(&mut attr), 0);
205+
206+
let min_stack = get_min_stack(&attr);
205207
assert_eq!(pthread_attr_setstacksize(&mut attr,
206-
stack as libc::size_t), 0);
208+
min_stack + stack as libc::size_t), 0);
207209
assert_eq!(pthread_attr_setdetachstate(&mut attr,
208210
PTHREAD_CREATE_JOINABLE), 0);
209211

@@ -228,6 +230,18 @@ mod imp {
228230
#[cfg(not(target_os = "macos"), not(target_os = "android"))]
229231
pub unsafe fn yield_now() { assert_eq!(pthread_yield(), 0); }
230232

233+
// Issue #6233. On some platforms, putting a lot of data in
234+
// thread-local storage means we need to set the stack-size to be
235+
// larger.
236+
#[cfg(target_os = "linux")]
237+
unsafe fn get_min_stack(attr: &libc::pthread_attr_t) -> libc::size_t {
238+
__pthread_get_minstack(attr)
239+
}
240+
#[cfg(not(target_os = "linux"))]
241+
unsafe fn get_min_stack(_: &libc::pthread_attr_t) -> libc::size_t {
242+
0
243+
}
244+
231245
extern {
232246
fn pthread_create(native: *mut libc::pthread_t,
233247
attr: *libc::pthread_attr_t,
@@ -247,6 +261,10 @@ mod imp {
247261
fn sched_yield() -> libc::c_int;
248262
#[cfg(not(target_os = "macos"), not(target_os = "android"))]
249263
fn pthread_yield() -> libc::c_int;
264+
265+
// This appears to be glibc specific
266+
#[cfg(target_os = "linux")]
267+
fn __pthread_get_minstack(attr: *libc::pthread_attr_t) -> libc::size_t;
250268
}
251269
}
252270

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 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+
// Issue #6233
12+
// xfail-fast feature doesn't work
13+
14+
#[feature(thread_local)];
15+
#[allow(dead_code)];
16+
17+
static SIZE: uint = 1 << 23;
18+
19+
#[thread_local]
20+
static FOO: [u8, .. SIZE] = [0, .. SIZE];
21+
22+
fn main() {}

0 commit comments

Comments
 (0)