| OLD | NEW |
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 } | 366 } |
| 367 | 367 |
| 368 | 368 |
| 369 bool VirtualMemory::Uncommit(void* address, size_t size) { | 369 bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 370 return mmap(address, size, PROT_NONE, | 370 return mmap(address, size, PROT_NONE, |
| 371 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, | 371 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, |
| 372 kMmapFd, kMmapFdOffset) != MAP_FAILED; | 372 kMmapFd, kMmapFdOffset) != MAP_FAILED; |
| 373 } | 373 } |
| 374 | 374 |
| 375 | 375 |
| 376 class ThreadHandle::PlatformData : public Malloced { | 376 class Thread::PlatformData : public Malloced { |
| 377 public: | 377 public: |
| 378 explicit PlatformData(ThreadHandle::Kind kind) { | 378 PlatformData() : thread_(kNoThread) { } |
| 379 Initialize(kind); | |
| 380 } | |
| 381 | |
| 382 void Initialize(ThreadHandle::Kind kind) { | |
| 383 switch (kind) { | |
| 384 case ThreadHandle::SELF: thread_ = pthread_self(); break; | |
| 385 case ThreadHandle::INVALID: thread_ = kNoThread; break; | |
| 386 } | |
| 387 } | |
| 388 | 379 |
| 389 pthread_t thread_; // Thread handle for pthread. | 380 pthread_t thread_; // Thread handle for pthread. |
| 390 }; | 381 }; |
| 391 | 382 |
| 392 | |
| 393 ThreadHandle::ThreadHandle(Kind kind) { | |
| 394 data_ = new PlatformData(kind); | |
| 395 } | |
| 396 | |
| 397 | |
| 398 void ThreadHandle::Initialize(ThreadHandle::Kind kind) { | |
| 399 data_->Initialize(kind); | |
| 400 } | |
| 401 | |
| 402 | |
| 403 ThreadHandle::~ThreadHandle() { | |
| 404 delete data_; | |
| 405 } | |
| 406 | |
| 407 | |
| 408 bool ThreadHandle::IsSelf() const { | |
| 409 return pthread_equal(data_->thread_, pthread_self()); | |
| 410 } | |
| 411 | |
| 412 | |
| 413 bool ThreadHandle::IsValid() const { | |
| 414 return data_->thread_ != kNoThread; | |
| 415 } | |
| 416 | |
| 417 | |
| 418 Thread::Thread(Isolate* isolate, const Options& options) | 383 Thread::Thread(Isolate* isolate, const Options& options) |
| 419 : ThreadHandle(ThreadHandle::INVALID), | 384 : data_(new PlatformData()), |
| 420 isolate_(isolate), | 385 isolate_(isolate), |
| 421 stack_size_(options.stack_size) { | 386 stack_size_(options.stack_size) { |
| 422 set_name(options.name); | 387 set_name(options.name); |
| 423 } | 388 } |
| 424 | 389 |
| 425 | 390 |
| 426 Thread::Thread(Isolate* isolate, const char* name) | 391 Thread::Thread(Isolate* isolate, const char* name) |
| 427 : ThreadHandle(ThreadHandle::INVALID), | 392 : data_(new PlatformData()), |
| 428 isolate_(isolate), | 393 isolate_(isolate), |
| 429 stack_size_(0) { | 394 stack_size_(0) { |
| 430 set_name(name); | 395 set_name(name); |
| 431 } | 396 } |
| 432 | 397 |
| 433 | 398 |
| 434 Thread::~Thread() { | 399 Thread::~Thread() { |
| 400 delete data_; |
| 435 } | 401 } |
| 436 | 402 |
| 437 | 403 |
| 438 static void* ThreadEntry(void* arg) { | 404 static void* ThreadEntry(void* arg) { |
| 439 Thread* thread = reinterpret_cast<Thread*>(arg); | 405 Thread* thread = reinterpret_cast<Thread*>(arg); |
| 440 // This is also initialized by the first argument to pthread_create() but we | 406 // This is also initialized by the first argument to pthread_create() but we |
| 441 // don't know which thread will run first (the original thread or the new | 407 // don't know which thread will run first (the original thread or the new |
| 442 // one) so we initialize it here too. | 408 // one) so we initialize it here too. |
| 443 thread->thread_handle_data()->thread_ = pthread_self(); | 409 thread->data()->thread_ = pthread_self(); |
| 444 ASSERT(thread->IsValid()); | 410 ASSERT(thread->data()->thread_ != kNoThread); |
| 445 Thread::SetThreadLocal(Isolate::isolate_key(), thread->isolate()); | 411 Thread::SetThreadLocal(Isolate::isolate_key(), thread->isolate()); |
| 446 thread->Run(); | 412 thread->Run(); |
| 447 return NULL; | 413 return NULL; |
| 448 } | 414 } |
| 449 | 415 |
| 450 | 416 |
| 451 void Thread::set_name(const char* name) { | 417 void Thread::set_name(const char* name) { |
| 452 strncpy(name_, name, sizeof(name_)); | 418 strncpy(name_, name, sizeof(name_)); |
| 453 name_[sizeof(name_) - 1] = '\0'; | 419 name_[sizeof(name_) - 1] = '\0'; |
| 454 } | 420 } |
| 455 | 421 |
| 456 | 422 |
| 457 void Thread::Start() { | 423 void Thread::Start() { |
| 458 pthread_attr_t* attr_ptr = NULL; | 424 pthread_attr_t* attr_ptr = NULL; |
| 459 pthread_attr_t attr; | 425 pthread_attr_t attr; |
| 460 if (stack_size_ > 0) { | 426 if (stack_size_ > 0) { |
| 461 pthread_attr_init(&attr); | 427 pthread_attr_init(&attr); |
| 462 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); | 428 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
| 463 attr_ptr = &attr; | 429 attr_ptr = &attr; |
| 464 } | 430 } |
| 465 pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); | 431 pthread_create(&data_->thread_, NULL, ThreadEntry, this); |
| 466 ASSERT(IsValid()); | 432 ASSERT(data_->thread_ != kNoThread); |
| 467 } | 433 } |
| 468 | 434 |
| 469 | 435 |
| 470 void Thread::Join() { | 436 void Thread::Join() { |
| 471 pthread_join(thread_handle_data()->thread_, NULL); | 437 pthread_join(data_->thread_, NULL); |
| 472 } | 438 } |
| 473 | 439 |
| 474 | 440 |
| 475 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { | 441 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 476 pthread_key_t key; | 442 pthread_key_t key; |
| 477 int result = pthread_key_create(&key, NULL); | 443 int result = pthread_key_create(&key, NULL); |
| 478 USE(result); | 444 USE(result); |
| 479 ASSERT(result == 0); | 445 ASSERT(result == 0); |
| 480 return static_cast<LocalStorageKey>(key); | 446 return static_cast<LocalStorageKey>(key); |
| 481 } | 447 } |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 data_->signal_handler_installed_ = false; | 753 data_->signal_handler_installed_ = false; |
| 788 } | 754 } |
| 789 | 755 |
| 790 // This sampler is no longer the active sampler. | 756 // This sampler is no longer the active sampler. |
| 791 active_sampler_ = NULL; | 757 active_sampler_ = NULL; |
| 792 } | 758 } |
| 793 | 759 |
| 794 #endif // ENABLE_LOGGING_AND_PROFILING | 760 #endif // ENABLE_LOGGING_AND_PROFILING |
| 795 | 761 |
| 796 } } // namespace v8::internal | 762 } } // namespace v8::internal |
| OLD | NEW |