Clean up the code a bit

This commit is contained in:
Gunnar Beutner 2014-11-11 23:06:47 +01:00
parent 9932f60522
commit ee980a788b
7 changed files with 49 additions and 36 deletions

View File

@ -28,7 +28,9 @@ using namespace icinga;
DynamicType::DynamicType(const String& name) DynamicType::DynamicType(const String& name)
: m_Name(name) : m_Name(name)
{ } {
InflateMutex();
}
DynamicType::Ptr DynamicType::GetByName(const String& name) DynamicType::Ptr DynamicType::GetByName(const String& name)
{ {

View File

@ -59,6 +59,11 @@ bool Object::OwnsLock(void) const
} }
#endif /* _DEBUG */ #endif /* _DEBUG */
void Object::InflateMutex(void)
{
m_Mutex.Inflate();
}
void Object::SetField(int, const Value&) void Object::SetField(int, const Value&)
{ {
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID.")); BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));

View File

@ -99,6 +99,8 @@ public:
bool OwnsLock(void) const; bool OwnsLock(void) const;
#endif /* _DEBUG */ #endif /* _DEBUG */
void InflateMutex(void);
private: private:
Object(const Object& other); Object(const Object& other);
Object& operator=(const Object& rhs); Object& operator=(const Object& rhs);

View File

@ -67,26 +67,7 @@ public:
#endif /* _DEBUG */ #endif /* _DEBUG */
} }
inline void Spin(unsigned int it) inline void Lock(bool make_native = false)
{
if (it < 8) {
/* Do nothing. */
}
#ifdef SPIN_PAUSE
else if (it < 16) {
SPIN_PAUSE();
}
#endif /* SPIN_PAUSE */
else {
#ifdef _WIN32
Sleep(0);
#else /* _WIN32 */
sched_yield();
#endif /* _WIN32 */
}
}
inline void Lock(void)
{ {
bool contended = false; bool contended = false;
unsigned int it = 0; unsigned int it = 0;
@ -111,16 +92,10 @@ public:
it++; it++;
} }
if (contended) if (contended || make_native)
MakeNative(); MakeNative();
} }
void MakeNative(void);
void DestroyNative(void);
void LockNative(void);
void UnlockNative(void);
inline void Unlock(void) inline void Unlock(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -135,10 +110,43 @@ public:
UnlockNative(); UnlockNative();
} }
inline void Inflate(void)
{
Lock(true);
Unlock();
}
#ifdef _DEBUG #ifdef _DEBUG
static void DebugTimerHandler(void); static void DebugTimerHandler(void);
#endif /* _DEBUG */ #endif /* _DEBUG */
private:
inline void Spin(unsigned int it)
{
if (it < 8) {
/* Do nothing. */
}
#ifdef SPIN_PAUSE
else if (it < 16) {
SPIN_PAUSE();
}
#endif /* SPIN_PAUSE */
else {
#ifdef _WIN32
DebugBreak();
Sleep(0);
#else /* _WIN32 */
sched_yield();
#endif /* _WIN32 */
}
}
void MakeNative(void);
void DestroyNative(void);
void LockNative(void);
void UnlockNative(void);
private: private:
#ifdef _WIN32 #ifdef _WIN32
# ifdef _WIN64 # ifdef _WIN64

View File

@ -137,9 +137,7 @@ DynamicObject::Ptr ConfigItem::Commit(bool discard)
/* Make sure the type is valid. */ /* Make sure the type is valid. */
Type::Ptr type = Type::GetByName(GetType()); Type::Ptr type = Type::GetByName(GetType());
ASSERT(type && Type::GetByName("DynamicObject")->IsAssignableFrom(type));
if (!type || !Type::GetByName("DynamicObject")->IsAssignableFrom(type))
BOOST_THROW_EXCEPTION(ConfigError("Type '" + GetType() + "' does not exist."));
if (IsAbstract()) if (IsAbstract())
return DynamicObject::Ptr(); return DynamicObject::Ptr();
@ -156,6 +154,7 @@ DynamicObject::Ptr ConfigItem::Commit(bool discard)
locals->Set("name", m_Name); locals->Set("name", m_Name);
dobj->SetParentScope(locals); dobj->SetParentScope(locals);
locals.reset();
DebugHint debugHints; DebugHint debugHints;
@ -171,7 +170,7 @@ DynamicObject::Ptr ConfigItem::Commit(bool discard)
if (discard) if (discard)
m_Expression.reset(); m_Expression.reset();
dobj->SetParentScope(Dictionary::Ptr()); dobj->SetParentScope(Object::Ptr());
String name = m_Name; String name = m_Name;
@ -199,6 +198,7 @@ DynamicObject::Ptr ConfigItem::Commit(bool discard)
persistentItem->Set("debug_hints", debugHints.ToDictionary()); persistentItem->Set("debug_hints", debugHints.ToDictionary());
ConfigCompilerContext::GetInstance()->WriteObject(persistentItem); ConfigCompilerContext::GetInstance()->WriteObject(persistentItem);
persistentItem.reset();
ConfigType::Ptr ctype = ConfigType::GetByName(GetType()); ConfigType::Ptr ctype = ConfigType::GetByName(GetType());

View File

@ -71,8 +71,6 @@ private:
bool m_Abstract; /**< Whether this is a template. */ bool m_Abstract; /**< Whether this is a template. */
boost::shared_ptr<Expression> m_Expression; boost::shared_ptr<Expression> m_Expression;
std::vector<String> m_ParentNames; /**< The names of parent configuration
items. */
DebugInfo m_DebugInfo; /**< Debug information. */ DebugInfo m_DebugInfo; /**< Debug information. */
Object::Ptr m_Scope; /**< variable scope. */ Object::Ptr m_Scope; /**< variable scope. */
String m_Zone; /**< The zone. */ String m_Zone; /**< The zone. */

View File

@ -68,8 +68,6 @@ void ConfigType::AddParentRules(std::vector<TypeRuleList::Ptr>& ruleLists, const
if (parent) { if (parent) {
AddParentRules(ruleLists, parent); AddParentRules(ruleLists, parent);
ObjectLock plock(parent);
ruleLists.push_back(parent->m_RuleList); ruleLists.push_back(parent->m_RuleList);
} }
} }