Skip to content

Commit 6959d07

Browse files
committed
Merge remote-tracking branch 'origin/topic/robin/threading-reorg'
* origin/topic/robin/threading-reorg: Small test case fix reported by thread sanitizer. Fix configure's `--sanitizer` argument. In `.schema` output, break out table parameters separately. Remove helper class we no longer need. Remove most locking from scheduler. Remove locking from most classes. Push all asynchronous activity back to the main thread.
2 parents ffe978a + 12ac2ec commit 6959d07

File tree

19 files changed

+195
-273
lines changed

19 files changed

+195
-273
lines changed

CHANGES

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2.0.2 | 2022-02-21 16:36:16 +0100
2+
3+
* Push all asynchronous activity to the main thread to avoid most
4+
inter-thread locking.
5+
6+
* Fix configure's `--sanitizer` argument.
7+
8+
* In `.schema` output, break out table parameters separately.
9+
110
2.0.1 | 2022-02-21 15:26:44 +0100
211

312
* Add a test build of the source code tarball to CI.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.1
1+
2.0.2

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ while [ $# -ne 0 ]; do
9999
--enable-debug) cmake_build_type="Debug";;
100100
--enable-osx-universal) cmake_osx_architectures="'arm64;x86_64'";;
101101
--enable-sanitizer) cmake_use_sanitizers="address";;
102-
--enable-sanitizer=*) cmake_use_sanitizers="${optargs}";;
102+
--enable-sanitizer=*) cmake_use_sanitizers="${optarg}";;
103103
--enable-static) cmake_use_static_linking="yes";;
104104
--enable-werror) cmake_use_werror="yes";;
105105
--generator=*) cmake_generator="${optarg}";;

src/core/configuration.cc

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,9 @@ Configuration::~Configuration() {
395395
logger()->set_level(pimpl()->old_log_level);
396396
}
397397

398-
const Options& Configuration::options() const {
399-
Synchronize _(this);
400-
return pimpl()->_options;
401-
}
398+
const Options& Configuration::options() const { return pimpl()->_options; }
402399

403400
Result<Nothing> Configuration::initFromArgv(int argc, const char* const* argv) {
404-
Synchronize _(this);
405-
406401
std::vector<std::string> vargv;
407402
vargv.reserve(argc);
408403
for ( auto i = 0; i < argc; i++ )
@@ -413,13 +408,11 @@ Result<Nothing> Configuration::initFromArgv(int argc, const char* const* argv) {
413408
}
414409

415410
Result<Nothing> Configuration::read(const filesystem::path& path) {
416-
Synchronize _(this);
417411
ZEEK_AGENT_DEBUG("configuration", "reading file {}", path.native());
418412
return pimpl()->read(path);
419413
}
420414

421415
Result<Nothing> Configuration::read(std::istream& in, const filesystem::path& path) {
422-
Synchronize _(this);
423416
ZEEK_AGENT_DEBUG("configuration", "reading stream associated with file {}", path.native());
424417
return pimpl()->read(in, path);
425418
}

src/core/configuration.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#pragma once
44

55
#include "util/filesystem.h"
6+
#include "util/helpers.h"
67
#include "util/pimpl.h"
78
#include "util/result.h"
8-
#include "util/threading.h"
99

1010
#include <optional>
1111
#include <string>
@@ -107,7 +107,7 @@ struct Options {
107107
*
108108
* All public methods in this class are thread-safe.
109109
*/
110-
class Configuration : public Pimpl<Configuration>, protected SynchronizedBase {
110+
class Configuration : public Pimpl<Configuration> {
111111
public:
112112
Configuration();
113113
~Configuration();

src/core/database.cc

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "sqlite.h"
88
#include "util/helpers.h"
99
#include "util/testing.h"
10-
#include "util/threading.h"
1110

1211
#include <algorithm>
1312
#include <list>
@@ -59,8 +58,6 @@ struct Pimpl<Database>::Implementation {
5958
// Helper to lookup scheduled query.
6059
std::optional<std::list<ScheduledQuery>::iterator> lookupQuery(query::ID);
6160

62-
SynchronizedBase* _synchronized =
63-
nullptr; // database's synchronizer, so that we can grab it during callback execution
6461
const Configuration* _configuration = nullptr; // configuration object, as passed into constructor
6562
Scheduler* _scheduler = nullptr; // scheduler as passed into constructor
6663
std::unique_ptr<SQLite> _sqlite; // SQLite backend for performing queries
@@ -127,9 +124,7 @@ void Database::Implementation::expire() {
127124
continue;
128125

129126
if ( (*i)->query.callback_done )
130-
_synchronized->unlockWhile([&, id = id, regular_shutdown = regular_shutdown]() {
131-
(*(*i)->query.callback_done)(id, ! regular_shutdown);
132-
});
127+
(*(*i)->query.callback_done)(id, ! regular_shutdown);
133128
}
134129

135130
for ( const auto& [id, regular_shutdown] : _cancelled_queries ) {
@@ -231,15 +226,12 @@ static auto newRows(std::vector<std::vector<Value>> old, std::vector<std::vector
231226
}
232227

233228
Interval Database::Implementation::timerCallback(timer::ID id) {
234-
SynchronizedBase::Synchronize _(_synchronized);
235-
236229
auto i = lookupQuery(id);
237230
if ( ! i || (*i)->query.cancelled )
238231
// already gone, or will be cleaned up shortly
239232
return 0s;
240233

241-
auto sql_result = _synchronized->unlockWhile(
242-
[&]() { return _sqlite->runStatement(*(*i)->prepared_query, (*i)->previous_execution); });
234+
auto sql_result = _sqlite->runStatement(*(*i)->prepared_query, (*i)->previous_execution);
243235

244236
// re-lookup because we released the lock
245237
i = lookupQuery(id);
@@ -282,14 +274,12 @@ Interval Database::Implementation::timerCallback(timer::ID id) {
282274
#endif
283275

284276
if ( (*i)->query.callback_result ) {
285-
_synchronized->unlockWhile([&]() {
286-
auto query_result = query::Result{.columns = sql_result->columns,
287-
.rows = std::move(rows),
288-
.cookie = (*i)->query.cookie,
289-
.initial_result = ! (*i)->previous_result.has_value()};
277+
auto query_result = query::Result{.columns = sql_result->columns,
278+
.rows = std::move(rows),
279+
.cookie = (*i)->query.cookie,
280+
.initial_result = ! (*i)->previous_result.has_value()};
290281

291-
(*(*i)->query.callback_result)(id, query_result);
292-
});
282+
(*(*i)->query.callback_result)(id, query_result);
293283

294284
// repeat search in case map was modified by callback
295285
i = lookupQuery(id);
@@ -328,7 +318,6 @@ Table* Database::Implementation::table(const std::string& name) {
328318

329319
Database::Database(Configuration* configuration, Scheduler* scheduler) {
330320
ZEEK_AGENT_DEBUG("database", "creating instance");
331-
pimpl()->_synchronized = this;
332321
pimpl()->_configuration = configuration;
333322
pimpl()->_scheduler = scheduler;
334323
pimpl()->_sqlite = std::make_unique<SQLite>();
@@ -349,15 +338,9 @@ Time Database::currentTime() const {
349338
return pimpl()->_scheduler->currentTime();
350339
}
351340

352-
size_t Database::numberQueries() const {
353-
Synchronize _(this);
354-
return pimpl()->_queries.size();
355-
}
341+
size_t Database::numberQueries() const { return pimpl()->_queries.size(); }
356342

357-
Table* Database::table(const std::string& name) {
358-
Synchronize _(this);
359-
return pimpl()->table(name);
360-
}
343+
Table* Database::table(const std::string& name) { return pimpl()->table(name); }
361344

362345
std::vector<const Table*> Database::tables() {
363346
std::vector<const Table*> out;
@@ -371,7 +354,6 @@ std::vector<const Table*> Database::tables() {
371354

372355
Result<std::optional<query::ID>> Database::query(const Query& q) {
373356
ZEEK_AGENT_DEBUG("database", "new query: {} ", q.sql_stmt);
374-
Synchronize _(this);
375357

376358
auto id = pimpl()->query(q);
377359
if ( id ) {
@@ -388,26 +370,21 @@ Result<std::optional<query::ID>> Database::query(const Query& q) {
388370

389371
void Database::cancel(query::ID id) {
390372
ZEEK_AGENT_DEBUG("database", "canceling query {}", id);
391-
Synchronize _(this);
392373
return pimpl()->cancel(id, false);
393374
}
394375

395376
void Database::poll() {
396377
ZEEK_AGENT_DEBUG("database", "polling database");
397-
Synchronize _(this);
398378
pimpl()->poll();
399379
pimpl()->expire();
400380
}
401381

402382
void Database::expire() {
403383
ZEEK_AGENT_DEBUG("database", "expiring database state");
404-
Synchronize _(this);
405384
pimpl()->expire();
406385
}
407386

408387
void Database::addTable(Table* t) {
409-
Synchronize _(this);
410-
411388
t->setDatabase(this);
412389

413390
if ( configuration().options().use_mock_data )

src/core/database.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "table.h"
66
#include "util/pimpl.h"
77
#include "util/result.h"
8-
#include "util/threading.h"
98

109
#include <map>
1110
#include <memory>
@@ -124,9 +123,9 @@ class Scheduler;
124123
* Tables are typically registered at agent startup, and then remain available
125124
* for querying through corresponding SQL statements.
126125
*
127-
* All public methods in this class are thread-safe.
126+
* Note that database methods are not safe against access from different threads.
128127
*/
129-
class Database : public Pimpl<Database>, public SynchronizedBase {
128+
class Database : public Pimpl<Database> {
130129
public:
131130
/**
132131
* Constructor.

0 commit comments

Comments
 (0)