Sunday, August 30, 2015

Prism 5.0 based WPF/MVVM Real-time streaming stack


V
|
|  (auto wire VM using VMLocatot)
VM
|
|  (IoC Services)
Services
|       \   (IExecutionCommand)
|        \
|       Network API ( Tibco, 29West, BB, etc.)
|            /
|           /  (IProdsumer:: Add/Take)
Model -----Producer-Consumer BlockingCollection


(1) VM will host data and Command properties
(2) Actual Processing code will be in Services
(3) Services: IExecutionCommand, IUICommand, IConfiguration, ISubscription(topic)
(4) Topic=>Model<Topic>=> IObserer<T>.OnNext=>ISubscription<T>

Saturday, August 29, 2015

Lock Free Volatile Field Updates


        void LockfreeUpdate(ref double volatileUpdateTarget, double value)
        {
            SpinWait sw = new SpinWait();
            while (true)
            {
                // after this line, other thread can update the ref double,
                double comparandAsSnapshot = volatileUpdateTarget;

                //w/o barrier, before instruction changing volatileUpdateTarget move to after Snapshot=> comparandAsSnapshot stale=>extra spin
                Thread.MemoryBarrier();

                // if preempted=>updateTarget changed != comparand=> no copy, and spin.
                double? originalRefValueBeforeExchComp = Interlocked.CompareExchange(ref volatileUpdateTarget, value, comparandAsSnapshot);
                if (originalRefValueBeforeExchComp == comparandAsSnapshot) return;  // no preemption
                sw.SpinOnce();
            }
        }
 
       void LockfreeUpdate<T>(ref T volatileUpdateTarget,T value) where T: class
        {
            SpinWait sw = new SpinWait();
            while (true)
            {
                // after this line, other thread can update the ref double,
                T comparandAsSnapshot = volatileUpdateTarget;

                 // ICE has internal MemoryBarrier so just extra spin here

                // if preempted=>updateTarget changed != comparand=> no copy, and spin.
                T originalRefValueBeforeExchComp = Interlocked.CompareExchange(ref volatileUpdateTarget, value, comparandAsSnapshot);
                if (originalRefValueBeforeExchComp == comparandAsSnapshot) return;  // no preemption
                sw.SpinOnce();
            }
        }

       void LockfreeUpdate<T>(ref T volatileUpdateTarget,func<T,T> updateFunc) where T: class
        {
            SpinWait sw = new SpinWait();
            while (true)
            {
                // after this line, other thread can update the ref double,
                T comparandAsSnapshot = volatileUpdateTarget;

                // if preempted=>updateTarget changed != comparand=> no copy, and spin.
                T originalRefValueBeforeExchComp = Interlocked.CompareExchange(ref volatileUpdateTarget, 
         updateFunc(comparandAsSnapshot ), comparandAsSnapshot);
                if (originalRefValueBeforeExchComp == comparandAsSnapshot) return;  // no preemption
                sw.SpinOnce();
            }
        }


Tuesday, August 18, 2015

WPF GridCOntrol Get to Cell content in Master-Detail


           var hPanel = VisualTreeHelpers.FindChild<HierarchyPanel>(AssociatedObject);
            if (hPanel == null) return;
            
            var listOfRowAndCell= new List<Tuple<bool,int, List<Tuple<object, string>>>>();
            foreach (var hpChild in hPanel.Children)
            {
                var row = hpChild as GridRow;
                if (row == null) continue;
                var sviPanel = VisualTreeHelpers.FindChild<StackVisibleIndexPanel>(row);
                if (sviPanel == null) continue;
 
                var list= new List<Tuple<object, string>>();
                foreach (var child in sviPanel.Children)
                {
                   var cellContentPresenter = child as GridCellContentPresenter; // now we got to cell level
                   if (cellContentPresenter == null) continue;
                   var cell = cellContentPresenter.Element as DevExpress.Xpf.Grid.CellEditor;
                   if (cell == null) continue;

                    // Store Visible Text Value for later Validation
                    var ev = cell.Edit.EditValue;
                    var fn = cell.Column.FieldName;
                   list.Add(new Tuple<object, string>(ev,fn));
                }

                if (row.RowDataContent == null || row.RowDataContent.DataContext == null  || row.RowDataContent.DataContext as RowData==null) continue;
                int rh = (row.RowDataContent.DataContext as RowData).RowHandle.Value;
                listOfRowAndCell.Add(new Tuple<bool,int, List<Tuple<object, string>>>(IsMasterRow(row),rh,list));
            }