Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static analysis tool to check locking before access to variable

I know there are a quite a few static analysis tools for C# or .Net around. See this question for a good list of available tools. I have used some of those in the past and they have a good way of detecting problems.

I am currently looking for a way to automatically enforce some locking rules we have in our teams. For example I would like to enforce the following rules:

"Every public method that uses member foo must acquire a lock on bar" Or "Every call to foobar event must be outside lock to bar"

Writing custom FxCop rules, if feasible, seems rather complex. Is there any simpler way of doing it?

like image 777
Vincent Hubert Avatar asked Feb 27 '12 16:02

Vincent Hubert


1 Answers

Multithreading is hard. Using locks is not the only way to make operations thread-safe. A developer may use non-blocking synchronization with a loop and Interlocked.CompareExchange, or some other mechanism instead. A rule can not determine if something is thread-safe.

If the purpose of rules is to ensure high quality code, I think the best way to go about this is to create a thread-safe version of your class which is simple to consume. Put checks in place that the more-complex synchronization code is only modified under code review by developers that understand multithreading.

like image 177
foson Avatar answered Oct 26 '22 03:10

foson