A Discrete-Event Network Simulator
API
 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 
35 public:
38 
39  void Lock (void);
40  void Unlock (void);
41 private:
42  pthread_mutex_t m_mutex;
43 };
44 
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 
69 {
70  NS_LOG_FUNCTION (this);
71  pthread_mutex_destroy (&m_mutex);
72 }
73 
74 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
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 
103  : m_priv (new SystemMutexPrivate ())
104 {
105  NS_LOG_FUNCTION (this);
106 }
107 
109 {
110  NS_LOG_FUNCTION (this);
111  delete m_priv;
112 }
113 
114 void
116 {
117  NS_LOG_FUNCTION (this);
118  m_priv->Lock ();
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION (this);
125  m_priv->Unlock ();
126 }
127 
129  : m_mutex (mutex)
130 {
131  NS_LOG_FUNCTION (this << &mutex);
132  m_mutex.Lock ();
133 }
134 
136 {
137  NS_LOG_FUNCTION (this);
138  m_mutex.Unlock ();
139 }
140 
141 } // namespace ns3
SystemMutexPrivate * m_priv
Definition: system-mutex.h:67
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
void Lock()
Acquire ownership of the Mutual Exclusion object.
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
CriticalSection(SystemMutex &mutex)
NS_LOG_COMPONENT_DEFINE("SystemMutex")
void Unlock()
Release ownership of the Mutual Exclusion object.
A class which provides a relatively platform-independent Mutual Exclusion thread synchronization prim...
Definition: system-mutex.h:50
SystemMutex & m_mutex
Definition: system-mutex.h:115