diff MonoPlay/Program.cs @ 136:e9e7940c7d98 v2

shared locks + tests
author cin
date Mon, 16 Feb 2015 01:14:09 +0300
parents f803565868a4
children f75cfa58e3d4
line wrap: on
line diff
--- a/MonoPlay/Program.cs	Fri Feb 13 02:08:01 2015 +0300
+++ b/MonoPlay/Program.cs	Mon Feb 16 01:14:09 2015 +0300
@@ -4,6 +4,7 @@
 using Implab;
 using System.Collections.Generic;
 using System.Collections.Concurrent;
+using System.Threading;
 
 namespace MonoPlay {
     class MainClass {
@@ -11,24 +12,82 @@
             if (args == null)
                 throw new ArgumentNullException("args");
 
-            const int count = 10000000;
-
             var t1 = Environment.TickCount;
 
-            for (int i = 0; i < count; i++) {
-                var p = new Promise<int>();
+            const int reads = 100000;
+            const int writes = 1000;
+            const int readThreads = 8;
+            const int writeThreads = 0;
+
+            var l = new SharedLock();
+            var st = new HashSet<int>();
 
-                p.On(x => {}).On(x => {});
+            Action reader1 = () => {
+                for (int i =0; i < reads; i++) {
+                    try {
+                        l.LockShared();
+                        st.Contains(i % 1000);
+                        Thread.Sleep(0);
+                    } finally {
+                        l.Release();
+                    }
+                }
+            };
+
+            Action reader2 = () => {
+                for(var i = 0; i < reads; i++)
+                    lock(st) {
+                        st.Contains(i % 1000);
+                        Thread.Sleep(0);
+                    }
+            };
 
-                p.Resolve(i);
+            Action writer1 = () => {
+                var rnd = new Random(Environment.TickCount);
+                for (int i = 0; i < writes; i++) {
+                    try {
+                        l.LockExclusive();
+                        st.Add(rnd.Next(1000));
+                        //Thread.Sleep(1);
+                    } finally {
+                        l.Release();
+                    }
+                }
+            };
 
-            }
+            Action writer2 = () => {
+                var rnd = new Random(Environment.TickCount);
+                for (int i = 0; i < writes; i++) {
+                    lock (st) {
+                        st.Add(rnd.Next(1000));
+                        //Thread.Sleep(1);
+                    }
+                }
+            };
+
+
 
-           
+            var readers = new IPromise[readThreads];
+            for (int i = 0; i < readThreads; i++)
+                readers[i] = AsyncPool.RunThread(reader1);
+
+            var writers = new IPromise[writeThreads];
+            for (int i = 0; i < writeThreads; i++)
+                writers[i] = AsyncPool.RunThread(writer1);
+
+
+            new [] {
+                readers.Bundle().On(() => Console.WriteLine("readers complete in {0} ms", Environment.TickCount - t1)),
+                writers.Bundle().On(() => Console.WriteLine("writers complete in {0} ms", Environment.TickCount - t1))
+            }.Bundle().Join();
+
+
 
             var t2 = Environment.TickCount;
             Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) );
 
         }
+
+
     }
 }