ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
waiter.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "waiter.h"
21 #include "utils.h"
22 #include "process.h"
23 #include "dce-manager.h"
24 #include "task-manager.h"
25 #include "ns3/simulator.h"
26 #include "ns3/event-id.h"
27 #include "ns3/log.h"
28 #include "ns3/assert.h"
29 #include <errno.h>
30 
31 NS_LOG_COMPONENT_DEFINE ("Waiter");
32 
33 namespace ns3 {
34 
36  : m_waiting (0),
37  m_timeout (Seconds (0.0))
38 {
39  NS_LOG_FUNCTION (this);
40 }
41 
43 {
44  NS_LOG_FUNCTION (this);
45 }
46 
47 void
48 Waiter::SetTimeout (Time timeout)
49 {
50  m_timeout = timeout;
51 }
52 Time
54 {
55  return m_timeout;
56 }
57 
60 {
61  Thread *current = Current ();
62  NS_LOG_FUNCTION (this << current);
63  NS_ASSERT (current != 0);
64 
65  m_waiting = current;
66  Time left = current->process->manager->Wait (m_timeout);
67  m_waiting = 0;
68  if (HasPendingSignal ())
69  {
70  m_timeout = Seconds (0.0);
71  NS_LOG_DEBUG ("interrupted self=" << this << " current=" << current);
72  return Waiter::INTERRUPTED;
73  }
74  if (!m_timeout.IsZero () && left.IsZero ())
75  {
76  m_timeout = Seconds (0.0);
77  NS_LOG_DEBUG ("timeout self=" << this << " current=" << current);
78  return Waiter::TIMEOUT;
79  }
80 
81  m_timeout = left;
82  NS_LOG_DEBUG ("ok self=" << this << " current=" << current);
83  return Waiter::OK;
84 }
85 bool
87 {
88  Thread *current = Current ();
89  NS_LOG_FUNCTION (this << current);
90  Waiter::Result result = Wait ();
91  switch (result)
92  {
94  UtilsDoSignal ();
95  current->err = EINTR;
96  return false;
97  break;
98  case Waiter::TIMEOUT:
99  current->err = EAGAIN;
100  return false;
101  break;
102  case Waiter::OK:
103  break;
104  }
105  return true;
106 }
107 
108 void
110 {
111  NS_LOG_FUNCTION (this << m_waiting);
112  if (m_waiting != 0)
113  {
114  /* The waiting thread could well be active because, it could have been
115  * unblocked but not scheduled yet which means that the assignment of
116  * m_waiting to zero has not been done yet in Waiter::Wait.
117  */
118  if (m_waiting->task->IsBlocked ())
119  {
121  }
122  else
123  {
124  NS_ASSERT (m_waiting->task->IsActive ());
125  }
126  }
127 }
128 
129 } // namespace ns3