Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active December 20, 2015 21:39
Show Gist options
  • Select an option

  • Save back2dos/6199575 to your computer and use it in GitHub Desktop.

Select an option

Save back2dos/6199575 to your computer and use it in GitHub Desktop.
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