View | Details | Raw Unified | Return to bug 903
Collapse All | Expand All

(-)a/src/core/system-thread.h (-39 lines)
 Lines 96-102    Link Here 
96
   * 
96
   * 
97
   * @warning The SystemThread uses SIGALRM to wake threads that are possibly
97
   * @warning The SystemThread uses SIGALRM to wake threads that are possibly
98
   * blocked on IO.
98
   * blocked on IO.
99
   * @see Shutdown
100
   *
99
   *
101
   * @warning I've made the system thread class look like a normal ns3 object
100
   * @warning I've made the system thread class look like a normal ns3 object
102
   * with smart pointers, and living in the heap.  This makes it very easy to
101
   * with smart pointers, and living in the heap.  This makes it very easy to
 Lines 126-171    Link Here 
126
   */
125
   */
127
  void Join (void);
126
  void Join (void);
128
127
129
  /**
130
   * @brief Indicates to a managed thread doing cooperative multithreading that
131
   * its managing thread wants it to exit.
132
   *
133
   * It is often the case that we want a thread to be off doing work until such
134
   * time as its job is done (typically when the simulation is done).  We then 
135
   * want the thread to exit itself.  This method provides a consistent way for
136
   * the managing thread to communicate with the managed thread.  After the
137
   * manager thread calls this method, the Break() method will begin returning
138
   * true, telling the managed thread to exit.
139
   *
140
   * This alone isn't really enough to merit these events, but in Unix, if a
141
   * worker thread is doing blocking IO, it will need to be woken up from that
142
   * read somehow.  This method also provides that functionality, by sending a
143
   * SIGALRM signal to the possibly blocked thread.
144
   *
145
   * @warning Uses SIGALRM to notify threads possibly blocked on IO.  Beware
146
   * if you are using signals.
147
   * @see Break
148
   */
149
  void Shutdown (void);
150
151
  /**
152
   * @brief Indicates to a thread doing cooperative multithreading that
153
   * its managing thread wants it to exit.
154
   *
155
   * It is often the case that we want a thread to be off doing work until such
156
   * time as its job is done.  We then want the thread to exit itself.  This
157
   * method allows a thread to query whether or not it should be running.  
158
   * Typically, the worker thread is running in a forever-loop, and will need to
159
   * "break" out of that loop to exit -- thus the name.
160
   *
161
   * @see Shutdown
162
   * @returns true if thread is expected to exit (break out of the forever-loop)
163
   */
164
  bool Break (void);
165
166
private:
128
private:
167
  SystemThreadImpl * m_impl;
129
  SystemThreadImpl * m_impl;
168
  bool m_break;
169
};
130
};
170
131
171
} //namespace ns3
132
} //namespace ns3
(-)a/src/core/unix-system-thread.cc (-46 / +2 lines)
 Lines 50-77    Link Here 
50
50
51
  void Start (void);
51
  void Start (void);
52
  void Join (void);
52
  void Join (void);
53
  void Shutdown (void);
54
  bool Break (void);
55
53
56
private:
54
private:
57
  static void *DoRun (void *arg);
55
  static void *DoRun (void *arg);
58
  Callback<void> m_callback;
56
  Callback<void> m_callback;
59
  pthread_t m_thread;
57
  pthread_t m_thread;
60
  bool m_break;
61
  void *    m_ret;
58
  void *    m_ret;
62
};
59
};
63
60
64
SystemThreadImpl::SystemThreadImpl (Callback<void> callback)
61
SystemThreadImpl::SystemThreadImpl (Callback<void> callback)
65
  : m_callback (callback), m_break (false)
62
  : m_callback (callback)
66
{
63
{
67
  NS_LOG_FUNCTION_NOARGS ();
64
  NS_LOG_FUNCTION_NOARGS ();
68
  // Make sure we have a SIGALRM handler which does not terminate
69
  // our process.
70
  struct sigaction act;
71
  act.sa_flags = 0;
72
  sigemptyset (&act.sa_mask);
73
  act.sa_handler = SIG_IGN;
74
  sigaction (SIGALRM, &act, 0);
75
}
65
}
76
66
77
  void
67
  void
 Lines 103-128    Link Here 
103
    }
93
    }
104
}
94
}
105
95
106
  void 
107
SystemThreadImpl::Shutdown (void)
108
{
109
  NS_LOG_FUNCTION_NOARGS ();
110
111
  m_break = true;
112
113
  // send a SIGALRM signal on the target thread to make sure that it
114
  // will unblock.
115
  pthread_kill (m_thread, SIGALRM);
116
}
117
118
  bool
119
SystemThreadImpl::Break (void)
120
{
121
  NS_LOG_FUNCTION_NOARGS ();
122
123
  return m_break;
124
}
125
126
  void *
96
  void *
127
SystemThreadImpl::DoRun (void *arg)
97
SystemThreadImpl::DoRun (void *arg)
128
{
98
{
 Lines 141-147    Link Here 
141
// class above.
111
// class above.
142
//
112
//
143
SystemThread::SystemThread (Callback<void> callback) 
113
SystemThread::SystemThread (Callback<void> callback) 
144
  : m_impl (new SystemThreadImpl (callback)), m_break (false)
114
  : m_impl (new SystemThreadImpl (callback))
145
{
115
{
146
  NS_LOG_FUNCTION_NOARGS ();
116
  NS_LOG_FUNCTION_NOARGS ();
147
}
117
}
 Lines 166-183    Link Here 
166
  m_impl->Join ();
136
  m_impl->Join ();
167
}  
137
}  
168
138
169
  void 
170
SystemThread::Shutdown (void) 
171
{
172
  NS_LOG_FUNCTION_NOARGS ();
173
  m_impl->Shutdown ();
174
}  
175
176
  bool 
177
SystemThread::Break (void) 
178
{
179
  NS_LOG_FUNCTION_NOARGS ();
180
  return m_impl->Break ();
181
}  
182
183
} // namespace ns3
139
} // namespace ns3

Return to bug 903