//================================= CLASS CONSTRUCTOR ========================================
function Semaphore()
{
//MEMBER VARIABLES
 this.value = Semaphore.RETRYCNT;
 this.timerId = null;
}

//====================================== STATIC ==============================================
//The times for the wait method to retry until we say that the child thread dead-locked ...
Semaphore.RETRYCNT = 600;
Semaphore.TIMEWAIT = 300;
Semaphore.TIMEWORK = 150;

//====================================== MEMBERS =============================================
Semaphore.prototype.reset = function()
{
 this.value = Semaphore.RETRYCNT;
}

Semaphore.prototype.unfreeze = function()
{
 this.value = 0;
}

Semaphore.prototype.startTimer = function()
{
//We'll start processing of keep-alive messages timer ...
 //this.timerId = setTimeout(this.childRun,Semaphore.TIMEWORK);
}

Semaphore.prototype.stopTimer = function()
{
//Killing the keepAlive timer ...
 if (this.timerId != null)
 {
  clearTimeout(this.timerId);
  this.timerId = null;
 }
}

Semaphore.prototype.childRun = function()
{
 try {
  //Resetting the semaphore to defaults ...(so it's again fully locked!)
    this.reset();
  //Restarting childRun timer ...
    this.startTimer();
 } catch(e) {
     ;
 }
}

Semaphore.prototype.waiting = function()
{
 if (this.value > 0)
  return true;
 return false;
}

Semaphore.prototype.parentWait = function(iTmp)
{
 while (this.waiting())
 {
  THREAD_sleep(Semaphore.TIMEWAIT);//pausing to let other threads do their job ...
//Since one tick passed we'll decrement the value ...
  this.value--;
 }
//resetting ...
 this.reset();
}

Semaphore.prototype.childStart = function()
{
//Running keep-alive thread ...
 this.pageLoaded = null;
 this.childRun();
}

Semaphore.prototype.childEnd = function(iTmp)
{
//Stopping the keep alive timer ...
 this.stopTimer();
//Unlocking the thread ...
 this.value = 0;
 this.pageLoaded = true;
}
