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-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
30
NS_LOG_COMPONENT_DEFINE
(
"SystemMutex"
);
31
32
namespace
ns3 {
33
34
class
SystemMutexPrivate
{
35
public
:
36
SystemMutexPrivate
();
37
~SystemMutexPrivate
();
38
39
void
Lock
(
void
);
40
void
Unlock
(
void
);
41
private
:
42
pthread_mutex_t
m_mutex
;
43
};
44
45
SystemMutexPrivate::SystemMutexPrivate
()
46
{
47
NS_LOG_FUNCTION
(
this
);
48
49
pthread_mutexattr_t attr;
50
pthread_mutexattr_init (&attr);
51
//
52
// Make this an error checking mutex. This will check to see if the current
53
// thread already owns the mutex before trying to lock it. Instead of
54
// deadlocking it returns an error. It will also check to make sure a thread
55
// has previously called pthread_mutex_lock when it calls pthread_mutex_unlock.
56
//
57
// Linux and OS X (at least) have, of course chosen different names for the
58
// error checking flags just to make life difficult.
59
//
60
#if defined (PTHREAD_MUTEX_ERRORCHECK_NP)
61
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
62
#else
63
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
64
#endif
65
pthread_mutex_init (&
m_mutex
, &attr);
66
}
67
68
SystemMutexPrivate::~SystemMutexPrivate
()
69
{
70
NS_LOG_FUNCTION
(
this
);
71
pthread_mutex_destroy (&
m_mutex
);
72
}
73
74
void
75
SystemMutexPrivate::Lock
(
void
)
76
{
77
NS_LOG_FUNCTION
(
this
);
78
79
int
rc = pthread_mutex_lock (&
m_mutex
);
80
if
(rc != 0)
81
{
82
NS_FATAL_ERROR
(
"SystemMutexPrivate::Lock()"
83
"pthread_mutex_lock failed: "
<< rc <<
" = \""
<<
84
std::strerror (rc) <<
"\""
);
85
}
86
}
87
88
void
89
SystemMutexPrivate::Unlock
(
void
)
90
{
91
NS_LOG_FUNCTION
(
this
);
92
93
int
rc = pthread_mutex_unlock (&
m_mutex
);
94
if
(rc != 0)
95
{
96
NS_FATAL_ERROR
(
"SystemMutexPrivate::Unlock()"
97
"pthread_mutex_unlock failed: "
<< rc <<
" = \""
<<
98
std::strerror (rc) <<
"\""
);
99
}
100
}
101
102
SystemMutex::SystemMutex
()
103
: m_priv (new
SystemMutexPrivate
())
104
{
105
NS_LOG_FUNCTION
(
this
);
106
}
107
108
SystemMutex::~SystemMutex
()
109
{
110
NS_LOG_FUNCTION
(
this
);
111
delete
m_priv
;
112
}
113
114
void
115
SystemMutex::Lock
()
116
{
117
NS_LOG_FUNCTION
(
this
);
118
m_priv
->
Lock
();
119
}
120
121
void
122
SystemMutex::Unlock
()
123
{
124
NS_LOG_FUNCTION
(
this
);
125
m_priv
->
Unlock
();
126
}
127
128
CriticalSection::CriticalSection
(
SystemMutex
&mutex)
129
: m_mutex (mutex)
130
{
131
NS_LOG_FUNCTION
(
this
<< &mutex);
132
m_mutex
.
Lock
();
133
}
134
135
CriticalSection::~CriticalSection
()
136
{
137
NS_LOG_FUNCTION
(
this
);
138
m_mutex
.
Unlock
();
139
}
140
141
}
// namespace ns3
fatal-error.h
ns3::SystemMutex::m_priv
SystemMutexPrivate * m_priv
Definition:
system-mutex.h:67
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
Definition:
log.h:345
ns3::SystemMutexPrivate::m_mutex
pthread_mutex_t m_mutex
Definition:
unix-system-mutex.cc:42
ns3::CriticalSection::~CriticalSection
~CriticalSection()
Definition:
unix-system-mutex.cc:135
ns3::SystemMutex::Lock
void Lock()
Acquire ownership of the Mutual Exclusion object.
Definition:
unix-system-mutex.cc:115
ns3::SystemMutex::SystemMutex
SystemMutex()
Definition:
unix-system-mutex.cc:102
ns3::SystemMutexPrivate::SystemMutexPrivate
SystemMutexPrivate()
Definition:
unix-system-mutex.cc:45
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition:
fatal-error.h:72
system-mutex.h
ns3::CriticalSection::CriticalSection
CriticalSection(SystemMutex &mutex)
Definition:
unix-system-mutex.cc:128
NS_LOG_COMPONENT_DEFINE
NS_LOG_COMPONENT_DEFINE("SystemMutex")
ns3::SystemMutex::Unlock
void Unlock()
Release ownership of the Mutual Exclusion object.
Definition:
unix-system-mutex.cc:122
ns3::SystemMutexPrivate::~SystemMutexPrivate
~SystemMutexPrivate()
Definition:
unix-system-mutex.cc:68
ns3::SystemMutex
A class which provides a relatively platform-independent Mutual Exclusion thread synchronization prim...
Definition:
system-mutex.h:50
ns3::SystemMutexPrivate::Lock
void Lock(void)
Definition:
unix-system-mutex.cc:75
ns3::CriticalSection::m_mutex
SystemMutex & m_mutex
Definition:
system-mutex.h:115
ns3::SystemMutexPrivate::Unlock
void Unlock(void)
Definition:
unix-system-mutex.cc:89
ns3::SystemMutex::~SystemMutex
~SystemMutex()
Definition:
unix-system-mutex.cc:108
log.h
ns3::SystemMutexPrivate
Definition:
unix-system-mutex.cc:34
src
core
model
unix-system-mutex.cc
Generated on Sat Apr 19 2014 14:06:52 for ns-3 by
1.8.6