Skip to content

Commit

Permalink
Minor simplification to the app handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhaowu committed Jul 18, 2024
1 parent 70dc51d commit 6e02c2a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
36 changes: 15 additions & 21 deletions include/cactus_rt/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class Thread {

std::atomic_bool stop_requested_ = false;

// Non-owning App pointer. Used only for notifying that the thread has
// started/stopped for tracing purposes.
App* app_ = nullptr;

pthread_t thread_;
int64_t start_monotonic_time_ns_ = 0;

Expand All @@ -45,6 +41,21 @@ class Thread {
*/
static void* RunThread(void* data);

friend class App;

// Non-owning App pointer. Used only for notifying that the thread has
// started/stopped for tracing purposes. Set by Thread::Start and read at
// the beginning of Thread::RunThread.
App* app_ = nullptr;

/**
* Starts the thread in the background.
*
* @param start_monotonic_time_ns should be the start time in nanoseconds for the monotonic clock.
* @param app The application that started this thread.
*/
void Start(int64_t start_monotonic_time_ns, App* app);

public:
/**
* Creates a new thread.
Expand All @@ -71,13 +82,6 @@ class Thread {
*/
inline const std::string& Name() { return name_; }

/**
* Starts the thread in the background.
*
* @param start_monotonic_time_ns should be the start time in nanoseconds for the monotonic clock.
*/
void Start(int64_t start_monotonic_time_ns);

/**
* Joins the thread.
*
Expand All @@ -92,16 +96,6 @@ class Thread {
stop_requested_ = true;
}

/**
* @brief Sets the trace_aggregator_ pointer so the thread can notify the
* trace_aggregator_ when it starts. This should only be called by App.
*
* @private
*/
inline void SetApp(App* app) {
app_ = app;
}

// The constructors and destructors are needed because we need to delete
// objects of type Thread polymorphically, through the map in the App class.
virtual ~Thread() = default;
Expand Down
3 changes: 1 addition & 2 deletions src/cactus_rt/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ using FileSink = cactus_rt::tracing::FileSink;
namespace cactus_rt {

void App::RegisterThread(std::shared_ptr<Thread> thread) {
thread->SetApp(this);
threads_.push_back(thread);
}

Expand Down Expand Up @@ -44,7 +43,7 @@ void App::Start() {

auto start_monotonic_time_ns = NowNs();
for (auto& thread : threads_) {
thread->Start(start_monotonic_time_ns);
thread->Start(start_monotonic_time_ns, this);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/cactus_rt/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ void* Thread::RunThread(void* data) {
return nullptr;
}

void Thread::Start(int64_t start_monotonic_time_ns) {
void Thread::Start(int64_t start_monotonic_time_ns, App* app) {
start_monotonic_time_ns_ = start_monotonic_time_ns;
app_ = app;

pthread_attr_t attr;

Expand Down

0 comments on commit 6e02c2a

Please sign in to comment.