<commit_before>#include "StdAfx.h"

#include "ObjectTable.h"

static BulletSharp::ObjectTable::ObjectTable()
{
	objectTable = gcnew Dictionary<intptr_t, Object^>();
}

void BulletSharp::ObjectTable::Add(Object^ obj, void* unmanagedPointer)
{
	objectTable->Add((intptr_t)unmanagedPointer, obj);
}

bool BulletSharp::ObjectTable::Contains(intptr_t unmanagedPointer)
{
	return objectTable->ContainsKey(unmanagedPointer);
}

bool BulletSharp::ObjectTable::Contains(Object^ managedObject)
{
	return objectTable->ContainsValue(managedObject);
}

void BulletSharp::ObjectTable::Remove(void* unmanagedPointer)
{
	objectTable->Remove((intptr_t)unmanagedPointer);
}

generic<class T>
T BulletSharp::ObjectTable::GetObject(intptr_t unmanagedPointer)
{
	return (T)objectTable[unmanagedPointer];
}

intptr_t BulletSharp::ObjectTable::GetUnmanagedObject(Object^ unmanagedPointer)
{
	for each(intptr_t key in objectTable->Keys)
		if (objectTable[key] == unmanagedPointer)
			return key;
	return 0;
}
<commit_msg>Mufti-threading protection for ObjectTable operations<commit_after>#include "StdAfx.h"

#include "ObjectTable.h"

using namespace System::Threading;

static BulletSharp::ObjectTable::ObjectTable()
{
	objectTable = gcnew Dictionary<intptr_t, Object^>();
}

void BulletSharp::ObjectTable::Add(Object^ obj, void* unmanagedPointer)
{
	Monitor::Enter(objectTable);
	try
	{
		if (!objectTable->ContainsKey((intptr_t)unmanagedPointer))
			objectTable->Add((intptr_t)unmanagedPointer, obj);
		else
		{
			//something when wrong here, so inform in debug mode
			Debug:assert(false, "ObjectTable key already exists.");
			//key already exists.. try to handle this by updating/overwriting key entry to newest value
			//this should protect us against a wrapper that did not unregister from object table, and race conditions that try to write the same key.
			objectTable[(intptr_t)unmanagedPointer] = obj;
		}
	}
	finally{ Monitor::Exit(objectTable); }
}

bool BulletSharp::ObjectTable::Contains(intptr_t unmanagedPointer)
{
	Monitor::Enter(objectTable);
	try
	{
		return objectTable->ContainsKey(unmanagedPointer);
	}
	finally{ Monitor::Exit(objectTable); }
}

bool BulletSharp::ObjectTable::Contains(Object^ managedObject)
{
	Monitor::Enter(objectTable);
	try
	{
		return objectTable->ContainsValue(managedObject);
	}
	finally{ Monitor::Exit(objectTable); }
}

void BulletSharp::ObjectTable::Remove(void* unmanagedPointer)
{
	Monitor::Enter(objectTable);
	try
	{
		objectTable->Remove((intptr_t)unmanagedPointer);
	}
	finally{ Monitor::Exit(objectTable); }
}

generic<class T> T BulletSharp::ObjectTable::GetObject(intptr_t unmanagedPointer)
{
	Monitor::Enter(objectTable);
	try
	{
		return (T)objectTable[unmanagedPointer];
	}
	finally{ Monitor::Exit(objectTable); }
}

intptr_t BulletSharp::ObjectTable::GetUnmanagedObject(Object^ unmanagedPointer)
{
	Monitor::Enter(objectTable);
	try
	{
		for each(intptr_t key in objectTable->Keys)
			if (objectTable[key] == unmanagedPointer)
				return key;
		return 0;
	}
	finally{ Monitor::Exit(objectTable); }
}

{'fraction_non_alphanumeric': 0.11840858856962425, 'fraction_numerical': 0.0006315124723713293, 'mean_word_length': 5.2732673267326735, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, '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': '6469239', 'n_tokens_mistral': 1057, 'n_tokens_neox': 998, 'n_words': 194}