Esempio n. 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public VALUE acquire(KEY key) throws NoSuchEntryException, ConcurrentAccessException
        public virtual VALUE Acquire(KEY key)
        {
            Entry entry = _repo.get(key);

            if (entry == null)
            {
                throw new NoSuchEntryException(string.Format("Cannot access '{0}', no such entry exists.", key));
            }
            if (entry.Acquire())
            {
                return(entry.Value);
            }
            throw new ConcurrentAccessException(string.Format("Cannot access '{0}', because another client is currently using it.", key));
        }
Esempio n. 2
0
        public override void Run()
        {
            long maxAllowedAge = _clock.millis() - _timeout;

            foreach (KEY key in Keys())
            {
                Entry entry = _repo.get(key);
                if ((entry != null) && (entry.LatestActivityTimestamp < maxAllowedAge))
                {
                    if ((entry.LatestActivityTimestamp < maxAllowedAge) && entry.Acquire())
                    {
                        End0(key, entry.Value);
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// End the life of a stored entry. If the entry is currently in use, it will be thrown out as soon as the other client
        /// is done with it.
        /// </summary>
        public virtual VALUE End(KEY key)
        {
            while (true)
            {
                Entry entry = _repo.get(key);
                if (entry == null)
                {
                    return(default(VALUE));
                }

                // Ending the life of an entry is somewhat complicated, because we promise the callee here that if someone
                // else is concurrently using the entry, we will ensure that either we or the other user will end the entry
                // life when the other user is done with it.

                // First, assume the entry is in use and try and mark it to be ended by the other user
                if (entry.MarkForEndingIfInUse())
                {
                    // The entry was indeed in use, and we successfully marked it to be ended. That's all we need to do here,
                    // the other user will see the ending flag when releasing the entry.
                    return(entry.Value);
                }

                // Marking it for ending failed, likely because the entry is currently idle - lets try and just acquire it
                // and throw it out ourselves
                if (entry.Acquire())
                {
                    // Got it, just throw it away
                    End0(key, entry.Value);
                    return(entry.Value);
                }

                // We didn't manage to mark this for ending, and we didn't manage to acquire it to end it ourselves, which
                // means either we're racing with another thread using it (and we just need to retry), or it's already
                // marked for ending. In the latter case, we can bail here.
                if (entry.MarkedForEnding)
                {
                    // Someone did indeed manage to mark it for ending, which means it will be thrown out (or has already).
                    return(entry.Value);
                }
            }
        }