src/core/unix-system-condition.cc
author craigdo@craig-dowells-imac.local
Thu, 17 Jul 2008 18:57:08 -0700
changeset 3434 ee113f8d83db
parent 3425 c69779f5e51e
permissions -rw-r--r--
fix darwin build break
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>
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#include <errno.h>
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
#include <sys/time.h>
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "fatal-error.h"
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include "system-condition.h"
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include "log.h"
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
NS_LOG_COMPONENT_DEFINE ("SystemCondition");
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
namespace ns3 {
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
class SystemConditionPrivate {    
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
public: 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
  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
    33
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
  SystemConditionPrivate ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
  ~SystemConditionPrivate ();
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 SetCondition (bool condition);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
  bool GetCondition (void);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
  void Signal (void);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
  void Broadcast (void);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
  void Wait (void);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
  bool TimedWait (uint64_t ns);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
private:
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
  pthread_mutex_t m_mutex;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
  pthread_cond_t  m_cond;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
  bool m_condition;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
};
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
SystemConditionPrivate::SystemConditionPrivate ()
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
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
  m_condition = false;
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
  pthread_mutexattr_t mAttr;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
  pthread_mutexattr_init (&mAttr);
3434
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    58
//
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    59
// 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
    60
// error checking flags just to make life difficult.
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    61
//
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    62
#if defined (PTHREAD_MUTEX_ERRORCHECK_NP)
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
  pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK_NP);
3434
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    64
#else
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    65
  pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK);
ee113f8d83db fix darwin build break
craigdo@craig-dowells-imac.local
parents: 3425
diff changeset
    66
#endif
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
  pthread_mutex_init (&m_mutex, &mAttr);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
  pthread_condattr_t cAttr;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
  pthread_condattr_init (&cAttr);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
  pthread_condattr_setpshared (&cAttr, PTHREAD_PROCESS_PRIVATE);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
  pthread_cond_init (&m_cond, &cAttr);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
}
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
SystemConditionPrivate::~SystemConditionPrivate() 
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
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  pthread_mutex_destroy (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  pthread_cond_destroy (&m_cond);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
}
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
  void 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
SystemConditionPrivate::SetCondition (bool condition)
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
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  m_condition = condition;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
}
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
  bool
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
SystemConditionPrivate::GetCondition (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
{
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  return m_condition;
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
	
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  void 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
SystemConditionPrivate::Signal (void)
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
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  pthread_cond_signal (&m_cond);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
  pthread_mutex_unlock (&m_mutex);
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
  void 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
SystemConditionPrivate::Broadcast (void)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
{
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
  NS_LOG_FUNCTION_NOARGS ();
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
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  pthread_cond_broadcast (&m_cond);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
  pthread_mutex_unlock (&m_mutex);
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
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
  void 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
SystemConditionPrivate::Wait (void)
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
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
  pthread_mutex_lock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
  m_condition = false;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
  while (m_condition == false)
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
      pthread_cond_wait (&m_cond, &m_mutex);
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_mutex_unlock (&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
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
  bool
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
SystemConditionPrivate::TimedWait (uint64_t ns)
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
  NS_LOG_FUNCTION_NOARGS ();
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
  struct timespec ts;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
  ts.tv_sec = ns / NS_PER_SEC;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
  ts.tv_nsec = ns % NS_PER_SEC;
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
  struct timeval tv;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
  gettimeofday(&tv, NULL);
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
  ts.tv_sec += tv.tv_sec;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
  ts.tv_nsec += tv.tv_usec * 1000;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
  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
   145
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
      ++ts.tv_sec;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
      ts.tv_nsec %= NS_PER_SEC;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
    }
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
  int rc;
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
  while (m_condition == false)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
      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
   156
      if (rc == ETIMEDOUT)
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
        {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
          pthread_mutex_unlock (&m_mutex); 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
         return true;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
        }
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_unlock (&m_mutex);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
  return false;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
}
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
SystemCondition::SystemCondition() 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
  : m_priv (new SystemConditionPrivate ())
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
{
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
SystemCondition::~SystemCondition () 
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
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
  delete m_priv;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
  void 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
SystemCondition::SetCondition (bool condition) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
{
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
  m_priv->SetCondition (condition);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
  bool
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
SystemCondition::GetCondition (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
{
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
  return m_priv->GetCondition ();
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
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
  void 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
SystemCondition::Signal (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
{
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
  m_priv->Signal ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
}
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
  void 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
SystemCondition::Broadcast (void) 
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
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
  m_priv->Broadcast ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
}  
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
  void 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
SystemCondition::Wait (void) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
{
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
  m_priv->Wait ();
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
  bool
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
SystemCondition::TimedWait (uint64_t ns) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   215
{
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
  NS_LOG_FUNCTION_NOARGS ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   217
  return m_priv->TimedWait (ns);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   218
}  
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   219
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   220
} // namespace ns3