SIMLIB/C++ 3.09
Loading...
Searching...
No Matches
semaphor.cc
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2//! \file semaphor.cc Process synchronization - semaphore
3//
4// Copyright (c) 1994-2004 Petr Peringer
5//
6// This library is licensed under GNU Library GPL. See the file COPYING.
7//
8
9//
10// class Semaphore implementation
11// TODO: correct too simple semantics, add capacity=1 default
12// it is like Store, but without statistics (different queue sort order?)
13//
14
15////////////////////////////////////////////////////////////////////////////
16// interface
17//
18
19#include "simlib.h"
20#include "internal.h"
21
22////////////////////////////////////////////////////////////////////////////
23// implementation
24//
25
26namespace simlib3 {
27
29
30////////////////////////////////////////////////////////////////////////////
31// constructors
32//
34{
35 Dprintf(("Semaphore::Semaphore()"));
36 n = 1;
37}
38
39Semaphore::Semaphore(const char *name)
40{
41 Dprintf(("Semaphore::Semaphore(\"%s\")",name));
42 SetName(name);
43 n = 1;
44}
45
46
47////////////////////////////////////////////////////////////////////////////
48// destructor
49//
51{
52 Dprintf(("Semaphore::~Semaphore() // \"%s\", %d ",
53 Name().c_str(), n));
54}
55
56////////////////////////////////////////////////////////////////////////////
57// initialization of state
58//
60 Dprintf(("%s.Clear()", Name().c_str()));
61 n = 1;
62 Q.Clear(); // queue initialization ###!!!
63}
64
65
66////////////////////////////////////////////////////////////////////////////
67// Output
68//
69void Semaphore::Output() const {
70 Print("Semaphore: %s [%d]\n", Name().c_str(), n);
71}
72
73
74////////////////////////////////////////////////////////////////////////////
75//
76//
78{
79 Dprintf(("Semaphore'%s'.P()", Name().c_str()));
80
81 while(n == 0) {
82 Q.Insert(Current); // Current==this
84 Q.Get(Current);
85 }
86 n--;
87}
88
89////////////////////////////////////////////////////////////////////////////
90//
91//
93{
94 Dprintf(("%s.V()", Name().c_str()));
95 if(n>=1)
97 n++;
98 Entity *p = Q.front(); // first entity in queue
99 if(p) p->Activate();
100}
101
102}
103// end
104
abstract base class for active entities (Process, Event) instances of derived classes provide Behavio...
Definition: simlib.h:375
void Activate()
activate now
Definition: simlib.h:408
virtual Entity * Get(iterator pos)
remove at position
Definition: queue.cc:133
virtual void Insert(Entity *e)
Definition: queue.cc:50
Entity * front()
Definition: simlib.h:701
void Clear()
Definition: simlib.h:706
virtual void V()
V operation.
Definition: semaphor.cc:92
virtual void Output() const override
print object to default output
Definition: semaphor.cc:69
Queue Q
internal quqeue
Definition: simlib.h:1761
virtual void P()
P operation.
Definition: semaphor.cc:77
void Clear()
initialization
Definition: semaphor.cc:59
virtual ~Semaphore()
Definition: semaphor.cc:50
void SetName(const std::string &name)
assign the name
Definition: object.cc:125
virtual std::string Name() const
get object name
Definition: object.cc:134
@ SemaphoreError
Definition: errors.h:69
Internal header file for SIMLIB/C++.
#define Dprintf(f)
Definition: internal.h:100
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition: algloop.cc:32
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
void Passivate(Entity *e)
passivate entity e
Definition: simlib.h:432
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
int Print(const char *fmt,...)
for Output methods, can be redirected
Definition: print.cc:92
Entity *const & Current
pointer to active (now running) entity
Definition: run.cc:54
Main SIMLIB/C++ interface.