A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
core
model
unix-system-mutex.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2008 INRIA
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
* Author: Mathieu Lacage <mathieu.lacage.inria.fr>
19
*/
20
21
#include <pthread.h>
22
#include <cstring>
23
#include <cerrno>
// for strerror
24
25
#include "
fatal-error.h
"
26
#include "
system-mutex.h
"
27
#include "
log.h
"
28
29
36
namespace
ns3
{
37
38
NS_LOG_COMPONENT_DEFINE_MASK
(
"SystemMutex"
,
ns3::LOG_PREFIX_TIME
);
39
41
class
SystemMutexPrivate
{
42
public
:
43
SystemMutexPrivate
();
44
~SystemMutexPrivate
();
45
46
void
Lock
(
void
);
47
void
Unlock
(
void
);
48
private
:
49
pthread_mutex_t
m_mutex
;
50
};
51
52
SystemMutexPrivate::SystemMutexPrivate
()
53
{
54
NS_LOG_FUNCTION
(
this
);
55
56
pthread_mutexattr_t attr;
57
pthread_mutexattr_init (&attr);
58
//
59
// Make this an error checking mutex. This will check to see if the current
60
// thread already owns the mutex before trying to lock it. Instead of
61
// deadlocking it returns an error. It will also check to make sure a thread
62
// has previously called pthread_mutex_lock when it calls pthread_mutex_unlock.
63
//
64
// Linux and OS X (at least) have, of course chosen different names for the
65
// error checking flags just to make life difficult.
66
//
67
#if defined (PTHREAD_MUTEX_ERRORCHECK_NP)
68
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
69
#else
70
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
71
#endif
72
pthread_mutex_init (&
m_mutex
, &attr);
73
}
74
75
SystemMutexPrivate::~SystemMutexPrivate
()
76
{
77
NS_LOG_FUNCTION
(
this
);
78
pthread_mutex_destroy (&
m_mutex
);
79
}
80
81
void
82
SystemMutexPrivate::Lock
(
void
)
83
{
84
NS_LOG_FUNCTION
(
this
);
85
86
int
rc = pthread_mutex_lock (&
m_mutex
);
87
if
(rc != 0)
88
{
89
NS_FATAL_ERROR
(
"SystemMutexPrivate::Lock()"
90
"pthread_mutex_lock failed: "
<< rc <<
" = \""
<<
91
std::strerror (rc) <<
"\""
);
92
}
93
}
94
95
void
96
SystemMutexPrivate::Unlock
(
void
)
97
{
98
NS_LOG_FUNCTION
(
this
);
99
100
int
rc = pthread_mutex_unlock (&
m_mutex
);
101
if
(rc != 0)
102
{
103
NS_FATAL_ERROR
(
"SystemMutexPrivate::Unlock()"
104
"pthread_mutex_unlock failed: "
<< rc <<
" = \""
<<
105
std::strerror (rc) <<
"\""
);
106
}
107
}
108
109
SystemMutex::SystemMutex
()
110
: m_priv (new
SystemMutexPrivate
())
111
{
112
NS_LOG_FUNCTION
(
this
);
113
}
114
115
SystemMutex::~SystemMutex
()
116
{
117
NS_LOG_FUNCTION
(
this
);
118
delete
m_priv
;
119
}
120
121
void
122
SystemMutex::Lock
()
123
{
124
NS_LOG_FUNCTION
(
this
);
125
m_priv
->
Lock
();
126
}
127
128
void
129
SystemMutex::Unlock
()
130
{
131
NS_LOG_FUNCTION
(
this
);
132
m_priv
->
Unlock
();
133
}
134
135
CriticalSection::CriticalSection
(
SystemMutex
&mutex)
136
: m_mutex (mutex)
137
{
138
NS_LOG_FUNCTION
(
this
<< &mutex);
139
m_mutex
.
Lock
();
140
}
141
142
CriticalSection::~CriticalSection
()
143
{
144
NS_LOG_FUNCTION
(
this
);
145
m_mutex
.
Unlock
();
146
}
147
148
}
// namespace ns3
fatal-error.h
NS_FATAL_x macro definitions.
ns3::SystemMutex::m_priv
SystemMutexPrivate * m_priv
The (system-dependent) implementation.
Definition:
system-mutex.h:76
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:213
ns3::SystemMutexPrivate::m_mutex
pthread_mutex_t m_mutex
The mutex.
Definition:
unix-system-mutex.cc:49
ns3::CriticalSection::~CriticalSection
~CriticalSection()
Destructor.
Definition:
unix-system-mutex.cc:142
ns3::SystemMutex::Lock
void Lock()
Acquire ownership of the Mutual Exclusion object.
Definition:
unix-system-mutex.cc:122
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition:
fatal-error.h:162
ns3::SystemMutex::SystemMutex
SystemMutex()
Definition:
unix-system-mutex.cc:109
ns3::SystemMutexPrivate::SystemMutexPrivate
SystemMutexPrivate()
Definition:
unix-system-mutex.cc:52
system-mutex.h
System-independent mutex primitive, ns3::SystemMutex, and ns3::CriticalSection.
ns3::CriticalSection::CriticalSection
CriticalSection(SystemMutex &mutex)
Construct with the required SystemMutex.
Definition:
unix-system-mutex.cc:135
NS_LOG_COMPONENT_DEFINE_MASK
#define NS_LOG_COMPONENT_DEFINE_MASK(name, mask)
Define a logging component with a mask.
Definition:
log.h:213
ns3::SystemMutex::Unlock
void Unlock()
Release ownership of the Mutual Exclusion object.
Definition:
unix-system-mutex.cc:129
ns3::LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition:
log.h:116
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SystemMutexPrivate::~SystemMutexPrivate
~SystemMutexPrivate()
Definition:
unix-system-mutex.cc:75
ns3::SystemMutex
A class which provides a relatively platform-independent Mutual Exclusion thread synchronization prim...
Definition:
system-mutex.h:58
ns3::SystemMutexPrivate::Lock
void Lock(void)
Acquire ownership of the mutex.
Definition:
unix-system-mutex.cc:82
ns3::CriticalSection::m_mutex
SystemMutex & m_mutex
The mutex.
Definition:
system-mutex.h:130
ns3::SystemMutexPrivate::Unlock
void Unlock(void)
Release ownership of the mutex.
Definition:
unix-system-mutex.cc:96
ns3::SystemMutex::~SystemMutex
~SystemMutex()
Definition:
unix-system-mutex.cc:115
log.h
Debug message logging.
ns3::SystemMutexPrivate
System-dependent implementation of SystemMutex.
Definition:
unix-system-mutex.cc:41
Generated on Wed Nov 7 2018 10:01:50 for ns-3 by
1.8.14