Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 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 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 17 | #ifndef PERFETTO_PERFETTO_BASE_THREAD_CHECKER_H_ |
| 18 | #define PERFETTO_PERFETTO_BASE_THREAD_CHECKER_H_ |
Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 19 | |
| 20 | #include <pthread.h> |
| 21 | #include <atomic> |
| 22 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 23 | #include "perfetto_base/logging.h" |
Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 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() |
Primiano Tucci | 49a2247 | 2017-11-08 23:43:52 +0000 | [diff] [blame] | 42 | #define PERFETTO_THREAD_CHECKER(name) base::ThreadChecker name; |
Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 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 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 55 | #endif // PERFETTO_PERFETTO_BASE_THREAD_CHECKER_H_ |