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 public:
45 
46  void Lock (void);
47  void Unlock (void);
48 private:
49  pthread_mutex_t m_mutex;
50 };
51 
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 
76 {
77  NS_LOG_FUNCTION (this);
78  pthread_mutex_destroy (&m_mutex);
79 }
80 
81 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
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 
110  : m_priv (new SystemMutexPrivate ())
111 {
112  NS_LOG_FUNCTION (this);
113 }
114 
116 {
117  NS_LOG_FUNCTION (this);
118  delete m_priv;
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION (this);
125  m_priv->Lock ();
126 }
127 
128 void
130 {
131  NS_LOG_FUNCTION (this);
132  m_priv->Unlock ();
133 }
134 
136  : m_mutex (mutex)
137 {
138  NS_LOG_FUNCTION (this << &mutex);
139  m_mutex.Lock ();
140 }
141 
143 {
144  NS_LOG_FUNCTION (this);
145  m_mutex.Unlock ();
146 }
147 
148 } // 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:162
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:215
void Unlock()
Release ownership of the Mutual Exclusion object.
Prefix all trace prints with simulation time.
Definition: log.h:118
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:130
void Unlock(void)
Release ownership of the mutex.
Debug message logging.
System-dependent implementation of SystemMutex.