Last active
December 20, 2015 21:39
-
-
Save back2dos/6199575 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ; | |
| import Map; | |
| import cpp.vm.Mutex; | |
| abstract ThreadSafeMap<K, V>({ data: IMap<K, V>, lock: Mutex }) { | |
| public function new(data:IMap<K, V>) | |
| this = { data: data, lock: new Mutex() } | |
| function calc<A>(f:Void->A) { | |
| this.lock.acquire(); | |
| var ret = f(); | |
| this.lock.release(); | |
| return ret; | |
| } | |
| function exec(f:Void->Void) { | |
| this.lock.acquire(); | |
| f(); | |
| this.lock.release(); | |
| } | |
| public function get(key:K) | |
| return calc(function () return this.data.get(key));//not even sure read access really needs synchronization, but let's do it!!!! | |
| public function update(key:K, op:Null<V>->V) | |
| exec(function () this.data.set(key, op(this.data.get(key))) | |
| public function iterator():Iterator<V> | |
| return calc(function () return [for (v in this.data) v]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment