| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 } | 581 } |
| 582 | 582 |
| 583 | 583 |
| 584 bool VirtualMemory::Uncommit(void* address, size_t size) { | 584 bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 585 return mmap(address, size, PROT_NONE, | 585 return mmap(address, size, PROT_NONE, |
| 586 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, | 586 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, |
| 587 kMmapFd, kMmapFdOffset) != MAP_FAILED; | 587 kMmapFd, kMmapFdOffset) != MAP_FAILED; |
| 588 } | 588 } |
| 589 | 589 |
| 590 | 590 |
| 591 class ThreadHandle::PlatformData : public Malloced { | 591 class Thread::PlatformData : public Malloced { |
| 592 public: | 592 public: |
| 593 explicit PlatformData(ThreadHandle::Kind kind) { | 593 PlatformData() : thread_(kNoThread) {} |
| 594 Initialize(kind); | |
| 595 } | |
| 596 | |
| 597 void Initialize(ThreadHandle::Kind kind) { | |
| 598 switch (kind) { | |
| 599 case ThreadHandle::SELF: thread_ = pthread_self(); break; | |
| 600 case ThreadHandle::INVALID: thread_ = kNoThread; break; | |
| 601 } | |
| 602 } | |
| 603 | 594 |
| 604 pthread_t thread_; // Thread handle for pthread. | 595 pthread_t thread_; // Thread handle for pthread. |
| 605 }; | 596 }; |
| 606 | 597 |
| 607 | |
| 608 ThreadHandle::ThreadHandle(Kind kind) { | |
| 609 data_ = new PlatformData(kind); | |
| 610 } | |
| 611 | |
| 612 | |
| 613 void ThreadHandle::Initialize(ThreadHandle::Kind kind) { | |
| 614 data_->Initialize(kind); | |
| 615 } | |
| 616 | |
| 617 | |
| 618 ThreadHandle::~ThreadHandle() { | |
| 619 delete data_; | |
| 620 } | |
| 621 | |
| 622 | |
| 623 bool ThreadHandle::IsSelf() const { | |
| 624 return pthread_equal(data_->thread_, pthread_self()); | |
| 625 } | |
| 626 | |
| 627 | |
| 628 bool ThreadHandle::IsValid() const { | |
| 629 return data_->thread_ != kNoThread; | |
| 630 } | |
| 631 | |
| 632 | |
| 633 Thread::Thread(Isolate* isolate, const Options& options) | 598 Thread::Thread(Isolate* isolate, const Options& options) |
| 634 : ThreadHandle(ThreadHandle::INVALID), | 599 : data_(new PlatformData()), |
| 635 isolate_(isolate), | 600 isolate_(isolate), |
| 636 stack_size_(options.stack_size) { | 601 stack_size_(options.stack_size) { |
| 637 set_name(options.name); | 602 set_name(options.name); |
| 638 } | 603 } |
| 639 | 604 |
| 640 | 605 |
| 641 Thread::Thread(Isolate* isolate, const char* name) | 606 Thread::Thread(Isolate* isolate, const char* name) |
| 642 : ThreadHandle(ThreadHandle::INVALID), | 607 : data_(new PlatformData()), |
| 643 isolate_(isolate), | 608 isolate_(isolate), |
| 644 stack_size_(0) { | 609 stack_size_(0) { |
| 645 set_name(name); | 610 set_name(name); |
| 646 } | 611 } |
| 647 | 612 |
| 648 | 613 |
| 649 Thread::~Thread() { | 614 Thread::~Thread() { |
| 615 delete data_; |
| 650 } | 616 } |
| 651 | 617 |
| 652 | 618 |
| 653 static void* ThreadEntry(void* arg) { | 619 static void* ThreadEntry(void* arg) { |
| 654 Thread* thread = reinterpret_cast<Thread*>(arg); | 620 Thread* thread = reinterpret_cast<Thread*>(arg); |
| 655 // This is also initialized by the first argument to pthread_create() but we | 621 // This is also initialized by the first argument to pthread_create() but we |
| 656 // don't know which thread will run first (the original thread or the new | 622 // don't know which thread will run first (the original thread or the new |
| 657 // one) so we initialize it here too. | 623 // one) so we initialize it here too. |
| 658 prctl(PR_SET_NAME, | 624 prctl(PR_SET_NAME, |
| 659 reinterpret_cast<unsigned long>(thread->name()), // NOLINT | 625 reinterpret_cast<unsigned long>(thread->name()), // NOLINT |
| 660 0, 0, 0); | 626 0, 0, 0); |
| 661 thread->thread_handle_data()->thread_ = pthread_self(); | 627 thread->data()->thread_ = pthread_self(); |
| 662 ASSERT(thread->IsValid()); | 628 ASSERT(thread->data()->thread_ != kNoThread); |
| 663 Thread::SetThreadLocal(Isolate::isolate_key(), thread->isolate()); | 629 Thread::SetThreadLocal(Isolate::isolate_key(), thread->isolate()); |
| 664 thread->Run(); | 630 thread->Run(); |
| 665 return NULL; | 631 return NULL; |
| 666 } | 632 } |
| 667 | 633 |
| 668 | 634 |
| 669 void Thread::set_name(const char* name) { | 635 void Thread::set_name(const char* name) { |
| 670 strncpy(name_, name, sizeof(name_)); | 636 strncpy(name_, name, sizeof(name_)); |
| 671 name_[sizeof(name_) - 1] = '\0'; | 637 name_[sizeof(name_) - 1] = '\0'; |
| 672 } | 638 } |
| 673 | 639 |
| 674 | 640 |
| 675 void Thread::Start() { | 641 void Thread::Start() { |
| 676 pthread_attr_t* attr_ptr = NULL; | 642 pthread_attr_t* attr_ptr = NULL; |
| 677 pthread_attr_t attr; | 643 pthread_attr_t attr; |
| 678 if (stack_size_ > 0) { | 644 if (stack_size_ > 0) { |
| 679 pthread_attr_init(&attr); | 645 pthread_attr_init(&attr); |
| 680 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); | 646 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); |
| 681 attr_ptr = &attr; | 647 attr_ptr = &attr; |
| 682 } | 648 } |
| 683 pthread_create(&thread_handle_data()->thread_, attr_ptr, ThreadEntry, this); | 649 pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); |
| 684 ASSERT(IsValid()); | 650 ASSERT(data_->thread_ != kNoThread); |
| 685 } | 651 } |
| 686 | 652 |
| 687 | 653 |
| 688 void Thread::Join() { | 654 void Thread::Join() { |
| 689 pthread_join(thread_handle_data()->thread_, NULL); | 655 pthread_join(data_->thread_, NULL); |
| 690 } | 656 } |
| 691 | 657 |
| 692 | 658 |
| 693 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { | 659 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 694 pthread_key_t key; | 660 pthread_key_t key; |
| 695 int result = pthread_key_create(&key, NULL); | 661 int result = pthread_key_create(&key, NULL); |
| 696 USE(result); | 662 USE(result); |
| 697 ASSERT(result == 0); | 663 ASSERT(result == 0); |
| 698 return static_cast<LocalStorageKey>(key); | 664 return static_cast<LocalStorageKey>(key); |
| 699 } | 665 } |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1111 | 1077 |
| 1112 void Sampler::Stop() { | 1078 void Sampler::Stop() { |
| 1113 ASSERT(IsActive()); | 1079 ASSERT(IsActive()); |
| 1114 SignalSender::RemoveActiveSampler(this); | 1080 SignalSender::RemoveActiveSampler(this); |
| 1115 SetActive(false); | 1081 SetActive(false); |
| 1116 } | 1082 } |
| 1117 | 1083 |
| 1118 #endif // ENABLE_LOGGING_AND_PROFILING | 1084 #endif // ENABLE_LOGGING_AND_PROFILING |
| 1119 | 1085 |
| 1120 } } // namespace v8::internal | 1086 } } // namespace v8::internal |
| OLD | NEW |