A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
unix-system-condition.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2008 University of Washington
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*/
18
19
#include <pthread.h>
20
#include <cerrno>
// for ETIMEDOUT
21
#include <sys/time.h>
22
23
#include "
fatal-error.h
"
24
#include "
system-condition.h
"
25
#include "
log.h
"
26
27
28
NS_LOG_COMPONENT_DEFINE
(
"SystemCondition"
);
29
30
namespace
ns3 {
31
32
class
SystemConditionPrivate
{
33
public
:
35
static
const
uint64_t
NS_PER_SEC
= (uint64_t)1000000000;
36
37
SystemConditionPrivate
();
38
~SystemConditionPrivate
();
39
40
void
SetCondition
(
bool
condition);
41
bool
GetCondition
(
void
);
42
void
Signal
(
void
);
43
void
Broadcast
(
void
);
44
void
Wait
(
void
);
45
bool
TimedWait
(uint64_t ns);
46
47
private
:
48
pthread_mutex_t
m_mutex
;
49
pthread_cond_t
m_cond
;
50
bool
m_condition
;
51
};
52
53
SystemConditionPrivate::SystemConditionPrivate
()
54
{
55
NS_LOG_FUNCTION
(
this
);
56
57
m_condition
=
false
;
58
59
pthread_mutexattr_t mAttr;
60
pthread_mutexattr_init (&mAttr);
61
//
62
// Linux and OS X (at least) have, of course chosen different names for the
63
// error checking flags just to make life difficult.
64
//
65
#if defined (PTHREAD_MUTEX_ERRORCHECK_NP)
66
pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK_NP);
67
#else
68
pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK);
69
#endif
70
pthread_mutex_init (&
m_mutex
, &mAttr);
71
72
pthread_condattr_t cAttr;
73
pthread_condattr_init (&cAttr);
74
pthread_condattr_setpshared (&cAttr, PTHREAD_PROCESS_PRIVATE);
75
pthread_cond_init (&
m_cond
, &cAttr);
76
}
77
78
SystemConditionPrivate::~SystemConditionPrivate
()
79
{
80
NS_LOG_FUNCTION
(
this
);
81
pthread_mutex_destroy (&
m_mutex
);
82
pthread_cond_destroy (&
m_cond
);
83
}
84
85
void
86
SystemConditionPrivate::SetCondition
(
bool
condition)
87
{
88
NS_LOG_FUNCTION
(
this
<< condition);
89
m_condition
= condition;
90
}
91
92
bool
93
SystemConditionPrivate::GetCondition
(
void
)
94
{
95
NS_LOG_FUNCTION
(
this
);
96
return
m_condition
;
97
}
98
99
void
100
SystemConditionPrivate::Signal
(
void
)
101
{
102
NS_LOG_FUNCTION
(
this
);
103
104
pthread_mutex_lock (&
m_mutex
);
105
pthread_cond_signal (&
m_cond
);
106
pthread_mutex_unlock (&
m_mutex
);
107
}
108
109
void
110
SystemConditionPrivate::Broadcast
(
void
)
111
{
112
NS_LOG_FUNCTION
(
this
);
113
114
pthread_mutex_lock (&
m_mutex
);
115
pthread_cond_broadcast (&
m_cond
);
116
pthread_mutex_unlock (&
m_mutex
);
117
}
118
119
void
120
SystemConditionPrivate::Wait
(
void
)
121
{
122
NS_LOG_FUNCTION
(
this
);
123
124
pthread_mutex_lock (&
m_mutex
);
125
m_condition
=
false
;
126
while
(
m_condition
==
false
)
127
{
128
pthread_cond_wait (&
m_cond
, &
m_mutex
);
129
}
130
pthread_mutex_unlock (&
m_mutex
);
131
}
132
133
bool
134
SystemConditionPrivate::TimedWait
(uint64_t ns)
135
{
136
NS_LOG_FUNCTION
(
this
<< ns);
137
138
struct
timespec ts;
139
ts.tv_sec = ns /
NS_PER_SEC
;
140
ts.tv_nsec = ns %
NS_PER_SEC
;
141
142
struct
timeval tv;
143
gettimeofday (&tv, NULL);
144
145
ts.tv_sec += tv.tv_sec;
146
ts.tv_nsec += tv.tv_usec * 1000;
147
if
(ts.tv_nsec > (int64_t)
NS_PER_SEC
)
148
{
149
++ts.tv_sec;
150
ts.tv_nsec %=
NS_PER_SEC
;
151
}
152
153
int
rc;
154
155
pthread_mutex_lock (&
m_mutex
);
156
while
(
m_condition
==
false
)
157
{
158
rc = pthread_cond_timedwait (&
m_cond
, &
m_mutex
, &ts);
159
if
(rc == ETIMEDOUT)
160
{
161
pthread_mutex_unlock (&
m_mutex
);
162
return
true
;
163
}
164
}
165
pthread_mutex_unlock (&
m_mutex
);
166
return
false
;
167
}
168
169
SystemCondition::SystemCondition
()
170
: m_priv (new
SystemConditionPrivate
())
171
{
172
NS_LOG_FUNCTION
(
this
);;
173
}
174
175
SystemCondition::~SystemCondition
()
176
{
177
NS_LOG_FUNCTION
(
this
);
178
delete
m_priv
;
179
}
180
181
void
182
SystemCondition::SetCondition
(
bool
condition)
183
{
184
NS_LOG_FUNCTION
(
this
<< condition);
185
m_priv
->
SetCondition
(condition);
186
}
187
188
bool
189
SystemCondition::GetCondition
(
void
)
190
{
191
NS_LOG_FUNCTION
(
this
);
192
return
m_priv
->
GetCondition
();
193
}
194
195
void
196
SystemCondition::Signal
(
void
)
197
{
198
NS_LOG_FUNCTION
(
this
);
199
m_priv
->
Signal
();
200
}
201
202
void
203
SystemCondition::Broadcast
(
void
)
204
{
205
NS_LOG_FUNCTION
(
this
);
206
m_priv
->
Broadcast
();
207
}
208
209
void
210
SystemCondition::Wait
(
void
)
211
{
212
NS_LOG_FUNCTION
(
this
);
213
m_priv
->
Wait
();
214
}
215
216
bool
217
SystemCondition::TimedWait
(uint64_t ns)
218
{
219
NS_LOG_FUNCTION
(
this
<< ns);
220
return
m_priv
->
TimedWait
(ns);
221
}
222
223
}
// namespace ns3
fatal-error.h
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Definition:
log-macros-enabled.h:207
ns3::SystemConditionPrivate::Signal
void Signal(void)
Definition:
unix-system-condition.cc:100
ns3::SystemCondition::~SystemCondition
~SystemCondition()
Definition:
unix-system-condition.cc:175
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:170
system-condition.h
ns3::SystemConditionPrivate::TimedWait
bool TimedWait(uint64_t ns)
Definition:
unix-system-condition.cc:134
ns3::SystemConditionPrivate::m_cond
pthread_cond_t m_cond
Definition:
unix-system-condition.cc:49
ns3::SystemCondition::Signal
void Signal(void)
Release one thread if waiting for the condition to be true.
Definition:
unix-system-condition.cc:196
ns3::SystemConditionPrivate::Broadcast
void Broadcast(void)
Definition:
unix-system-condition.cc:110
ns3::SystemCondition::Broadcast
void Broadcast(void)
Release all threads waiting for the condition to be true.
Definition:
unix-system-condition.cc:203
ns3::SystemConditionPrivate::SetCondition
void SetCondition(bool condition)
Definition:
unix-system-condition.cc:86
ns3::SystemCondition::Wait
void Wait(void)
Wait, possibly forever, for the condition to be true.
Definition:
unix-system-condition.cc:210
ns3::SystemCondition::SetCondition
void SetCondition(bool condition)
Set the value of the underlying condition.
Definition:
unix-system-condition.cc:182
ns3::SystemCondition::GetCondition
bool GetCondition(void)
Get the value of the underlying condition.
Definition:
unix-system-condition.cc:189
ns3::SystemConditionPrivate::Wait
void Wait(void)
Definition:
unix-system-condition.cc:120
ns3::SystemCondition::TimedWait
bool TimedWait(uint64_t ns)
Wait a maximum of ns nanoseconds for the condition to be true.
Definition:
unix-system-condition.cc:217
ns3::SystemConditionPrivate::m_condition
bool m_condition
Definition:
unix-system-condition.cc:50
ns3::SystemConditionPrivate::SystemConditionPrivate
SystemConditionPrivate()
Definition:
unix-system-condition.cc:53
ns3::SystemConditionPrivate::~SystemConditionPrivate
~SystemConditionPrivate()
Definition:
unix-system-condition.cc:78
ns3::SystemConditionPrivate
Definition:
unix-system-condition.cc:32
ns3::SystemCondition::m_priv
SystemConditionPrivate * m_priv
Definition:
system-condition.h:101
ns3::SystemConditionPrivate::NS_PER_SEC
static const uint64_t NS_PER_SEC
Conversion from ns to s.
Definition:
unix-system-condition.cc:35
ns3::SystemConditionPrivate::m_mutex
pthread_mutex_t m_mutex
Definition:
unix-system-condition.cc:48
ns3::SystemCondition::SystemCondition
SystemCondition()
Definition:
unix-system-condition.cc:169
log.h
ns3::SystemConditionPrivate::GetCondition
bool GetCondition(void)
Definition:
unix-system-condition.cc:93
src
core
model
unix-system-condition.cc
Generated on Fri Dec 5 2014 17:29:06 for ns-3 by
1.8.6