Skip to content

Commit

Permalink
[fix] Fixed cross-thread deallocation of pjs::Strs in pipy::Data::Pro…
Browse files Browse the repository at this point in the history
…ducer
  • Loading branch information
pajama-coder committed Jun 17, 2024
1 parent 159d4e8 commit 279b365
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace pipy {

List<Data::Producer> Data::Producer::s_all_producers;
std::mutex Data::Producer::s_all_producers_mutex;
Data::Producer Data::s_unknown_producer("Unknown");

auto Data::Producer::unknown() -> Producer* {
Expand Down
10 changes: 7 additions & 3 deletions src/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <cstring>
#include <functional>
#include <atomic>
#include <mutex>

namespace pipy {

Expand Down Expand Up @@ -74,16 +75,18 @@ class Data : public EventTemplate<Data> {
static auto unknown() -> Producer*;

static void for_each(const std::function<void(Producer*)> &cb) {
std::lock_guard<std::mutex> lock(s_all_producers_mutex);
for (auto p = s_all_producers.head(); p; p = p->next()) {
cb(p);
}
}

Producer(const std::string &name) : m_name(pjs::Str::make(name)), m_count(0) {
Producer(const std::string &name) : m_name(name), m_count(0) {
std::lock_guard<std::mutex> lock(s_all_producers_mutex);
s_all_producers.push(this);
}

auto name() const -> pjs::Str::CharData* { return m_name->data(); }
auto name() const -> const std::string& { return m_name; }
auto count() const -> size_t { return m_count.load(std::memory_order_relaxed); }

Data* make(int size) { return Data::make(size, this); }
Expand All @@ -103,13 +106,14 @@ class Data : public EventTemplate<Data> {
void pack(Data *data, const Data *appendant, double vacancy = 0.5) { data->pack(*appendant, this, vacancy); }

private:
pjs::Ref<pjs::Str> m_name;
std::string m_name;
std::atomic<size_t> m_count;

void increase() { m_count.fetch_add(1, std::memory_order_relaxed); }
void decrease() { m_count.fetch_sub(1, std::memory_order_relaxed); }

static List<Producer> s_all_producers;
static std::mutex s_all_producers_mutex;

friend struct Chunk;
};
Expand Down
2 changes: 1 addition & 1 deletion src/status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ void Status::update_local() {
if (WorkerThread::current()->index() == 0) {
Data::Producer::for_each([&](Data::Producer *producer) {
chunks.insert({
producer->name()->str(),
producer->name(),
producer->count(),
});
});
Expand Down

0 comments on commit 279b365

Please sign in to comment.