SIMLIB/C++
3.09
Toggle main menu visibility
Loading...
Searching...
No Matches
src
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
26
namespace
simlib3
{
27
28
SIMLIB_IMPLEMENTATION
;
29
30
////////////////////////////////////////////////////////////////////////////
31
// constructors
32
//
33
Semaphore::Semaphore
()
34
{
35
Dprintf
((
"Semaphore::Semaphore()"
));
36
n
= 1;
37
}
38
39
Semaphore::Semaphore
(
const
char
*name)
40
{
41
Dprintf
((
"Semaphore::Semaphore(\"%s\")"
,name));
42
SetName
(name);
43
n
= 1;
44
}
45
46
47
////////////////////////////////////////////////////////////////////////////
48
// destructor
49
//
50
Semaphore::~Semaphore
()
51
{
52
Dprintf
((
"Semaphore::~Semaphore() // \"%s\", %d "
,
53
Name
().c_str(),
n
));
54
}
55
56
////////////////////////////////////////////////////////////////////////////
57
// initialization of state
58
//
59
void
Semaphore::Clear
() {
60
Dprintf
((
"%s.Clear()"
,
Name
().c_str()));
61
n
= 1;
62
Q
.
Clear
();
// queue initialization ###!!!
63
}
64
65
66
////////////////////////////////////////////////////////////////////////////
67
// Output
68
//
69
void
Semaphore::Output
()
const
{
70
Print
(
"Semaphore: %s [%d]\n"
,
Name
().c_str(),
n
);
71
}
72
73
74
////////////////////////////////////////////////////////////////////////////
75
//
76
//
77
void
Semaphore::P
()
78
{
79
Dprintf
((
"Semaphore'%s'.P()"
,
Name
().c_str()));
80
81
while
(
n
== 0) {
82
Q
.
Insert
(
Current
);
// Current==this
83
Passivate
(
Current
);
84
Q
.
Get
(
Current
);
85
}
86
n
--;
87
}
88
89
////////////////////////////////////////////////////////////////////////////
90
//
91
//
92
void
Semaphore::V
()
93
{
94
Dprintf
((
"%s.V()"
,
Name
().c_str()));
95
if
(
n
>=1)
96
SIMLIB_error
(
SemaphoreError
);
97
n
++;
98
Entity
*p =
Q
.
front
();
// first entity in queue
99
if
(p) p->
Activate
();
100
}
101
102
}
103
// end
104
simlib3::Entity
abstract base class for active entities (Process, Event) instances of derived classes provide Behavio...
Definition
simlib.h:380
simlib3::Entity::Activate
void Activate()
activate now
Definition
simlib.h:413
simlib3::Queue::Get
virtual Entity * Get(iterator pos)
remove at position
Definition
queue.cc:133
simlib3::Queue::Insert
virtual void Insert(Entity *e)
Definition
queue.cc:50
simlib3::Queue::front
Entity * front()
Definition
simlib.h:706
simlib3::Queue::Clear
void Clear()
Definition
simlib.h:711
simlib3::Semaphore::V
virtual void V()
V operation.
Definition
semaphor.cc:92
simlib3::Semaphore::Output
virtual void Output() const override
print object to default output
Definition
semaphor.cc:69
simlib3::Semaphore::Q
Queue Q
internal quqeue
Definition
simlib.h:1766
simlib3::Semaphore::n
int n
Definition
simlib.h:1764
simlib3::Semaphore::P
virtual void P()
P operation.
Definition
semaphor.cc:77
simlib3::Semaphore::Clear
void Clear()
initialization
Definition
semaphor.cc:59
simlib3::Semaphore::~Semaphore
virtual ~Semaphore()
Definition
semaphor.cc:50
simlib3::Semaphore::Semaphore
Semaphore()
Definition
semaphor.cc:33
simlib3::SimObject::SetName
void SetName(const std::string &name)
assign the name
Definition
object.cc:125
simlib3::SimObject::Name
virtual std::string Name() const
get object name
Definition
object.cc:134
internal.h
Internal header file for SIMLIB/C++.
Dprintf
#define Dprintf(f)
Definition
internal.h:100
simlib3
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition
algloop.cc:32
simlib3::SIMLIB_error
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition
error.cc:38
simlib3::Passivate
void Passivate(Entity *e)
passivate entity e
Definition
simlib.h:437
simlib3::SIMLIB_IMPLEMENTATION
SIMLIB_IMPLEMENTATION
Definition
algloop.cc:34
simlib3::SemaphoreError
@ SemaphoreError
Definition
errors.cc:70
simlib3::Print
int Print(const char *fmt,...)
for Output methods, can be redirected
Definition
print.cc:92
simlib3::Current
Entity *const & Current
pointer to active (now running) entity
Definition
run.cc:54
simlib.h
Main SIMLIB/C++ interface.
Generated on
for SIMLIB/C++ by
1.18.0