src/core/system-mutex.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
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
#ifndef SYSTEM_MUTEX_H
craigdo@3425
    22
#define SYSTEM_MUTEX_H
craigdo@3425
    23
craigdo@3425
    24
#include "ptr.h"
craigdo@3425
    25
craigdo@3425
    26
namespace ns3 { 
craigdo@3425
    27
	
craigdo@3425
    28
class SystemMutexPrivate;
craigdo@3425
    29
craigdo@3425
    30
/**
craigdo@3425
    31
 * @brief A class which provides a relatively platform-independent Mutual
craigdo@3425
    32
 * Exclusion thread synchronization primitive.
craigdo@3425
    33
 *
craigdo@3425
    34
 * When more than one thread needs to access a shared resource (data structure
craigdo@3425
    35
 * or device), the system needs to provide a way to serialize access to the
craigdo@3425
    36
 * resource.  An operating system will typically provide a Mutual Exclusion
craigdo@3425
    37
 * primitive to provide that capability.  We provide plattorm-independent
craigdo@3425
    38
 * access to the OS-dependent capability with the SystemMutex class.
craigdo@3425
    39
 *
craigdo@3425
    40
 * There are two operations:  Lock and Unlock.  Lock allows an executing 
craigdo@3425
    41
 * SystemThread to attempt to acquire ownership of the Mutual Exclusion 
craigdo@3425
    42
 * object.  If the SystemMutex object is not owned by another thread, then
craigdo@3425
    43
 * ownership is granted to the calling SystemThread and Lock returns 
craigdo@3425
    44
 * immediately,  However, if the SystemMutex is already owned by another
craigdo@3425
    45
 * SystemThread, the calling SystemThread is blocked until the current owner
craigdo@3425
    46
 * releases the SystemMutex by calling Unlock.
craigdo@3425
    47
 *
craigdo@3425
    48
 * @see CriticalSection
craigdo@3425
    49
 */
craigdo@3425
    50
class SystemMutex 
craigdo@3425
    51
{
craigdo@3425
    52
public:
craigdo@3425
    53
  SystemMutex ();
craigdo@3425
    54
  ~SystemMutex ();
craigdo@3425
    55
craigdo@3425
    56
  /**
craigdo@3425
    57
   * Acquire ownership of the Mutual Exclusion object.
craigdo@3425
    58
   */
craigdo@3425
    59
  void Lock ();
craigdo@3425
    60
craigdo@3425
    61
  /**
craigdo@3425
    62
   * Release ownership of the Mutual Exclusion object.
craigdo@3425
    63
   */
craigdo@3425
    64
  void Unlock ();
craigdo@3425
    65
	
craigdo@3425
    66
private:
craigdo@3425
    67
  SystemMutexPrivate * m_priv;    
craigdo@3425
    68
};
craigdo@3425
    69
craigdo@3425
    70
/**
craigdo@3425
    71
 * @brief A class which provides a simple way to implement a Critical Section.
craigdo@3425
    72
 *
craigdo@3425
    73
 * When more than one SystemThread needs to access a shared resource, we
craigdo@3425
    74
 * conrol access by acquiring a SystemMutex.  The CriticalSection class uses
craigdo@3425
    75
 * the C++ scoping rules to automatically perform the required Lock and Unlock
craigdo@3425
    76
 * operations to implement a Critical Section.
craigdo@3425
    77
 *
craigdo@3425
    78
 * If one wants to treat an entire method call as a critical section, one would
craigdo@3425
    79
 * do something like,
craigdo@3425
    80
 *
craigdo@3425
    81
 * Class::Method ()
craigdo@3425
    82
 * {
craigdo@3425
    83
 *   CriticalSection cs (mutex);
craigdo@3425
    84
 *   ...
craigdo@3425
    85
 * }
craigdo@3425
    86
 *
craigdo@3425
    87
 * In this case, the critical section is entered when the CriticalSection 
craigdo@3425
    88
 * object is created, and the critical section is exited when the 
craigdo@3425
    89
 * CriticalSection object goes out of scope at the end of the method.
craigdo@3425
    90
 *
craigdo@3425
    91
 * Finer granularity is achieved by using local scope blocks.
craigdo@3425
    92
 *
craigdo@3425
    93
 * Class::Method ()
craigdo@3425
    94
 * {
craigdo@3425
    95
 *   ...
craigdo@3425
    96
 *   {
craigdo@3425
    97
 *     CriticalSection cs (mutex);
craigdo@3425
    98
 *   }
craigdo@3425
    99
 *   ...
craigdo@3425
   100
 * }
craigdo@3425
   101
 *
craigdo@3425
   102
 * Here, the critical section is entered partway through the method when the
craigdo@3425
   103
 * CriticalSection object is created in the local scope block (the braces).
craigdo@3425
   104
 * The critical section is exited when the CriticalSection object goes out of
craigdo@3425
   105
 * scope at the end of block.
craigdo@3425
   106
 *
craigdo@3425
   107
 * @see SystemMutex
craigdo@3425
   108
 */
craigdo@3425
   109
class CriticalSection
craigdo@3425
   110
{
craigdo@3425
   111
public:
craigdo@3425
   112
  CriticalSection (SystemMutex &mutex);
craigdo@3425
   113
  ~CriticalSection ();
craigdo@3425
   114
private:
craigdo@3425
   115
  SystemMutex &m_mutex;
craigdo@3425
   116
};
craigdo@3425
   117
craigdo@3425
   118
} //namespace ns3
craigdo@3425
   119
craigdo@3425
   120
#endif /* SYSTEM_MUTEX_H */