Skip to content

Commit

Permalink
Don't use constructor to implement track initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
dsharlet committed Nov 20, 2024
1 parent ec4429c commit e6f51e2
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pthread_trace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,10 @@ class incremental_clock {
static std::atomic<int> next_track_id{0};

class track {
circular_file* global_buffer;
// This cannot have a constructor, to avoid recursive hook issues with malloc implementations that use functions we
// hook. The C++ mechanism to register a destructor at thread exit uses malloc.
bool constructed = false;
circular_file* global_buffer = nullptr;
int id;
std::array<uint8_t, 4> sequence_id;

Expand Down Expand Up @@ -547,17 +550,18 @@ class track {
}
}

public:
NOINLINE track() : global_buffer(nullptr), id(next_track_id++) {
thread_local bool initializing = false;
if (initializing) return;
initializing = true;
void construct() {
id = next_track_id++;
init_trace();
global_buffer = &*file;
begin_block(/*first=*/true);
}

NOINLINE ~track() { write_block(); }
public:
// This cannot have a non-trivial constructor.
track() = default;

NOINLINE ~track() noexcept { write_block(); }

// This is inline so we can see constexpr track_events.
template <typename TrackEvent>
Expand Down Expand Up @@ -603,6 +607,10 @@ class track {

static track& get_thread() {
thread_local track t;
if (!t.constructed) {
t.construct();
t.constructed = true;
}
return t;
}
};
Expand Down

0 comments on commit e6f51e2

Please sign in to comment.