Mercurial > projects > dreactor
view asyncdreactor/util/ThreadSafeQueue.d @ 11:5836613d16ac
reorg! reorg!
author | rick@minifunk |
---|---|
date | Tue, 12 Aug 2008 16:59:56 -0400 |
parents | dreactor/util/ThreadSafeQueue.d@e3dbc9208822 |
children |
line wrap: on
line source
module dreactor.util.ThreadSafeQueue; import tango.util.collection.CircularSeq; import tango.core.Atomic; import tango.util.log.Log; import tango.util.log.Config; /****************************************************************************** ThreadSafeQueue Queue that is probably thread safe. It acts as a job queue, in that you can push or pop off of the queue. Or you can processAll, which will apply a delegate to each item, then clear the list. ******************************************************************************/ class ThreadSafeQueue(TYPE) { public this(int maxsz = 1000) { list_ = new CircularSeq!(TYPE); maxsize_ = maxsz; size_ = 0; log = Log.lookup("dreactor.util.ThreadSafeQueue"); } synchronized bool pop(ref TYPE t) { if (size_ > 0) { t = list_.head(); list_.removeHead(); size_--; return true; } else return false; } synchronized bool push(TYPE t) { if (size_ < maxsize_) { list_.append(t); size_++; return true; } else return false; } synchronized int size() { return size_; } synchronized int processAll(int delegate(ref TYPE value) dg) { if (0 >= size_) return 0; int count = 0; foreach(TYPE t; list_) { if (dg(t) < 0) break; ++count; } if (count == size_) { clear_(); size_ = 0; } else { list_.removeRange(0, count); size_ -= count; } return count; } synchronized void clear() { clear_(); } private void clear_() { list_.clear(); size_ = 0 ; } int maxsize_; int size_; Logger log; CircularSeq!(TYPE) list_; }