Exemplo n.º 1
0
        /// <summary>
        /// Get an access to the buffer.
        /// </summary>
        /// <param name="usage">Specifies the access usage.</param>
        /// <returns>An usage instance.</returns>
        public ObjectUsage <T> Get(UsageType usage)
        {
            switch (usage)
            {
            case UsageType.Write:

                lock (buffer)
                {
                    // - Find the free buffer for write
                    while (buffer[writePointer]?.Usage == UsageType.Read || writePointer == freePointer)
                    {
                        writePointer = (writePointer + 1) % 3;
                    }
                }

                // - If buffer is null instance it and set it on Write
                if (buffer[writePointer] == null)
                {
                    ObjectUsage <T> obj = new ObjectUsage <T>
                    {
                        Usage = UsageType.Write
                    };

                    obj.Action = () => Close(obj);

                    buffer[writePointer] = obj;
                }
                else
                {
                    buffer[writePointer].Usage = UsageType.Write;
                }

                return(buffer[writePointer]);

            case UsageType.Read:

                if (freePointer < 0)
                {
                    return(null);
                }

                lock (buffer)
                {
                    readPointer = freePointer;
                    buffer[readPointer].Usage = UsageType.Read;
                }

                return(buffer[readPointer]);
            }

            return(null);
        }
Exemplo n.º 2
0
        private void Close(ObjectUsage <T> obj)
        {
            switch (obj.Usage)
            {
            case UsageType.Read:

                lock (buffer)
                    buffer[readPointer].Usage = UsageType.None;

                break;

            case UsageType.Write:

                lock (buffer)
                {
                    buffer[writePointer].Usage = UsageType.None;
                    freePointer = writePointer;
                }

                break;
            }
        }