/////////////////////////////////////////////////////////////////////////////
// semaphor.cc
//
// SIMLIB version: 2.18
// Date: 2004-01-25
//
// Copyright (c) 1994-2004 Petr Peringer
//
// This library is licensed under GNU Library GPL. See the file COPYING.
//

//
//  class Semaphore implementation
//  it is like Store, but without statistics (different queue sort order?)
//

////////////////////////////////////////////////////////////////////////////
//  interface
//

#include "simlib.h"
#include "internal.h"

////////////////////////////////////////////////////////////////////////////
//  implementation
//

SIMLIB_IMPLEMENTATION

////////////////////////////////////////////////////////////////////////////
//  constructors
//
Semaphore::Semaphore()
{
  dprintf(("Semaphore::Semaphore()"));
  n = 1;
}

Semaphore::Semaphore(const char *name)
{
  dprintf(("Semaphore::Semaphore(\"%s\")",name));
  SetName(name);
  n = 1;
}


////////////////////////////////////////////////////////////////////////////
//  destructor
//
Semaphore::~Semaphore()
{
  dprintf(("Semaphore::~Semaphore()  // \"%s\", %d ",
           Name(), n));
}

////////////////////////////////////////////////////////////////////////////
//  initialization of state
//
void Semaphore::Clear() {
  dprintf(("%s.Clear()", Name()));
  n = 1;
  Q.Clear(); // queue initialization ###!!!
}


////////////////////////////////////////////////////////////////////////////
//  Output
//
void Semaphore::Output() {
  Print("Semaphore: %s [%d]\n", Name(), n);
}


////////////////////////////////////////////////////////////////////////////
//
//
void Semaphore::P()
{
  dprintf(("Semaphore'%s'.P()", Name()));

  while(n == 0) {
    Q.Insert(Current);
    Passivate(Current);
    Q.Get(Current);
  }
  n = 0;
}

////////////////////////////////////////////////////////////////////////////
//
//
void Semaphore::V()
{
  dprintf(("%s.V()", Name()));
  if(n==1)
    SIMLIB_error(SemaphoreError);
  n = 1;
  Entity *p = Q.front(); // first entity in queue
  if(p) p->Activate();
}


// end



syntax highlighted by Code2HTML, v. 0.9.1