Esempio n. 1
0
        public void TestThread1()
        {
            var rnd = new System.Random();
            //put at most 1000 changes:
            int count = rnd.Next(1000);

            Interlocked.Add(ref good, count);
            var inc_up = new Inc();
            var ups    = new Mutable <TestState> .Updater[count];

            for (int i = 0; i < count; i++)
            {
                ups[i] = inc_up;
            }
            m_state.UpdateSeq(ups);
            //Now decrement:
            count = rnd.Next(1000);
            Interlocked.Add(ref good, -count);
            var downs  = new Mutable <TestState> .Updater[count];
            var dec_up = new Dec();

            for (int i = 0; i < count; i++)
            {
                downs[i] = dec_up;
            }
            m_state.UpdateSeq(downs);
        }
Esempio n. 2
0
        /** Update and return old and new state
         * this is faster than the delegate based approach
         * NO GUARANTEE THAT update_meth IS ONLY CALLED ONCE!!!!
         * update_meth should return a new state based on the old state
         */
        public Pair <T, T> Update(Mutable <T> .Updater update_meth)
        {
            T old_state = _state;
            T state;
            T new_state;

            do
            {
                state     = old_state;
                new_state = update_meth.ComputeNewState(state);
                old_state = Interlocked.CompareExchange <T>(ref _state, new_state, state);
            } while(old_state != state);
            return(new Pair <T, T>(state, new_state));
        }
Esempio n. 3
0
        /** Update and return old, new and side result
         * this is faster than the delegate based approach
         * NO GUARANTEE THAT update_meth IS ONLY CALLED ONCE!!!!
         * update_meth should return a pair that has the new state, and a side
         * result
         * the returned value gives the old state, new state and that side value
         */
        public Triple <T, T, R> Update <R>(Mutable <T> .Updater <R> update_meth)
        {
            T           old_state = _state;
            T           state;
            Pair <T, R> res;

            do
            {
                state     = old_state;
                res       = update_meth.ComputeNewStateSide(state);
                old_state = Interlocked.CompareExchange(ref _state, res.First, state);
            } while(old_state != state);
            return(new Triple <T, T, R>(state, res.First, res.Second));
        }
Esempio n. 4
0
 public MutableTester()
 {
     good    = 0;
     m_state = new Mutable <TestState>(new TestState(0));
 }
Esempio n. 5
0
 public ExclusiveServer(int max_servers)
 {
     _state = new Mutable <SQState <C> >(new SQState <C>(max_servers));
 }