From ffbda10200ef57eceec49e8a24d3d224101f4c8f Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 13 Nov 2018 14:14:42 -0500 Subject: [PATCH] Fix capture of transient 'this' pointer --- src/lib/md/MDProducerHandle.h | 2 +- src/lib/md/SecurityIDRecorder.cpp | 65 ++++++++++++++++++++----------- src/lib/md/SecurityIDRecorder.h | 5 --- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/lib/md/MDProducerHandle.h b/src/lib/md/MDProducerHandle.h index 050dec46d..5f5e5f52b 100644 --- a/src/lib/md/MDProducerHandle.h +++ b/src/lib/md/MDProducerHandle.h @@ -118,7 +118,7 @@ private: // Private default constructor allows friend classes to store this class in a concurrent_queue MDProducerHandle(); friend class tbb::concurrent_queue < MDProducerHandle > ; - friend class SecurityIDRecorder; + friend class SecurityIDRecorderState; friend struct bts::co2::Work; HotEntry& hdata() const { diff --git a/src/lib/md/SecurityIDRecorder.cpp b/src/lib/md/SecurityIDRecorder.cpp index 8b4fc9711..192ca68e2 100644 --- a/src/lib/md/SecurityIDRecorder.cpp +++ b/src/lib/md/SecurityIDRecorder.cpp @@ -31,12 +31,31 @@ struct SecurityIDRecorderState { SecurityIDRecorderState() : work(), pending_handle(), - db(nullptr, sqlite3_close) + db(nullptr, sqlite3_close), + timer(), + id((size_t)this) {} tbb::concurrent_queue> work; tbb::concurrent_queue pending_handle; sqlite3_ptr db; unique_ptr timer; + size_t id; + + void run(utils::Dispatcher& dispatcher) { + timer = std::make_unique( + dispatcher, 500, + [=]() { do_work(); }); + timer->start(); + } + void do_work() { + work_handles(); + work_db(); + } + void work_handles(); + void work_db(); + void assign_key(const api::Security& security, const SecurityIDRecorder::Callback& key_assigned); + security_key search(const std::string& descriptor); + security_key insert(const std::string& descriptor, SecurityType); }; SecurityIDRecorder::SecurityIDRecorder(): @@ -49,10 +68,7 @@ SecurityIDRecorder::SecurityIDRecorder(utils::Dispatcher& dispatcher): _self(new SecurityIDRecorderState()) { init(); - _self->timer = std::make_unique( - dispatcher, 250, - [=]() { this->do_work(); }); - _self->timer->start(); + _self->run(dispatcher); } SecurityIDRecorder::~SecurityIDRecorder() { } @@ -77,19 +93,22 @@ void SecurityIDRecorder::init() { void SecurityIDRecorder::ensure_key(MDProducerHandle handle) { _self->pending_handle.push(handle); } + void SecurityIDRecorder::assign_key(const api::Security& security, const SecurityIDRecorder::Callback& callback) { + _self->assign_key(security, callback); +} + +void SecurityIDRecorderState::assign_key(const api::Security& security, const SecurityIDRecorder::Callback& callback) { if (security.id() == invalid_security_key) { auto work = std::make_shared(); work->security = security; work->callback = callback; - _self->work.push(work); + this->work.push(work); } } void SecurityIDRecorder::do_work() { - LOG(md, Info, "do_work"); - work_handles(); - work_db(); + _self->do_work(); } bool has_all_leg_keys(const MDProducerHandle& handle) { @@ -105,11 +124,12 @@ bool has_all_leg_keys(const MDProducerHandle& handle) { return true; } -void SecurityIDRecorder::work_handles() { +void SecurityIDRecorderState::work_handles() { Security security; MDProducerHandle handle; vector retry; - while (_self->pending_handle.try_pop(handle)) { + int count = 0; + while (pending_handle.try_pop(handle)) { security.Clear(); // if a spread does not have all of its legs we need to wait if (!has_all_leg_keys(handle)) { @@ -117,6 +137,7 @@ void SecurityIDRecorder::work_handles() { } else { convert_security(handle.security(), &security); + ++count; assign_key(security, [handle](const api::Security& security) mutable { handle.set_key(security.id()); }); @@ -124,11 +145,11 @@ void SecurityIDRecorder::work_handles() { } for(const auto& h : retry) { - _self->pending_handle.push(h); + pending_handle.push(h); } } -void SecurityIDRecorder::work_db() { +void SecurityIDRecorderState::work_db() { // Drain the work queue // Query the database for each security. If it has an ID set it // If it does not have an ID add it to a list @@ -142,7 +163,7 @@ void SecurityIDRecorder::work_db() { vector new_descriptors; size_t count = 0; string descriptor; - while (_self->work.try_pop(temp)) { + while (work.try_pop(temp)) { descriptor = security_descriptor(temp->security); // If we're unable to create a descriptor try again later @@ -170,12 +191,12 @@ void SecurityIDRecorder::work_db() { } for (auto& d : defer) { - _self->work.push(move(d)); + work.push(move(d)); } if (!new_entries.empty()) { - sqlite3_exec_nofail(_self->db.get(), "BEGIN TRANSACTION"); + sqlite3_exec_nofail(db.get(), "BEGIN TRANSACTION"); for (size_t x = 0; x < new_entries.size(); ++x) { Security& security = new_entries[x]->security; @@ -184,7 +205,7 @@ void SecurityIDRecorder::work_db() { security.set_id(key); } - sqlite3_exec_nofail(_self->db.get(), "COMMIT TRANSACTION"); + sqlite3_exec_nofail(db.get(), "COMMIT TRANSACTION"); } // record the securities @@ -228,8 +249,8 @@ void SecurityIDRecorder::bulk_insert(const std::vector& da sqlite3_exec_nofail(_self->db.get(), "COMMIT TRANSACTION"); } -security_key SecurityIDRecorder::search(const std::string& descriptor) { - auto select_stmt = prepare_query(_self->db, "SELECT rowid from security_keys where descriptor=? LIMIT 1;"); +security_key SecurityIDRecorderState::search(const std::string& descriptor) { + auto select_stmt = prepare_query(db, "SELECT rowid from security_keys where descriptor=? LIMIT 1;"); sqlite3_bind_text(select_stmt.get(), 1, descriptor.c_str(), descriptor.size(), SQLITE_STATIC); int result = sqlite3_step(select_stmt.get()); if (result == SQLITE_DONE || result == SQLITE_ROW) { @@ -238,13 +259,13 @@ security_key SecurityIDRecorder::search(const std::string& descriptor) { return invalid_security_key; } -security_key SecurityIDRecorder::insert(const std::string& descriptor, SecurityType type) { - auto insert_stmt = prepare_query(_self->db, "INSERT INTO security_keys (descriptor, type) values (?, ?)"); +security_key SecurityIDRecorderState::insert(const std::string& descriptor, SecurityType type) { + auto insert_stmt = prepare_query(db, "INSERT INTO security_keys (descriptor, type) values (?, ?)"); sqlite3_bind_text(insert_stmt.get(), 1, descriptor.c_str(), descriptor.size(), SQLITE_STATIC); sqlite3_bind_int(insert_stmt.get(), 2, (int)type); int result = sqlite3_step(insert_stmt.get()); if (result == SQLITE_DONE || result == SQLITE_ROW) { - return sqlite3_last_insert_rowid(_self->db.get()); + return sqlite3_last_insert_rowid(db.get()); } // Just in case our insert failed due to duplicate row auto key = search(descriptor); diff --git a/src/lib/md/SecurityIDRecorder.h b/src/lib/md/SecurityIDRecorder.h index e42aab2b6..f63731ff1 100644 --- a/src/lib/md/SecurityIDRecorder.h +++ b/src/lib/md/SecurityIDRecorder.h @@ -41,11 +41,6 @@ private: std::shared_ptr _self; void init(); - security_key search(const std::string& descriptor); - security_key insert(const std::string& descriptor, SecurityType); - - void work_handles(); - void work_db(); }; }}//namespace