src/core/model/unix-system-condition.cc
author Maja Grubišić <maja.grubisic@live.com>
Sat, 10 Nov 2012 19:16:38 +0100
changeset 9134 7a750f032acd
parent 9063 32755d0516f4
child 10602 c09b70543458
permissions -rw-r--r--
Clean up function logging of core module.
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
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
NS_LOG_COMPONENT_DEFINE ("SystemCondition");
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
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    32
class SystemConditionPrivate {
3425
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
  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
    35
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
  SystemConditionPrivate ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
  ~SystemConditionPrivate ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
	
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
  void SetCondition (bool condition);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
  bool GetCondition (void);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
  void Signal (void);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
  void Broadcast (void);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
  void Wait (void);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
  bool TimedWait (uint64_t ns);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
private:
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
  pthread_mutex_t m_mutex;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
  pthread_cond_t  m_cond;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
  bool m_condition;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
};
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
SystemConditionPrivate::SystemConditionPrivate ()
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    54
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
  m_condition = false;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
  pthread_mutexattr_t mAttr;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
  pthread_mutexattr_init (&mAttr);
3434
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    60
//
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    61
// 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
    62
// error checking flags just to make life difficult.
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    63
//
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    64
#if defined (PTHREAD_MUTEX_ERRORCHECK_NP)
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK_NP);
3434
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    66
#else
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    67
  pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK);
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    68
#endif
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
  pthread_mutex_init (&m_mutex, &mAttr);
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
  pthread_condattr_t cAttr;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
  pthread_condattr_init (&cAttr);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
  pthread_condattr_setpshared (&cAttr, PTHREAD_PROCESS_PRIVATE);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
  pthread_cond_init (&m_cond, &cAttr);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
}
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    76
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
SystemConditionPrivate::~SystemConditionPrivate() 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    79
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
  pthread_mutex_destroy (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
  pthread_cond_destroy (&m_cond);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
	
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    84
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
SystemConditionPrivate::SetCondition (bool condition)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    87
  NS_LOG_FUNCTION (this << condition);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  m_condition = 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
	
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    91
bool
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
SystemConditionPrivate::GetCondition (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    94
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
  return m_condition;
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
	
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    98
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
SystemConditionPrivate::Signal (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   101
  NS_LOG_FUNCTION (this);
3425
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
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
  pthread_cond_signal (&m_cond);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
  pthread_mutex_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
	
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   108
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
SystemConditionPrivate::Broadcast (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   111
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
  pthread_cond_broadcast (&m_cond);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
  pthread_mutex_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   118
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
SystemConditionPrivate::Wait (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   121
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
  m_condition = false;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
  while (m_condition == false)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
      pthread_cond_wait (&m_cond, &m_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
  pthread_mutex_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   132
bool
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
SystemConditionPrivate::TimedWait (uint64_t ns)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   135
  NS_LOG_FUNCTION (this << ns);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
  struct timespec ts;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
  ts.tv_sec = ns / NS_PER_SEC;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
  ts.tv_nsec = ns % NS_PER_SEC;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
  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
   142
  gettimeofday (&tv, NULL);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
  ts.tv_sec += tv.tv_sec;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
  ts.tv_nsec += tv.tv_usec * 1000;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
  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
   147
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
      ++ts.tv_sec;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
      ts.tv_nsec %= NS_PER_SEC;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
    }
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
  int rc;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
  while (m_condition == false)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
      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
   158
      if (rc == ETIMEDOUT)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
        {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
          pthread_mutex_unlock (&m_mutex); 
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   161
          return true;
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
        }
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
    }
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
  pthread_mutex_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
  return false;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
}
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
SystemCondition::SystemCondition() 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
  : m_priv (new SystemConditionPrivate ())
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   171
  NS_LOG_FUNCTION (this);;
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   173
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   174
SystemCondition::~SystemCondition () 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   176
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
  delete m_priv;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   180
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
SystemCondition::SetCondition (bool condition) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   183
  NS_LOG_FUNCTION (this << condition);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
  m_priv->SetCondition (condition);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   187
bool
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
SystemCondition::GetCondition (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   190
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
  return m_priv->GetCondition ();
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
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   194
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
SystemCondition::Signal (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   197
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
  m_priv->Signal ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   201
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   202
SystemCondition::Broadcast (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   204
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
  m_priv->Broadcast ();
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   206
}
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   208
void
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
SystemCondition::Wait (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   211
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
  m_priv->Wait ();
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   213
}
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   215
bool
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
SystemCondition::TimedWait (uint64_t ns) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   217
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   218
  NS_LOG_FUNCTION (this << ns);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   219
  return m_priv->TimedWait (ns);
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   220
}
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   221
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   222
} // namespace ns3