src/core/model/time.cc
author Peter D. Barnes, Jr. <barnes26@llnl.gov>
Fri, 16 Aug 2013 11:10:19 -0700
changeset 10176 73dfb0c88ed6
parent 10175 d7b91afc71c6
child 10180 2564598017c1
permissions -rw-r--r--
[Bug 954] Protect g_markingTimes with a mutex.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 144
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     2
/*
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     3
 * Copyright (c) 2005,2006 INRIA
680
0bbe0e4d73c6 add reference to Emmanuelle in Authors/Copyright
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 679
diff changeset
     4
 * Copyright (c) 2007 Emmanuelle Laprise
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     5
 *
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     9
 *
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    13
 * GNU General Public License for more details.
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    14
 *
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    18
 *
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    19
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
680
0bbe0e4d73c6 add reference to Emmanuelle in Authors/Copyright
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 679
diff changeset
    20
 * TimeStep support by Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    21
 */
679
0c4773ca0f29 avoid cyclic dependency between Simulator and Time code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 678
diff changeset
    22
#include "nstime.h"
7383
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    23
#include "abort.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    24
#include "global-value.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    25
#include "enum.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    26
#include "string.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    27
#include "object.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    28
#include "config.h"
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    29
#include "system-mutex.h"
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
    30
#include "log.h"
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7579
diff changeset
    31
#include <cmath>
10174
09eca1533421 Restore "x.0ns" to result of operator<< (Time)
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10173
diff changeset
    32
#include <iomanip>  // showpos
6511
d9f23a956ef2 add missing include
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6506
diff changeset
    33
#include <sstream>
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    34
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    35
NS_LOG_COMPONENT_DEFINE ("Time");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    36
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    37
namespace ns3 {
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    38
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    39
// The set of marked times
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    40
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    41
Time::MarkedTimes * Time::g_markingTimes = 0;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    42
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    43
/**
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    44
 * Get mutex for critical sections around modification of Time::g_markingTimes
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    45
 *
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    46
 * \relates Time
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    47
 */
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    48
SystemMutex &
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    49
GetMarkingMutex ()
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    50
{
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    51
  static SystemMutex * g_markingMutex = new SystemMutex;
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    52
  return *g_markingMutex;
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    53
}
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    54
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    55
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
    56
// Function called to force static initialization
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    57
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    58
bool Time::StaticInit ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    59
{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    60
  static bool firstTime = true;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    61
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    62
  CriticalSection critical (GetMarkingMutex ());
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    63
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    64
  if (firstTime)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    65
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    66
      if (! g_markingTimes)
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    67
        {
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    68
          g_markingTimes = new Time::MarkedTimes;
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    69
        }
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    70
      else
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    71
        {
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    72
          NS_LOG_ERROR ("firstTime but g_markingTimes != 0");
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
    73
        }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
    74
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    75
      // Schedule the cleanup.
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    76
      // We'd really like:
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    77
      //   NS_LOG_LOGIC ("scheduling ClearMarkedTimes()");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    78
      //   Simulator::Schedule ( Seconds (0), & ClearMarkedTimes);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    79
      //   [or even better:  Simulator::AtStart ( & ClearMarkedTimes ); ]
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    80
      // But this triggers a static initialization order error,
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    81
      // since the Simulator static initialization may not have occurred.
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    82
      // Instead, we call ClearMarkedTimes directly from Simulator::Run ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    83
      firstTime = false;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    84
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
    85
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    86
  return firstTime;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    87
}
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    88
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
    89
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
    90
Time::Time (const std::string& s)
372
c242f488ef0b Added back corrected version of TimeUnit(string)
Raj@rajib-bhattacharjeas-computer.local
parents: 367
diff changeset
    91
{
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
    92
  NS_LOG_FUNCTION (this << &s);
7579
91a0ff9ee4eb Bug 1204 - Can't Parse Time +100000000.0ns
Mitch Watrous <watrous@u.washington.edu>
parents: 7383
diff changeset
    93
  std::string::size_type n = s.find_first_not_of ("+-0123456789.");
372
c242f488ef0b Added back corrected version of TimeUnit(string)
Raj@rajib-bhattacharjeas-computer.local
parents: 367
diff changeset
    94
  if (n != std::string::npos)
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    95
    { // Found non-numeric
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    96
      std::istringstream iss;
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    97
      iss.str (s.substr (0, n));
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    98
      double r;
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    99
      iss >> r;
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   100
      std::string trailer = s.substr (n, std::string::npos);
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   101
      if (trailer == std::string ("s"))
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   102
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   103
          *this = Time::FromDouble (r, Time::S);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   104
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   105
      else if (trailer == std::string ("ms"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   106
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   107
          *this = Time::FromDouble (r, Time::MS);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   108
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   109
      else if (trailer == std::string ("us"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   110
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   111
          *this = Time::FromDouble (r, Time::US);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   112
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   113
      else if (trailer == std::string ("ns"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   114
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   115
          *this = Time::FromDouble (r, Time::NS);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   116
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   117
      else if (trailer == std::string ("ps"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   118
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   119
          *this = Time::FromDouble (r, Time::PS);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   120
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   121
      else if (trailer == std::string ("fs"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   122
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   123
          *this = Time::FromDouble (r, Time::FS);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   124
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   125
      else
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   126
        {
10172
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   127
          NS_ABORT_MSG ("Can't Parse Time " << s);
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   128
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   129
    }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   130
  else
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   131
    {
10172
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   132
      // they didn't provide units, assume seconds
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   133
      std::istringstream iss;
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   134
      iss.str (s);
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   135
      double v;
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   136
      iss >> v;
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   137
      *this = Time::FromDouble (v, Time::S);
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   138
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   139
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   140
  if (g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   141
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   142
      Mark (this);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   143
    }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   144
}
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   145
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   146
// static
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   147
struct Time::Resolution
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   148
Time::SetDefaultNsResolution (void)
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   149
{
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   150
  NS_LOG_FUNCTION_NOARGS ();
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   151
  struct Resolution resolution;
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   152
  SetResolution (Time::NS, &resolution, false);
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   153
  return resolution;
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   154
}
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   155
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   156
// static
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   157
void
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   158
Time::SetResolution (enum Unit resolution)
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   159
{
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   160
  NS_LOG_FUNCTION (resolution);
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   161
  SetResolution (resolution, PeekResolution ());
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   162
}
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   163
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   164
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   165
// static
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   166
void
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   167
Time::SetResolution (enum Unit unit, struct Resolution *resolution,
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   168
                     const bool convert /* = true */)
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   169
{
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   170
  NS_LOG_FUNCTION (resolution);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   171
  if (convert)
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   172
    {
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   173
      // We have to convert existing Times with the old
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   174
      // conversion values, so do it first
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   175
      ConvertTimes (unit);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   176
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   177
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   178
  int8_t power [LAST] = { 15, 12, 9, 6, 3, 0};
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   179
  for (int i = 0; i < Time::LAST; i++)
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   180
    {
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   181
      int shift = power[i] - power[(int)unit];
9972
7805771a836a Fix unit conversion of negative Times
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9891
diff changeset
   182
      int64_t factor = (int64_t) std::pow (10, std::fabs (shift));
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   183
      struct Information *info = &resolution->info[i];
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   184
      info->factor = factor;
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   185
      if (shift == 0)
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   186
        {
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   187
          info->timeFrom = int64x64_t (1);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   188
          info->timeTo = int64x64_t (1);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   189
          info->toMul = true;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   190
          info->fromMul = true;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   191
        }
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   192
      else if (shift > 0)
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   193
        {
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   194
          info->timeFrom = int64x64_t (factor);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   195
          info->timeTo = int64x64_t::Invert (factor);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   196
          info->toMul = false;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   197
          info->fromMul = true;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   198
        }
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   199
      else
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   200
        {
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   201
          NS_ASSERT (shift < 0);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   202
          info->timeFrom = int64x64_t::Invert (factor);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   203
          info->timeTo = int64x64_t (factor);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   204
          info->toMul = true;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   205
          info->fromMul = false;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   206
        }
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   207
    }
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   208
  resolution->unit = unit;
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   209
}
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   210
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   211
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   212
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   213
void
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   214
Time::ClearMarkedTimes ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   215
{
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   216
  /**
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   217
   * \internal
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   218
   *
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   219
   * We're called by Simulator::Run, which knows nothing about the mutex,
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   220
   * so we need a critical section here.
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   221
   *
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   222
   * It would seem natural to use this function at the end of
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   223
   * ConvertTimes, but that function already has the mutex.
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   224
   * Our SystemMutex throws a fatal error if we try to lock it more than
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   225
   * once in the same thread (at least in the unix implementation),
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   226
   * so calling this function from ConvertTimes is a bad idea.
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   227
   *
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   228
   * Instead, we copy this body into ConvertTimes.
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   229
   */
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   230
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   231
  CriticalSection critical (GetMarkingMutex ());
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   232
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   233
  NS_LOG_FUNCTION_NOARGS ();
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   234
  if (g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   235
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   236
      NS_LOG_LOGIC ("clearing MarkedTimes");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   237
      delete g_markingTimes;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   238
      g_markingTimes = 0;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   239
    }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   240
}  // Time::ClearMarkedTimes
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   241
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   242
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   243
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   244
void
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   245
Time::Mark (Time * const time)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   246
{
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   247
  CriticalSection critical (GetMarkingMutex ());
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   248
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   249
  NS_LOG_FUNCTION (time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   250
  NS_ASSERT (time != 0);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   251
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   252
  // Repeat the g_markingTimes test here inside the CriticalSection,
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   253
  // since earlier test was outside and might be stale.
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   254
  if (g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   255
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   256
      std::pair< MarkedTimes::iterator, bool> ret;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   257
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   258
      ret = g_markingTimes->insert ( time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   259
      NS_LOG_LOGIC ("\t[" << g_markingTimes->size () << "] recording " << time);
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   260
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   261
      if (ret.second == false)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   262
        {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   263
          NS_LOG_WARN ("already recorded " << time << "!");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   264
        }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   265
   }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   266
}  // Time::Mark ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   267
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   268
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   269
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   270
void
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   271
Time::Clear (Time * const time)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   272
{
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   273
  CriticalSection critical (GetMarkingMutex ());
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   274
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   275
  NS_LOG_FUNCTION (time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   276
  NS_ASSERT (time != 0);
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   277
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   278
  if (g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   279
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   280
      NS_ASSERT_MSG (g_markingTimes->count (time) == 1,
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   281
                     "Time object " << time <<
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   282
                     " registered " << g_markingTimes->count (time) <<
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   283
                     " times (should be 1)." );
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   284
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   285
      MarkedTimes::size_type num = g_markingTimes->erase (time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   286
      if (num != 1)
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   287
        {
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   288
          NS_LOG_WARN ("unexpected result erasing " << time << "!");
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   289
          NS_LOG_WARN ("got " << num << ", expected 1");
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   290
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   291
      else
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   292
        {
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   293
          NS_LOG_LOGIC ("\t[" << g_markingTimes->size () << "] removing  " << time);
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   294
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   295
    }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   296
}  // Time::Clear ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   297
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   298
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   299
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   300
void
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   301
Time::ConvertTimes (const enum Unit unit)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   302
{
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   303
  CriticalSection critical (GetMarkingMutex ());
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   304
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   305
  NS_LOG_FUNCTION_NOARGS();
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   306
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   307
  NS_ASSERT_MSG (g_markingTimes != 0,
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   308
                 "No MarkedTimes registry. "
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   309
                 "Time::SetResolution () called more than once?");
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   310
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   311
  for ( MarkedTimes::iterator it = g_markingTimes->begin();
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   312
        it != g_markingTimes->end();
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   313
        it++ )
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   314
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   315
      Time * const tp = *it;
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   316
      if ( ! (    (tp->m_data == std::numeric_limits<int64_t>::min ())
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   317
               || (tp->m_data == std::numeric_limits<int64_t>::max ())
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   318
             )
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   319
         )
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   320
        {
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   321
      tp->m_data = tp->ToInteger (unit);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   322
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   323
    }
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   324
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   325
  NS_LOG_LOGIC ("logged " << g_markingTimes->size () << " Time objects.");
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   326
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   327
  // Body of ClearMarkedTimes
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   328
  // Assert above already guarantees g_markingTimes != 0
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   329
  NS_LOG_LOGIC ("clearing MarkedTimes");
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   330
  delete g_markingTimes;
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   331
  g_markingTimes = 0;
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   332
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   333
}  // Time::ConvertTimes ()
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   334
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   335
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   336
// static
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   337
enum Time::Unit
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   338
Time::GetResolution (void)
9186
8934b7c0c1cb bug 954: avoid segfault upon startup due to recursion in TimeSet code to Simulator::Schedule
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 9183
diff changeset
   339
{
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   340
  // No function log b/c it interferes with operator<<
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   341
  return PeekResolution ()->unit;
9183
1a2abe07b53d bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9134
diff changeset
   342
}
1a2abe07b53d bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9134
diff changeset
   343
1a2abe07b53d bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9134
diff changeset
   344
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   345
std::ostream&
2409
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   346
operator<< (std::ostream& os, const Time & time)
209
8b343b9727d7 fix EventId::IsExpired and Simulator::IsExpired, add EventId::IsRunning, add relevant tests, replace Seconds, MilliSeconds, MicroSeconds, and, NanoSeconds classes by functions named similarly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 199
diff changeset
   347
{
911
de8102a51fbf append correct unit when printing Time objects
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 684
diff changeset
   348
  std::string unit;
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   349
  Time::Unit res = Time::GetResolution ();
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   350
  switch (res)
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   351
    {
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   352
    case Time::S:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   353
      unit = "s";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   354
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   355
    case Time::MS:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   356
      unit = "ms";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   357
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   358
    case Time::US:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   359
      unit = "us";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   360
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   361
    case Time::NS:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   362
      unit = "ns";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   363
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   364
    case Time::PS:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   365
      unit = "ps";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   366
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   367
    case Time::FS:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   368
      unit = "fs";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   369
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   370
    case Time::LAST:
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   371
      NS_ABORT_MSG ("can't be reached");
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   372
      unit = "unreachable";
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   373
      break;
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   374
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   375
  int64_t v = time.ToInteger (res);
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   376
10174
09eca1533421 Restore "x.0ns" to result of operator<< (Time)
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10173
diff changeset
   377
  std::ios_base::fmtflags ff = os.flags ();
10175
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   378
  { // See bug 1737:  gcc libstc++ 4.2 bug
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   379
    if (v == 0)
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   380
      {
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   381
        os << '+';
10175
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   382
      }
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   383
    else
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   384
      {
10176
73dfb0c88ed6 [Bug 954] Protect g_markingTimes with a mutex.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10175
diff changeset
   385
        os << std::showpos;
10175
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   386
      }
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   387
  }
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   388
  os << v << ".0" << unit;
10174
09eca1533421 Restore "x.0ns" to result of operator<< (Time)
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10173
diff changeset
   389
  os.flags (ff);  // Restore stream flags
209
8b343b9727d7 fix EventId::IsExpired and Simulator::IsExpired, add EventId::IsRunning, add relevant tests, replace Seconds, MilliSeconds, MicroSeconds, and, NanoSeconds classes by functions named similarly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 199
diff changeset
   390
  return os;
8b343b9727d7 fix EventId::IsExpired and Simulator::IsExpired, add EventId::IsRunning, add relevant tests, replace Seconds, MilliSeconds, MicroSeconds, and, NanoSeconds classes by functions named similarly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 199
diff changeset
   391
}
2409
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   392
std::istream& operator>> (std::istream& is, Time & time)
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   393
{
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   394
  std::string value;
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   395
  is >> value;
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   396
  time = Time (value);
2409
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   397
  return is;
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   398
}
143
552ff26b4224 new Time implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 131
diff changeset
   399
2447
eee344085a96 convert to new helpers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2436
diff changeset
   400
ATTRIBUTE_VALUE_IMPLEMENT (Time);
9891
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   401
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   402
Ptr<const AttributeChecker>
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   403
MakeTimeChecker (const Time min, const Time max)
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   404
{
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   405
  NS_LOG_FUNCTION (min << max);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   406
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   407
  struct Checker : public AttributeChecker
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   408
  {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   409
    Checker (const Time minValue, const Time maxValue)
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   410
      : m_minValue (minValue),
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   411
        m_maxValue (maxValue) {}
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   412
    virtual bool Check (const AttributeValue &value) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   413
      NS_LOG_FUNCTION (&value);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   414
      const TimeValue *v = dynamic_cast<const TimeValue *> (&value);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   415
      if (v == 0)
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   416
        {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   417
          return false;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   418
        }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   419
      return v->Get () >= m_minValue && v->Get () <= m_maxValue;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   420
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   421
    virtual std::string GetValueTypeName (void) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   422
      NS_LOG_FUNCTION_NOARGS ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   423
      return "ns3::TimeValue";
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   424
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   425
    virtual bool HasUnderlyingTypeInformation (void) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   426
      NS_LOG_FUNCTION_NOARGS ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   427
      return true;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   428
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   429
    virtual std::string GetUnderlyingTypeInformation (void) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   430
      NS_LOG_FUNCTION_NOARGS ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   431
      std::ostringstream oss;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   432
      oss << "Time" << " " << m_minValue << ":" << m_maxValue;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   433
      return oss.str ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   434
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   435
    virtual Ptr<AttributeValue> Create (void) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   436
      NS_LOG_FUNCTION_NOARGS ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   437
      return ns3::Create<TimeValue> ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   438
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   439
    virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   440
      NS_LOG_FUNCTION (&source << &destination);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   441
      const TimeValue *src = dynamic_cast<const TimeValue *> (&source);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   442
      TimeValue *dst = dynamic_cast<TimeValue *> (&destination);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   443
      if (src == 0 || dst == 0)
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   444
        {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   445
          return false;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   446
        }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   447
      *dst = *src;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   448
      return true;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   449
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   450
    Time m_minValue;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   451
    Time m_maxValue;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   452
  } *checker = new Checker (min, max);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   453
  return Ptr<const AttributeChecker> (checker, false);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   454
}
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   455
2427
9245ec163111 split checker from ParamSpec.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2409
diff changeset
   456
2390
7e6dfae536cd add Value support to Time
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2170
diff changeset
   457
} // namespace ns3
143
552ff26b4224 new Time implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 131
diff changeset
   458