src/core/unix-system-condition.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3425 c69779f5e51e
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
craigdo@3425
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
craigdo@3425
     2
/*
craigdo@3425
     3
 * Copyright (c) 2008 University of Washington
craigdo@3425
     4
 *
craigdo@3425
     5
 * This program is free software; you can redistribute it and/or modify
craigdo@3425
     6
 * it under the terms of the GNU General Public License version 2 as
craigdo@3425
     7
 * published by the Free Software Foundation;
craigdo@3425
     8
 *
craigdo@3425
     9
 * This program is distributed in the hope that it will be useful,
craigdo@3425
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
craigdo@3425
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
craigdo@3425
    12
 * GNU General Public License for more details.
craigdo@3425
    13
 *
craigdo@3425
    14
 * You should have received a copy of the GNU General Public License
craigdo@3425
    15
 * along with this program; if not, write to the Free Software
craigdo@3425
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
craigdo@3425
    17
 */
craigdo@3425
    18
craigdo@3425
    19
#include <pthread.h>
craigdo@3425
    20
#include <errno.h>
craigdo@3425
    21
#include <sys/time.h>
craigdo@3425
    22
#include "fatal-error.h"
craigdo@3425
    23
#include "system-condition.h"
craigdo@3425
    24
#include "log.h"
craigdo@3425
    25
craigdo@3425
    26
NS_LOG_COMPONENT_DEFINE ("SystemCondition");
craigdo@3425
    27
craigdo@3425
    28
namespace ns3 {
craigdo@3425
    29
craigdo@3425
    30
class SystemConditionPrivate {    
craigdo@3425
    31
public: 
craigdo@3425
    32
  static const uint64_t NS_PER_SEC = (uint64_t)1000000000;
craigdo@3425
    33
craigdo@3425
    34
  SystemConditionPrivate ();
craigdo@3425
    35
  ~SystemConditionPrivate ();
craigdo@3425
    36
	
craigdo@3425
    37
  void SetCondition (bool condition);
craigdo@3425
    38
  bool GetCondition (void);
craigdo@3425
    39
  void Signal (void);
craigdo@3425
    40
  void Broadcast (void);
craigdo@3425
    41
  void Wait (void);
craigdo@3425
    42
  bool TimedWait (uint64_t ns);
craigdo@3425
    43
craigdo@3425
    44
private:
craigdo@3425
    45
  pthread_mutex_t m_mutex;
craigdo@3425
    46
  pthread_cond_t  m_cond;
craigdo@3425
    47
  bool m_condition;
craigdo@3425
    48
};
craigdo@3425
    49
craigdo@3425
    50
SystemConditionPrivate::SystemConditionPrivate ()
craigdo@3425
    51
{
craigdo@3425
    52
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
    53
craigdo@3425
    54
  m_condition = false;
craigdo@3425
    55
craigdo@3425
    56
  pthread_mutexattr_t mAttr;
craigdo@3425
    57
  pthread_mutexattr_init (&mAttr);
craigdo@3434
    58
//
craigdo@3434
    59
// Linux and OS X (at least) have, of course chosen different names for the 
craigdo@3434
    60
// error checking flags just to make life difficult.
craigdo@3434
    61
//
craigdo@3434
    62
#if defined (PTHREAD_MUTEX_ERRORCHECK_NP)
craigdo@3425
    63
  pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK_NP);
craigdo@3434
    64
#else
craigdo@3434
    65
  pthread_mutexattr_settype (&mAttr, PTHREAD_MUTEX_ERRORCHECK);
craigdo@3434
    66
#endif
craigdo@3425
    67
  pthread_mutex_init (&m_mutex, &mAttr);
craigdo@3425
    68
craigdo@3425
    69
  pthread_condattr_t cAttr;
craigdo@3425
    70
  pthread_condattr_init (&cAttr);
craigdo@3425
    71
  pthread_condattr_setpshared (&cAttr, PTHREAD_PROCESS_PRIVATE);
craigdo@3425
    72
  pthread_cond_init (&m_cond, &cAttr);
craigdo@3425
    73
}
craigdo@3425
    74
    
craigdo@3425
    75
SystemConditionPrivate::~SystemConditionPrivate() 
craigdo@3425
    76
{
craigdo@3425
    77
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
    78
  pthread_mutex_destroy (&m_mutex);
craigdo@3425
    79
  pthread_cond_destroy (&m_cond);
craigdo@3425
    80
}
craigdo@3425
    81
	
craigdo@3425
    82
  void 
craigdo@3425
    83
SystemConditionPrivate::SetCondition (bool condition)
craigdo@3425
    84
{
craigdo@3425
    85
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
    86
  m_condition = condition;
craigdo@3425
    87
}
craigdo@3425
    88
	
craigdo@3425
    89
  bool
craigdo@3425
    90
SystemConditionPrivate::GetCondition (void)
craigdo@3425
    91
{
craigdo@3425
    92
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
    93
  return m_condition;
craigdo@3425
    94
}
craigdo@3425
    95
	
craigdo@3425
    96
  void 
craigdo@3425
    97
SystemConditionPrivate::Signal (void)
craigdo@3425
    98
{
craigdo@3425
    99
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   100
craigdo@3425
   101
  pthread_mutex_lock (&m_mutex);
craigdo@3425
   102
  pthread_cond_signal (&m_cond);
craigdo@3425
   103
  pthread_mutex_unlock (&m_mutex);
craigdo@3425
   104
}
craigdo@3425
   105
	
craigdo@3425
   106
  void 
craigdo@3425
   107
SystemConditionPrivate::Broadcast (void)
craigdo@3425
   108
{
craigdo@3425
   109
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   110
craigdo@3425
   111
  pthread_mutex_lock (&m_mutex);
craigdo@3425
   112
  pthread_cond_broadcast (&m_cond);
craigdo@3425
   113
  pthread_mutex_unlock (&m_mutex);
craigdo@3425
   114
}
craigdo@3425
   115
craigdo@3425
   116
  void 
craigdo@3425
   117
SystemConditionPrivate::Wait (void)
craigdo@3425
   118
{
craigdo@3425
   119
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   120
craigdo@3425
   121
  pthread_mutex_lock (&m_mutex);
craigdo@3425
   122
  m_condition = false;
craigdo@3425
   123
  while (m_condition == false)
craigdo@3425
   124
    {
craigdo@3425
   125
      pthread_cond_wait (&m_cond, &m_mutex);
craigdo@3425
   126
    }
craigdo@3425
   127
  pthread_mutex_unlock (&m_mutex);
craigdo@3425
   128
}
craigdo@3425
   129
craigdo@3425
   130
  bool
craigdo@3425
   131
SystemConditionPrivate::TimedWait (uint64_t ns)
craigdo@3425
   132
{
craigdo@3425
   133
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   134
craigdo@3425
   135
  struct timespec ts;
craigdo@3425
   136
  ts.tv_sec = ns / NS_PER_SEC;
craigdo@3425
   137
  ts.tv_nsec = ns % NS_PER_SEC;
craigdo@3425
   138
craigdo@3425
   139
  struct timeval tv;
craigdo@3425
   140
  gettimeofday(&tv, NULL);
craigdo@3425
   141
craigdo@3425
   142
  ts.tv_sec += tv.tv_sec;
craigdo@3425
   143
  ts.tv_nsec += tv.tv_usec * 1000;
craigdo@3425
   144
  if (ts.tv_nsec > (int64_t)NS_PER_SEC)
craigdo@3425
   145
    {
craigdo@3425
   146
      ++ts.tv_sec;
craigdo@3425
   147
      ts.tv_nsec %= NS_PER_SEC;
craigdo@3425
   148
    }
craigdo@3425
   149
craigdo@3425
   150
  int rc;
craigdo@3425
   151
craigdo@3425
   152
  pthread_mutex_lock (&m_mutex);
craigdo@3425
   153
  while (m_condition == false)
craigdo@3425
   154
    {
craigdo@3425
   155
      rc = pthread_cond_timedwait (&m_cond, &m_mutex, &ts);
craigdo@3425
   156
      if (rc == ETIMEDOUT)
craigdo@3425
   157
        {
craigdo@3425
   158
          pthread_mutex_unlock (&m_mutex); 
craigdo@3425
   159
         return true;
craigdo@3425
   160
        }
craigdo@3425
   161
    }
craigdo@3425
   162
  pthread_mutex_unlock (&m_mutex);
craigdo@3425
   163
  return false;
craigdo@3425
   164
}
craigdo@3425
   165
	
craigdo@3425
   166
SystemCondition::SystemCondition() 
craigdo@3425
   167
  : m_priv (new SystemConditionPrivate ())
craigdo@3425
   168
{
craigdo@3425
   169
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   170
}
craigdo@3425
   171
craigdo@3425
   172
SystemCondition::~SystemCondition () 
craigdo@3425
   173
{
craigdo@3425
   174
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   175
  delete m_priv;
craigdo@3425
   176
}
craigdo@3425
   177
craigdo@3425
   178
  void 
craigdo@3425
   179
SystemCondition::SetCondition (bool condition) 
craigdo@3425
   180
{
craigdo@3425
   181
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   182
  m_priv->SetCondition (condition);
craigdo@3425
   183
}
craigdo@3425
   184
craigdo@3425
   185
  bool
craigdo@3425
   186
SystemCondition::GetCondition (void) 
craigdo@3425
   187
{
craigdo@3425
   188
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   189
  return m_priv->GetCondition ();
craigdo@3425
   190
}
craigdo@3425
   191
craigdo@3425
   192
  void 
craigdo@3425
   193
SystemCondition::Signal (void) 
craigdo@3425
   194
{
craigdo@3425
   195
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   196
  m_priv->Signal ();
craigdo@3425
   197
}
craigdo@3425
   198
craigdo@3425
   199
  void 
craigdo@3425
   200
SystemCondition::Broadcast (void) 
craigdo@3425
   201
{
craigdo@3425
   202
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   203
  m_priv->Broadcast ();
craigdo@3425
   204
}  
craigdo@3425
   205
craigdo@3425
   206
  void 
craigdo@3425
   207
SystemCondition::Wait (void) 
craigdo@3425
   208
{
craigdo@3425
   209
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   210
  m_priv->Wait ();
craigdo@3425
   211
}  
craigdo@3425
   212
craigdo@3425
   213
  bool
craigdo@3425
   214
SystemCondition::TimedWait (uint64_t ns) 
craigdo@3425
   215
{
craigdo@3425
   216
  NS_LOG_FUNCTION_NOARGS ();
craigdo@3425
   217
  return m_priv->TimedWait (ns);
craigdo@3425
   218
}  
craigdo@3425
   219
craigdo@3425
   220
} // namespace ns3