Beispiel #1
0
        /// <summary>
        /// Creates new instance of <see cref="Atomic{T}"/>
        /// </summary>
        /// <param name="value">The value to store</param>
        /// <param name="order">Affects the way store operation occur. Default is <see cref="MemoryOrder.SeqCst"/> semantics which hurt performance</param>
        /// <param name="align">True to store the underlying value aligned, otherwise False</param>
        public Atomic(T value, MemoryOrder order = MemoryOrder.SeqCst, bool align = false)
        {
            if (!order.IsSpported())
            {
                throw new ArgumentException(string.Format("{0} is not supported", order));
            }

            _storage = GetStorage(order, align);

            if (Intrinsics != null && Intrinsics.Supports <T>())
            {
                _writer = Intrinsics;
            }
            else if (_storage as Atomic <T> == this)
            {
                _writer = this;
            }
            else if (_storage.Supports <T>())
            {
                _writer = _storage;
            }
            else
            {
                throw new NotSupportedException(string.Format("{0} type is not supported", typeof(T)));
            }

            if (object.ReferenceEquals(_storage, this) || _storage is LockBasedAtomic)
            {
                _instanceLock = new object();
            }

            _order         = order;
            _storage.Value = value;
        }
Beispiel #2
0
        /// <summary>
        /// Creates new instance of <see cref="AtomicLong"/>
        /// </summary>
        /// <param name="initialValue">The value to store</param>
        /// <param name="order">Affects the way store operation occur. Load operations are always use <see cref="MemoryOrder.Acquire"/> semantics</param>
        public AtomicReference(T initialValue, MemoryOrder order = MemoryOrder.AcqRel)
        {
            if (!order.IsSpported())
            {
                throw new ArgumentException(string.Format("{0} is not supported", order));
            }

            _order      = order;
            this._value = initialValue;
        }
Beispiel #3
0
        /// <summary>
        /// Creates new instance of <see cref="AtomicLong"/>
        /// </summary>
        /// <param name="value">The value to store</param>
        /// <param name="order">Affects the way store operation occur. Load operations are always use <see cref="MemoryOrder.Acquire"/> semantics</param>
        /// <param name="align">True to store the underlying value aligned, otherwise False</param>
        public AtomicLong(long value, MemoryOrder order = MemoryOrder.SeqCst, bool align = false)
        {
            if (!order.IsSpported())
            {
                throw new ArgumentException(string.Format("{0} is not supported", order));
            }

            _order        = order;
            this._storage = BoxedInt64.Create(value, align);
        }