diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2013-09-28 10:51:12 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2013-09-28 10:51:12 +0200 |
commit | b4825d95472f8d9e2b9892a56c585a035b6230ac (patch) | |
tree | 05d7124cc044205160c4bf4c8bcf8a7bf87dc2d4 /odb/database.cxx | |
parent | 2e258ea80deae21b478c6aa8deae50bd24194c11 (diff) |
Make schema version access (but not modification) thread-safe
Also cache the version in statements so that we don't have to lock
the mutex (slow) every time we need to check the version.
Diffstat (limited to 'odb/database.cxx')
-rw-r--r-- | odb/database.cxx | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/odb/database.cxx b/odb/database.cxx index c9692ba..5578abd 100644 --- a/odb/database.cxx +++ b/odb/database.cxx @@ -4,10 +4,14 @@ #include <odb/database.hxx> +#include <odb/details/lock.hxx> + using namespace std; namespace odb { + using details::lock; + database:: ~database () { @@ -20,25 +24,23 @@ namespace odb return c.execute (st, n); } - const database::schema_version_info& database:: - schema_version_migration_ (const string& name) const + const database::schema_version_migration_type& database:: + schema_version_migration (const string& name) const { + lock l (mutex_); // Prevents concurrent loading. + schema_version_map::const_iterator i (schema_version_map_.find (name)); - const schema_version_info& svi ( - i != schema_version_map_.end () && i->second.version != 0 + return i != schema_version_map_.end () && i->second.version != 0 ? i->second - : load_schema_version (name)); - - if (default_schema_version_ == 0 && name.empty ()) - default_schema_version_ = &svi; - - return svi; + : load_schema_version (name); } void database:: schema_version_migration (const schema_version_migration_type& svm, const string& name) { + // Note: no lock, not thread-safe. + schema_version_info& svi (schema_version_map_[name]); if (svi.version != svm.version || svi.migration != svm.migration) { @@ -46,8 +48,5 @@ namespace odb svi.migration = svm.migration; schema_version_seq_++; } - - if (default_schema_version_ == 0 && name.empty ()) - default_schema_version_ = &svi; } } |