ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wait-queue.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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: Frederic Urbani <frederic.urbani@inria.fr>
19  *
20  */
21 #include "file-usage.h"
22 #include "ns3/log.h"
23 #include "ns3/nstime.h"
24 #include "unix-fd.h"
25 #include "dce-poll.h"
26 #include "dce-manager.h"
27 #include "process.h"
28 #include "utils.h"
29 #include <map>
30 
31 NS_LOG_COMPONENT_DEFINE ("WaitQueue");
32 
33 namespace ns3 {
35 {
36 }
37 
38 WaitQueueEntryPoll::WaitQueueEntryPoll (Callback<void> cb) : m_func (cb)
39 {
40 }
41 
42 void
44 {
45  m_pollTableEntry = p;
46 }
47 
48 void
50 {
51  short event = (0 != key) ? (*((short*)key)) : 0; // Deference a short * if not 0
52 
54  {
55  m_func ();
56  }
57 }
59  m_wait (0),
60  m_eventMask (0)
61 {
62 }
64  : m_file (file),
65  m_wait (wait),
66  m_eventMask (em)
67 {
68 }
70 {
71  if (m_wait)
72  {
73  delete m_wait;
74  }
75 }
76 void
78 {
79  m_file->RemoveWaitQueue (m_wait, false);
80 }
81 
82 int
84 {
85  return e & m_eventMask;
86 }
87 //WaitPoint
88 WaitPoint::WaitPoint () : m_waitTask (0)
89 {
90 }
92 WaitPoint::Wait (Time to)
93 {
94  Thread *current = Current ();
95  NS_LOG_FUNCTION (this << current);
96  NS_ASSERT (current != 0);
97 
98  m_waitTask = current;
99  Time left = current->process->manager->Wait (to);
100  m_waitTask = 0;
101  if (HasPendingSignal ())
102  {
103  return PollTable::INTERRUPTED;
104  }
105  if (!to.IsZero () && left.IsZero ())
106  {
107  return PollTable::TIMEOUT;
108  }
109  return PollTable::OK;
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this << m_waitTask);
116  if (m_waitTask != 0)
117  {
118  /* The waiting thread could well be active because, it could have been
119  * unblocked but not scheduled yet which means that the assignment of
120  * m_waiting to zero has not been done yet in Waiter::Wait.
121  */
122  if (m_waitTask->task->IsBlocked ())
123  {
124  // NS_ASSERT (Simulator::GetContext () == m_waitTask->process->nodeId); TODO Fix this assert or Remove it :)
126  }
127  else
128  {
129  NS_ASSERT (m_waitTask->task->IsActive ());
130  }
131  }
132 }
133 
134 
136 {
137 }
138 
140 {
141  for (std::list <PollTableEntry*>::iterator i = m_pollEntryList.begin ();
142  i != m_pollEntryList.end (); ++i)
143  {
144  delete (*i);
145  }
146  m_pollEntryList.clear ();
147 }
148 
149 void
151 {
152  WaitQueueEntryPoll* we = new WaitQueueEntryPoll (MakeCallback (&PollTable::WakeUpCallback, this));
153  PollTableEntry* e = new PollTableEntry (file, we, m_eventMask);
154  we->SetPollTableEntry (e);
155  m_pollEntryList.push_back (e);
156  file->AddWaitQueue (we, false);
157 }
158 void
159 PollTable::PollWait (void *ref, Callback<void, void*> cb)
160 {
161  m_pollEntryList.push_back (new PollTableEntryLinux (ref, cb));
162 }
163 void
165 {
166  for (std::list <PollTableEntry*> :: iterator i = m_pollEntryList.begin ();
167  i != m_pollEntryList.end (); ++i)
168  {
169  (*i)->FreeWait ();
170  }
171 }
172 void
174 {
175  m_eventMask = e;
176 }
177 short
179 {
180  return m_eventMask;
181 }
182 WaitQueueEntryTimeout::WaitQueueEntryTimeout (short em, Time to) : m_waitTask (0),
183  m_eventMask (em)
184 {
185  if (to.IsZero ())
186  {
187  m_lastTime = to;
188  }
189  else
190  {
191  m_lastTime = Simulator::Now () + to;
192  }
193 }
194 
195 void
197 {
198  short event = (0 != key) ? (*((short*)key)) : 0; // Deference a short * if not 0
199 
200  if (event & m_eventMask)
201  {
202  WakeUpCallback ();
203  }
204 }
207 {
208  NS_LOG_FUNCTION (m_lastTime);
209  if (m_lastTime.IsNegative ())
210  {
211  return WaitPoint::Wait (Seconds (0));
212  }
213  else
214  {
215  Time rest = m_lastTime - Simulator::Now ();
216 
217  if (rest.IsNegative ())
218  {
219  return TIMEOUT;
220  }
221  return WaitPoint::Wait (rest);
222  }
223 }
224 
225 PollTableEntryLinux::PollTableEntryLinux (void *kernelReference, Callback<void, void*> cb)
226  : m_kernelRef (kernelReference),
227  m_freeCb (cb)
228 {
229 }
230 void
232 {
234 }
235 }