Skip to content

Commit

Permalink
Revert of how setApp is done
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhaowu committed Jul 23, 2024
1 parent 577ff17 commit 1cd42c3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
1 change: 0 additions & 1 deletion examples/lockless_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <cactus_rt/rt.h>

#include <chrono>
#include <iostream>

using cactus_rt::App;
using cactus_rt::CyclicThread;
Expand Down
1 change: 0 additions & 1 deletion include/cactus_rt/cyclic_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define CACTUS_RT_CYCLIC_THREAD_

#include "thread.h"
#include "utils.h"

namespace cactus_rt {
class CyclicThread : public Thread {
Expand Down
33 changes: 25 additions & 8 deletions include/cactus_rt/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ class Thread {
// 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 Down Expand Up @@ -91,6 +83,10 @@ class Thread {

/**
* Requests the thread to stop with an atomic.
*
* Note: currently a single thread object is only designed to run once. If you
* need to run another thread, create another object from your Thread class
* until a workaround is found.
*/
void RequestStop() noexcept {
stop_requested_ = true;
Expand All @@ -110,6 +106,27 @@ class Thread {
Thread(Thread&&) noexcept = delete;
Thread& operator=(Thread&&) noexcept = delete;

/**
* Starts the thread in the background.
*
* Note: for the time being, a single thread is supposed to only be started
* once. If you want to start another thread, create another Thread object
* from the same class.
*
* @param start_monotonic_time_ns should be the start time in nanoseconds for the monotonic clock.
*/
void Start(int64_t start_monotonic_time_ns);

/**
* @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;
}

protected:
inline quill::Logger* Logger() const { return logger_; }
inline tracing::ThreadTracer& Tracer() { return *tracer_; }
Expand Down
3 changes: 2 additions & 1 deletion src/cactus_rt/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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 @@ -43,7 +44,7 @@ void App::Start() {

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

Expand Down
2 changes: 2 additions & 0 deletions src/cactus_rt/cyclic_thread.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "cactus_rt/cyclic_thread.h"

#include "cactus_rt/utils.h"

namespace cactus_rt {

void CyclicThread::Run() noexcept {
Expand Down
7 changes: 4 additions & 3 deletions src/cactus_rt/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ void* Thread::RunThread(void* data) {
if (thread->app_ != nullptr) {
thread->app_->RegisterThreadTracer(thread->tracer_);
} else {
LOG_WARNING(thread->Logger(), "thread {} does not have trace_aggregator_ and tracing is disabled. Did you all App::RegisterThread?", thread->name_);
LOG_WARNING(thread->Logger(), "thread {} does not have app_ and tracing is disabled for this thread. Did you call App::RegisterThread?", thread->name_);
}

quill::preallocate(); // Pre-allocates thread-local data to avoid the need to allocate on the first log message

thread->BeforeRun();
thread->Run();
thread->AfterRun();

return nullptr;
}

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

pthread_attr_t attr;

Expand Down
2 changes: 0 additions & 2 deletions tests/tracing/helpers/assert_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <google/protobuf/util/json_util.h>
#include <gtest/gtest.h>

#include <utility>

std::string ProtoToJson(const google::protobuf::Message& proto) {
std::string json;
google::protobuf::util::MessageToJsonString(proto, &json);
Expand Down

0 comments on commit 1cd42c3

Please sign in to comment.