Safe for multi threading...
The easiest way of getting there is to have no mutable state. And here is a simple test to ensure that. Seeing how CouchDB code works and how erlag handle things is quite educating in this regard.
[TestFixture]
public class EnssureTypesSafeForMultiThreadingTestFixture
{
[Test]
public void TypeIsSafeForMultiThreading()
{
var visitedTypes = new List<Type>
{ // immutable types, partial list
typeof(int),
typeof(long),
typeof(string),
typeof(DateTime)
};
foreach (var type in GetRootTypesToCheck())
{
CheckType(type, visitedTypes);
}
}
private static void CheckType(Type type, ICollection<Type> types)
{
if(types.Contains(type))
return;
types.Add(type);
var fields = type.GetFields(BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);
foreach (var info in fields)
{
var isReadOnlyField = (info.Attributes & FieldAttributes.InitOnly)==FieldAttributes.InitOnly;
if(isReadOnlyField==false)
throw new InvalidAsynchronousStateException("Dude, " + type + "." + info.Name +
" is not marked as read only. You are NOT safe for multi threading, enjoy the deadlock, bye!");
CheckType(info.FieldType, types);
}
}
private static IEnumerable<Type> GetRootTypesToCheck()
{
// return types that I am interested in verifying
}
}