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
36namespace ns3 {
37
39
42{
43public:
46
47 void Lock (void);
48 void Unlock (void);
50private:
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
83void
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
97void
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
123void
125{
126 NS_LOG_FUNCTION (this);
127 m_priv->Lock ();
128}
129
130void
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
SystemMutex & m_mutex
The mutex.
Definition: system-mutex.h:131
CriticalSection(SystemMutex &mutex)
Construct with the required SystemMutex.
A class which provides a relatively platform-independent Mutual Exclusion thread synchronization prim...
Definition: system-mutex.h:59
SystemMutexPrivate * m_priv
The (system-dependent) implementation.
Definition: system-mutex.h:76
void Lock()
Acquire ownership of the Mutual Exclusion object.
void Unlock()
Release ownership of the Mutual Exclusion object.
System-dependent implementation of SystemMutex.
void Unlock(void)
Release ownership of the mutex.
pthread_mutex_t m_mutex
The mutex.
void Lock(void)
Acquire ownership of the mutex.
NS_FATAL_x macro definitions.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE_MASK(name, mask)
Define a logging component with a mask.
Definition: log.h:216
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
System-independent mutex primitive, ns3::SystemMutex, and ns3::CriticalSection.