DBreeze.TransactionTablesLocker.AddSession C# (CSharp) Method

AddSession() public method

public AddSession ( eTransactionTablesLockTypes lockType, string tables ) : bool
lockType eTransactionTablesLockTypes
tables string
return bool
        public bool AddSession(eTransactionTablesLockTypes lockType, string[] tables)
        {
            lock (lock_disposed)
            {
                if (disposed)
                    return true;
            }

            internSession iSession = null;
            bool ret = true;

            _sync.EnterWriteLock();
            try
            {
                foreach (var ses in _acceptedSessions)
                {
                    if (DbUserTables.TableNamesIntersect(ses.Value.tables.ToList(), tables.ToList()))
                    {
                       if (ses.Value.lockType == eTransactionTablesLockTypes.EXCLUSIVE || lockType == eTransactionTablesLockTypes.EXCLUSIVE)
                        {
                            //Lock
                            ret = false;
                            break;
                        }
                    }
                }

                if (!ret)
                {
                    internSession xSes = null;
                    foreach (var ses in _waitingSessionSequence)
                    {

                        if (ses == System.Threading.Thread.CurrentThread.ManagedThreadId)
                            break;

                        _waitingSessions.TryGetValue(ses, out xSes);

                        if (DbUserTables.TableNamesIntersect(xSes.tables.ToList(), tables.ToList()))
                        {
                            if (xSes.lockType == eTransactionTablesLockTypes.EXCLUSIVE || lockType == eTransactionTablesLockTypes.EXCLUSIVE)
                            {
                                //Lock
                                ret = false;
                                break;
                            }
                        }
                    }
                }

                if (_waitingSessions.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out iSession))
                {
                    //This session was in the waiting list once
                    if (ret)
                    {
                        //We have to take away session from waiting list
                        iSession.gator.Dispose();
                        iSession.gator = null;
                        _waitingSessions.Remove(System.Threading.Thread.CurrentThread.ManagedThreadId);
                        _waitingSessionSequence.Remove(System.Threading.Thread.CurrentThread.ManagedThreadId);
                    }
                    else
                    {
                        iSession.gator.CloseGate();
                    }
                }
                else
                {
                    //Creating new session
                    iSession = new internSession()
                    {
                        lockType = lockType,
                        tables = tables
                    };

                    if (!ret)
                    {
                        iSession.gator = new DbThreadsGator(false);
                        _waitingSessions.Add(System.Threading.Thread.CurrentThread.ManagedThreadId, iSession);
                        _waitingSessionSequence.Add(System.Threading.Thread.CurrentThread.ManagedThreadId);
                    }
                }

                if (ret)
                {
                    //Adding into accepted sessions
                    _acceptedSessions.Add(System.Threading.Thread.CurrentThread.ManagedThreadId, iSession);
                }
            }
            finally
            {
                _sync.ExitWriteLock();
            }

            if (!ret)
            {
                //putting gate
                iSession.gator.PutGateHere();
            }

            return ret;
        }