<commit_before>
// DeadlockDetect.cpp

// Declares the cDeadlockDetect class that tries to detect deadlocks and aborts the server when it detects one

#include "Globals.h"
#include "DeadlockDetect.h"
#include "Root.h"
#include "World.h"





/// Number of milliseconds per cycle
const int CYCLE_MILLISECONDS = 100;

/// When the number of cycles for the same world age hits this value, it is considered a deadlock
const int NUM_CYCLES_LIMIT = 200;  // 200 = twenty seconds





cDeadlockDetect::cDeadlockDetect(void) :
	super("DeadlockDetect")
{
}





bool cDeadlockDetect::Start(void)
{
	// Read the initial world data:
	class cFillIn :
		public cWorldListCallback
	{
	public:
		cFillIn(cDeadlockDetect * a_Detect) :
			m_Detect(a_Detect)
		{
		}
		
		virtual bool Item(cWorld * a_World) override
		{
			m_Detect->SetWorldAge(a_World->GetName(), a_World->GetWorldAge());
			return false;
		}
		
	protected:
		cDeadlockDetect * m_Detect;
	} FillIn(this);
	cRoot::Get()->ForEachWorld(FillIn);
	return super::Start();
}





void cDeadlockDetect::Execute(void)
{
	// Loop until the signal to terminate:
	while (!m_ShouldTerminate)
	{
		// Check the world ages:
		class cChecker :
			public cWorldListCallback
		{
		public:
			cChecker(cDeadlockDetect * a_Detect) :
				m_Detect(a_Detect)
			{
			}
			
		protected:
			cDeadlockDetect * m_Detect;
			
			virtual bool Item(cWorld * a_World) override
			{
				m_Detect->CheckWorldAge(a_World->GetName(), a_World->GetWorldAge());
				return false;
			}
		} Checker(this);
		cRoot::Get()->ForEachWorld(Checker);
		
		cSleep::MilliSleep(CYCLE_MILLISECONDS);
	}  // while (should run)
}





void cDeadlockDetect::SetWorldAge(const AString & a_WorldName, Int64 a_Age)
{
	m_WorldAges[a_WorldName].m_Age = a_Age;
	m_WorldAges[a_WorldName].m_NumCyclesSame = 0;
}





void cDeadlockDetect::CheckWorldAge(const AString & a_WorldName, Int64 a_Age)
{
	WorldAges::iterator itr = m_WorldAges.find(a_WorldName);
	if (itr == m_WorldAges.end())
	{
		ASSERT(!"Unknown world in cDeadlockDetect");
		return;
	}
	if (itr->second.m_Age == a_Age)
	{
		itr->second.m_NumCyclesSame += 1;
		if (itr->second.m_NumCyclesSame > NUM_CYCLES_LIMIT)
		{
			DeadlockDetected();
			return;
		}
	}
	else
	{
		itr->second.m_Age = a_Age;
		itr->second.m_NumCyclesSame = 0;
	}
}





void cDeadlockDetect::DeadlockDetected(void)
{
	ASSERT(!"Deadlock detected");
	
	// TODO: Make a crashdump / coredump
	
	// Crash the server intentionally:
	*((int *)0) = 0;
}



<commit_msg>Made the cDeadlockDetect class work in clang.<commit_after>
// DeadlockDetect.cpp

// Declares the cDeadlockDetect class that tries to detect deadlocks and aborts the server when it detects one

#include "Globals.h"
#include "DeadlockDetect.h"
#include "Root.h"
#include "World.h"





/// Number of milliseconds per cycle
const int CYCLE_MILLISECONDS = 100;

/// When the number of cycles for the same world age hits this value, it is considered a deadlock
const int NUM_CYCLES_LIMIT = 200;  // 200 = twenty seconds





cDeadlockDetect::cDeadlockDetect(void) :
	super("DeadlockDetect")
{
}





bool cDeadlockDetect::Start(void)
{
	// Read the initial world data:
	class cFillIn :
		public cWorldListCallback
	{
	public:
		cFillIn(cDeadlockDetect * a_Detect) :
			m_Detect(a_Detect)
		{
		}
		
		virtual bool Item(cWorld * a_World) override
		{
			m_Detect->SetWorldAge(a_World->GetName(), a_World->GetWorldAge());
			return false;
		}
		
	protected:
		cDeadlockDetect * m_Detect;
	} FillIn(this);
	cRoot::Get()->ForEachWorld(FillIn);
	return super::Start();
}





void cDeadlockDetect::Execute(void)
{
	// Loop until the signal to terminate:
	while (!m_ShouldTerminate)
	{
		// Check the world ages:
		class cChecker :
			public cWorldListCallback
		{
		public:
			cChecker(cDeadlockDetect * a_Detect) :
				m_Detect(a_Detect)
			{
			}
			
		protected:
			cDeadlockDetect * m_Detect;
			
			virtual bool Item(cWorld * a_World) override
			{
				m_Detect->CheckWorldAge(a_World->GetName(), a_World->GetWorldAge());
				return false;
			}
		} Checker(this);
		cRoot::Get()->ForEachWorld(Checker);
		
		cSleep::MilliSleep(CYCLE_MILLISECONDS);
	}  // while (should run)
}





void cDeadlockDetect::SetWorldAge(const AString & a_WorldName, Int64 a_Age)
{
	m_WorldAges[a_WorldName].m_Age = a_Age;
	m_WorldAges[a_WorldName].m_NumCyclesSame = 0;
}





void cDeadlockDetect::CheckWorldAge(const AString & a_WorldName, Int64 a_Age)
{
	WorldAges::iterator itr = m_WorldAges.find(a_WorldName);
	if (itr == m_WorldAges.end())
	{
		ASSERT(!"Unknown world in cDeadlockDetect");
		return;
	}
	if (itr->second.m_Age == a_Age)
	{
		itr->second.m_NumCyclesSame += 1;
		if (itr->second.m_NumCyclesSame > NUM_CYCLES_LIMIT)
		{
			DeadlockDetected();
			return;
		}
	}
	else
	{
		itr->second.m_Age = a_Age;
		itr->second.m_NumCyclesSame = 0;
	}
}





void cDeadlockDetect::DeadlockDetected(void)
{
	ASSERT(!"Deadlock detected");
	
	// TODO: Make a crashdump / coredump
	
	// Crash the server intentionally:
	*((volatile int *)0) = 0;
}




{'fraction_non_alphanumeric': 0.11918546616091036, 'fraction_numerical': 0.007187063286085047, 'mean_word_length': 3.8878048780487804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5598259', 'n_tokens_mistral': 2175, 'n_tokens_neox': 1832, 'n_words': 391}