Exemplo n.º 1
0
        public DBObjectPool(DBObjectPoolControl ctrl)
        {
            _state = State.Initializing;
            _ctrl  = ctrl;
#if USECOUNTEROBJECT
            _poolCounter = new Counter();
#endif //USECOUNTEROBJECT
            _stackOld    = new InterlockedStack();
            _stackNew    = new InterlockedStack();
            _waitHandles = new ObjectPoolWaitHandle[3];
            _waitHandles[SEMAPHORE_HANDLE] = CreateWaitHandle(SafeNativeMethods.CreateSemaphore(IntPtr.Zero, 0, MAX_Q_SIZE, IntPtr.Zero), false);
            _waitHandles[ERROR_HANDLE]     = CreateWaitHandle(SafeNativeMethods.CreateEvent(IntPtr.Zero, 1, 0, IntPtr.Zero), false);
            _creationMutex = new Mutex();
            _waitHandles[CREATION_HANDLE] = CreateWaitHandle(_creationMutex.Handle, true);
            _errorWait   = ERROR_WAIT_DEFAULT;
            _cleanupWait = 0;    // Set in CreateCleanupTimer
            _errorTimer  = null; // No error yet.
            _objectList  = new ArrayList(_ctrl.MaxPool);

            if (ctrl.TransactionAffinity)
            {
                OperatingSystem osversion = Environment.OSVersion;

                if (PlatformID.Win32NT == osversion.Platform && 5 <= osversion.Version.Major)    // TODO: create an ADP.IsPlatformNT5 function?
                {
                    _txPool = CreateResourcePool();
                }
            }

            _cleanupTimer = CreateCleanupTimer();
            _state        = State.Running;

            // PerfCounters - this counter will never be decremented!
            IncrementPoolCount();

            // Make sure we're at quota by posting a callback to the threadpool.
            ThreadPool.QueueUserWorkItem(new WaitCallback(PoolCreateRequest));
        }
        public DBObjectPool FindOrCreatePool(DBObjectPoolControl ctrl)
        {
            Debug.Assert(ctrl != null, "Unexpected DefaultPoolControl null!");

            String       poolKey = ctrl.Key;
            DBObjectPool pool    = FindPool(poolKey);

            if (pool == null)
            {
                lock (_map.SyncRoot) {
                    // Did somebody else created it while we were waiting?
                    pool = FindPool(poolKey);

                    if (pool == null)
                    {
                        // Create a new pool, shove it into the map:
                        pool          = new DBObjectPool(ctrl);
                        _map[poolKey] = pool;
                    }
                }
            }

            return(pool);
        }