src/core/unix-system-mutex.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3433 d1e0154c45b3
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
craigdo@3425
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
craigdo@3425
     2
/*
craigdo@3425
     3
 * Copyright (c) 2008 INRIA
craigdo@3425
     4
 *
craigdo@3425
     5
 * This program is free software; you can redistribute it and/or modify
craigdo@3425
     6
 * it under the terms of the GNU General Public License version 2 as
craigdo@3425
     7
 * published by the Free Software Foundation;
craigdo@3425
     8
 *
craigdo@3425
     9
 * This program is distributed in the hope that it will be useful,
craigdo@3425
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
craigdo@3425
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
craigdo@3425
    12
 * GNU General Public License for more details.
craigdo@3425
    13
 *
craigdo@3425
    14
 * You should have received a copy of the GNU General Public License
craigdo@3425
    15
 * along with this program; if not, write to the Free Software
craigdo@3425
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
craigdo@3425
    17
 *
craigdo@3425
    18
 * Author: Mathieu Lacage <mathieu.lacage.inria.fr>
craigdo@3425
    19
 */
craigdo@3425
    20
craigdo@3425
    21
#include <pthread.h>
vincent@3488
    22
#include <string.h>
craigdo@3425
    23
#include <errno.h>
craigdo@3425
    24
#include "fatal-error.h"
craigdo@3425
    25
#include "system-mutex.h"
craigdo@3425
    26
#include "log.h"
craigdo@3425
    27
craigdo@3425
    28
NS_LOG_COMPONENT_DEFINE ("SystemMutex");
craigdo@3425
    29
craigdo@3425
    30
namespace ns3 {
craigdo@3425
    31
craigdo@3425
    32
class SystemMutexPrivate {    
craigdo@3425
    33
public: 
craigdo@3425
    34
  SystemMutexPrivate ();
craigdo@3425
    35
  ~SystemMutexPrivate ();
craigdo@3425
    36
	
craigdo@3425
    37
  void Lock (void);
craigdo@3425
    38
  void Unlock (void);
craigdo@3425
    39
private:
craigdo@3425
    40
  pthread_mutex_t m_mutex;
craigdo@3425
    41
};
craigdo@3425
    42
craigdo@3425
    43
SystemMutexPrivate::SystemMutexPrivate ()
craigdo@3425
    44
{
craigdo@3425
    45
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
    46
craigdo@3425
    47
  pthread_mutexattr_t attr;
craigdo@3425
    48
  pthread_mutexattr_init (&attr);
craigdo@3425
    49
//
craigdo@3425
    50
// Make this an error checking mutex.  This will check to see if the current
craigdo@3425
    51
// thread already owns the mutex before trying to lock it.  Instead of 
craigdo@3425
    52
// deadlocking it returns an error.  It will also check to make sure a thread
craigdo@3425
    53
// has previously called pthread_mutex_lock when it calls pthread_mutex_unlock.
craigdo@3425
    54
//
craigdo@3433
    55
// Linux and OS X (at least) have, of course chosen different names for the 
craigdo@3433
    56
// error checking flags just to make life difficult.
craigdo@3433
    57
//
craigdo@3433
    58
#if defined (PTHREAD_MUTEX_ERRORCHECK_NP)
craigdo@3425
    59
  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
craigdo@3433
    60
#else
craigdo@3433
    61
  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
craigdo@3433
    62
#endif
craigdo@3425
    63
  pthread_mutex_init (&m_mutex, &attr);
craigdo@3425
    64
}
craigdo@3425
    65
    
craigdo@3425
    66
SystemMutexPrivate::~SystemMutexPrivate() 
craigdo@3425
    67
{
craigdo@3425
    68
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
    69
  pthread_mutex_destroy (&m_mutex);
craigdo@3425
    70
}
craigdo@3425
    71
	
craigdo@3425
    72
  void 
craigdo@3425
    73
SystemMutexPrivate::Lock (void)
craigdo@3425
    74
{
craigdo@3425
    75
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
    76
craigdo@3425
    77
  int rc = pthread_mutex_lock (&m_mutex);
craigdo@3425
    78
  if (rc != 0) 
craigdo@3425
    79
    {
craigdo@3425
    80
      NS_FATAL_ERROR ("SystemMutexPrivate::Lock()"
craigdo@3425
    81
        "pthread_mutex_lock failed: " << rc << " = \"" << 
craigdo@3425
    82
        strerror(rc) << "\"");
craigdo@3425
    83
    }
craigdo@3425
    84
}
craigdo@3425
    85
	
craigdo@3425
    86
  void
craigdo@3425
    87
SystemMutexPrivate::Unlock (void) 
craigdo@3425
    88
{
craigdo@3425
    89
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
    90
craigdo@3425
    91
  int rc = pthread_mutex_unlock (&m_mutex);
craigdo@3425
    92
  if (rc != 0)
craigdo@3425
    93
    {
craigdo@3425
    94
      NS_FATAL_ERROR ("SystemMutexPrivate::Unlock()"
craigdo@3425
    95
        "pthread_mutex_unlock failed: " << rc << " = \"" << 
craigdo@3425
    96
        strerror(rc) << "\"");
craigdo@3425
    97
    }
craigdo@3425
    98
}
craigdo@3425
    99
craigdo@3425
   100
SystemMutex::SystemMutex() 
craigdo@3425
   101
  : m_priv (new SystemMutexPrivate ())
craigdo@3425
   102
{
craigdo@3425
   103
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   104
}
craigdo@3425
   105
craigdo@3425
   106
SystemMutex::~SystemMutex() 
craigdo@3425
   107
{
craigdo@3425
   108
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   109
  delete m_priv;
craigdo@3425
   110
}
craigdo@3425
   111
craigdo@3425
   112
  void 
craigdo@3425
   113
SystemMutex::Lock() 
craigdo@3425
   114
{
craigdo@3425
   115
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   116
  m_priv->Lock ();
craigdo@3425
   117
}
craigdo@3425
   118
craigdo@3425
   119
  void 
craigdo@3425
   120
SystemMutex::Unlock() 
craigdo@3425
   121
{
craigdo@3425
   122
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   123
  m_priv->Unlock ();
craigdo@3425
   124
}  
craigdo@3425
   125
craigdo@3425
   126
CriticalSection::CriticalSection (SystemMutex &mutex)
craigdo@3425
   127
  : m_mutex(mutex)
craigdo@3425
   128
{
craigdo@3425
   129
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   130
  m_mutex.Lock ();
craigdo@3425
   131
}
craigdo@3425
   132
craigdo@3425
   133
CriticalSection::~CriticalSection ()
craigdo@3425
   134
{
craigdo@3425
   135
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   136
  m_mutex.Unlock ();
craigdo@3425
   137
}
craigdo@3425
   138
craigdo@3425
   139
} // namespace ns3