Пример #1
0
        public bool Reserve(LimitableResource Resource, long Amount, long Timeout)
        {
            long EndTimePoint = KTimeManager.ConvertNanosecondsToMilliseconds(Timeout);

            EndTimePoint += PerformanceCounter.ElapsedMilliseconds;

            bool Success = false;

            int Index = GetIndex(Resource);

            lock (LockObj)
            {
                long NewCurrent = Current[Index] + Amount;

                while (NewCurrent > Limit[Index] && Available[Index] + Amount <= Limit[Index])
                {
                    WaitingThreadsCount++;

                    KConditionVariable.Wait(System, WaitingThreads, LockObj, Timeout);

                    WaitingThreadsCount--;

                    NewCurrent = Current[Index] + Amount;

                    if (Timeout >= 0 && PerformanceCounter.ElapsedMilliseconds > EndTimePoint)
                    {
                        break;
                    }
                }

                if (NewCurrent <= Limit[Index])
                {
                    Current[Index] = NewCurrent;

                    Success = true;
                }
            }

            return(Success);
        }
Пример #2
0
        public bool Reserve(LimitableResource resource, long amount, long timeout)
        {
            long endTimePoint = KTimeManager.ConvertNanosecondsToMilliseconds(timeout);

            endTimePoint += PerformanceCounter.ElapsedMilliseconds;

            bool success = false;

            int index = GetIndex(resource);

            lock (_lockObj)
            {
                long newCurrent = _current[index] + amount;

                while (newCurrent > _limit[index] && _available[index] + amount <= _limit[index])
                {
                    _waitingThreadsCount++;

                    KConditionVariable.Wait(_system, _waitingThreads, _lockObj, timeout);

                    _waitingThreadsCount--;

                    newCurrent = _current[index] + amount;

                    if (timeout >= 0 && PerformanceCounter.ElapsedMilliseconds > endTimePoint)
                    {
                        break;
                    }
                }

                if (newCurrent <= _limit[index])
                {
                    _current[index] = newCurrent;

                    success = true;
                }
            }

            return(success);
        }