コード例 #1
0
ファイル: EntitySet.cs プロジェクト: sunwei3126/CodexMicroORM
        public bool AddWrappedItem(object o, bool allowLinking = true)
        {
            if (o is T cast)
            {
                if (!_contains.Contains(cast))
                {
                    WriterLock wl = null;
                    var        wasEnableLinked = EnableLinking;

                    try
                    {
                        if (!allowLinking && EnableLinking)
                        {
                            wl            = new WriterLock(_lock);
                            EnableLinking = false;
                        }

                        this.Add(cast);
                    }
                    finally
                    {
                        if (wl != null)
                        {
                            EnableLinking = wasEnableLinked;
                            wl.Dispose();
                        }
                    }

                    return(true);
                }
            }

            return(false);
        }
コード例 #2
0
        public IDisposable Write()
        {
            var locker = new WriterLock(this);

            lock (_lockers) _lockers.Add(locker);
            return(locker);
        }
コード例 #3
0
 public void ClearListeners()
 {
     using (WriterLock wl = new WriterLock(listenerRWLock)) {
         listenerEndPoints.Clear();
         doSendDataValues = false;
     }
 }
コード例 #4
0
        public void RemoveListener(IPEndPoint ep)
        {
            // acquire a write lock
            using (WriterLock wl = new WriterLock(listenerRWLock)) {
                // add the end point if it doesn't exists already
                listenerEndPoints.Remove(ep);

                doSendDataValues = listenerEndPoints.Count > 0;
            }
        }
コード例 #5
0
        public void RegisterListener(IPEndPoint ep)
        {
            // acquire a write lock
            using (WriterLock wl = new WriterLock(listenerRWLock)) {
                // add the end point if it doesn't exists already
                if (!listenerEndPoints.Contains(ep))
                {
                    listenerEndPoints.Add(ep);
                }

                // indicate that we're now sending value
                doSendDataValues = true;
            }
        }
        public void WriterTimedOutIsSetIfTimeoutTimespan()
        {
            ReaderWriterLock rwLock = new ReaderWriterLock();
            Thread           t      = new Thread(new ParameterizedThreadStart(AcquireReaderLock));

            t.Start(rwLock);

            Thread.Sleep(20);

            using (WriterLock l = new WriterLock(rwLock, TimeSpan.FromMilliseconds(10)))
            {
                Assert.IsTrue(l.TimedOut);
            }

            Assert.IsFalse(rwLock.IsWriterLockHeld);
        }
コード例 #7
0
        public static void Animate(Gdi::Image image, System.EventHandler onFrameChangedHandler)
        {
            if (image == null)
            {
                return;
            }

            ImageInfo item = null;

            lock (image) item = new ImageInfo(image);

            StopAnimate(image, onFrameChangedHandler);

            WriterLock w_lock;

            using (new WriterLockWait())
                w_lock = new WriterLock(rwImgListLock);

            using (w_lock){
                if (!item.animated)
                {
                    return;
                }

                if (imageInfoList == null)
                {
                    imageInfoList = new Gen::List <ImageInfo>();
                }
                item.onFrameChangedHandler = onFrameChangedHandler;
                imageInfoList.Add(item);
                if (animationThread == null)
                {
                    animationThread              = new Thr::Thread(new Thr::ThreadStart(ImageAnimator.AnimateImages50ms));
                    animationThread.Name         = typeof(ImageAnimator).Name;
                    animationThread.IsBackground = true;
                    animationThread.Start();
                }
            }
        }
コード例 #8
0
        public static void StopAnimate(Gdi::Image image, System.EventHandler onFrameChangedHandler)
        {
            if (image == null || imageInfoList == null)
            {
                return;
            }

            WriterLock w_lock;

            using (new WriterLockWait())
                w_lock = new WriterLock(rwImgListLock);

            using (w_lock){
                ImageInfo item = imageInfoList.Find(delegate(ImageInfo info){ return(image == info.Image); });
                if (item == null)
                {
                    return;
                }
                if (onFrameChangedHandler == item.onFrameChangedHandler || onFrameChangedHandler != null && onFrameChangedHandler.Equals(item.onFrameChangedHandler))
                {
                    imageInfoList.Remove(item);
                }
            }
        }
コード例 #9
0
        public virtual bool SetValue(string propName, object value, Type preferredType = null, bool isRequired = false)
        {
            using (var wl = new WriterLock(_lock))
            {
                if (preferredType != null)
                {
                    if (preferredType.IsValueType && !isRequired && !(preferredType.IsGenericType && preferredType.GetGenericTypeDefinition() == typeof(Nullable <>)))
                    {
                        preferredType = typeof(Nullable <>).MakeGenericType(preferredType);
                    }

                    _preferredType[propName] = preferredType;
                }

                if (_preferredType.TryGetValue(propName, out Type pt))
                {
                    try
                    {
                        bool isnull = pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable <>);

                        if (value != null)
                        {
                            if (value.GetType() != pt && (!isnull || value.GetType() != Nullable.GetUnderlyingType(pt)))
                            {
                                if (value is IConvertible)
                                {
                                    if (isnull)
                                    {
                                        value = InternalChangeType(value, Nullable.GetUnderlyingType(pt));
                                    }
                                    else
                                    {
                                        value = InternalChangeType(value, pt);
                                    }
                                }
                                else
                                {
                                    throw new InvalidCastException("Cannot coerce type.");
                                }
                            }
                        }
                        else
                        {
                            if (isnull)
                            {
                                value = pt.FastCreateNoParm();
                            }
                        }
                    }
                    catch
                    {
                    }
                }

                // Special case - if setting a property where there exists a shadow property already AND the new prop is NOT default - blow away the shadow property!
                if (value != null && _shadowPropCount > 0 && propName[0] != KeyService.SHADOW_PROP_PREFIX && _valueBag.ContainsKey(KeyService.SHADOW_PROP_PREFIX + propName))
                {
                    var defVal = WrappingHelper.GetDefaultForType(value.GetType());

                    if (!defVal.IsSame(value))
                    {
                        var k = KeyService.SHADOW_PROP_PREFIX + propName;

                        if (_valueBag.ContainsKey(k))
                        {
                            _valueBag.Remove(k);
                            _shadowPropCount--;
                        }
                    }
                }

                wl.Release();

                if (_source != null)
                {
                    var info = _source.FastGetAllProperties(true, true, propName);

                    if (info.Any())
                    {
                        wl.Reacquire();

                        var oldVal = _source.FastGetValue(propName);

                        if (!oldVal.IsSame(value))
                        {
                            _source.FastSetValue(propName, InternalChangeType(value, info.First().type));

                            wl.Release();
                            OnPropertyChanged(propName, oldVal, value, false);
                            return(true);
                        }
                        return(false);
                    }
                }

                wl.Reacquire();

                if (_valueBag.ContainsKey(propName))
                {
                    if (propName[0] != KeyService.SHADOW_PROP_PREFIX)
                    {
                        var oldVal = _valueBag[propName];

                        if (!oldVal.IsSame(value))
                        {
                            _isBagChanged       = true;
                            _valueBag[propName] = value;

                            wl.Release();
                            OnPropertyChanged(propName, oldVal, value, true);
                            return(true);
                        }
                    }

                    return(false);
                }

                _valueBag[propName] = value;

                if (propName[0] == KeyService.SHADOW_PROP_PREFIX)
                {
                    _shadowPropCount++;
                    return(false);
                }

                _isBagChanged = true;

                wl.Release();
                OnPropertyChanged(propName, null, value, true);
                return(true);
            }
        }
コード例 #10
0
        private void SenderThread()
        {
            // mark this thread as a background thread
            Thread.CurrentThread.IsBackground = true;
            Thread.CurrentThread.Name         = "Dataset sender thread";

            // keep track of client to remove
            List <IPEndPoint> removeList = new List <IPEndPoint>();

            // enter the send queue lock
            Monitor.Enter(sendQueue);
            while (true)
            {
                if (sendQueue.Count == 0)
                {
                    Monitor.Wait(sendQueue);
                }

                // assume that now there is an entry in the queue
                if (sendQueue.Count > 0)
                {
                    // dequeue the entry
                    SendQueueEntry entry = sendQueue.Dequeue();

                    // fill up the buffer until we're at our target size
                    while (sendQueue.Count > 0)
                    {
                        SendQueueEntry nextEntry = sendQueue.Peek();
                        if (entry.length + nextEntry.length <= target_packet_size)
                        {
                            // add in this entry
                            // actually dequeue the entry
                            sendQueue.Dequeue();
                            // copy the next entry into our working buffer
                            Buffer.BlockCopy(nextEntry.buffer, 0, entry.buffer, entry.length, nextEntry.length);
                            // update the entry length
                            entry.length += nextEntry.length;

                            // free the next entry buffer
                            FreeBuffer(nextEntry.buffer);
                        }
                        else
                        {
                            // leave the loop, adding the next packet would go over the target size
                            break;
                        }
                    }

                    // increment the packet count and byte count
                    packetCount++;

                    // release the lock
                    Monitor.Exit(sendQueue);
                    try {
                        // acquire a reader lock
                        using (ReaderLock rl = new ReaderLock(listenerRWLock)) {
                            // clear the remove list
                            removeList.Clear();

                            // iterate through the listeners and send to each of them
                            foreach (IPEndPoint listener in listenerEndPoints)
                            {
                                try {
                                    sender.SendTo(entry.buffer, entry.length, SocketFlags.None, listener);
                                    byteCount += entry.length;
                                }
                                catch (Exception) {
                                    // for any client error, add to the remove list
                                    removeList.Add(listener);
                                }
                            }

                            // if there are listeners to remove, acquire a writer lock and remove them
                            // only wait for 20 milliseconds to get the writer lock to avoid blocking
                            using (WriterLock wl = rl.UpgradeToWriter(20)) {
                                foreach (IPEndPoint ep in removeList)
                                {
                                    listenerEndPoints.Remove(ep);
                                }
                            }

                            doSendDataValues = listenerEndPoints.Count > 0;
                        }
                    }
                    catch (Exception) {
                        // ignore any exceptions
                    }
                    finally {
                        // re-enter the send queue lock
                        Monitor.Enter(sendQueue);
                    }

                    // free the buffer entry
                    FreeBuffer(entry.buffer);
                }
            }
        }
コード例 #11
0
 public ReadWriteLock(LockRecursionPolicy policy = LockRecursionPolicy.SupportsRecursion)
 {
     _lock  = new ReaderWriterLockSlim(policy);
     _rlock = new ReaderLock(this);
     _wlock = new WriterLock(this);
 }
コード例 #12
0
		public void ClearListeners() {
			using (WriterLock wl = new WriterLock(listenerRWLock)) {
				listenerEndPoints.Clear();
				doSendDataValues = false;
			}
		}
コード例 #13
0
		public void RemoveListener(IPEndPoint ep) {
			// acquire a write lock
			using (WriterLock wl = new WriterLock(listenerRWLock)) {
				// add the end point if it doesn't exists already
				listenerEndPoints.Remove(ep);

				doSendDataValues = listenerEndPoints.Count > 0;
			}
		}
コード例 #14
0
		public void RegisterListener(IPEndPoint ep) {
			// acquire a write lock
			using (WriterLock wl = new WriterLock(listenerRWLock)) {
				// add the end point if it doesn't exists already
				if (!listenerEndPoints.Contains(ep)) {
					listenerEndPoints.Add(ep);
				}

				// indicate that we're now sending value
				doSendDataValues = true;
			}
		}
コード例 #15
0
 private void ReleaseWriterLock(WriterLock locker)
 {
     _lock.ReleaseWriterLock();
     lock (_lockers) _lockers.Remove(locker);
 }