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/. */
7 #ifndef ProfilerThreadRegistrationInfo_h
8 #define ProfilerThreadRegistrationInfo_h
10 #include "mozilla/BaseAndGeckoProfilerDetail.h"
11 #include "mozilla/ProfilerUtils.h"
12 #include "mozilla/TimeStamp.h"
16 namespace mozilla::profiler
{
18 // This class contains immutable information about a thread which needs to be
19 // stored across restarts of the profiler and which can be useful even after the
20 // thread has stopped running.
21 class ThreadRegistrationInfo
{
23 // Construct on the thread.
24 explicit ThreadRegistrationInfo(const char* aName
) : mName(aName
) {}
26 // Construct for a foreign thread (e.g., Java).
27 ThreadRegistrationInfo(const char* aName
, ProfilerThreadId aThreadId
,
28 bool aIsMainThread
, const TimeStamp
& aRegisterTime
)
30 mRegisterTime(aRegisterTime
),
32 mIsMainThread(aIsMainThread
) {}
34 // Only allow move construction, for extraction when the thread ends.
35 ThreadRegistrationInfo(ThreadRegistrationInfo
&&) = default;
37 // Other copies/moves disallowed.
38 ThreadRegistrationInfo(const ThreadRegistrationInfo
&) = delete;
39 ThreadRegistrationInfo
& operator=(const ThreadRegistrationInfo
&) = delete;
40 ThreadRegistrationInfo
& operator=(ThreadRegistrationInfo
&&) = delete;
42 [[nodiscard
]] const char* Name() const { return mName
.c_str(); }
43 [[nodiscard
]] const TimeStamp
& RegisterTime() const { return mRegisterTime
; }
44 [[nodiscard
]] ProfilerThreadId
ThreadId() const { return mThreadId
; }
45 [[nodiscard
]] bool IsMainThread() const { return mIsMainThread
; }
48 static TimeStamp
ExistingRegisterTimeOrNow() {
49 TimeStamp registerTime
= baseprofiler::detail::GetThreadRegistrationTime();
51 registerTime
= TimeStamp::Now();
56 const std::string mName
;
57 const TimeStamp mRegisterTime
= ExistingRegisterTimeOrNow();
58 const ProfilerThreadId mThreadId
= profiler_current_thread_id();
59 const bool mIsMainThread
= profiler_is_main_thread();
62 } // namespace mozilla::profiler
64 #endif // ProfilerThreadRegistrationInfo_h