Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: base/debug/trace_event_impl.cc

Issue 26541005: Avoid threading races on TraceSamplingThread's members. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/debug/trace_event_impl.h" 5 #include "base/debug/trace_event_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/base_switches.h" 9 #include "base/base_switches.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 // Not thread-safe. Once the ThreadMain has been called, this can no longer 709 // Not thread-safe. Once the ThreadMain has been called, this can no longer
710 // be called. 710 // be called.
711 void RegisterSampleBucket(TRACE_EVENT_API_ATOMIC_WORD* bucket, 711 void RegisterSampleBucket(TRACE_EVENT_API_ATOMIC_WORD* bucket,
712 const char* const name, 712 const char* const name,
713 TraceSampleCallback callback); 713 TraceSampleCallback callback);
714 // Splits a combined "category\0name" into the two component parts. 714 // Splits a combined "category\0name" into the two component parts.
715 static void ExtractCategoryAndName(const char* combined, 715 static void ExtractCategoryAndName(const char* combined,
716 const char** category, 716 const char** category,
717 const char** name); 717 const char** name);
718 std::vector<TraceBucketData> sample_buckets_; 718 std::vector<TraceBucketData> sample_buckets_;
719 Lock lock_;
719 bool thread_running_; 720 bool thread_running_;
720 scoped_ptr<CancellationFlag> cancellation_flag_; 721 scoped_ptr<CancellationFlag> cancellation_flag_;
721 scoped_ptr<WaitableEvent> waitable_event_for_testing_; 722 scoped_ptr<WaitableEvent> waitable_event_for_testing_;
722 }; 723 };
723 724
724 725
725 TraceSamplingThread::TraceSamplingThread() 726 TraceSamplingThread::TraceSamplingThread()
726 : thread_running_(false) { 727 : thread_running_(false) {
728 AutoLock lock(lock_);
727 cancellation_flag_.reset(new CancellationFlag); 729 cancellation_flag_.reset(new CancellationFlag);
728 } 730 }
729 731
730 TraceSamplingThread::~TraceSamplingThread() { 732 TraceSamplingThread::~TraceSamplingThread() {
731 } 733 }
732 734
733 void TraceSamplingThread::ThreadMain() { 735 void TraceSamplingThread::ThreadMain() {
734 PlatformThread::SetName("Sampling Thread"); 736 PlatformThread::SetName("Sampling Thread");
735 thread_running_ = true; 737 thread_running_ = true;
736 const int kSamplingFrequencyMicroseconds = 1000; 738 const int kSamplingFrequencyMicroseconds = 1000;
737 while (!cancellation_flag_->IsSet()) { 739 while (true) {
738 PlatformThread::Sleep( 740 PlatformThread::Sleep(
739 TimeDelta::FromMicroseconds(kSamplingFrequencyMicroseconds)); 741 TimeDelta::FromMicroseconds(kSamplingFrequencyMicroseconds));
740 GetSamples(); 742 GetSamples();
743
744 AutoLock lock(lock_);
745 if (cancellation_flag_->IsSet())
746 break;
Alexander Potapenko 2013/10/09 06:55:32 Nit: please fix the indentation
haraken 2013/10/09 07:05:07 Done.
741 if (waitable_event_for_testing_.get()) 747 if (waitable_event_for_testing_.get())
742 waitable_event_for_testing_->Signal(); 748 waitable_event_for_testing_->Signal();
743 } 749 }
744 } 750 }
745 751
746 // static 752 // static
747 void TraceSamplingThread::DefaultSamplingCallback( 753 void TraceSamplingThread::DefaultSamplingCallback(
748 TraceBucketData* bucket_data) { 754 TraceBucketData* bucket_data) {
749 TRACE_EVENT_API_ATOMIC_WORD category_and_name = 755 TRACE_EVENT_API_ATOMIC_WORD category_and_name =
750 TRACE_EVENT_API_ATOMIC_LOAD(*bucket_data->bucket); 756 TRACE_EVENT_API_ATOMIC_LOAD(*bucket_data->bucket);
(...skipping 26 matching lines...) Expand all
777 783
778 // static 784 // static
779 void TraceSamplingThread::ExtractCategoryAndName(const char* combined, 785 void TraceSamplingThread::ExtractCategoryAndName(const char* combined,
780 const char** category, 786 const char** category,
781 const char** name) { 787 const char** name) {
782 *category = combined; 788 *category = combined;
783 *name = &combined[strlen(combined) + 1]; 789 *name = &combined[strlen(combined) + 1];
784 } 790 }
785 791
786 void TraceSamplingThread::Stop() { 792 void TraceSamplingThread::Stop() {
793 AutoLock lock(lock_);
787 cancellation_flag_->Set(); 794 cancellation_flag_->Set();
788 } 795 }
789 796
790 void TraceSamplingThread::InstallWaitableEventForSamplingTesting( 797 void TraceSamplingThread::InstallWaitableEventForSamplingTesting(
791 WaitableEvent* waitable_event) { 798 WaitableEvent* waitable_event) {
799 AutoLock lock(lock_);
792 waitable_event_for_testing_.reset(waitable_event); 800 waitable_event_for_testing_.reset(waitable_event);
793 } 801 }
794 802
795 TraceBucketData::TraceBucketData(base::subtle::AtomicWord* bucket, 803 TraceBucketData::TraceBucketData(base::subtle::AtomicWord* bucket,
796 const char* name, 804 const char* name,
797 TraceSampleCallback callback) 805 TraceSampleCallback callback)
798 : bucket(bucket), 806 : bucket(bucket),
799 bucket_name(name), 807 bucket_name(name),
800 callback(callback) { 808 callback(callback) {
801 } 809 }
(...skipping 1268 matching lines...) Expand 10 before | Expand all | Expand 10 after
2070 0, // num_args 2078 0, // num_args
2071 NULL, // arg_names 2079 NULL, // arg_names
2072 NULL, // arg_types 2080 NULL, // arg_types
2073 NULL, // arg_values 2081 NULL, // arg_values
2074 NULL, // convertable values 2082 NULL, // convertable values
2075 TRACE_EVENT_FLAG_NONE); // flags 2083 TRACE_EVENT_FLAG_NONE); // flags
2076 } 2084 }
2077 } 2085 }
2078 2086
2079 } // namespace trace_event_internal 2087 } // namespace trace_event_internal
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698