dtoolbase: make TypeRegistry lock statically initialized

This is possible now that all MutexImpl have a constexpr constructor.
This commit is contained in:
rdb 2019-02-11 15:45:54 +01:00
parent fb5191e812
commit b466e77fe3
3 changed files with 48 additions and 62 deletions

View File

@ -36,16 +36,6 @@ ptr() {
return _global_pointer; return _global_pointer;
} }
/**
* Ensures the lock pointer has been allocated.
*/
INLINE void TypeRegistry::
init_lock() {
if (_lock == nullptr) {
_lock = new MutexImpl;
}
}
/** /**
* Returns the TypeRegistryNode associated with the indicated TypeHandle. If * Returns the TypeRegistryNode associated with the indicated TypeHandle. If
* there is no associated TypeRegistryNode, reports an error condition and * there is no associated TypeRegistryNode, reports an error condition and

View File

@ -25,7 +25,7 @@ using std::ostream;
using std::ostringstream; using std::ostringstream;
using std::string; using std::string;
MutexImpl *TypeRegistry::_lock = nullptr; MutexImpl TypeRegistry::_lock;
TypeRegistry *TypeRegistry::_global_pointer = nullptr; TypeRegistry *TypeRegistry::_global_pointer = nullptr;
/** /**
@ -37,7 +37,7 @@ TypeRegistry *TypeRegistry::_global_pointer = nullptr;
*/ */
bool TypeRegistry:: bool TypeRegistry::
register_type(TypeHandle &type_handle, const string &name) { register_type(TypeHandle &type_handle, const string &name) {
_lock->lock(); _lock.lock();
if (type_handle != TypeHandle::none()) { if (type_handle != TypeHandle::none()) {
// Here's a type that was already registered. Just make sure everything's // Here's a type that was already registered. Just make sure everything's
@ -45,7 +45,7 @@ register_type(TypeHandle &type_handle, const string &name) {
TypeRegistryNode *rnode = look_up(type_handle, nullptr); TypeRegistryNode *rnode = look_up(type_handle, nullptr);
if (&type_handle == &rnode->_ref) { if (&type_handle == &rnode->_ref) {
// No problem. // No problem.
_lock->unlock(); _lock.unlock();
assert(rnode->_name == name); assert(rnode->_name == name);
return false; return false;
} }
@ -67,7 +67,7 @@ register_type(TypeHandle &type_handle, const string &name) {
_derivations_fresh = false; _derivations_fresh = false;
type_handle = new_handle; type_handle = new_handle;
_lock->unlock(); _lock.unlock();
return true; return true;
} }
TypeRegistryNode *rnode = (*ri).second; TypeRegistryNode *rnode = (*ri).second;
@ -83,7 +83,7 @@ register_type(TypeHandle &type_handle, const string &name) {
if (type_handle == rnode->_handle) { if (type_handle == rnode->_handle) {
// No problem. // No problem.
_lock->unlock(); _lock.unlock();
return false; return false;
} }
// But wait--the type_handle has changed! We kept a reference to the // But wait--the type_handle has changed! We kept a reference to the
@ -92,7 +92,7 @@ register_type(TypeHandle &type_handle, const string &name) {
// time, but now it's different! Bad juju. // time, but now it's different! Bad juju.
cerr << "Reregistering " << name << "\n"; cerr << "Reregistering " << name << "\n";
type_handle = rnode->_handle; type_handle = rnode->_handle;
_lock->unlock(); _lock.unlock();
return false; return false;
} }
@ -108,7 +108,7 @@ register_type(TypeHandle &type_handle, const string &name) {
type_handle = rnode->_handle; type_handle = rnode->_handle;
} }
_lock->unlock(); _lock.unlock();
return false; return false;
} }
@ -119,7 +119,7 @@ register_type(TypeHandle &type_handle, const string &name) {
*/ */
TypeHandle TypeRegistry:: TypeHandle TypeRegistry::
register_dynamic_type(const string &name) { register_dynamic_type(const string &name) {
_lock->lock(); _lock.lock();
NameRegistry::iterator ri; NameRegistry::iterator ri;
ri = _name_registry.find(name); ri = _name_registry.find(name);
@ -139,14 +139,14 @@ register_dynamic_type(const string &name) {
_name_registry[name] = rnode; _name_registry[name] = rnode;
_derivations_fresh = false; _derivations_fresh = false;
_lock->unlock(); _lock.unlock();
return *new_handle; return *new_handle;
} }
// Return the TypeHandle previously obtained. // Return the TypeHandle previously obtained.
TypeRegistryNode *rnode = (*ri).second; TypeRegistryNode *rnode = (*ri).second;
TypeHandle handle = rnode->_handle; TypeHandle handle = rnode->_handle;
_lock->unlock(); _lock.unlock();
return handle; return handle;
} }
@ -157,7 +157,7 @@ register_dynamic_type(const string &name) {
*/ */
void TypeRegistry:: void TypeRegistry::
record_derivation(TypeHandle child, TypeHandle parent) { record_derivation(TypeHandle child, TypeHandle parent) {
_lock->lock(); _lock.lock();
TypeRegistryNode *cnode = look_up(child, nullptr); TypeRegistryNode *cnode = look_up(child, nullptr);
assert(cnode != nullptr); assert(cnode != nullptr);
@ -176,7 +176,7 @@ record_derivation(TypeHandle child, TypeHandle parent) {
_derivations_fresh = false; _derivations_fresh = false;
} }
_lock->unlock(); _lock.unlock();
} }
/** /**
@ -187,7 +187,7 @@ record_derivation(TypeHandle child, TypeHandle parent) {
*/ */
void TypeRegistry:: void TypeRegistry::
record_alternate_name(TypeHandle type, const string &name) { record_alternate_name(TypeHandle type, const string &name) {
_lock->lock(); _lock.lock();
TypeRegistryNode *rnode = look_up(type, nullptr); TypeRegistryNode *rnode = look_up(type, nullptr);
if (rnode != nullptr) { if (rnode != nullptr) {
@ -195,7 +195,7 @@ record_alternate_name(TypeHandle type, const string &name) {
_name_registry.insert(NameRegistry::value_type(name, rnode)).first; _name_registry.insert(NameRegistry::value_type(name, rnode)).first;
if ((*ri).second != rnode) { if ((*ri).second != rnode) {
_lock->unlock(); _lock.unlock();
cerr cerr
<< "Name " << name << " already assigned to TypeHandle " << "Name " << name << " already assigned to TypeHandle "
<< rnode->_name << "; cannot reassign to " << type << "\n"; << rnode->_name << "; cannot reassign to " << type << "\n";
@ -204,7 +204,7 @@ record_alternate_name(TypeHandle type, const string &name) {
} }
_lock->unlock(); _lock.unlock();
} }
#ifdef HAVE_PYTHON #ifdef HAVE_PYTHON
@ -214,14 +214,14 @@ record_alternate_name(TypeHandle type, const string &name) {
*/ */
void TypeRegistry:: void TypeRegistry::
record_python_type(TypeHandle type, PyObject *python_type) { record_python_type(TypeHandle type, PyObject *python_type) {
_lock->lock(); _lock.lock();
TypeRegistryNode *rnode = look_up(type, nullptr); TypeRegistryNode *rnode = look_up(type, nullptr);
if (rnode != nullptr) { if (rnode != nullptr) {
rnode->_python_type = python_type; rnode->_python_type = python_type;
} }
_lock->unlock(); _lock.unlock();
} }
#endif #endif
@ -231,7 +231,7 @@ record_python_type(TypeHandle type, PyObject *python_type) {
*/ */
TypeHandle TypeRegistry:: TypeHandle TypeRegistry::
find_type(const string &name) const { find_type(const string &name) const {
_lock->lock(); _lock.lock();
TypeHandle handle = TypeHandle::none(); TypeHandle handle = TypeHandle::none();
NameRegistry::const_iterator ri; NameRegistry::const_iterator ri;
@ -239,7 +239,7 @@ find_type(const string &name) const {
if (ri != _name_registry.end()) { if (ri != _name_registry.end()) {
handle = (*ri).second->_handle; handle = (*ri).second->_handle;
} }
_lock->unlock(); _lock.unlock();
return handle; return handle;
} }
@ -271,11 +271,11 @@ find_type_by_id(int id) const {
*/ */
string TypeRegistry:: string TypeRegistry::
get_name(TypeHandle type, TypedObject *object) const { get_name(TypeHandle type, TypedObject *object) const {
_lock->lock(); _lock.lock();
TypeRegistryNode *rnode = look_up(type, object); TypeRegistryNode *rnode = look_up(type, object);
assert(rnode != nullptr); assert(rnode != nullptr);
string name = rnode->_name; string name = rnode->_name;
_lock->unlock(); _lock.unlock();
return name; return name;
} }
@ -296,7 +296,7 @@ get_name(TypeHandle type, TypedObject *object) const {
bool TypeRegistry:: bool TypeRegistry::
is_derived_from(TypeHandle child, TypeHandle base, is_derived_from(TypeHandle child, TypeHandle base,
TypedObject *child_object) { TypedObject *child_object) {
_lock->lock(); _lock.lock();
const TypeRegistryNode *child_node = look_up(child, child_object); const TypeRegistryNode *child_node = look_up(child, child_object);
const TypeRegistryNode *base_node = look_up(base, nullptr); const TypeRegistryNode *base_node = look_up(base, nullptr);
@ -307,7 +307,7 @@ is_derived_from(TypeHandle child, TypeHandle base,
freshen_derivations(); freshen_derivations();
bool result = TypeRegistryNode::is_derived_from(child_node, base_node); bool result = TypeRegistryNode::is_derived_from(child_node, base_node);
_lock->unlock(); _lock.unlock();
return result; return result;
} }
@ -316,9 +316,9 @@ is_derived_from(TypeHandle child, TypeHandle base,
*/ */
int TypeRegistry:: int TypeRegistry::
get_num_typehandles() { get_num_typehandles() {
_lock->lock(); _lock.lock();
int num_types = (int)_handle_registry.size(); int num_types = (int)_handle_registry.size();
_lock->unlock(); _lock.unlock();
return num_types; return num_types;
} }
@ -327,12 +327,12 @@ get_num_typehandles() {
*/ */
TypeHandle TypeRegistry:: TypeHandle TypeRegistry::
get_typehandle(int n) { get_typehandle(int n) {
_lock->lock(); _lock.lock();
TypeRegistryNode *rnode = nullptr; TypeRegistryNode *rnode = nullptr;
if (n >= 0 && n < (int)_handle_registry.size()) { if (n >= 0 && n < (int)_handle_registry.size()) {
rnode = _handle_registry[n]; rnode = _handle_registry[n];
} }
_lock->unlock(); _lock.unlock();
if (rnode != nullptr) { if (rnode != nullptr) {
return rnode->_handle; return rnode->_handle;
@ -347,10 +347,10 @@ get_typehandle(int n) {
*/ */
int TypeRegistry:: int TypeRegistry::
get_num_root_classes() { get_num_root_classes() {
_lock->lock(); _lock.lock();
freshen_derivations(); freshen_derivations();
int num_roots = (int)_root_classes.size(); int num_roots = (int)_root_classes.size();
_lock->unlock(); _lock.unlock();
return num_roots; return num_roots;
} }
@ -359,7 +359,7 @@ get_num_root_classes() {
*/ */
TypeHandle TypeRegistry:: TypeHandle TypeRegistry::
get_root_class(int n) { get_root_class(int n) {
_lock->lock(); _lock.lock();
freshen_derivations(); freshen_derivations();
TypeHandle handle; TypeHandle handle;
if (n >= 0 && n < (int)_root_classes.size()) { if (n >= 0 && n < (int)_root_classes.size()) {
@ -367,7 +367,7 @@ get_root_class(int n) {
} else { } else {
handle = TypeHandle::none(); handle = TypeHandle::none();
} }
_lock->unlock(); _lock.unlock();
return handle; return handle;
} }
@ -385,11 +385,11 @@ get_root_class(int n) {
*/ */
int TypeRegistry:: int TypeRegistry::
get_num_parent_classes(TypeHandle child, TypedObject *child_object) const { get_num_parent_classes(TypeHandle child, TypedObject *child_object) const {
_lock->lock(); _lock.lock();
TypeRegistryNode *rnode = look_up(child, child_object); TypeRegistryNode *rnode = look_up(child, child_object);
assert(rnode != nullptr); assert(rnode != nullptr);
int num_parents = (int)rnode->_parent_classes.size(); int num_parents = (int)rnode->_parent_classes.size();
_lock->unlock(); _lock.unlock();
return num_parents; return num_parents;
} }
@ -399,7 +399,7 @@ get_num_parent_classes(TypeHandle child, TypedObject *child_object) const {
*/ */
TypeHandle TypeRegistry:: TypeHandle TypeRegistry::
get_parent_class(TypeHandle child, int index) const { get_parent_class(TypeHandle child, int index) const {
_lock->lock(); _lock.lock();
TypeHandle handle; TypeHandle handle;
TypeRegistryNode *rnode = look_up(child, nullptr); TypeRegistryNode *rnode = look_up(child, nullptr);
assert(rnode != nullptr); assert(rnode != nullptr);
@ -408,7 +408,7 @@ get_parent_class(TypeHandle child, int index) const {
} else { } else {
handle = TypeHandle::none(); handle = TypeHandle::none();
} }
_lock->unlock(); _lock.unlock();
return handle; return handle;
} }
@ -422,11 +422,11 @@ get_parent_class(TypeHandle child, int index) const {
*/ */
int TypeRegistry:: int TypeRegistry::
get_num_child_classes(TypeHandle child, TypedObject *child_object) const { get_num_child_classes(TypeHandle child, TypedObject *child_object) const {
_lock->lock(); _lock.lock();
TypeRegistryNode *rnode = look_up(child, child_object); TypeRegistryNode *rnode = look_up(child, child_object);
assert(rnode != nullptr); assert(rnode != nullptr);
int num_children = (int)rnode->_child_classes.size(); int num_children = (int)rnode->_child_classes.size();
_lock->unlock(); _lock.unlock();
return num_children; return num_children;
} }
@ -436,7 +436,7 @@ get_num_child_classes(TypeHandle child, TypedObject *child_object) const {
*/ */
TypeHandle TypeRegistry:: TypeHandle TypeRegistry::
get_child_class(TypeHandle child, int index) const { get_child_class(TypeHandle child, int index) const {
_lock->lock(); _lock.lock();
TypeHandle handle; TypeHandle handle;
TypeRegistryNode *rnode = look_up(child, nullptr); TypeRegistryNode *rnode = look_up(child, nullptr);
assert(rnode != nullptr); assert(rnode != nullptr);
@ -445,7 +445,7 @@ get_child_class(TypeHandle child, int index) const {
} else { } else {
handle = TypeHandle::none(); handle = TypeHandle::none();
} }
_lock->unlock(); _lock.unlock();
return handle; return handle;
} }
@ -462,7 +462,7 @@ get_child_class(TypeHandle child, int index) const {
TypeHandle TypeRegistry:: TypeHandle TypeRegistry::
get_parent_towards(TypeHandle child, TypeHandle base, get_parent_towards(TypeHandle child, TypeHandle base,
TypedObject *child_object) { TypedObject *child_object) {
_lock->lock(); _lock.lock();
TypeHandle handle; TypeHandle handle;
const TypeRegistryNode *child_node = look_up(child, child_object); const TypeRegistryNode *child_node = look_up(child, child_object);
const TypeRegistryNode *base_node = look_up(base, nullptr); const TypeRegistryNode *base_node = look_up(base, nullptr);
@ -470,7 +470,7 @@ get_parent_towards(TypeHandle child, TypeHandle base,
base_node != nullptr); base_node != nullptr);
freshen_derivations(); freshen_derivations();
handle = TypeRegistryNode::get_parent_towards(child_node, base_node); handle = TypeRegistryNode::get_parent_towards(child_node, base_node);
_lock->unlock(); _lock.unlock();
return handle; return handle;
} }
@ -484,8 +484,7 @@ get_parent_towards(TypeHandle child, TypeHandle base,
*/ */
void TypeRegistry:: void TypeRegistry::
reregister_types() { reregister_types() {
init_lock(); _lock.lock();
_lock->lock();
HandleRegistry::iterator ri; HandleRegistry::iterator ri;
TypeRegistry *reg = ptr(); TypeRegistry *reg = ptr();
for (ri = reg->_handle_registry.begin(); for (ri = reg->_handle_registry.begin();
@ -496,7 +495,7 @@ reregister_types() {
cerr << "Reregistering " << rnode->_name << "\n"; cerr << "Reregistering " << rnode->_name << "\n";
} }
} }
_lock->unlock(); _lock.unlock();
} }
@ -506,9 +505,9 @@ reregister_types() {
*/ */
void TypeRegistry:: void TypeRegistry::
write(ostream &out) const { write(ostream &out) const {
_lock->lock(); _lock.lock();
do_write(out); do_write(out);
_lock->unlock(); _lock.unlock();
} }
/** /**
@ -540,7 +539,6 @@ TypeRegistry() {
*/ */
void TypeRegistry:: void TypeRegistry::
init_global_pointer() { init_global_pointer() {
init_lock();
init_memory_hook(); init_memory_hook();
_global_pointer = new TypeRegistry; _global_pointer = new TypeRegistry;
} }
@ -636,9 +634,9 @@ look_up_invalid(TypeHandle handle, TypedObject *object) const {
// But we're lucky enough to have a TypedObject pointer handy! Maybe we // But we're lucky enough to have a TypedObject pointer handy! Maybe we
// can use it to resolve the error. We have to drop the lock while we // can use it to resolve the error. We have to drop the lock while we
// do this, so we don't get a recursive lock. // do this, so we don't get a recursive lock.
_lock->unlock(); _lock.unlock();
handle = object->force_init_type(); handle = object->force_init_type();
_lock->lock(); _lock.lock();
if (handle._index == 0) { if (handle._index == 0) {
// Strange. // Strange.

View File

@ -101,8 +101,6 @@ private:
void write_node(std::ostream &out, int indent_level, void write_node(std::ostream &out, int indent_level,
const TypeRegistryNode *node) const; const TypeRegistryNode *node) const;
static INLINE void init_lock();
typedef std::vector<TypeRegistryNode *> HandleRegistry; typedef std::vector<TypeRegistryNode *> HandleRegistry;
HandleRegistry _handle_registry; HandleRegistry _handle_registry;
@ -114,7 +112,7 @@ private:
bool _derivations_fresh; bool _derivations_fresh;
static MutexImpl *_lock; static MutexImpl _lock;
static TypeRegistry *_global_pointer; static TypeRegistry *_global_pointer;
friend class TypeHandle; friend class TypeHandle;