1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INCLUDE_PERFETTO_BASE_THREAD_CHECKER_H_ 18 #define INCLUDE_PERFETTO_BASE_THREAD_CHECKER_H_ 19 20 #include <pthread.h> 21 #include <atomic> 22 23 #include "perfetto/base/logging.h" 24 25 namespace perfetto { 26 namespace base { 27 28 class ThreadChecker { 29 public: 30 ThreadChecker(); 31 ~ThreadChecker(); 32 ThreadChecker(const ThreadChecker&); 33 ThreadChecker& operator=(const ThreadChecker&); 34 bool CalledOnValidThread() const __attribute__((warn_unused_result)); 35 void DetachFromThread(); 36 37 private: 38 mutable std::atomic<pthread_t> thread_id_; 39 }; 40 41 #if PERFETTO_DCHECK_IS_ON() 42 #define PERFETTO_THREAD_CHECKER(name) base::ThreadChecker name; 43 #define PERFETTO_DCHECK_THREAD(name) \ 44 PERFETTO_DCHECK((name).CalledOnValidThread()) 45 #define PERFETTO_DETACH_FROM_THREAD(name) (name).DetachFromThread() 46 #else 47 #define PERFETTO_THREAD_CHECKER(name) 48 #define PERFETTO_DCHECK_THREAD(name) 49 #define PERFETTO_DETACH_FROM_THREAD(name) 50 #endif // PERFETTO_DCHECK_IS_ON() 51 52 } // namespace base 53 } // namespace perfetto 54 55 #endif // INCLUDE_PERFETTO_BASE_THREAD_CHECKER_H_ 56