Debian Codegraph @

mozglue/baseprofiler/core/ThreadInfo.h

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef ThreadInfo_h
8 #define ThreadInfo_h
9
10 #include "mozilla/Atomics.h"
11 #include "mozilla/BaseProfilerUtils.h"
12 #include "mozilla/TimeStamp.h"
13
14 namespace mozilla {
15 namespace baseprofiler {
16
17 // This class contains information about a thread which needs to be stored
18 // across restarts of the profiler and which can be useful even after the
19 // thread has stopped running.
20 // It uses threadsafe refcounting and only contains immutable data.
21 class ThreadInfo final {
22 public:
23 ThreadInfo(const char* aName, BaseProfilerThreadId aThreadId,
24 bool aIsMainThread,
25 const TimeStamp& aRegisterTime = TimeStamp::Now())
26 : mName(aName),
27 mRegisterTime(aRegisterTime),
28 mThreadId(aThreadId),
29 mIsMainThread(aIsMainThread),
30 mRefCnt(0) {
31 MOZ_ASSERT(aThreadId.IsSpecified(),
32 "Given aThreadId should not be unspecified");
33 }
34
35 // Using hand-rolled ref-counting, because RefCounted.h macros don't produce
36 // the same code between mozglue and libxul, see bug 1536656.
37 MFBT_API void AddRef() const { ++mRefCnt; }
38 MFBT_API void Release() const {
39 MOZ_ASSERT(int32_t(mRefCnt) > 0);
40 if (--mRefCnt == 0) {
41 delete this;
42 }
43 }
44
45 const char* Name() const { return mName.c_str(); }
46 TimeStamp RegisterTime() const { return mRegisterTime; }
47 BaseProfilerThreadId ThreadId() const { return mThreadId; }
48 bool IsMainThread() const { return mIsMainThread; }
49
50 private:
51 const std::string mName;
52 const TimeStamp mRegisterTime;
53 const BaseProfilerThreadId mThreadId;
54 const bool mIsMainThread;
55
56 mutable Atomic<int32_t, MemoryOrdering::ReleaseAcquire> mRefCnt;
57 };
58
59 } // namespace baseprofiler
60 } // namespace mozilla
61
62 #endif // ThreadInfo_h