simxVoid extApi_globalSimpleLock()
{
#ifdef _WIN32
	_simpleLock(_globalMutex);
#elif defined (__linux) || defined (__APPLE__)
	_simpleLock(&_globalMutex);
#endif
}
simxVoid extApi_lockSendStart(simxInt clientID)
{
#ifdef _WIN32
	_simpleLock(_mutex2Aux[clientID]);
	if ( (GetCurrentThreadId()==_lock2ThreadId[clientID]) && (_mutex2LockLevel[clientID]>0) )
	{ // Already locked by this thread
		_mutex2LockLevel[clientID]++;
		_simpleUnlock(_mutex2Aux[clientID]);
		return;
	}
	// First level lock
	_simpleUnlock(_mutex2Aux[clientID]);
	_simpleLock(_mutex2[clientID]);
	_simpleLock(_mutex2Aux[clientID]);
	_lock2ThreadId[clientID]=GetCurrentThreadId();
	_mutex2LockLevel[clientID]=1;
	_simpleUnlock(_mutex2Aux[clientID]);
#elif defined (__linux) || defined (__APPLE__)
	_simpleLock(&_mutex2Aux[clientID]);
	if ( (pthread_self()==_lock2ThreadId[clientID]) && (_mutex2LockLevel[clientID]>0) )
	{ // Already locked by this thread
		_mutex2LockLevel[clientID]++;
		_simpleUnlock(&_mutex2Aux[clientID]);
		return;
	}
	// First level lock
	_simpleUnlock(&_mutex2Aux[clientID]);
	_simpleLock(&_mutex2[clientID]);
	_simpleLock(&_mutex2Aux[clientID]);
	_lock2ThreadId[clientID]=pthread_self();
	_mutex2LockLevel[clientID]=1;
	_simpleUnlock(&_mutex2Aux[clientID]);
#endif
}
void CSimxSocket::_lock()
{
#ifdef _WIN32
	_simpleLock(_mutexAux);
	if ( (GetCurrentThreadId()==_lockThreadId) && (_lockLevel>0) )
	{ // Already locked by this thread
		_lockLevel++;
		_simpleUnlock(_mutexAux);
		return;
	}
	// First level lock
	_simpleUnlock(_mutexAux);
	_simpleLock(_mutex);
	_simpleLock(_mutexAux);
	_lockThreadId=GetCurrentThreadId();
	_lockLevel=1;
	_simpleUnlock(_mutexAux);
#elif defined (__linux) || defined (__APPLE__)
	_simpleLock(&_mutexAux);
	if ( (pthread_self()==_lockThreadId) && (_lockLevel>0) )
	{ // Already locked by this thread
		_lockLevel++;
		_simpleUnlock(&_mutexAux);
		return;
	}
	// First level lock
	_simpleUnlock(&_mutexAux);
	_simpleLock(&_mutex);
	_simpleLock(&_mutexAux);
	_lockThreadId=pthread_self();
	_lockLevel=1;
	_simpleUnlock(&_mutexAux);
#endif
}
simxVoid extApi_unlockSendStart(simxInt clientID)
{
#ifdef _WIN32
	_simpleLock(_mutex2Aux[clientID]);
	_mutex2LockLevel[clientID]--;
	if (_mutex2LockLevel[clientID]==0)
	{
		_simpleUnlock(_mutex2Aux[clientID]);
		_simpleUnlock(_mutex2[clientID]);
	}
	else
		_simpleUnlock(_mutex2Aux[clientID]);
#elif defined (__linux) || defined (__APPLE__)
	_simpleLock(&_mutex2Aux[clientID]);
	_mutex2LockLevel[clientID]--;
	if (_mutex2LockLevel[clientID]==0)
	{
		_simpleUnlock(&_mutex2Aux[clientID]);
		_simpleUnlock(&_mutex2[clientID]);
	}
	else
		_simpleUnlock(&_mutex2Aux[clientID]);
#endif
}
void CSimxSocket::_unlock()
{
#ifdef _WIN32
	_simpleLock(_mutexAux);
	_lockLevel--;
	if (_lockLevel==0)
	{
		_simpleUnlock(_mutexAux);
		_simpleUnlock(_mutex);
	}
	else
		_simpleUnlock(_mutexAux);
#elif defined (__linux) || defined (__APPLE__)
	_simpleLock(&_mutexAux);
	_lockLevel--;
	if (_lockLevel==0)
	{
		_simpleUnlock(&_mutexAux);
		_simpleUnlock(&_mutex);
	}
	else
		_simpleUnlock(&_mutexAux);
#endif
}