blob: bc323a64a3e6843c30e55817546146cffa3ead4a [file] [log] [blame]
Primiano Tucci575af772017-11-08 18:14:17 +00001/*
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 PERFETTO_BASE_THREAD_CHECKER_H_
18#define PERFETTO_BASE_THREAD_CHECKER_H_
19
20#include <pthread.h>
21#include <atomic>
22
23#include "base/logging.h"
24
25namespace perfetto {
26namespace base {
27
28class 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 Tucci49a22472017-11-08 23:43:52 +000042#define PERFETTO_THREAD_CHECKER(name) base::ThreadChecker name;
Primiano Tucci575af772017-11-08 18:14:17 +000043#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 // PERFETTO_BASE_THREAD_CHECKER_H_