﻿
function Map(){this.map=new Object();this.add=function(k,o){this.map[k]=o;}
this.remove=function(k){delete this.map[k];}
this.get=function(k){return k==null?null:this.map[k];}
this.first=function(){return this.get(this.nextKey());}
this.next=function(k){return this.get(this.nextKey(k));}
this.nextKey=function(k){for(i in this.map){if(!k)return i;if(k==i)k=null;}
return null;}}
function Mutex(cmdObject,methodName,param1,param2,param3){if(!Mutex.Wait)Mutex.Wait=new Map();Mutex.SLICE=function(cmdID,startID){var aCmd=Mutex.Wait.get(cmdID);if(aCmd)
{aCmd.attempt(Mutex.Wait.get(startID));}}
this.attempt=function(start){for(var j=start;j;j=Mutex.Wait.next(j.c.id)){if(j.enter||(j.number&&(j.number<this.number||(j.number==this.number&&j.c.id<this.c.id))))
return setTimeout("Mutex.SLICE("+this.c.id+","+j.c.id+")",10);}
if(this.param1!=undefined&&this.param2!=undefined&&this.param3!=undefined)
{this.c[this.methodID](param1,param2,param3);}
else if(this.param1!=undefined&&this.param2!=undefined)
{this.c[this.methodID](param1,param2);}
else if(this.param1!=undefined)
{this.c[this.methodID](param1);}
else
{this.c[this.methodID]();}
this.number=0;Mutex.Wait.remove(this.c.id);}
this.c=cmdObject;this.methodID=methodName;this.param1=param1;this.param2=param2;this.param3=param3;Mutex.Wait.add(this.c.id,this);this.enter=true;this.number=(new Date()).getTime();this.enter=false;this.attempt(Mutex.Wait.first());}
