A Discrete-Event Network Simulator
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 
39 
42 {
43 public:
46 
47  void Lock (void);
48  void Unlock (void);
50 private:
51  pthread_mutex_t m_mutex;
52 };
53 
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 
78 {
79  NS_LOG_FUNCTION (this);
80  pthread_mutex_destroy (&m_mutex);
81 }
82 
83 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
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 
112  : m_priv (new SystemMutexPrivate ())
113 {
114  NS_LOG_FUNCTION (this);
115 }
116 
118 {
119  NS_LOG_FUNCTION (this);
120  delete m_priv;
121 }
122 
123 void
125 {
126  NS_LOG_FUNCTION (this);
127  m_priv->Lock ();
128 }
129 
130 void
132 {
133  NS_LOG_FUNCTION (this);
134  m_priv->Unlock ();
135 }
136 
138  : m_mutex (mutex)
139 {
140  NS_LOG_FUNCTION (this << &mutex);
141  m_mutex.Lock ();
142 }
143 
145 {
146  NS_LOG_FUNCTION (this);
147  m_mutex.Unlock ();
148 }
149 
150 } // namespace ns3
NS_FATAL_x macro definitions.
SystemMutexPrivate * m_priv
The (system-dependent) implementation.
Definition: system-mutex.h:76
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
pthread_mutex_t m_mutex
The mutex.
~CriticalSection()
Destructor.
void Lock()
Acquire ownership of the Mutual Exclusion object.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
System-independent mutex primitive, ns3::SystemMutex, and ns3::CriticalSection.
CriticalSection(SystemMutex &mutex)
Construct with the required SystemMutex.
#define NS_LOG_COMPONENT_DEFINE_MASK(name, mask)
Define a logging component with a mask.
Definition: log.h:216
void Unlock()
Release ownership of the Mutual Exclusion object.
Prefix all trace prints with simulation time.
Definition: log.h:119
Every class exported by the ns3 library is enclosed in the ns3 namespace.
A class which provides a relatively platform-independent Mutual Exclusion thread synchronization prim...
Definition: system-mutex.h:58
void Lock(void)
Acquire ownership of the mutex.
SystemMutex & m_mutex
The mutex.
Definition: system-mutex.h:131
void Unlock(void)
Release ownership of the mutex.
Debug message logging.
System-dependent implementation of SystemMutex.