Exemple #1
0
        /// <summary>
        /// Creates a lane at specific slot with the size given by the settings NextCapacity callback.
        /// The slot must be null or Disposed.
        /// </summary>
        /// <param name="index">The index of the lane in the highway.</param>
        /// <returns>The newly created lane instance or null if fails.</returns>
        /// <exception cref="MemoryLaneException">Thresholds are reached:
        /// MaxLanesCountReached or MaxTotalAllocBytesReached</exception>
        public MemoryLane ReopenLane(int index)
        {
            // [i] The Allocation procedure will never look back at null slots when expanding
            // thus there is no competition that may require locking.

            if (Lanes.AppendIndex < index)
            {
                return(null);
            }

            var lane = Lanes[index];

            if (lane == null || lane.IsDisposed)
            {
                var newLane = allocLane(settings.NextCapacity(index), true);

                // In case that multiple threads attempt to reopen the same slot
                if (Lanes.CAS(index, newLane, lane) != lane)
                {
                    newLane.Dispose();
                }

                return(Lanes[index]);
            }
            else
            {
                return(null);
            }
        }