From d79664fd2ce3b72f70b91ba8c55f32b17c6c2ce8 Mon Sep 17 00:00:00 2001 From: Emil Westergren Date: Tue, 5 Jul 2016 19:40:44 +0200 Subject: [PATCH 1/2] Fixed race condition in RecycleBin. --- source/concurrent/RecycleBin.ooc | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/source/concurrent/RecycleBin.ooc b/source/concurrent/RecycleBin.ooc index 9cc5b2f4a..f44cb3d0b 100644 --- a/source/concurrent/RecycleBin.ooc +++ b/source/concurrent/RecycleBin.ooc @@ -10,30 +10,40 @@ use concurrent RecycleBin: class { _free: Func (T) - _list: SynchronizedList + _list: VectorList _size: Int count ::= this _list count isFull ::= this count == this _size isEmpty ::= this _list empty - init: func (=_size, =_free) { this _list = SynchronizedList new(_size) } + _mutex := Mutex new() + init: func (=_size, =_free) { this _list = VectorList new(_size) } free: override func { this clear() this _list free() (this _free as Closure) free(Owner Receiver) + this _mutex free() super() } add: func (object: T) { + this _mutex lock() if (this isFull) this _free(this _list remove(0)) this _list add(object) + this _mutex unlock() } search: func (matches: Func (T) -> Bool) -> T { - index := this _list search(matches) result: T = null - (index > -1) ? this _list remove(index) : result + this _mutex lock() + index := this _list search(matches) + if (index > -1) + result = this _list remove(index) + this _mutex unlock() + result } clear: func { + this _mutex lock() while (!this isEmpty) this _free(this _list remove()) + this _mutex unlock() } } From 7470305ef5addd683691c53d0d5bae5d61f26b9e Mon Sep 17 00:00:00 2001 From: Emil Westergren Date: Wed, 6 Jul 2016 11:22:40 +0200 Subject: [PATCH 2/2] Moved result variable definition. --- source/concurrent/RecycleBin.ooc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/concurrent/RecycleBin.ooc b/source/concurrent/RecycleBin.ooc index f44cb3d0b..691cae8fd 100644 --- a/source/concurrent/RecycleBin.ooc +++ b/source/concurrent/RecycleBin.ooc @@ -32,9 +32,9 @@ RecycleBin: class { this _mutex unlock() } search: func (matches: Func (T) -> Bool) -> T { - result: T = null this _mutex lock() index := this _list search(matches) + result: T = null if (index > -1) result = this _list remove(index) this _mutex unlock()