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