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