Example #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 (_lock)
            {
                if (_current2[index] >= _limit[index])
                {
                    return(false);
                }

                long newCurrent = _current[index] + amount;

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

                    KConditionVariable.Wait(KernelContext, _waitingThreads, _lock, timeout);

                    _waitingThreadsCount--;

                    newCurrent = _current[index] + amount;

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

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

                    if (_current[index] > _peak[index])
                    {
                        _peak[index] = _current[index];
                    }

                    success = true;
                }
            }

            return(success);
        }
Example #2
0
 public bool Reserve(LimitableResource resource, long amount)
 {
     return(Reserve(resource, amount, KTimeManager.ConvertMillisecondsToNanoseconds(DefaultTimeoutMs)));
 }