Example #1
0
        protected void FloodFillAreas(Node rootNode, int area)
        {
            var list = PoolQueue <Node> .Spawn(this.nodes.Count);

            list.Enqueue(rootNode);
            while (list.Count > 0)
            {
                var node = list.Dequeue();

                var connections = node.GetConnections();
                for (int j = 0; j < connections.Length; ++j)
                {
                    var connection = connections[j];
                    if (connection.index >= 0)
                    {
                        var nb = this.nodes[connection.index];
                        if (nb.area == 0 && nb.walkable == true)
                        {
                            nb.area = area;
                            //this.FloodFillAreas(nb, area);
                            list.Enqueue(nb);
                        }
                    }
                }
            }

            PoolQueue <Node> .Recycle(ref list);
        }
Example #2
0
        protected ListCopyable <Node> AstarSearch(Graph graph, ListCopyable <Node> visited, Node startNode, Node endNode, Constraint constraint, int threadIndex)
        {
            var openList = PoolQueue <Node> .Spawn(10);

            startNode.startToCurNodeLen[threadIndex] = 0f;

            openList.Enqueue(startNode);
            startNode.isOpened[threadIndex] = true;

            while (openList.Count > 0)
            {
                var node = openList.Dequeue();
                node.isClosed[threadIndex] = true;

                visited.Add(node);

                if (node.index == endNode.index)
                {
                    PoolQueue <Node> .Recycle(ref openList);

                    return(this.RetracePath(threadIndex, endNode));
                }

                var neighbors = node.GetConnections();
                foreach (var conn in neighbors)
                {
                    if (conn.index < 0)
                    {
                        continue;
                    }

                    var neighbor = graph.nodes[conn.index];
                    if (neighbor.isClosed[threadIndex] == true)
                    {
                        continue;
                    }
                    if (neighbor.IsSuitable(constraint) == false)
                    {
                        continue;
                    }

                    float ng = node.startToCurNodeLen[threadIndex] + conn.cost;
                    if (neighbor.isOpened[threadIndex] == false || ng < neighbor.startToCurNodeLen[threadIndex])
                    {
                        neighbor.startToCurNodeLen[threadIndex] = ng;
                        neighbor.parent[threadIndex]            = node;
                        if (neighbor.isOpened[threadIndex] == false)
                        {
                            openList.Enqueue(neighbor);
                            visited.Add(neighbor);
                            neighbor.isOpened[threadIndex] = true;
                        }
                    }
                }
            }

            PoolQueue <Node> .Recycle(ref openList);

            return(null);
        }
 void Awake(){
     if(useAsSingleton) Instance = this;
     if(poolDictionary == null) poolDictionary = new Dictionary<string, PoolQueue<GameObject>>();
     else return;
     
     for(int i = 0; i < pools.Count; i++)
     {
         Pool pool = pools[i];
         PoolQueue<GameObject> objectPool = new PoolQueue<GameObject>();
         
         for(int j = 0;j<pool.size; j++)
         {
             GameObject obj = Instantiate(pool.prefab, gameObject.transform);
             obj.SetActive(false);
             
             MonoBehaviourPooledObject PooledObjectScript = obj.GetComponent<MonoBehaviourPooledObject>();
             
             if(PooledObjectScript != null) {
                 PooledObjectScript.SetQueue(objectPool);
             }
             
             objectPool.Enqueue(obj);
         }
         
         poolDictionary.Add(pool.tag, objectPool);
         objectPool.poolParent = pool;
     }
     
 }
Example #4
0
    void Awake()
    {
        if (poolQueue == null)
        {
            poolQueue = new PoolQueue <GameObject>();
        }
        else
        {
            return;
        }

        for (int j = 0; j < pool.size; j++)
        {
            GameObject obj = Instantiate(pool.prefab, gameObject.transform);
            obj.SetActive(false);

            MonoBehaviourPooledObject PooledObjectScript = obj.GetComponent <MonoBehaviourPooledObject>();

            if (PooledObjectScript != null)
            {
                PooledObjectScript.SetQueue(poolQueue);
            }

            poolQueue.Enqueue(obj);
        }

        poolQueue.poolParent = pool;
    }
Example #5
0
    public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation)
    {
        if (!poolDictionary.ContainsKey(tag))
        {
            Debug.LogWarning("Pool with tag " + tag + " doesn't exist.");
            return(null);
        }

        PoolQueue <GameObject> poolQueue = poolDictionary[tag];
        GameObject             objectToSpawn;

        if (poolQueue.Count > 0)
        {
            objectToSpawn = (GameObject)poolQueue.Dequeue();
        }
        else
        {
            objectToSpawn = CreateObjectBlank(poolQueue.poolParent);
        }

        IPooledObject ObjectInterface = objectToSpawn.GetComponent <IPooledObject>(); //Get rid of GetComponent call.

        if (ObjectInterface != null)
        {
            ObjectInterface.OnObjectSpawn();
        }

        objectToSpawn.transform.position = position;
        objectToSpawn.transform.rotation = rotation;
        objectToSpawn.SetActive(true);

        return(objectToSpawn);
    }
Example #6
0
        private void DoTestLoad(Taste.Recommender.Recommender recommender, int allowedTimeSec)
        {
            // TODO: how to specify number of threads
            PoolQueue queue = new PoolQueue(new DefaultThreadPool(), new CommandExecutor());

            Command cmd = delegate
            {
                for (int i = 0; i < NUM_USERS; i++)
                {
                    string id = random.Next(NUM_USERS).ToString();
                    recommender.Recommend(id, 10);
                    if (i % 100 == 50)
                    {
                        recommender.Refresh();
                    }
                }
            };

            long start = DateTime.Now.Ticks;

            for (int i = 0; i < NUM_THREADS; i++)
            {
                queue.Enqueue(cmd);
            }
            long end = DateTime.Now.Ticks;

            double timeMS = TimeSpan.FromTicks(end - start).TotalMilliseconds;

            log.Info("Load test completed in " + timeMS + "ms");
            Assert.IsTrue(timeMS < 1000L * (long)allowedTimeSec);
        }
Example #7
0
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// We do not have to thread-lock anything in this function, because it
        /// is only called by other functions above which already take the lock.
        /// </summary>
        /// <param name="queue">
        /// The pool queue to resize.
        /// </param>
        /// <param name="add">
        /// If a function intends to add to the pool, this is true, which
        /// forces the resize to take one more than it needs from the pool.
        /// </param>
        private static void ResizePool(
            PoolQueue queue,
            bool add
            )
        {
            int target = queue.MaxPoolSize;

            if (add && target > 0)
            {
                target--;
            }

            Queue <WeakReference> poolQueue = queue.Queue;

            if (poolQueue == null)
            {
                return;
            }

            while (poolQueue.Count > target)
            {
                WeakReference connection = poolQueue.Dequeue();

                if (connection == null)
                {
                    continue;
                }

                SQLiteConnectionHandle handle =
                    connection.Target as SQLiteConnectionHandle;

                if (handle != null)
                {
                    handle.Dispose();
                }

                GC.KeepAlive(handle);
            }
        }
Example #8
0
        private VerifyCenter(int maxTask)
        {
            try
            {
                MaxThreads = maxTask;

                this.maxAccepted = new Semaphore(MaxThreads, MaxThreads);

                tmpRequest = new Dictionary <string, Action <VerifyResult> >();

                evtHandlers = new PoolQueue <VerifyProcess>(MaxThreads + 2);
                for (int i = 0; i < MaxThreads; i++)
                {
                    VerifyProcess verTask = new VerifyProcess();
                    verTask.OnCompleted += verTask_OnCompleted;
                    evtHandlers.Enqueue(verTask);
                }
            }
            catch (Exception exp)
            {
                Logger.AddLog(this.GetType(), "VerifyCenter", exp.Message);
            }
        }
Example #9
0
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Removes a connection from the pool of those associated with the
        /// specified database file name with the intent of using it to
        /// interact with the database.
        /// </summary>
        /// <param name="fileName">
        /// The database file name.
        /// </param>
        /// <param name="maxPoolSize">
        /// The new maximum size of the connection pool for the specified
        /// database file name.
        /// </param>
        /// <param name="version">
        /// The connection pool version associated with the returned database
        /// connection handle, if any.
        /// </param>
        /// <returns>
        /// The database connection handle associated with the specified
        /// database file name or null if it cannot be obtained.
        /// </returns>
        internal static SQLiteConnectionHandle Remove(
            string fileName,
            int maxPoolSize,
            out int version
            )
        {
            ISQLiteConnectionPool connectionPool = GetConnectionPool();

            if (connectionPool != null)
            {
                return(connectionPool.Remove(fileName, maxPoolSize,
                                             out version) as SQLiteConnectionHandle);
            }
            else
            {
                int localVersion;
                Queue <WeakReference> poolQueue;

                //
                // NOTE: This lock cannot be held while checking the queue for
                //       available connections because other methods of this
                //       class are called from the GC finalizer thread and we
                //       use the WaitForPendingFinalizers method (below).
                //       Holding this lock while calling that method would
                //       therefore result in a deadlock.  Instead, this lock
                //       is held only while a temporary copy of the queue is
                //       created, and if necessary, when committing changes
                //       back to that original queue prior to returning from
                //       this method.
                //
                lock (_syncRoot)
                {
                    PoolQueue queue;

                    //
                    // NOTE: Default to the highest pool version.
                    //
                    version = _poolVersion;

                    //
                    // NOTE: If we didn't find a pool for this file, create one
                    //       even though it will be empty.  We have to do this
                    //       here because otherwise calling ClearPool() on the
                    //       file will not work for active connections that have
                    //       never seen the pool yet.
                    //
                    if (!_queueList.TryGetValue(fileName, out queue))
                    {
                        queue = new PoolQueue(_poolVersion, maxPoolSize);
                        _queueList.Add(fileName, queue);

                        return(null);
                    }

                    //
                    // NOTE: We found a pool for this file, so use its version
                    //       number.
                    //
                    version           = localVersion = queue.PoolVersion;
                    queue.MaxPoolSize = maxPoolSize;

                    //
                    // NOTE: Now, resize the pool to the new maximum size, if
                    //       necessary.
                    //
                    ResizePool(queue, false);

                    //
                    // NOTE: Try and get a pooled connection from the queue.
                    //
                    poolQueue = queue.Queue;
                    if (poolQueue == null)
                    {
                        return(null);
                    }

                    //
                    // NOTE: Temporarily tranfer the queue for this file into
                    //       a local variable.  The queue for this file will
                    //       be modified and then committed back to the real
                    //       pool list (below) prior to returning from this
                    //       method.
                    //
                    _queueList.Remove(fileName);
                    poolQueue = new Queue <WeakReference>(poolQueue);
                }

                try
                {
                    while (poolQueue.Count > 0)
                    {
                        WeakReference connection = poolQueue.Dequeue();

                        if (connection == null)
                        {
                            continue;
                        }

                        SQLiteConnectionHandle handle =
                            connection.Target as SQLiteConnectionHandle;

                        if (handle == null)
                        {
                            continue;
                        }

                        //
                        // BUGFIX: For ticket [996d13cd87], step #1.  After
                        //         this point, make sure that the finalizer for
                        //         the connection handle just obtained from the
                        //         queue cannot START running (i.e. it may
                        //         still be pending but it will no longer start
                        //         after this point).
                        //
                        GC.SuppressFinalize(handle);

                        try
                        {
                            //
                            // BUGFIX: For ticket [996d13cd87], step #2.  Now,
                            //         we must wait for all pending finalizers
                            //         which have STARTED running and have not
                            //         yet COMPLETED.  This must be done just
                            //         in case the finalizer for the connection
                            //         handle just obtained from the queue has
                            //         STARTED running at some point before
                            //         SuppressFinalize was called on it.
                            //
                            //         After this point, checking properties of
                            //         the connection handle (e.g. IsClosed)
                            //         should work reliably without having to
                            //         worry that they will (due to the
                            //         finalizer) change out from under us.
                            //
                            GC.WaitForPendingFinalizers();

                            //
                            // BUGFIX: For ticket [996d13cd87], step #3.  Next,
                            //         verify that the connection handle is
                            //         actually valid and [still?] not closed
                            //         prior to actually returning it to our
                            //         caller.
                            //
                            if (!handle.IsInvalid && !handle.IsClosed)
                            {
                                Interlocked.Increment(ref _poolOpened);
                                return(handle);
                            }
                        }
                        finally
                        {
                            //
                            // BUGFIX: For ticket [996d13cd87], step #4.  Next,
                            //         we must re-register the connection
                            //         handle for finalization now that we have
                            //         a strong reference to it (i.e. the
                            //         finalizer will not run at least until
                            //         the connection is subsequently closed).
                            //
                            GC.ReRegisterForFinalize(handle);
                        }

                        GC.KeepAlive(handle);
                    }
                }
                finally
                {
                    //
                    // BUGFIX: For ticket [996d13cd87], step #5.  Finally,
                    //         commit any changes to the pool/queue for this
                    //         database file.
                    //
                    lock (_syncRoot)
                    {
                        //
                        // NOTE: We must check [again] if a pool exists for
                        //       this file because one may have been added
                        //       while the search for an available connection
                        //       was in progress (above).
                        //
                        PoolQueue             queue;
                        Queue <WeakReference> newPoolQueue;
                        bool addPool;

                        if (_queueList.TryGetValue(fileName, out queue))
                        {
                            addPool = false;
                        }
                        else
                        {
                            addPool = true;
                            queue   = new PoolQueue(localVersion, maxPoolSize);
                        }

                        newPoolQueue = queue.Queue;

                        while (poolQueue.Count > 0)
                        {
                            newPoolQueue.Enqueue(poolQueue.Dequeue());
                        }

                        ResizePool(queue, false);

                        if (addPool)
                        {
                            _queueList.Add(fileName, queue);
                        }
                    }
                }

                return(null);
            }
        }
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// We do not have to thread-lock anything in this function, because it
        /// is only called by other functions above which already take the lock.
        /// </summary>
        /// <param name="queue">
        /// The pool queue to resize.
        /// </param>
        /// <param name="add">
        /// If a function intends to add to the pool, this is true, which
        /// forces the resize to take one more than it needs from the pool.
        /// </param>
        private static void ResizePool(
            PoolQueue queue,
            bool add
            )
        {
            int target = queue.MaxPoolSize;

            if (add && target > 0) target--;

            Queue<WeakReference> poolQueue = queue.Queue;
            if (poolQueue == null) return;

            while (poolQueue.Count > target)
            {
                WeakReference connection = poolQueue.Dequeue();

                if (connection == null) continue;

                SQLiteConnectionHandle handle =
                    connection.Target as SQLiteConnectionHandle;

                if (handle != null)
                    handle.Dispose();

                GC.KeepAlive(handle);
            }
        }
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Removes a connection from the pool of those associated with the
        /// specified database file name with the intent of using it to
        /// interact with the database.
        /// </summary>
        /// <param name="fileName">
        /// The database file name.
        /// </param>
        /// <param name="maxPoolSize">
        /// The new maximum size of the connection pool for the specified
        /// database file name.
        /// </param>
        /// <param name="version">
        /// The connection pool version associated with the returned database
        /// connection handle, if any.
        /// </param>
        /// <returns>
        /// The database connection handle associated with the specified
        /// database file name or null if it cannot be obtained.
        /// </returns>
        internal static SQLiteConnectionHandle Remove(
            string fileName,
            int maxPoolSize,
            out int version
            )
        {
            ISQLiteConnectionPool connectionPool = GetConnectionPool();

            if (connectionPool != null)
            {
                return connectionPool.Remove(fileName, maxPoolSize,
                    out version) as SQLiteConnectionHandle;
            }
            else
            {
                int localVersion;
                Queue<WeakReference> poolQueue;

                //
                // NOTE: This lock cannot be held while checking the queue for
                //       available connections because other methods of this
                //       class are called from the GC finalizer thread and we
                //       use the WaitForPendingFinalizers method (below).
                //       Holding this lock while calling that method would
                //       therefore result in a deadlock.  Instead, this lock
                //       is held only while a temporary copy of the queue is
                //       created, and if necessary, when committing changes
                //       back to that original queue prior to returning from
                //       this method.
                //
                lock (_syncRoot)
                {
                    PoolQueue queue;

                    //
                    // NOTE: Default to the highest pool version.
                    //
                    version = _poolVersion;

                    //
                    // NOTE: If we didn't find a pool for this file, create one
                    //       even though it will be empty.  We have to do this
                    //       here because otherwise calling ClearPool() on the
                    //       file will not work for active connections that have
                    //       never seen the pool yet.
                    //
                    if (!_queueList.TryGetValue(fileName, out queue))
                    {
                        queue = new PoolQueue(_poolVersion, maxPoolSize);
                        _queueList.Add(fileName, queue);

                        return null;
                    }

                    //
                    // NOTE: We found a pool for this file, so use its version
                    //       number.
                    //
                    version = localVersion = queue.PoolVersion;
                    queue.MaxPoolSize = maxPoolSize;

                    //
                    // NOTE: Now, resize the pool to the new maximum size, if
                    //       necessary.
                    //
                    ResizePool(queue, false);

                    //
                    // NOTE: Try and get a pooled connection from the queue.
                    //
                    poolQueue = queue.Queue;
                    if (poolQueue == null) return null;

                    //
                    // NOTE: Temporarily tranfer the queue for this file into
                    //       a local variable.  The queue for this file will
                    //       be modified and then committed back to the real
                    //       pool list (below) prior to returning from this
                    //       method.
                    //
                    _queueList.Remove(fileName);
                    poolQueue = new Queue<WeakReference>(poolQueue);
                }

                try
                {
                    while (poolQueue.Count > 0)
                    {
                        WeakReference connection = poolQueue.Dequeue();

                        if (connection == null) continue;

                        SQLiteConnectionHandle handle =
                            connection.Target as SQLiteConnectionHandle;

                        if (handle == null) continue;

                        //
                        // BUGFIX: For ticket [996d13cd87], step #1.  After
                        //         this point, make sure that the finalizer for
                        //         the connection handle just obtained from the
                        //         queue cannot START running (i.e. it may
                        //         still be pending but it will no longer start
                        //         after this point).
                        //
                        GC.SuppressFinalize(handle);

                        try
                        {
                            //
                            // BUGFIX: For ticket [996d13cd87], step #2.  Now,
                            //         we must wait for all pending finalizers
                            //         which have STARTED running and have not
                            //         yet COMPLETED.  This must be done just
                            //         in case the finalizer for the connection
                            //         handle just obtained from the queue has
                            //         STARTED running at some point before
                            //         SuppressFinalize was called on it.
                            //
                            //         After this point, checking properties of
                            //         the connection handle (e.g. IsClosed)
                            //         should work reliably without having to
                            //         worry that they will (due to the
                            //         finalizer) change out from under us.
                            //
                            GC.WaitForPendingFinalizers();

                            //
                            // BUGFIX: For ticket [996d13cd87], step #3.  Next,
                            //         verify that the connection handle is
                            //         actually valid and [still?] not closed
                            //         prior to actually returning it to our
                            //         caller.
                            //
                            if (!handle.IsInvalid && !handle.IsClosed)
                            {
                                Interlocked.Increment(ref _poolOpened);
                                return handle;
                            }
                        }
                        finally
                        {
                            //
                            // BUGFIX: For ticket [996d13cd87], step #4.  Next,
                            //         we must re-register the connection
                            //         handle for finalization now that we have
                            //         a strong reference to it (i.e. the
                            //         finalizer will not run at least until
                            //         the connection is subsequently closed).
                            //
                            GC.ReRegisterForFinalize(handle);
                        }

                        GC.KeepAlive(handle);
                    }
                }
                finally
                {
                    //
                    // BUGFIX: For ticket [996d13cd87], step #5.  Finally,
                    //         commit any changes to the pool/queue for this
                    //         database file.
                    //
                    lock (_syncRoot)
                    {
                        //
                        // NOTE: We must check [again] if a pool exists for
                        //       this file because one may have been added
                        //       while the search for an available connection
                        //       was in progress (above).
                        //
                        PoolQueue queue;
                        Queue<WeakReference> newPoolQueue;
                        bool addPool;

                        if (_queueList.TryGetValue(fileName, out queue))
                        {
                            addPool = false;
                        }
                        else
                        {
                            addPool = true;
                            queue = new PoolQueue(localVersion, maxPoolSize);
                        }

                        newPoolQueue = queue.Queue;

                        while (poolQueue.Count > 0)
                            newPoolQueue.Enqueue(poolQueue.Dequeue());

                        ResizePool(queue, false);

                        if (addPool)
                            _queueList.Add(fileName, queue);
                    }
                }

                return null;
            }
        }
Example #12
0
        private void DoTestLoad(Taste.Recommender.Recommender recommender, int allowedTimeSec)
	    {
           
            // TODO: how to specify number of threads
            PoolQueue queue = new PoolQueue(new DefaultThreadPool(), new CommandExecutor());
  		   
            Command cmd = delegate
            {
                for (int i = 0; i < NUM_USERS; i++)
                {
                    string id = random.Next(NUM_USERS).ToString();
                    recommender.Recommend(id, 10);
                    if (i % 100 == 50)
                    {
                        recommender.Refresh();
                    }
                }
            };

		    long start = DateTime.Now.Ticks;
		    for (int i = 0; i < NUM_THREADS; i++) 
		    {
                queue.Enqueue(cmd);			    
		    }
		    long end = DateTime.Now.Ticks;	

		    double timeMS = TimeSpan.FromTicks(end - start).TotalMilliseconds;
		    log.Info("Load test completed in " + timeMS + "ms");
		    Assert.IsTrue(timeMS < 1000L * (long) allowedTimeSec);
	    }
 public void SetQueue(PoolQueue <GameObject> queue)
 {
     queueBelongsTo = queue;
 }