예제 #1
0
        public void New_TryDequeue_TryEnqueue_x50_TryDequeue_x50_TryDequeue_Count()
        {
            int value;
            var q = new LockFreeQueue <int>();

            Assert.AreEqual(0, q.Count);

            q.TryDequeue(out value);
            Assert.AreEqual(0, q.Count);

            for (int i = 0; i < 50; ++i)
            {
                q.TryEnqueue(100 - i);
            }
            Assert.AreEqual(50, q.Count);

            for (int i = 0; i < 50; ++i)
            {
                q.TryDequeue(out value);
                Assert.AreEqual(100 - i, value);
            }
            Assert.AreEqual(0, q.Count);

            q.TryDequeue(out value);
            Assert.AreEqual(0, q.Count);
        }
 public static void Initialize(int toSendElements)
 {
     _internalQueue = new LockFreeQueue<CollectionElement>();
     _collectedElements = 0;
     _sentElements = 0;
     _toSendElements = toSendElements;
 }
예제 #3
0
        public void LockFreeQueuePeek()
        {
            var   queue = new LockFreeQueue <int>();
            Int32 t     = 0;

            //We enqueue some items to test
            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(5);
            //We check that after a peeking we have the correct value but the item
            //is not deleted
            Assert.IsTrue(queue.Peek(ref t));
            Assert.AreEqual(t, 1);
            Assert.AreEqual(queue.Count, 5);
            queue.DequeueSingle();
            Assert.IsTrue(queue.Peek(ref t));
            Assert.AreEqual(t, 2);
            Assert.AreEqual(queue.Count, 4);
            queue.DequeueSingle();
            Assert.IsTrue(queue.Peek(ref t));
            Assert.AreEqual(t, 3);
            Assert.AreEqual(queue.Count, 3);
            queue.DequeueSingle();
            Assert.IsTrue(queue.Peek(ref t));
            Assert.AreEqual(t, 4);
            Assert.AreEqual(queue.Count, 2);
            queue.DequeueSingle();
            Assert.IsTrue(queue.Peek(ref t));
            Assert.AreEqual(t, 5);
            Assert.AreEqual(queue.Count, 1);
            queue.DequeueSingle();
            Assert.IsFalse(queue.Peek(ref t));
        }
예제 #4
0
    public LuaScriptMgr()
    {
        Instance       = this;
        LuaStatic.Load = Loader;
        lua            = new LuaState();
        _translator    = lua.GetTranslator();
        LuaDLL.luaopen_pb(lua.L);
        //LuaDLL.luaopen_ffi(lua.L);
        //OpenXml();
        //OpenLuaCmd();

        fileList  = new HashSet <string>();
        dict      = new Dictionary <string, LuaBase>();
        refGCList = new LockFreeQueue <int>(2048);

        LuaDLL.lua_pushstring(lua.L, "ToLua_Index");
        LuaDLL.luaL_dostring(lua.L, luaIndex);
        LuaDLL.lua_rawset(lua.L, (int)LuaIndexes.LUA_REGISTRYINDEX);

        LuaDLL.lua_pushstring(lua.L, "ToLua_NewIndex");
        LuaDLL.luaL_dostring(lua.L, luaNewIndex);
        LuaDLL.lua_rawset(lua.L, (int)LuaIndexes.LUA_REGISTRYINDEX);

        LuaDLL.lua_pushstring(lua.L, "luaTableCall");
        LuaDLL.luaL_dostring(lua.L, luaTableCall);
        LuaDLL.lua_rawset(lua.L, (int)LuaIndexes.LUA_REGISTRYINDEX);

        Bind();

#if MULTI_STATE
        mgrList.Add(this);
#endif
        //CmdTable.RegisterCommand("ToLua", ToLua.Generate);
        //CmdTable.RegisterCommand("LuaGC", LuaGC);
    }
예제 #5
0
        /// <summary>
        ///Clear 的测试
        ///</summary>
        public void ClearTestHelper <ValueT>()
        {
            LockFreeQueue <ValueT> target = new LockFreeQueue <ValueT>(); // TODO: 初始化为适当的值

            target.Clear();
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
예제 #6
0
 public SelfRunningTaskPool(int interval, string name)
 {
     m_messageQueue = new LockFreeQueue <IMessage>();
     m_queueTimer   = Stopwatch.StartNew();
     UpdateInterval = interval;
     Name           = name;
 }
예제 #7
0
 public MappedChart(string chartName, string xAxis, string yAxis, int length = 32, Color? primaryColor = null)
 {
     values = new double[length];
     valueLength = length;
     queuedPoints = new LockFreeQueue<DataPoint>();
     CreateChart(chartName, xAxis, yAxis, primaryColor ?? Color.FromArgb(44, 139, 221), length);
 }
예제 #8
0
 protected SelfRunningTaskQueue(int updateInterval)
 {
     m_timers       = new List <SimplerTimer>();
     m_messageQueue = new LockFreeQueue <Action>();
     m_queueTimer   = Stopwatch.StartNew();
     UpdateInterval = updateInterval;
 }
예제 #9
0
 public EventDispatcher(LockFreeQueue <Coroutines.EventMessage> events, CoroutineThread[] threads, CounterInt64 counter, Dictionary <string, Dictionary <Type, CoroutineEventDescriptor> > eventsList)
 {
     _events     = events;
     _threads    = threads;
     _counter    = counter;
     _eventsList = eventsList;
 }
예제 #10
0
 public EventManagerCoroutine(LockFreeQueue <CoroutineEventDescriptor> eventQueue, CounterInt64 counter,
                              Dictionary <string, Dictionary <Type, CoroutineEventDescriptor> > eventsDescriptors)
 {
     _eventsDescriptors     = eventsDescriptors;
     _eventDescriptorsQueue = eventQueue;
     _counter = counter;
 }
예제 #11
0
 protected SelfRunningTaskQueue(int updateInterval)
 {
     m_timers = new List<SimplerTimer>();
     m_messageQueue = new LockFreeQueue<Action>();
     m_queueTimer = Stopwatch.StartNew();
     UpdateInterval = updateInterval;
 }
예제 #12
0
        public void EnqueueTest_MutipleThread()
        {
            int seed = 0;

            Thread[]            threads = new Thread[this.m_threadCount];
            LockFreeQueue <int> queue   = new LockFreeQueue <int>();

            for (int i = 0; i < this.m_threadCount; i++)
            {
                threads[i] = new Thread(() => {
                    for (int j = 0; j < this.m_dataCount; j++)
                    {
                        queue.Enqueue(Interlocked.Increment(ref seed));
                    }
                });
            }

            for (int i = 0; i < this.m_threadCount; i++)
            {
                threads[i].Start();
            }

            for (int i = 0; i < this.m_threadCount; i++)
            {
                threads[i].Join();
            }

            Assert.AreEqual(this.m_threadCount * this.m_dataCount, queue.Count);
            foreach (int item in queue)
            {
                Assert.AreNotEqual(0, item);
            }
        }
예제 #13
0
 public MappedChart(string chartName, string xAxis, string yAxis, int length = 32, Color?primaryColor = null)
 {
     values       = new double[length];
     valueLength  = length;
     queuedPoints = new LockFreeQueue <DataPoint>();
     CreateChart(chartName, xAxis, yAxis, primaryColor ?? Color.FromArgb(44, 139, 221), length);
 }
예제 #14
0
 public static void Initialize(int toSendElements)
 {
     _internalQueue     = new LockFreeQueue <CollectionElement>();
     _collectedElements = 0;
     _sentElements      = 0;
     _toSendElements    = toSendElements;
 }
예제 #15
0
 protected BaseMessageThread(ILogger logger, String threadName, bool restartOnError = true)
     : base(logger, threadName, restartOnError)
 {
     _incomingMessages = new LockFreeQueue<IMessage>();
     _outgoingMessages = new LockFreeQueue<IMessage>();
     MaxMesssagesPerCycle = -1;
 }
예제 #16
0
        public string[] IterateFiles(string currentPath)
        {
            List <string>          foundFiles = new List <string>();
            LockFreeQueue <string> subPaths   = new LockFreeQueue <string>();

            do
            {
                try
                {
                    string[] subDirectories = Directory.GetDirectories(currentPath);
                    for (int i = 0; i < subDirectories.Length; ++i)
                    {
                        subPaths.Push(subDirectories[i]);
                    }

                    string[] subFiles = Directory.GetFiles(currentPath);
                    foundFiles.AddRange(subFiles);
                }
                catch (Exception)
                {
                    /// We don't care.
                }
            } while (subPaths.Pop(out currentPath));

            return(foundFiles.ToArray());
        }
예제 #17
0
        /**
         * @param port the port to listen on
         * @param ipList an IEnumerable object of IPAddress objects.
         * @param ta_auth the TAAuthorizer to use for remote nodes
         */
        public TcpEdgeListener(int port, IEnumerable local_config_ips, TAAuthorizer ta_auth)
        {
            _is_started  = 0;
            _listen_sock = new Socket(AddressFamily.InterNetwork,
                                      SocketType.Stream, ProtocolType.Tcp);
            _listen_sock.LingerState = new LingerOption(true, 0);
            IPEndPoint tmp_ep = new IPEndPoint(IPAddress.Any, port);

            _listen_sock.Bind(tmp_ep);
            _local_endpoint = (IPEndPoint)_listen_sock.LocalEndPoint;
            port            = _local_endpoint.Port;
            _count          = 0;

            /**
             * We get all the IPAddresses for this computer
             */
            if (local_config_ips == null)
            {
                _tas = TransportAddressFactory.CreateForLocalHost(TransportAddress.TAType.Tcp, port);
            }
            else
            {
                _tas = TransportAddressFactory.Create(TransportAddress.TAType.Tcp, port, local_config_ips);
            }

            _ta_auth = ta_auth;
            if (_ta_auth == null)
            {
                //Always authorize in this case:
                _ta_auth = new ConstantAuthorizer(TAAuthorizer.Decision.Allow);
            }
            _loop = new Thread(this.SelectLoop);
            //This is how we push jobs into the SelectThread
            ActionQueue = new LockFreeQueue <SocketStateAction>();
        }
예제 #18
0
 private void Awake()
 {
     pool     = new MessagePool();
     received = new LockFreeQueue <ReferenceCountedMessage>();
     pc       = GetComponentInParent <WebRtcPeerConnection>();
     id       = -1;
 }
예제 #19
0
        /// <summary>
        ///Enqueue 的测试
        ///</summary>
        public void EnqueueTestHelper <ValueT>()
        {
            LockFreeQueue <ValueT> target = new LockFreeQueue <ValueT>(); // TODO: 初始化为适当的值
            ValueT itemT = default(ValueT);                               // TODO: 初始化为适当的值

            target.Enqueue(itemT);
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
예제 #20
0
 public AsyncLog(string outputFile)
 {
     sb = new StringBuilder(1024);
     filepath = outputFile;
     lfq = new LockFreeQueue<Info>();
     logThread = new Thread(new ThreadStart(RunLog));
     logThread.Start();
 }
예제 #21
0
 public RoomPainter(TTClient parent)
 {
     client = parent;
     paintingQueue = new LockFreeQueue<Action>();
     chatLines = new string[0];
     threadTicker = new Timer(Tick, null, 300, 300); /// 4 fps :D
     Instance = this;
 }
예제 #22
0
 public BufferManager(int segmentCount, int segmentSize)
 {
     this.m_segmentCount      = segmentCount;
     this.m_segmentSize       = segmentSize;
     this.m_buffers           = new List <ArrayBuffer>();
     this.m_availableSegments = new LockFreeQueue <BufferSegment>();
     BufferManager.Managers.Add(this);
 }
예제 #23
0
        public ClientHandler(ILog log)
        {
            cjdBuy   = new Dictionary <long, Msg300192_011>();
            cjdSell  = new Dictionary <long, Msg300192_011>();
            sddBuy   = new Dictionary <long, Msg300192_011>();
            sddSell  = new Dictionary <long, Msg300192_011>();
            tljdBuy  = new Dictionary <long, Msg300192_011>();
            tljdSell = new Dictionary <long, Msg300192_011>();
            tljdList = new List <Msg300192_011>();
            zl       = new Dictionary <long, Msg300192_011>();

            lockFreeQueue = new LockFreeQueue <IByteBuffer>();
            // redis = new RedisHelper(9, "128.6.5.10:6379,allowadmin=true");//包含DBNub,port
            redis = new RedisHelper(Convert.ToInt32(ConfigHelper.Configuration["redisDB"]), ConfigHelper.Configuration["redisConn"]); //包含DBNub,port
                                                                                                                                      // redisAsync = new RedisHelper(Convert.ToInt32(ConfigHelper.Configuration["redisDBAsync"]), ConfigHelper.Configuration["redisConnAsync"]);//包含DBNub,port
                                                                                                                                      //redis = new RedisHelper(0, "128.6.2.85:6380,password = 123456");//包含DBNub,port

            this.log = log;
            //short[] shorts = new short[10];
            // Buffer.SetByte(shorts, 0, Encoding.UTF8.GetBytes("1"));
            // Console.WriteLine(Buffer);

            //IByteBuffer bf = this.initialMessage.Allocator.Buffer(100);

            this.initialMessage = Unpooled.Buffer(100);
            // byte[] messageBytes = Encoding.UTF8.GetBytes("Hello world");
            // this.initialMessage.WriteBytes(messageBytes);


            //4
            this.initialMessage.WriteInt(msgType);
            //4
            this.initialMessage.WriteInt(92);
            //20
            this.initialMessage.WriteString(senderCompID, Encoding.UTF8);
            this.initialMessage.SetWriterIndex(28);
            //20
            this.initialMessage.WriteString(targetCompID, Encoding.UTF8);
            this.initialMessage.SetWriterIndex(48);
            //4
            this.initialMessage.WriteInt(heartBtInt);
            // this.initialMessage.SetWriterIndex(52);
            //16
            this.initialMessage.WriteString(password, Encoding.UTF8);
            this.initialMessage.SetWriterIndex(68);
            //32
            this.initialMessage.WriteString(defaultApplVerID, Encoding.UTF8);
            this.initialMessage.SetWriterIndex(100);

            this.initialMessage.WriteInt(GenerateCheckSum(this.initialMessage, 100));

            Thread threadLevel2 = new Thread(new ThreadStart(QueueLevel2));

            threadLevel2.Start();
            Thread lockFreeCount = new Thread(new ThreadStart(GetlockFreeCount));

            lockFreeCount.Start();
        }
예제 #24
0
        public void New_TryEnqueue_Count()
        {
            var q = new LockFreeQueue <int>();

            Assert.AreEqual(0, q.Count);

            q.TryEnqueue(42);
            Assert.AreEqual(1, q.Count);
        }
예제 #25
0
        private void PrepareQueueData(LockFreeQueue <int> queue, int dataCount)
        {
            int seed = 0;

            for (int j = 0; j < dataCount; j++)
            {
                queue.Enqueue(Interlocked.Increment(ref seed));
            }
        }
예제 #26
0
        public void StartIteration(string path)
        {
            TerminateCurrentIteration();

            FoundDataForTreeView = new LockFreeQueue <DirectoryElement>();
            FoundDataForXml      = new LockFreeQueue <DirectoryElement>();

            IterateDirectoryThread.Start(path);
        }
예제 #27
0
 /// <summary>
 ///     Constructs a new <see cref="Default"></see> object
 /// </summary>
 /// <param name="segmentCount">The number of chunks tocreate per segment</param>
 /// <param name="segmentSize">The size of a chunk in bytes</param>
 public BufferManager(int segmentCount, int segmentSize)
 {
     m_segmentCount      = segmentCount;
     m_segmentSize       = segmentSize;
     m_buffers           = new List <ArrayBuffer>();
     m_availableSegments = new LockFreeQueue <BufferSegment>();
     m_segmentsInUse     = new ConcurrentDictionary <int, BufferSegment>();
     Managers.Add(this);
 }
예제 #28
0
 public LimitedPriorityQueue(IPriorityConverter <P> converter)
 {
     this.converter = converter;
     this.queueList = new LockFreeQueue <T> [converter.PriorityCount];
     for (int i = 0; i < queueList.Length; i++)
     {
         queueList[i] = new LockFreeQueue <T>();
     }
 }
예제 #29
0
        public void New_TryDequeue_Count()
        {
            int value;
            var q = new LockFreeQueue<int>();
            Assert.AreEqual(0, q.Count);

            q.TryDequeue(out value);
            Assert.AreEqual(0, q.Count);
        }
예제 #30
0
 static RequestObjectMessage()
 {
     _resetEvents = new LockFreeQueue<ManualResetEventSlim>();
     int count = 100;
     while (count >= 0)
     {
         _resetEvents.Enqueue(new ManualResetEventSlim(false));
         count--;
     }
 }
예제 #31
0
        /// <summary>
        ///CopyTo 的测试
        ///</summary>
        public void CopyToTestHelper <ValueT>()
        {
            LockFreeQueue <ValueT> target = new LockFreeQueue <ValueT>(); // TODO: 初始化为适当的值

            ValueT[] arrayT      = null;                                  // TODO: 初始化为适当的值
            int      iArrayIndex = 0;                                     // TODO: 初始化为适当的值

            target.CopyTo(arrayT, iArrayIndex);
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
예제 #32
0
        /// <summary>
        ///ToArrayAndClear 的测试
        ///</summary>
        public void ToArrayAndClearTestHelper <ValueT>()
        {
            LockFreeQueue <ValueT> target = new LockFreeQueue <ValueT>(); // TODO: 初始化为适当的值

            ValueT[] expected = null;                                     // TODO: 初始化为适当的值
            ValueT[] actual;
            actual = target.ToArrayAndClear();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
 public SelfRunningTaskPool(int interval, string name)
 {
     this.m_messageQueue = new LockFreeQueue <IMessage>();
     this.m_queueTimer   = Stopwatch.StartNew();
     this.m_simpleTimers = new List <SimpleTimerEntry>();
     this.m_timers       = new List <TimerEntry>();
     this.UpdateInterval = interval;
     this.Name           = name;
     this.Start();
 }
예제 #34
0
        public void New_TryDequeue_Count()
        {
            int value;
            var q = new LockFreeQueue <int>();

            Assert.AreEqual(0, q.Count);

            q.TryDequeue(out value);
            Assert.AreEqual(0, q.Count);
        }
예제 #35
0
        /// <summary>
        ///GetEnumerator 的测试
        ///</summary>
        public void GetEnumeratorTestHelper <ValueT>()
        {
            LockFreeQueue <ValueT> target   = new LockFreeQueue <ValueT>(); // TODO: 初始化为适当的值
            IEnumerator <ValueT>   expected = null;                         // TODO: 初始化为适当的值
            IEnumerator <ValueT>   actual;

            actual = target.GetEnumerator();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
예제 #36
0
        /// <summary>
        ///Dequeue 的测试
        ///</summary>
        public void DequeueTestHelper <ValueT>()
        {
            LockFreeQueue <ValueT> target = new LockFreeQueue <ValueT>(); // TODO: 初始化为适当的值
            ValueT expected = default(ValueT);                            // TODO: 初始化为适当的值
            ValueT actual;

            actual = target.Dequeue();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
        public void LockFreeQueueVsConcurrentQueue()
        {
            lq            = new LockFreeQueue <int>();
            cq            = new ConcurrentQueue <int>();
            lqDestination = new LockFreeQueue <int>();
            cqDestination = new ConcurrentQueue <int>();

            addLqDone = new ManualResetEvent(false);
            addCqDone = new ManualResetEvent(false);
            remLqDone = new ManualResetEvent(false);
            remCqDone = new ManualResetEvent(false);

            // Create a thread for each adding-process.
            BackgroundWorker adderLq = new BackgroundWorker();

            adderLq.DoWork             += addLq_DoWork;
            adderLq.RunWorkerCompleted += addLq_Completed;

            BackgroundWorker adderCq = new BackgroundWorker();

            adderCq.DoWork             += addCq_DoWork;
            adderCq.RunWorkerCompleted += addCq_Completed;

            // Create a thread for each removal-process.
            BackgroundWorker removerLq = new BackgroundWorker();

            removerLq.DoWork             += removeLq_DoWork;
            removerLq.RunWorkerCompleted += removeLq_Completed;

            BackgroundWorker removerCq = new BackgroundWorker();

            removerCq.DoWork             += removeCq_DoWork;
            removerCq.RunWorkerCompleted += removeCq_Completed;

            // Start the whole shebang.
            start = DateTime.Now;
            //adderLq.RunWorkerAsync();
            adderCq.RunWorkerAsync();
            //addLqDone.WaitOne();
            addCqDone.WaitOne();
            //removerLq.RunWorkerAsync();
            removerCq.RunWorkerAsync();

            // Wait for all tests to finish.
            //addLqDone.WaitOne();
            addCqDone.WaitOne();
            //remLqDone.WaitOne();
            remCqDone.WaitOne();

            Assert.AreEqual(0, lq.Count);
            Assert.AreEqual(NUMBER_OF_OPERATIONS, lqDestination.Count);

            Assert.IsEmpty(cq);
            Assert.AreEqual(NUMBER_OF_OPERATIONS, cqDestination.Count);
        }
예제 #38
0
        static EventThrower()
        {
            peakQueueLength = 0;

            eventThread = new Thread(new ThreadStart(EventThread));
            syncWorkItems = new LockFreeQueue< WorkItem>(); 
            newWorkItem = new AutoResetEvent(false);

            eventThread.IsBackground = true;    // So it will not linger once a main thread is gone
            eventThread.Name = "EventThrower";
            eventThread.Start();
        }
예제 #39
0
 /**
  * @param max_enqueues the maximum number of times Enqueue is allowed, after
  * @param state some arbitrary object we might want later
  * that it will throw InvalidOperationException and the queue will be closed
  */
 public Channel(int max_enqueues, object state) {
   if( max_enqueues == 0 ) {
     //This doesn't make sense
     throw new ArgumentOutOfRangeException("max_enqueues", max_enqueues, "cannot be zero");
   }
   State = state;
   _closed = 0;
   _queue = new LockFreeQueue<object>();
   _max_enqueues = max_enqueues;
   _enqueues = 0;
   _count = 0;
   _close_event = new FireOnceEvent();
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="updateInterval"></param>
        public TaskProcessorBase(string name, int updateInterval = 10)
        {
            UpdateInterval = updateInterval;
            Name = name;

            m_running = false;
            m_messageQueue = new LockFreeQueue<Action>();
            m_updatableObjects = new List<Updatable>();
            m_timerList = new List<UpdatableTimer>();
            m_queueTimer = new Stopwatch();

            Start();
        }
예제 #41
0
 public void ItShouldBePossibleToClearAQueueAndDequeueASingleItem()
 {
     var lfq = new LockFreeQueue<string>();
     var lele = new List<string>();
     for (int i = 0; i < ENQUEUED_DATA; i++)
     {
         lele.Add("TEST_" + i);
     }
     lfq.Enqueue(lele);
     Assert.AreEqual("TEST_0", lfq.DequeueSingle());
     lfq.Clear();
     Assert.IsNull(lfq.DequeueSingle());
 }
예제 #42
0
 public void ItShouldBePossibleToEnqueuAndDequeuNonNullableElements()
 {
     var lfq = new LockFreeQueue<int>();
     var lele = new List<int>();
     for (int i = 0; i < ENQUEUED_DATA; i++)
     {
         lele.Add(i);
     }
     lfq.Enqueue(lele);
     Assert.AreEqual(0, lfq.DequeueSingle());
     lfq.Clear();
     Assert.AreEqual(0,lfq.Count);
     Assert.AreEqual(0,lfq.DequeueSingle());
 }
예제 #43
0
        public void DoTestMultiWithParAndReadingThread()
        {
            const int iterations = 10000;
            var lfq = new LockFreeQueue<int>();
            _sendCounter = new CounterContainer();
            _firstTestCounter = new CounterContainer();
            _errorTestCounter = new CounterContainer();
            var ttr = new TestThreads(false, ThirdTestReader, 5);
            ttr.RunParallel(iterations, lfq, true);

            var tts = new TestThreads(false, ThirdTestSender, 5);
            tts.RunParallel(iterations, lfq);
            while (_firstTestCounter.Counter < (iterations-1) && _errorTestCounter.Counter == 0)
            {
                Thread.Sleep(10);
            }
            Assert.AreEqual(0, _errorTestCounter.Counter);
            Assert.AreEqual(iterations, _sendCounter.Counter);
            Assert.AreEqual(iterations-1, _firstTestCounter.Counter);
            tts.Terminate();
            ttr.Terminate();
        }
예제 #44
0
        public long RunParallel(int count, object param = null,bool infinite=false)
        {
            var sw = new Stopwatch();

            CyclesCounter = new CounterContainer();
            Exceptions = new LockFreeQueue<Exception>();
            Results = new LockFreeQueue<object>();
            if (count % _maxDegreeOfParallelism != 0) throw new Exception();
            var steps = count / _maxDegreeOfParallelism;
            _runningThreads = _maxDegreeOfParallelism;
            _eventStart = new ManualResetEventSlim(false);

            for (int i = 0; i < _maxDegreeOfParallelism; i++)
            {
                var from = steps * i;
                var to = steps * (i + 1);
                var thread = new Thread(RunTask);
                thread.Start(new Tuple<int, int, object,bool>(from, to, param, infinite));
                _threads.Add(thread);
            }
            sw.Start();
            _eventStart.Set();
            if (_waitForTermination)
            {
                while (CyclesCounter.Counter < count)
                {
                    Thread.Sleep(100);
                }
            }
            else
            {
                return 0;
            }
            sw.Stop();
            return sw.ElapsedMilliseconds;
        }
예제 #45
0
        public void New_TryEnqueue_Count()
        {
            var q = new LockFreeQueue<int>();
            Assert.AreEqual(0, q.Count);

            q.TryEnqueue(42);
            Assert.AreEqual(1, q.Count);
        }
예제 #46
0
 public LogAction(TimeSpan interval, LockFreeQueue<SocketStateAction> q) {
   _last_debug = DateTime.UtcNow;
   _debug_period = interval;
   _q = q;
 }
예제 #47
0
    /**
     * @param port the port to listen on
     * @param ipList an IEnumerable object of IPAddress objects.
     * @param ta_auth the TAAuthorizer to use for remote nodes
     */
    public TcpEdgeListener(int port, IEnumerable local_config_ips, TAAuthorizer ta_auth)
    {
      _is_started = 0;
      _listen_sock = new Socket(AddressFamily.InterNetwork,
                                SocketType.Stream, ProtocolType.Tcp);
      _listen_sock.LingerState = new LingerOption (true, 0);
      IPEndPoint tmp_ep = new IPEndPoint(IPAddress.Any, port);
      _listen_sock.Bind(tmp_ep);
      _local_endpoint = (IPEndPoint) _listen_sock.LocalEndPoint;
      port = _local_endpoint.Port;
      _count = 0;
      /**
       * We get all the IPAddresses for this computer
       */
      if( local_config_ips == null ) {
        _tas = TransportAddressFactory.CreateForLocalHost(TransportAddress.TAType.Tcp, port);
      }
      else {
        _tas = TransportAddressFactory.Create(TransportAddress.TAType.Tcp, port, local_config_ips);
      }

      _ta_auth = ta_auth;
      if( _ta_auth == null ) {
        //Always authorize in this case:
        _ta_auth = new ConstantAuthorizer(TAAuthorizer.Decision.Allow);
      }
      _loop = new Thread( this.SelectLoop );
      //This is how we push jobs into the SelectThread
      ActionQueue = new LockFreeQueue<SocketStateAction>();
    }
예제 #48
0
 public void New_Count()
 {
     var q = new LockFreeQueue<int>();
     Assert.AreEqual(0, q.Count);
 }
예제 #49
0
        public string[] IterateFiles(string currentPath)
        {
            List<string> foundFiles = new List<string>();
            LockFreeQueue<string> subPaths = new LockFreeQueue<string>();

            do
            {
                try
                {
                    string[] subDirectories = Directory.GetDirectories(currentPath);
                    for (int i = 0; i < subDirectories.Length; ++i) subPaths.Push(subDirectories[i]);

                    string[] subFiles = Directory.GetFiles(currentPath);
                    foundFiles.AddRange(subFiles);
                }
                catch (Exception)
                {
                    /// We don't care.
                }
            } while (subPaths.Pop(out currentPath));

            return foundFiles.ToArray();
        }
        /// <summary>
        /// Continues to get more information about recipients after all information about endpoints is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="recipientIdentities"></param>
        /// <param name="loadedEndpoints"></param>
        /// <param name="callback"></param>
        private void GetRecipientInfos(
            IUserOrGroup sender,
            bool forceRefresh,
            HashSet<string> recipientIdentities, 
            LockFreeQueue<Endpoints> loadedEndpoints,
            ParticleEndpoint particleEndpoint,
            Action<EndpointInfo> callback,
            Action<IEnumerable<string>> errorCallback,
            Action<Exception> exceptionCallback)
        {
            try
            {
                // All of the unique particle endpoints, with the recipients at each
                Dictionary<string, List<string>> recipientsAtEndpoints = new Dictionary<string, List<string>>();
                Dictionary<string, string> establishTrustEndpoints = new Dictionary<string, string>();
                Dictionary<string, string> requestedEndpoints = new Dictionary<string, string>();

                Endpoints particleEndpoints;
                while (loadedEndpoints.Dequeue(out particleEndpoints))
                {
                    string endpoint;
                    if (particleEndpoints.TryGetEndpoint(particleEndpoint, out endpoint))
                    {
                        List<string> users;
                        if (recipientsAtEndpoints.TryGetValue(particleEndpoints[ParticleEndpoint.ReceiveNotification], out users))
                            users.Add(particleEndpoints.OpenIdOrWebFinger);
                        else
                        {
                            users = new List<string>();
                            users.Add(particleEndpoints.OpenIdOrWebFinger);

                            recipientsAtEndpoints[particleEndpoints[ParticleEndpoint.ReceiveNotification]] = users;
                            establishTrustEndpoints[particleEndpoints[ParticleEndpoint.ReceiveNotification]] = particleEndpoints[ParticleEndpoint.EstablishTrust];
                            requestedEndpoints[particleEndpoints[ParticleEndpoint.ReceiveNotification]] = particleEndpoints[particleEndpoint];
                        }
                    }
                }

                if (!forceRefresh)
                {
                    // Load for situations where trust is already established
                    // copy is to avoid locked the database
                    this.persistedUserManagerData.Read(userManagerData =>
                    {
                        var recipientUser = userManagerData.GetUser(sender.Id);

                        foreach (var recipientAndToken in recipientUser.receiveNotificationEndpointsBySenderToken.Where(
                            r => recipientsAtEndpoints.ContainsKey(r.Value)))
                        {
                            var receiveNotificationEndpoint = recipientAndToken.Value;
                            var senderToken = recipientAndToken.Key;

                            string endpoint;
                            if (requestedEndpoints.TryGetValue(receiveNotificationEndpoint, out endpoint))
                            {
                                var recipientInfo = new EndpointInfo()
                                {
                                    RecipientIdentities = recipientsAtEndpoints[receiveNotificationEndpoint],
                                    Endpoint = endpoint,
                                    SenderToken = senderToken
                                };

                                recipientsAtEndpoints.Remove(receiveNotificationEndpoint);

                                callback(recipientInfo);
                            }
                        }
                    });
                }

                // For situations where trust isn't established, establish trust and then use the callback
                foreach (KeyValuePair<string, List<string>> endpointAndRecipients in recipientsAtEndpoints)
                    GetRecipientInfos(
                        sender,
                        endpointAndRecipients.Key,
                        establishTrustEndpoints[endpointAndRecipients.Key],
                        endpointAndRecipients.Value,
                        requestedEndpoints[endpointAndRecipients.Key],
                        callback,
                        errorCallback);
            }
            catch (Exception e)
            {
                exceptionCallback(e);
            }
        }
예제 #51
0
        public void New_TryEnqueue_x50_TryDequeue_x50_TryDequeue_Count()
        {
            int value;
            var q = new LockFreeQueue<int>();
            Assert.AreEqual(0, q.Count);

            for(int i = 0; i < 50; ++i) {
                q.TryEnqueue(100 - i);
            }
            Assert.AreEqual(50, q.Count);

            for(int i = 0; i < 50; ++i) {
                q.TryDequeue(out value);
                Assert.AreEqual(100 - i, value);
            }
            Assert.AreEqual(0, q.Count);

            q.TryDequeue(out value);
            Assert.AreEqual(0, q.Count);
        }
예제 #52
0
 /// <summary>
 /// 
 /// </summary>
 protected Updatable()
 {
     m_messagesQueue = new LockFreeQueue<Action>();
     m_subUpdatableObjects = new List<Updatable>();
     m_timerList = new List<UpdatableTimer>();
 }
예제 #53
0
 public CloseAction(TcpEdge e, LockFreeQueue<SocketStateAction> q) {
   _e = e;
   _queue = q;
 }
예제 #54
0
        /// <summary>
        /// Downloads all objects from the server. Used in order to fill the cache.
        /// This function should be used with XenServer up to version 6.0. For XenServer higher than 6.0, GetAllObjects() should be used instead.
        /// </summary>
        /// <param name="session">The session over which to download the objects. Must not be null.</param>
        /// <param name="changes">The queue that the ObjectChanges will be put into. Must not be null.</param>
        public static void DownloadObjects(Session session, LockFreeQueue<ObjectChange> changes)
        {
            List<ObjectChange> list = new List<ObjectChange>();

            Download_Task(session, list);
            Download_Pool(session, list);
            Download_VM(session, list);
            Download_VM_metrics(session, list);
            Download_VM_guest_metrics(session, list);
            Download_Host(session, list);
            Download_Host_crashdump(session, list);
            Download_Host_patch(session, list);
            Download_Host_metrics(session, list);
            Download_Host_cpu(session, list);
            Download_Network(session, list);
            Download_VIF(session, list);
            Download_VIF_metrics(session, list);
            Download_PIF(session, list);
            Download_PIF_metrics(session, list);
            Download_SM(session, list);
            Download_SR(session, list);
            Download_VDI(session, list);
            Download_VBD(session, list);
            Download_VBD_metrics(session, list);
            Download_PBD(session, list);
            Download_Crashdump(session, list);
            Download_Console(session, list);

            if (session.APIVersion >= API_Version.API_1_2)
            {
                // Download Miami-only objects
                Download_Pool_patch(session, list);
                Download_Bond(session, list);
                Download_VLAN(session, list);
            }

            if (session.APIVersion >= API_Version.API_1_3)
            {
                // Download Orlando-only objects
                Download_Blob(session, list);
                Download_Message(session, list);
            }

            if (session.APIVersion >= API_Version.API_1_6)
            {
                // Download George-only objects
                Download_Subject(session, list);
            }

            if (session.APIVersion >= API_Version.API_1_7)
            {
                // Download Midnight Ride-only objects
                Download_Role(session, list);
            }

            if (session.APIVersion >= API_Version.API_1_8)
            {
                // Download Cowley-only objects
                Download_VMPP(session, list);
                Download_Tunnel(session, list);
            }

            if (session.APIVersion >= API_Version.API_1_9)
            {
                // Download Boston-only objects
                Download_VM_appliance(session, list);
                Download_DR_task(session, list);
                Download_PCI(session, list);
                Download_PGPU(session, list);
                Download_GPU_group(session, list);
                Download_VGPU(session, list);
            }

            foreach (ObjectChange o in list)
            {
                changes.Enqueue(o);
            }
        }
예제 #55
0
        /// <summary>
        /// Blocks until events are sent on the session, then processes any received events and adds them
        /// to eventQueue. Will always add at least one event to eventQueue.
        /// This function should be used with XenServer up to version 6.0. For XenServer higher than 6.0, GetEvents() should be used instead.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="eventQueue"></param>
        /// <param name="cancelled"></param>
        public static void GetNextEvents(Session session, LockFreeQueue<ObjectChange> eventQueue, HTTP.FuncBool cancelled)
        {
            Proxy_Event[] proxyEvents;

            try
            {
                proxyEvents = Event.next(session);
            }
            catch (WebException e)
            {
                // Catch timeout, and turn it into an EventNextBlockedException so we can recognise it later (CA-33145)
                if (e.Status == WebExceptionStatus.Timeout)
                    throw new EventNextBlockedException();
                else
                    throw;
            }

            if (proxyEvents.Length == 0)
                throw new IOException("Event.next() returned no events; the server is misbehaving.");

            if (cancelled())
                return;

            //We want to do the marshalling on this bg thread so as not to block the gui thread
            foreach (Proxy_Event proxyEvent in proxyEvents)
            {
                ObjectChange objectChange = ProcessEvent(proxyEvent);

                if (objectChange != null)
                    eventQueue.Enqueue(objectChange);
            }
        }
예제 #56
0
        static void Main(string[] args)
        {
            Console.Write(@"Test Lock-free ");
            if (testObject == TestObject.Pool)
            {
                Console.WriteLine("Pool");
                testPool = true;
            }
            else if (testObject == TestObject.FastPool)
            {
                Console.WriteLine("Fast Pool");
                testPool = true;
            }
            else if (testObject == TestObject.StackQueue)
            {
                Console.WriteLine("Stack & Queue (Not Pool)");
                testPool = false;
            }
            else
                throw new NotImplementedException();

            Console.WriteLine(@"ThreadPool Test Starting...");

            Console.WriteLine(@"  All threads: {0}", threads);
            Console.WriteLine(@"  Buffers per thread: {0}", actions);
            Console.WriteLine(@"  Buffer pool size: {0}", poolSize);

            Console.WriteLine();

            if (testPool)
            {
                if (testObject == TestObject.Pool)
                    pool = new LockFreePool<Item>(poolSize);
                if (testObject == TestObject.FastPool)
                    pool = new LockFreeFastPool<Item>(poolSize);
            }
            else
            {
                array = new LockFreeItem<Item>[poolSize + 1];
                for (int i = 1; i < array.Length; i++)
                    array[i].Value = new Item();

                queue = new LockFreeQueue<Item>(array, 0, poolSize + 1);
                stack = new LockFreeStack<Item>(array, -1, -1);
            }

            items = new bool[65536];

            run = true;
            for (int i = 0; i < threads; i++)
            {
                var thread = new Thread(TestBufferPool);
                thread.Start();
            }

            Console.WriteLine(@"Started. Press any key to stop...");
            Console.ReadKey(true);

            run = false;

            while (count > 0)
                Thread.Sleep(25);
        }
        // TODO
        // A lot of the logic in this file that's not tied to the DB should move someplace else so that independent implementations can use it
        /// <summary>
        /// Gets information about recipients for sending a notification
        /// </summary>
        /// <param name="openIdOrWebFinger"></param>
        /// <param name="forceRefresh"></param>
        /// <returns></returns>
        public void GetEndpointInfos(
            IUserOrGroup sender,
            bool forceRefresh, 
            IEnumerable<string> recipientIdentitiesArg,
            ParticleEndpoint particleEndpoint,
            Action<EndpointInfo> callback,
            Action<IEnumerable<string>> errorCallback,
            Action<Exception> exceptionCallback)
        {
            HashSet<string> recipientIdentities = new HashSet<string>(recipientIdentitiesArg);

            long outstandingRequests = recipientIdentities.Count;

            LockFreeQueue<Endpoints> loadedEndpoints = new LockFreeQueue<Endpoints>();

            Action<Endpoints> endpointLoaded = delegate(Endpoints endpoints)
            {
                loadedEndpoints.Enqueue(endpoints);

                if (0 == Interlocked.Decrement(ref outstandingRequests))
                    GetRecipientInfos(sender, forceRefresh, recipientIdentities, loadedEndpoints, particleEndpoint, callback, errorCallback, exceptionCallback);
            };

            Action<Exception> endpointException = delegate(Exception e)
            {
                if (0 == Interlocked.Decrement(ref outstandingRequests))
                    GetRecipientInfos(sender, forceRefresh, recipientIdentities, loadedEndpoints, particleEndpoint, callback, errorCallback, exceptionCallback);
            };

            foreach (string openIdOrWebFinger in recipientIdentities)
                Endpoints.GetEndpoints(openIdOrWebFinger, forceRefresh, endpointLoaded, endpointException);
        }
예제 #58
0
        /// <summary>
        /// Blocks until events are sent on the session, or timeout is reached, then processes any received events and adds them
        /// to eventQueue. This function implements the new event system, available in API version 1.9. 
        /// In the new event system, (GetAllRecords + GetEvents) sequence will replace (RegisterForEvents + DownloadObjects + GetNextEvents).
        /// </summary>
        /// <param name="session"></param>
        /// <param name="eventQueue"></param>
        /// <param name="cancelled"></param>
        /// <param name="legacyEventSystem">True if legacy event system (event.next) should be used.</param>
        /// <param name="token">A token used by event.from(). 
        /// It should be the empty string when event.from is first called, which is the replacement of get_all_records.
        /// </param>
        public static void GetEvents(Session session, LockFreeQueue<ObjectChange> eventQueue, HTTP.FuncBool cancelled, bool legacyEventSystem, ref string token)
        {
            if (legacyEventSystem)
            {
                GetNextEvents(session, eventQueue, cancelled);
                return;
            }

            Proxy_Event[] proxyEvents;
            try
            {
                var classes = new [] { "*" }; // classes that we are interested in receiving events from
                var eventResult = Event.from(session, classes, token, EVENT_FROM_TIMEOUT);
                token = eventResult.token;
                proxyEvents = eventResult.events;
            }
            catch (WebException e)
            {
                // Catch timeout, and turn it into an EventNextBlockedException so we can recognise it later (CA-33145)
                if (e.Status == WebExceptionStatus.Timeout)
                    throw new EventNextBlockedException();
                else
                    throw;
            }

            if (cancelled())
                return;

            //We want to do the marshalling on this bg thread so as not to block the gui thread
            foreach (Proxy_Event proxyEvent in proxyEvents)
            {
                ObjectChange objectChange = ProcessEvent(proxyEvent);

                if (objectChange != null)
                    eventQueue.Enqueue(objectChange);
            }
        }
예제 #59
0
 static CachedPictureBox()
 {
     cachedImages = new Dictionary<string, Image>();
     cacheQueue = new LockFreeQueue<CachedPictureBox>();
 }
예제 #60
0
        /// <sumary>
        /// Gets all objects from the server. Used in order to fill the cache.
        /// This function implements the new event system, available from in API version 1.9.
        /// In the new event system, (GetAllRecords + GetEvents) sequence will replace (RegisterForEvents + DownloadObjects + GetNextEvents).
        /// </summary>
        /// <param name="session">The session over which to download the objects. Must not be null.</param>
        /// <param name="changes">The queue that the ObjectChanges will be put into. Must not be null.</param>
        /// <param name="cancelled">Used by GetEvents().</param>
        /// <param name="legacyEventSystem">True if legacy event system (event.next) should to be used.</param>
        /// <param name="token">Used by GetEvents().</param>
        public static void GetAllObjects(Session session, LockFreeQueue<ObjectChange> changes, HTTP.FuncBool cancelled, bool legacyEventSystem, ref string token)
        {
            if (legacyEventSystem)
            {
                DownloadObjects(session, changes);
                return;
            }

            // download objects that are not covered by event.from(), e.g. Roles
            List<ObjectChange> list = new List<ObjectChange>();
            Download_Role(session, list);
            foreach (ObjectChange o in list)
                changes.Enqueue(o);

            // get all objects with event.from()
            token = "";
            GetEvents(session, changes, cancelled, false, ref token);
        }