diff --git a/src/runtime/c/db.cxx b/src/runtime/c/db.cxx index c32e432cd..01a283631 100644 --- a/src/runtime/c/db.cxx +++ b/src/runtime/c/db.cxx @@ -6,8 +6,9 @@ #include "data.h" -unsigned char* current_base; -DB* current_db; +PGF_INTERNAL __thread unsigned char* current_base __attribute__((tls_model("initial-exec"))) = NULL; +PGF_INTERNAL __thread DB* current_db __attribute__((tls_model("initial-exec"))) = NULL; +PGF_INTERNAL __thread DB_scope *last_db_scope __attribute__((tls_model("initial-exec"))) = NULL; #ifndef DEFAULT_TOP_PAD #define DEFAULT_TOP_PAD (0) @@ -274,8 +275,10 @@ DB::DB(const char* pathname, int flags, int mode) { size_t file_size; bool is_new = false; + fd = -1; + ms = NULL; + if (pathname == NULL) { - fd = -1; file_size = getpagesize(); is_new = true; } else { @@ -306,18 +309,24 @@ DB::DB(const char* pathname, int flags, int mode) { init_state(file_size); } - current_base = (unsigned char*) ms; - current_db = this; + int res = pthread_rwlock_init(&rwlock, NULL); + if (res != 0) { + throw std::system_error(res, std::generic_category()); + } } DB::~DB() { - size_t size = - ms->top + size + sizeof(size_t); + if (ms != NULL) { + size_t size = + ms->top + size + sizeof(size_t); - munmap(ms,size); + munmap(ms,size); + } if (fd >= 0) close(fd); + + pthread_rwlock_destroy(&rwlock); } void DB::sync() @@ -932,3 +941,37 @@ DB::free_internal(moffset o) } } } + + +DB_scope::DB_scope(DB *db, DB_scope_mode tp) +{ + int res = + (tp == READER_SCOPE) ? pthread_rwlock_rdlock(&db->rwlock) + : pthread_rwlock_wrlock(&db->rwlock); + if (res != 0) + throw std::system_error(res, std::generic_category()); + + save_db = current_db; + current_db = db; + current_base = (unsigned char*) current_db->ms; + + next_scope = last_db_scope; + last_db_scope = this; +} + +DB_scope::~DB_scope() +{ + int res = pthread_rwlock_unlock(¤t_db->rwlock); + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wterminate" + if (res != 0) + throw std::system_error(res, std::generic_category()); +#pragma GCC diagnostic pop + + current_db = save_db; + current_base = current_db ? (unsigned char*) current_db->ms + : NULL; + + last_db_scope = next_scope; +} diff --git a/src/runtime/c/db.h b/src/runtime/c/db.h index 1e2139945..5c4466c74 100644 --- a/src/runtime/c/db.h +++ b/src/runtime/c/db.h @@ -3,8 +3,8 @@ class DB; -extern PGF_INTERNAL_DECL unsigned char* current_base; -extern PGF_INTERNAL_DECL DB* current_db; +extern PGF_INTERNAL_DECL __thread unsigned char* current_base __attribute__((tls_model("initial-exec"))); +extern PGF_INTERNAL_DECL __thread DB* current_db __attribute__((tls_model("initial-exec"))); typedef size_t moffset; @@ -60,6 +60,8 @@ private: int fd; malloc_state* ms; + pthread_rwlock_t rwlock; + friend class PgfReader; public: @@ -98,6 +100,22 @@ private: void set_root_internal(moffset root_offset); unsigned char* relocate(unsigned char* ptr); + + friend class DB_scope; }; +enum DB_scope_mode {READER_SCOPE, WRITER_SCOPE}; + +class PGF_INTERNAL_DECL DB_scope { +public: + DB_scope(DB *db, DB_scope_mode type); + ~DB_scope(); + +private: + DB* save_db; + DB_scope* next_scope; +}; + +extern PGF_INTERNAL_DECL thread_local DB_scope *last_db_scope; + #endif diff --git a/src/runtime/c/pgf.cxx b/src/runtime/c/pgf.cxx index a3aac5c32..e0d694cae 100644 --- a/src/runtime/c/pgf.cxx +++ b/src/runtime/c/pgf.cxx @@ -20,18 +20,18 @@ PgfPGF *pgf_read_pgf(const char* fpath, PgfExn* err) try { pgf = new PgfPGF(NULL, 0, 0); - if (DB::get_root() == 0) { - std::ifstream in(fpath, std::ios::binary); - if (in.fail()) { - throw std::system_error(errno, std::generic_category()); - } + std::ifstream in(fpath, std::ios::binary); + if (in.fail()) { + throw std::system_error(errno, std::generic_category()); + } + + { + DB_scope scope(pgf, WRITER_SCOPE); PgfReader rdr(&in); ref pgf_root = rdr.read_pgf(); pgf->set_root(pgf_root); - - DB::sync(); } return pgf; @@ -64,12 +64,16 @@ PgfPGF *pgf_boot_ngf(const char* pgf_path, const char* ngf_path, PgfExn* err) throw std::system_error(errno, std::generic_category()); } - PgfReader rdr(&in); - ref pgf_root = rdr.read_pgf(); + { + DB_scope scope(pgf, WRITER_SCOPE); - pgf->set_root(pgf_root); + PgfReader rdr(&in); + ref pgf_root = rdr.read_pgf(); - DB::sync(); + pgf->set_root(pgf_root); + + DB::sync(); + } return pgf; } catch (std::system_error& e) { @@ -97,15 +101,17 @@ PgfPGF *pgf_read_ngf(const char *fpath, PgfExn* err) pgf = new PgfPGF(fpath, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); if (DB::get_root() == 0) { - ref pgf = DB::malloc(); - pgf->major_version = 2; - pgf->minor_version = 0; - pgf->gflags = 0; - pgf->abstract.name = DB::malloc(); - pgf->abstract.name->size = 0; - pgf->abstract.aflags = 0; - pgf->abstract.funs = 0; - pgf->abstract.cats = 0; + DB_scope scope(pgf, WRITER_SCOPE); + + ref root = DB::malloc(); + root->major_version = 2; + root->minor_version = 0; + root->gflags = 0; + root->abstract.name = DB::malloc(); + root->abstract.name->size = 0; + root->abstract.aflags = 0; + root->abstract.funs = 0; + root->abstract.cats = 0; } return pgf; @@ -132,18 +138,24 @@ void pgf_free(PgfPGF *pgf) PGF_API PgfText *pgf_abstract_name(PgfPGF* pgf) { - return textdup(&(*pgf->get_root()->abstract.name)); + DB_scope scope(pgf, READER_SCOPE); + + return textdup(&(*pgf->get_root()->abstract.name)); } PGF_API void pgf_iter_categories(PgfPGF* pgf, PgfItor* itor) { - namespace_iter(pgf->get_root()->abstract.cats, itor); + DB_scope scope(pgf, READER_SCOPE); + + namespace_iter(pgf->get_root()->abstract.cats, itor); } PGF_API void pgf_iter_functions(PgfPGF* pgf, PgfItor* itor) { + DB_scope scope(pgf, READER_SCOPE); + namespace_iter(pgf->get_root()->abstract.funs, itor); } @@ -165,6 +177,8 @@ void iter_by_cat_helper(PgfItor* itor, PgfText* key, void* value) PGF_API void pgf_iter_functions_by_cat(PgfPGF* pgf, PgfText* cat, PgfItor* itor) { + DB_scope scope(pgf, READER_SCOPE); + PgfItorHelper helper; helper.fn = iter_by_cat_helper; helper.cat = cat;