Exemple #1
0
        /// <summary>
        /// Constructs a {@code TerminalOp} that implements a functional reduce on
        /// {@code long} values.
        /// </summary>
        /// <param name="identity"> the identity for the combining function </param>
        /// <param name="operator"> the combining function </param>
        /// <returns> a {@code TerminalOp} implementing the reduction </returns>
        public static TerminalOp <Long, Long> MakeLong(long identity, LongBinaryOperator @operator)
        {
            Objects.RequireNonNull(@operator);
//JAVA TO C# CONVERTER TODO TASK: Local classes are not converted by Java to C# Converter:
//			class ReducingSink implements AccumulatingSink<Long, Long, ReducingSink>, Sink_OfLong
            //		{
            //			private long state;
            //
            //			@@Override public void begin(long size)
            //			{
            //				state = identity;
            //			}
            //
            //			@@Override public void accept(long t)
            //			{
            //				state = @operator.applyAsLong(state, t);
            //			}
            //
            //			@@Override public Long get()
            //			{
            //				return state;
            //			}
            //
            //			@@Override public void combine(ReducingSink other)
            //			{
            //				accept(other.state);
            //			}
            //		}
            return(new ReduceOpAnonymousInnerClassHelper8());
        }
 /// <summary>
 /// Subtask constructor </summary>
 internal LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function, long[] array, int origin, int fence, int threshold, int lo, int hi) : base(parent)
 {
     this.Function  = function;
     this.Array     = array;
     this.Origin    = origin;
     this.Fence     = fence;
     this.Threshold = threshold;
     this.Lo        = lo;
     this.Hi        = hi;
 }
            /// <summary>
            /// Root task constructor </summary>
            public LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function, long[] array, int lo, int hi) : base(parent)
            {
                this.Function = function;
                this.Array    = array;
                this.Lo       = this.Origin = lo;
                this.Hi       = this.Fence = hi;
                int p;

                this.Threshold = (p = (hi - lo) / (ForkJoinPool.CommonPoolParallelism << 3)) <= MIN_PARTITION ? MIN_PARTITION : p;
            }
Exemple #4
0
        /// <summary>
        /// Atomically updates the current value with the results of
        /// applying the given function to the current and given values,
        /// returning the updated value. The function should be
        /// side-effect-free, since it may be re-applied when attempted
        /// updates fail due to contention among threads.  The function
        /// is applied with the current value as its first argument,
        /// and the given update as the second argument.
        /// </summary>
        /// <param name="x"> the update value </param>
        /// <param name="accumulatorFunction"> a side-effect-free function of two arguments </param>
        /// <returns> the updated value
        /// @since 1.8 </returns>
        public long AccumulateAndGet(long x, LongBinaryOperator accumulatorFunction)
        {
            long prev, next;

            do
            {
                prev = Get();
                next = accumulatorFunction.ApplyAsLong(prev, x);
            } while (!CompareAndSet(prev, next));
            return(next);
        }
Exemple #5
0
        /// <summary>
        /// Atomically updates the field of the given object managed by this
        /// updater with the results of applying the given function to the
        /// current and given values, returning the previous value. The
        /// function should be side-effect-free, since it may be re-applied
        /// when attempted updates fail due to contention among threads.  The
        /// function is applied with the current value as its first argument,
        /// and the given update as the second argument.
        /// </summary>
        /// <param name="obj"> An object whose field to get and set </param>
        /// <param name="x"> the update value </param>
        /// <param name="accumulatorFunction"> a side-effect-free function of two arguments </param>
        /// <returns> the previous value
        /// @since 1.8 </returns>
        public long GetAndAccumulate(T obj, long x, LongBinaryOperator accumulatorFunction)
        {
            long prev, next;

            do
            {
                prev = Get(obj);
                next = accumulatorFunction.ApplyAsLong(prev, x);
            } while (!CompareAndSet(obj, prev, next));
            return(prev);
        }
Exemple #6
0
        /// <summary>
        /// Atomically updates the element at index {@code i} with the
        /// results of applying the given function to the current and
        /// given values, returning the updated value. The function should
        /// be side-effect-free, since it may be re-applied when attempted
        /// updates fail due to contention among threads.  The function is
        /// applied with the current value at index {@code i} as its first
        /// argument, and the given update as the second argument.
        /// </summary>
        /// <param name="i"> the index </param>
        /// <param name="x"> the update value </param>
        /// <param name="accumulatorFunction"> a side-effect-free function of two arguments </param>
        /// <returns> the updated value
        /// @since 1.8 </returns>
        public long AccumulateAndGet(int i, long x, LongBinaryOperator accumulatorFunction)
        {
            long offset = CheckedByteOffset(i);
            long prev, next;

            do
            {
                prev = GetRaw(offset);
                next = accumulatorFunction.ApplyAsLong(prev, x);
            } while (!CompareAndSetRaw(offset, prev, next));
            return(next);
        }
Exemple #7
0
        /// <summary>
        /// Constructs a {@code TerminalOp} that implements a functional reduce on
        /// {@code long} values, producing an optional long result.
        /// </summary>
        /// <param name="operator"> the combining function </param>
        /// <returns> a {@code TerminalOp} implementing the reduction </returns>
        public static TerminalOp <Long, OptionalLong> MakeLong(LongBinaryOperator @operator)
        {
            Objects.RequireNonNull(@operator);
//JAVA TO C# CONVERTER TODO TASK: Local classes are not converted by Java to C# Converter:
//			class ReducingSink implements AccumulatingSink<Long, java.util.OptionalLong, ReducingSink>, Sink_OfLong
            //		{
            //			private boolean empty;
            //			private long state;
            //
            //			public void begin(long size)
            //			{
            //				empty = true;
            //				state = 0;
            //			}
            //
            //			@@Override public void accept(long t)
            //			{
            //				if (empty)
            //				{
            //					empty = false;
            //					state = t;
            //				}
            //				else
            //				{
            //					state = @operator.applyAsLong(state, t);
            //				}
            //			}
            //
            //			@@Override public OptionalLong get()
            //			{
            //				return empty ? OptionalLong.empty() : OptionalLong.of(state);
            //			}
            //
            //			@@Override public void combine(ReducingSink other)
            //			{
            //				if (!other.empty)
            //					accept(other.state);
            //			}
            //		}
            return(new ReduceOpAnonymousInnerClassHelper9());
        }
Exemple #8
0
        public static Object Arith(Object a, Object b, ArithOpEnum op)
        {
            LongBinaryOperator   integerFunc = integerOps[(int)op];
            DoubleBinaryOperator floatFunc   = floatOps[(int)op];

            if (floatFunc == null)
            {
                long?x = LuaValue.ToInteger(a);
                if (x != null)
                {
                    long?y = LuaValue.ToInteger(b);
                    if (y != null)
                    {
                        return(integerFunc((long)x, (long)y));
                    }
                }
            }
            else
            {
                if (integerFunc != null)
                {
                    if (TypeExtension.TypeEqual <long>(a) &&
                        TypeExtension.TypeEqual <long>(b))
                    {
                        return(integerFunc((long)a, (long)b));
                    }
                }
                double?x = LuaValue.ToFloat(a);
                if (x != null)
                {
                    double?y = LuaValue.ToFloat(b);
                    if (y != null)
                    {
                        return(floatFunc((double)x, (double)y));
                    }
                }
            }
            return(null);
        }
 /// <summary>
 /// Creates a new instance using the given accumulator function
 /// and identity element. </summary>
 /// <param name="accumulatorFunction"> a side-effect-free function of two arguments </param>
 /// <param name="identity"> identity (initial value) for the accumulator function </param>
 public LongAccumulator(LongBinaryOperator accumulatorFunction, long identity)
 {
     this.Function = accumulatorFunction;
     @base         = this.Identity = identity;
 }
 internal SerializationProxy(LongAccumulator a)
 {
     Function = a.Function;
     Identity = a.Identity;
     Value    = a.Get();
 }
Exemple #11
0
 public long getAndAccumulate(long arg0, LongBinaryOperator arg1)
 {
     return Instance.CallMethod<long>("getAndAccumulate", "(JLjava/util/function/LongBinaryOperator;)J", arg0, arg1);
 }
 public LongAccumulator(LongBinaryOperator arg0, long arg1)
     : base(ProxyCtor.I)
 {
     Instance.CallConstructor("(Ljava/util/function/LongBinaryOperator;J)V", arg0, arg1);
 }
 public long accumulateAndGet(int arg0, long arg1, LongBinaryOperator arg2)
 {
     return Instance.CallMethod<long>("accumulateAndGet", "(IJLjava/util/function/LongBinaryOperator;)J", arg0, arg1, arg2);
 }
Exemple #14
0
        /// <summary>
        /// Handles cases of updates involving initialization, resizing,
        /// creating new Cells, and/or contention. See above for
        /// explanation. This method suffers the usual non-modularity
        /// problems of optimistic retry code, relying on rechecked sets of
        /// reads.
        /// </summary>
        /// <param name="x"> the value </param>
        /// <param name="fn"> the update function, or null for add (this convention
        /// avoids the need for an extra field or function in LongAdder). </param>
        /// <param name="wasUncontended"> false if CAS failed before call </param>
        internal void LongAccumulate(long x, LongBinaryOperator fn, bool wasUncontended)
        {
            int h;

            if ((h = Probe) == 0)
            {
                ThreadLocalRandom.Current();                 // force initialization
                h = Probe;
                wasUncontended = true;
            }
            bool collide = false;             // True if last slot nonempty

            for (;;)
            {
                Cell[] @as;
                Cell   a;
                int    n;
                long   v;
                if ((@as = Cells) != null && (n = @as.Length) > 0)
                {
                    if ((a = @as[(n - 1) & h]) == null)
                    {
                        if (CellsBusy == 0)                         // Try to attach new Cell
                        {
                            Cell r = new Cell(x);                   // Optimistically create
                            if (CellsBusy == 0 && CasCellsBusy())
                            {
                                bool created = false;
                                try                                 // Recheck under lock
                                {
                                    Cell[] rs;
                                    int    m, j;
                                    if ((rs = Cells) != null && (m = rs.Length) > 0 && rs[j = (m - 1) & h] == null)
                                    {
                                        rs[j]   = r;
                                        created = true;
                                    }
                                }
                                finally
                                {
                                    CellsBusy = 0;
                                }
                                if (created)
                                {
                                    break;
                                }
                                continue;                                 // Slot is now non-empty
                            }
                        }
                        collide = false;
                    }
                    else if (!wasUncontended)                     // CAS already known to fail
                    {
                        wasUncontended = true;                    // Continue after rehash
                    }
                    else if (a.Cas(v = a.Value, ((fn == null) ? v + x : fn.ApplyAsLong(v, x))))
                    {
                        break;
                    }
                    else if (n >= NCPU || Cells != @as)
                    {
                        collide = false;                         // At max size or stale
                    }
                    else if (!collide)
                    {
                        collide = true;
                    }
                    else if (CellsBusy == 0 && CasCellsBusy())
                    {
                        try
                        {
                            if (Cells == @as)                             // Expand table unless stale
                            {
                                Cell[] rs = new Cell[n << 1];
                                for (int i = 0; i < n; ++i)
                                {
                                    rs[i] = @as[i];
                                }
                                Cells = rs;
                            }
                        }
                        finally
                        {
                            CellsBusy = 0;
                        }
                        collide = false;
                        continue;                         // Retry with expanded table
                    }
                    h = AdvanceProbe(h);
                }
                else if (CellsBusy == 0 && Cells == @as && CasCellsBusy())
                {
                    bool init = false;
                    try                     // Initialize table
                    {
                        if (Cells == @as)
                        {
                            Cell[] rs = new Cell[2];
                            rs[h & 1] = new Cell(x);
                            Cells     = rs;
                            init      = true;
                        }
                    }
                    finally
                    {
                        CellsBusy = 0;
                    }
                    if (init)
                    {
                        break;
                    }
                }
                else if (CasBase(v = @base, ((fn == null) ? v + x : fn.ApplyAsLong(v, x))))
                {
                    break;                     // Fall back on using base
                }
            }
        }