summaryrefslogtreecommitdiff
path: root/src/thread.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/thread.cc')
-rw-r--r--src/thread.cc28
1 files changed, 15 insertions, 13 deletions
diff --git a/src/thread.cc b/src/thread.cc
index baa659c4d7..eb7a4f727d 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -9,6 +9,7 @@
#include <list>
#include "class_linker.h"
+#include "jni_internal.h"
#include "object.h"
#include "runtime.h"
#include "utils.h"
@@ -53,20 +54,21 @@ void* ThreadStart(void *arg) {
return NULL;
}
-Thread* Thread::Create(size_t stack_size) {
- int prot = PROT_READ | PROT_WRITE;
- // TODO: require the stack size to be page aligned?
- size_t length = RoundUp(stack_size, 0x1000);
- void* stack_limit = mmap(NULL, length, prot, MAP_PRIVATE, -1, 0);
- if (stack_limit == MAP_FAILED) {
- LOG(FATAL) << "mmap";
- return false;
+Thread* Thread::Create(const Runtime* runtime) {
+ size_t stack_size = runtime->GetStackSize();
+ scoped_ptr<MemMap> stack(MemMap::Map(stack_size, PROT_READ | PROT_WRITE));
+ if (stack == NULL) {
+ LOG(FATAL) << "failed to allocate thread stack";
+ // notreached
+ return NULL;
}
Thread* new_thread = new Thread;
new_thread->InitCpu();
- new_thread->stack_limit_ = static_cast<byte*>(stack_limit);
- new_thread->stack_base_ = new_thread->stack_limit_ + length;
+ new_thread->stack_.reset(stack.release());
+ // Since stacks are assumed to grown downward the base is the limit and the limit is the base.
+ new_thread->stack_limit_ = stack->GetAddress();
+ new_thread->stack_base_ = stack->GetLimit();
pthread_attr_t attr;
int result = pthread_attr_init(&attr);
@@ -103,7 +105,7 @@ Thread* Thread::Attach(const Runtime* runtime) {
PLOG(FATAL) << "pthread_setspecific failed";
}
- JavaVMExt* vm = reinterpret_cast<JavaVMExt*>(runtime->GetJavaVM());
+ JavaVMExt* vm = runtime->GetJavaVM();
CHECK(vm != NULL);
bool check_jni = vm->check_jni;
thread->jni_env_ = reinterpret_cast<JNIEnv*>(new JNIEnvExt(thread, check_jni));
@@ -211,9 +213,9 @@ ThreadList::~ThreadList() {
// Make sure that all threads have exited and unregistered when we
// reach this point. This means that all daemon threads had been
// shutdown cleanly.
- CHECK_EQ(list_.size(), 1U);
+ CHECK_LE(list_.size(), 1U);
// TODO: wait for all other threads to unregister
- CHECK_EQ(list_.front(), Thread::Current());
+ CHECK(list_.size() == 0 || list_.front() == Thread::Current());
// TODO: detach the current thread
delete lock_;
lock_ = NULL;