Esempio n. 1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ThreadSafeStack{T}" /> class.
        /// </summary>
        public ThreadSafeStack(IEnumerable <T> source)
        {
            foreach (var item in source)
            {
                _root = Node <T> .GetNode(_root, item);

                _count++;
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ThreadSafeStack{T}" /> class.
        /// </summary>
        public ThreadSafeStack(IEnumerable <T> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            foreach (var item in source)
            {
                _root = Node <T> .GetNode(_root, item);

                _count++;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Attempts to Adds the specified item at the front.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>
        ///   <c>true</c> if the item was added; otherwise, <c>false</c>.
        /// </returns>
        public void Add(T item)
        {
            var root = Volatile.Read(ref _root);
            var node = Node <T> .GetNode(root, item);

            var spinWait = new SpinWait();

            while (true)
            {
                var found = Interlocked.CompareExchange(ref _root, node, root);
                if (found == root)
                {
                    Interlocked.Increment(ref _count);
                    return;
                }
                root      = Volatile.Read(ref _root);
                node.Link = root;
                spinWait.SpinOnce();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Attempts to Adds the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        public void Add(T item)
        {
            var spinWait = new SpinWait();

            while (true)
            {
                var tail = Volatile.Read(ref _tail);
                if (tail.Value.TryAdd(item))
                {
                    Interlocked.Increment(ref _count);
                    return;
                }
                var node = Node <FixedSizeQueue <T> > .GetNode(null, new FixedSizeQueue <T>(64));

                var found = Interlocked.CompareExchange(ref tail.Link, node, null);
                if (found == null)
                {
                    Volatile.Write(ref _tail, node);
                }
                spinWait.SpinOnce();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SafeQueue{T}" /> class.
        /// </summary>
        public SafeQueue()
        {
            _root = Node <FixedSizeQueue <T> > .GetNode(null, new FixedSizeQueue <T>(64));

            _tail = _root;
        }