src/core/system-condition.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 University of Washington
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
craigdo@3425
    19
#ifndef SYSTEM_CONDITION_H
craigdo@3425
    20
#define SYSTEM_CONDITION_H
craigdo@3425
    21
craigdo@3425
    22
#include "ptr.h"
craigdo@3425
    23
craigdo@3425
    24
namespace ns3 { 
craigdo@3425
    25
	
craigdo@3425
    26
class SystemConditionPrivate;
craigdo@3425
    27
craigdo@3425
    28
/**
craigdo@3425
    29
 * @brief A class which provides a relatively platform-independent 
craigdo@3425
    30
 * conditional-wait thread synchronization primitive.
craigdo@3425
    31
 *
craigdo@3425
    32
 * It is often desirable to have a mechanism by which a thread can suspend its
craigdo@3425
    33
 * execution and relinquish the process until some condition to becomes true.
craigdo@3425
    34
 * We provide platform-independent access to this OS-dependent capability with
craigdo@3425
    35
 * the SystemCondition class.
craigdo@3425
    36
 *
craigdo@3425
    37
 * There are two ways to tell the underlying primitive that the condition has
craigdo@3425
    38
 * become true:  Signal and Broadcast.  Signal will only wake up one thread
craigdo@3425
    39
 * waiting on the condition (according to the OS scheduling policy); 
craigdo@3425
    40
 * Broadcast will wake up all of the threads waiting on the condition 
craigdo@3425
    41
 * (cf. "The Thundering Herd").
craigdo@3425
    42
 * 
craigdo@3425
    43
 * In order to wait for the underlying condition, you also have two
craigdo@3425
    44
 * alternatives:  Wait and TimedWait.  The Wait call will wait forever for the
craigdo@3425
    45
 * condition to become true; but the TimedWait has a timeout.
craigdo@3425
    46
 *
craigdo@3425
    47
 * The condition underlying this class is a simple boolean variable.  It is
craigdo@3425
    48
 * set to false in each call to Wait and TimedWait.  It is set to true in each
craigdo@3425
    49
 * call to Signal and Broadcast.  This is a fairly simple-minded condition
craigdo@3425
    50
 * designed for 
craigdo@3425
    51
 *
craigdo@3425
    52
 * A typical use case will be to call Wait() or TimedWait() in one thread
craigdo@3425
    53
 * context and put the processor to sleep until an event happens somewhere 
craigdo@3425
    54
 * else that 
craigdo@3425
    55
 */
craigdo@3425
    56
class SystemCondition
craigdo@3425
    57
{
craigdo@3425
    58
public:
craigdo@3425
    59
  SystemCondition ();
craigdo@3425
    60
  ~SystemCondition ();
craigdo@3425
    61
craigdo@3425
    62
  /**
craigdo@3425
    63
   * Set the value of the underlying condition.
craigdo@3425
    64
   */
craigdo@3425
    65
  void SetCondition (bool condition);
craigdo@3425
    66
craigdo@3425
    67
  /**
craigdo@3425
    68
   * Get the value of the underlying condition.
craigdo@3425
    69
   */
craigdo@3425
    70
  bool GetCondition (void);
craigdo@3425
    71
craigdo@3425
    72
  /**
craigdo@3425
    73
   * Release one thread if waiting for the condition to be true.  If you want 
craigdo@3425
    74
   * a waiting thread to return, you should have done a SetCondition (true)
craigdo@3425
    75
   * prior to calling.
craigdo@3425
    76
   */
craigdo@3425
    77
  void Signal (void);
craigdo@3425
    78
craigdo@3425
    79
  /**
craigdo@3425
    80
   * Release all threads waiting for the condition to be true.  If you want 
craigdo@3425
    81
   * all waiting threads to return, you should have done a SetCondition (true)
craigdo@3425
    82
   * prior to calling.
craigdo@3425
    83
   */
craigdo@3425
    84
  void Broadcast (void);
craigdo@3425
    85
craigdo@3425
    86
  /**
craigdo@3425
    87
   * Wait, possibly forever, for the condition to be true.  
craigdo@3425
    88
   */
craigdo@3425
    89
  void Wait (void);
craigdo@3425
    90
	
craigdo@3425
    91
  /**
craigdo@3425
    92
   * Wait a maximum of ns nanoseconds for the condition to be true.  If the
craigdo@3425
    93
   * wait times out, return true else return false.
craigdo@3425
    94
   */
craigdo@3425
    95
  bool TimedWait (uint64_t ns);
craigdo@3425
    96
	
craigdo@3425
    97
craigdo@3425
    98
private:
craigdo@3425
    99
  SystemConditionPrivate * m_priv;    
craigdo@3425
   100
};
craigdo@3425
   101
craigdo@3425
   102
} //namespace ns3
craigdo@3425
   103
craigdo@3425
   104
#endif /* SYSTEM_CONDITION_H */
craigdo@3425
   105
craigdo@3425
   106