Fix the 051-thread test.
This changes Thread::Create to clean up the requested stack size before
passing it to pthread_attr_setstacksize.
I modified the test to work around the inherent raciness in the test, which
currently causes us trouble because with all the debug logging, the thread
that initializes System.out takes longer than 1ms, so the first 40 or so
threads all get woken in non-numeric order.
Change-Id: I8047bf00330aaf4210dcb53a4437b1fe0562179e
diff --git a/src/thread.cc b/src/thread.cc
index 0dd3766..e997723 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -214,13 +214,33 @@
return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread)));
}
-void Thread::Create(Object* peer, size_t stack_size) {
- CHECK(peer != NULL);
-
+size_t FixStackSize(size_t stack_size) {
+ // A stack size of zero means "use the default".
if (stack_size == 0) {
stack_size = Runtime::Current()->GetDefaultStackSize();
}
+ // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN.
+ if (stack_size < PTHREAD_STACK_MIN) {
+ stack_size = PTHREAD_STACK_MIN;
+ }
+
+ // It's likely that callers are trying to ensure they have at least a certain amount of
+ // stack space, so we should add our reserved space on top of what they requested, rather
+ // than implicitly take it away from them.
+ stack_size += Thread::kStackOverflowReservedBytes;
+
+ // Some systems require the stack size to be a multiple of the system page size, so round up.
+ stack_size = RoundUp(stack_size, kPageSize);
+
+ return stack_size;
+}
+
+void Thread::Create(Object* peer, size_t stack_size) {
+ CHECK(peer != NULL);
+
+ stack_size = FixStackSize(stack_size);
+
Thread* native_thread = new Thread;
native_thread->peer_ = peer;