mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-01 07:12:50 -06:00
implement DB_scopes
This commit is contained in:
@@ -6,8 +6,9 @@
|
|||||||
|
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
|
|
||||||
unsigned char* current_base;
|
PGF_INTERNAL __thread unsigned char* current_base __attribute__((tls_model("initial-exec"))) = NULL;
|
||||||
DB* current_db;
|
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
|
#ifndef DEFAULT_TOP_PAD
|
||||||
#define DEFAULT_TOP_PAD (0)
|
#define DEFAULT_TOP_PAD (0)
|
||||||
@@ -274,8 +275,10 @@ DB::DB(const char* pathname, int flags, int mode) {
|
|||||||
size_t file_size;
|
size_t file_size;
|
||||||
bool is_new = false;
|
bool is_new = false;
|
||||||
|
|
||||||
|
fd = -1;
|
||||||
|
ms = NULL;
|
||||||
|
|
||||||
if (pathname == NULL) {
|
if (pathname == NULL) {
|
||||||
fd = -1;
|
|
||||||
file_size = getpagesize();
|
file_size = getpagesize();
|
||||||
is_new = true;
|
is_new = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -306,18 +309,24 @@ DB::DB(const char* pathname, int flags, int mode) {
|
|||||||
init_state(file_size);
|
init_state(file_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
current_base = (unsigned char*) ms;
|
int res = pthread_rwlock_init(&rwlock, NULL);
|
||||||
current_db = this;
|
if (res != 0) {
|
||||||
|
throw std::system_error(res, std::generic_category());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DB::~DB() {
|
DB::~DB() {
|
||||||
size_t size =
|
if (ms != NULL) {
|
||||||
ms->top + size + sizeof(size_t);
|
size_t size =
|
||||||
|
ms->top + size + sizeof(size_t);
|
||||||
|
|
||||||
munmap(ms,size);
|
munmap(ms,size);
|
||||||
|
}
|
||||||
|
|
||||||
if (fd >= 0)
|
if (fd >= 0)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
pthread_rwlock_destroy(&rwlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DB::sync()
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
class DB;
|
class DB;
|
||||||
|
|
||||||
extern PGF_INTERNAL_DECL unsigned char* current_base;
|
extern PGF_INTERNAL_DECL __thread unsigned char* current_base __attribute__((tls_model("initial-exec")));
|
||||||
extern PGF_INTERNAL_DECL DB* current_db;
|
extern PGF_INTERNAL_DECL __thread DB* current_db __attribute__((tls_model("initial-exec")));
|
||||||
|
|
||||||
typedef size_t moffset;
|
typedef size_t moffset;
|
||||||
|
|
||||||
@@ -60,6 +60,8 @@ private:
|
|||||||
int fd;
|
int fd;
|
||||||
malloc_state* ms;
|
malloc_state* ms;
|
||||||
|
|
||||||
|
pthread_rwlock_t rwlock;
|
||||||
|
|
||||||
friend class PgfReader;
|
friend class PgfReader;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -98,6 +100,22 @@ private:
|
|||||||
void set_root_internal(moffset root_offset);
|
void set_root_internal(moffset root_offset);
|
||||||
|
|
||||||
unsigned char* relocate(unsigned char* ptr);
|
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
|
#endif
|
||||||
|
|||||||
@@ -20,18 +20,18 @@ PgfPGF *pgf_read_pgf(const char* fpath, PgfExn* err)
|
|||||||
try {
|
try {
|
||||||
pgf = new PgfPGF(NULL, 0, 0);
|
pgf = new PgfPGF(NULL, 0, 0);
|
||||||
|
|
||||||
if (DB::get_root<PgfPGFRoot>() == 0) {
|
std::ifstream in(fpath, std::ios::binary);
|
||||||
std::ifstream in(fpath, std::ios::binary);
|
if (in.fail()) {
|
||||||
if (in.fail()) {
|
throw std::system_error(errno, std::generic_category());
|
||||||
throw std::system_error(errno, std::generic_category());
|
}
|
||||||
}
|
|
||||||
|
{
|
||||||
|
DB_scope scope(pgf, WRITER_SCOPE);
|
||||||
|
|
||||||
PgfReader rdr(&in);
|
PgfReader rdr(&in);
|
||||||
ref<PgfPGFRoot> pgf_root = rdr.read_pgf();
|
ref<PgfPGFRoot> pgf_root = rdr.read_pgf();
|
||||||
|
|
||||||
pgf->set_root(pgf_root);
|
pgf->set_root(pgf_root);
|
||||||
|
|
||||||
DB::sync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pgf;
|
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());
|
throw std::system_error(errno, std::generic_category());
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfReader rdr(&in);
|
{
|
||||||
ref<PgfPGFRoot> pgf_root = rdr.read_pgf();
|
DB_scope scope(pgf, WRITER_SCOPE);
|
||||||
|
|
||||||
pgf->set_root(pgf_root);
|
PgfReader rdr(&in);
|
||||||
|
ref<PgfPGFRoot> pgf_root = rdr.read_pgf();
|
||||||
|
|
||||||
DB::sync();
|
pgf->set_root(pgf_root);
|
||||||
|
|
||||||
|
DB::sync();
|
||||||
|
}
|
||||||
|
|
||||||
return pgf;
|
return pgf;
|
||||||
} catch (std::system_error& e) {
|
} 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);
|
pgf = new PgfPGF(fpath, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||||
|
|
||||||
if (DB::get_root<PgfPGFRoot>() == 0) {
|
if (DB::get_root<PgfPGFRoot>() == 0) {
|
||||||
ref<PgfPGFRoot> pgf = DB::malloc<PgfPGFRoot>();
|
DB_scope scope(pgf, WRITER_SCOPE);
|
||||||
pgf->major_version = 2;
|
|
||||||
pgf->minor_version = 0;
|
ref<PgfPGFRoot> root = DB::malloc<PgfPGFRoot>();
|
||||||
pgf->gflags = 0;
|
root->major_version = 2;
|
||||||
pgf->abstract.name = DB::malloc<PgfText>();
|
root->minor_version = 0;
|
||||||
pgf->abstract.name->size = 0;
|
root->gflags = 0;
|
||||||
pgf->abstract.aflags = 0;
|
root->abstract.name = DB::malloc<PgfText>();
|
||||||
pgf->abstract.funs = 0;
|
root->abstract.name->size = 0;
|
||||||
pgf->abstract.cats = 0;
|
root->abstract.aflags = 0;
|
||||||
|
root->abstract.funs = 0;
|
||||||
|
root->abstract.cats = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pgf;
|
return pgf;
|
||||||
@@ -132,18 +138,24 @@ void pgf_free(PgfPGF *pgf)
|
|||||||
PGF_API
|
PGF_API
|
||||||
PgfText *pgf_abstract_name(PgfPGF* pgf)
|
PgfText *pgf_abstract_name(PgfPGF* pgf)
|
||||||
{
|
{
|
||||||
return textdup(&(*pgf->get_root<PgfPGFRoot>()->abstract.name));
|
DB_scope scope(pgf, READER_SCOPE);
|
||||||
|
|
||||||
|
return textdup(&(*pgf->get_root<PgfPGFRoot>()->abstract.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
PGF_API
|
PGF_API
|
||||||
void pgf_iter_categories(PgfPGF* pgf, PgfItor* itor)
|
void pgf_iter_categories(PgfPGF* pgf, PgfItor* itor)
|
||||||
{
|
{
|
||||||
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.cats, itor);
|
DB_scope scope(pgf, READER_SCOPE);
|
||||||
|
|
||||||
|
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.cats, itor);
|
||||||
}
|
}
|
||||||
|
|
||||||
PGF_API
|
PGF_API
|
||||||
void pgf_iter_functions(PgfPGF* pgf, PgfItor* itor)
|
void pgf_iter_functions(PgfPGF* pgf, PgfItor* itor)
|
||||||
{
|
{
|
||||||
|
DB_scope scope(pgf, READER_SCOPE);
|
||||||
|
|
||||||
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.funs, itor);
|
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.funs, itor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +177,8 @@ void iter_by_cat_helper(PgfItor* itor, PgfText* key, void* value)
|
|||||||
PGF_API
|
PGF_API
|
||||||
void pgf_iter_functions_by_cat(PgfPGF* pgf, PgfText* cat, PgfItor* itor)
|
void pgf_iter_functions_by_cat(PgfPGF* pgf, PgfText* cat, PgfItor* itor)
|
||||||
{
|
{
|
||||||
|
DB_scope scope(pgf, READER_SCOPE);
|
||||||
|
|
||||||
PgfItorHelper helper;
|
PgfItorHelper helper;
|
||||||
helper.fn = iter_by_cat_helper;
|
helper.fn = iter_by_cat_helper;
|
||||||
helper.cat = cat;
|
helper.cat = cat;
|
||||||
|
|||||||
Reference in New Issue
Block a user