src/core/model/unix-system-condition.cc
author Peter D. Barnes, Jr. <barnes26@llnl.gov>
Tue, 09 Dec 2014 13:17:49 -0800
changeset 11097 caafe12b0378
parent 10968 2d29fee2b7b8
child 11538 397bd6465dd9
permissions -rw-r--r--
[Doxygen] OS functions: filesystem, threading, wall clock.
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 University of Washington
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
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
#include <pthread.h>
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7256
diff changeset
    20
#include <cerrno> // for ETIMEDOUT
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
#include <sys/time.h>
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7256
diff changeset
    22
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include "fatal-error.h"
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include "system-condition.h"
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
#include "log.h"
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7256
diff changeset
    27
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    28
/**
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    29
 * \file
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    30
 * \ingroup thread
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    31
 * Thread conditional wait implementation for Unix-like systems.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    32
 */
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    33
10968
2d29fee2b7b8 [Bug 1551] Redux: NS_LOG_COMPONENT_DEFINE inside or outside of ns3 namespace?
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10602
diff changeset
    34
namespace ns3 {
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
10968
2d29fee2b7b8 [Bug 1551] Redux: NS_LOG_COMPONENT_DEFINE inside or outside of ns3 namespace?
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10602
diff changeset
    36
NS_LOG_COMPONENT_DEFINE ("SystemCondition");
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    38
/**
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    39
 * \ingroup thread
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    40
 * Implementation of SystemCondition for Unix-like systems.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    41
 */
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    42
class SystemConditionPrivate {
10602
c09b70543458 [doxygen] Fix some minor doxygen errors.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9134
diff changeset
    43
public:
c09b70543458 [doxygen] Fix some minor doxygen errors.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9134
diff changeset
    44
  /// Conversion from ns to s.
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
  static const uint64_t NS_PER_SEC = (uint64_t)1000000000;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    47
  /** Constructor. */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
  SystemConditionPrivate ();
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    49
  /** Destructor. */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
  ~SystemConditionPrivate ();
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    51
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    52
  /**
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    53
   * Set the condition.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    54
   *
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    55
   * \param condition The new condition value.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    56
   */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
  void SetCondition (bool condition);
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    58
  /**
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    59
   * Get the condition value.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    60
   *
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    61
   * \returns The condition value.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    62
   */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
  bool GetCondition (void);
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    64
  /** Signal the condition. */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  void Signal (void);
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    66
  /** Broadcast the condition. */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
  void Broadcast (void);
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    68
  /**
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    69
   * Unset the condition, then wait for another thread
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    70
   * to set it with SetCondition. */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
  void Wait (void);
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    72
  /**
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    73
   * Unset the condition, then wait for a limited amount of wall-clock
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    74
   * time for another thread to set it with SetCondition.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    75
   *
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    76
   * \param ns Maximum time to wait, in ns.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    77
   * \returns \c true if the condition timed out; \c false if the other
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    78
   * thread set it.
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    79
   */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
  bool TimedWait (uint64_t ns);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
private:
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    83
  /** Mutex controlling access to the condition. */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  pthread_mutex_t m_mutex;
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    85
  /** The pthread condition variable. */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  pthread_cond_t  m_cond;
11097
caafe12b0378 [Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
    87
  /** The condition state. */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  bool m_condition;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
};
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
SystemConditionPrivate::SystemConditionPrivate ()
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    93
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
  m_condition = false;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  pthread_mutexattr_t mAttr;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
  pthread_mutexattr_init (&mAttr);
3434
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    99
//
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
   100
// Linux and OS X (at least) have, of course chosen different names for the 
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
   101
// error checking flags just to make life difficult.
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
   102
//
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
   103
#if defined (PTHREAD_MUTEX_ERRORCHECK_NP)
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
  pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK_NP);
3434
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
   105
#else
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
   106
  pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK);
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
   107
#endif
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
  pthread_mutex_init (&m_mutex, &mAttr);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
  pthread_condattr_t cAttr;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
  pthread_condattr_init (&cAttr);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  pthread_condattr_setpshared (&cAttr, PTHREAD_PROCESS_PRIVATE);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
  pthread_cond_init (&m_cond, &cAttr);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
}
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   115
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
SystemConditionPrivate::~SystemConditionPrivate() 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   118
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
  pthread_mutex_destroy (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
  pthread_cond_destroy (&m_cond);
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
	
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   123
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
SystemConditionPrivate::SetCondition (bool condition)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   126
  NS_LOG_FUNCTION (this << condition);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
  m_condition = condition;
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
	
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   130
bool
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
SystemConditionPrivate::GetCondition (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   132
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   133
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
  return m_condition;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
	
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   137
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
SystemConditionPrivate::Signal (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   140
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
  pthread_cond_signal (&m_cond);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
  pthread_mutex_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
	
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   147
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
SystemConditionPrivate::Broadcast (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   150
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
  pthread_cond_broadcast (&m_cond);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
  pthread_mutex_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   157
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
SystemConditionPrivate::Wait (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   160
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
  m_condition = false;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
  while (m_condition == false)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
      pthread_cond_wait (&m_cond, &m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
    }
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
  pthread_mutex_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   171
bool
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
SystemConditionPrivate::TimedWait (uint64_t ns)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   173
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   174
  NS_LOG_FUNCTION (this << ns);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
  struct timespec ts;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
  ts.tv_sec = ns / NS_PER_SEC;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
  ts.tv_nsec = ns % NS_PER_SEC;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
  struct timeval tv;
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7169
diff changeset
   181
  gettimeofday (&tv, NULL);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
  ts.tv_sec += tv.tv_sec;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
  ts.tv_nsec += tv.tv_usec * 1000;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
  if (ts.tv_nsec > (int64_t)NS_PER_SEC)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
      ++ts.tv_sec;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
      ts.tv_nsec %= NS_PER_SEC;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
    }
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
  int rc;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
  while (m_condition == false)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
      rc = pthread_cond_timedwait (&m_cond, &m_mutex, &ts);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
      if (rc == ETIMEDOUT)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
        {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
          pthread_mutex_unlock (&m_mutex); 
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   200
          return true;
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   201
        }
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   202
    }
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
  pthread_mutex_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
  return false;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   206
	
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
SystemCondition::SystemCondition() 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
  : m_priv (new SystemConditionPrivate ())
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   210
  NS_LOG_FUNCTION (this);;
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
SystemCondition::~SystemCondition () 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   215
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
  delete m_priv;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   217
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   218
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   219
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   220
SystemCondition::SetCondition (bool condition) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   221
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   222
  NS_LOG_FUNCTION (this << condition);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   223
  m_priv->SetCondition (condition);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   224
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   225
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   226
bool
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   227
SystemCondition::GetCondition (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   228
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   229
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   230
  return m_priv->GetCondition ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   231
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   232
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   233
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   234
SystemCondition::Signal (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   235
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   236
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   237
  m_priv->Signal ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   238
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   239
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   240
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   241
SystemCondition::Broadcast (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   242
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   243
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   244
  m_priv->Broadcast ();
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   245
}
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   246
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   247
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   248
SystemCondition::Wait (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   249
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   250
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   251
  m_priv->Wait ();
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   252
}
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   253
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   254
bool
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   255
SystemCondition::TimedWait (uint64_t ns) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   256
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   257
  NS_LOG_FUNCTION (this << ns);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   258
  return m_priv->TimedWait (ns);
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   259
}
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   260
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   261
} // namespace ns3