LOCK correction, test change, re_alloc codding

LOCK extra protect of all functions to determine deadlock & segmentation
fault piece of code
Thread safe improvement

The position of LOCK/UNLOCK macros is changed due to errors while different
threats change state of global wariables while other use previous data to
make and calculate desigions