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

(-)a/src/core/unix-system-thread.cc (+11 lines)
 Lines 20-25    Link Here 
20
20
21
#include <pthread.h>
21
#include <pthread.h>
22
#include <string.h>
22
#include <string.h>
23
#include <signal.h>
23
#include "fatal-error.h"
24
#include "fatal-error.h"
24
#include "system-thread.h"
25
#include "system-thread.h"
25
#include "log.h"
26
#include "log.h"
 Lines 61-66   SystemThreadImpl::SystemThreadImpl (Call Link Here 
61
  : m_callback (callback)
62
  : m_callback (callback)
62
{
63
{
63
  NS_LOG_FUNCTION_NOARGS ();
64
  NS_LOG_FUNCTION_NOARGS ();
65
  // Make sure we have a SIGALRM handler which does not terminate
66
  // our process.
67
  struct sigaction act;
68
  act.sa_flags = 0;
69
  sigemptyset (&act.sa_mask);
70
  act.sa_handler = SIG_IGN;
71
  sigaction (SIGALRM, &act, 0);
64
}
72
}
65
73
66
  void
74
  void
 Lines 83-88   SystemThreadImpl::Join (void) Link Here 
83
{
91
{
84
  NS_LOG_FUNCTION_NOARGS ();
92
  NS_LOG_FUNCTION_NOARGS ();
85
93
94
  // send a SIGALRM signal on the target thread to make sure that it
95
  // will unblock.
96
  pthread_kill (m_thread, SIGALRM);
86
  void *thread_return;
97
  void *thread_return;
87
  int rc = pthread_join (m_thread, &thread_return);
98
  int rc = pthread_join (m_thread, &thread_return);
88
  if (rc) 
99
  if (rc) 
(-)a/src/core/system-thread.h (+41 lines)
 Lines 84-89   public: Link Here 
84
   * method provided to do this is Join (). If you call Join() you will block
84
   * method provided to do this is Join (). If you call Join() you will block
85
   * until the SystemThread run method returns.
85
   * until the SystemThread run method returns.
86
   *
86
   *
87
   * @warning The SystemThread uses SIGALRM to wake threads that are possibly
88
   * blocked on IO.
89
   * @see Shutdown
90
   *
87
   * @warning I've made the system thread class look like a normal ns3 object
91
   * @warning I've made the system thread class look like a normal ns3 object
88
   * with smart pointers, and living in the heap.  This makes it very easy to
92
   * with smart pointers, and living in the heap.  This makes it very easy to
89
   * manage threads from a single master thread context.  You should be very
93
   * manage threads from a single master thread context.  You should be very
 Lines 128-136   public: Link Here 
128
   */
132
   */
129
  void Join (void);
133
  void Join (void);
130
134
135
  /**
136
   * @brief Indicates to a managed thread doing cooperative multithreading that
137
   * its managing thread wants it to exit.
138
   *
139
   * It is often the case that we want a thread to be off doing work until such
140
   * time as its job is done (typically when the simulation is done).  We then 
141
   * want the thread to exit itself.  This method provides a consistent way for
142
   * the managing thread to communicate with the managed thread.  After the
143
   * manager thread calls this method, the Break() method will begin returning
144
   * true, telling the managed thread to exit.
145
   *
146
   * This alone isn't really enough to merit these events, but in Unix, if a
147
   * worker thread is doing blocking IO, it will need to be woken up from that
148
   * read somehow.  This method also provides that functionality, by sending a
149
   * SIGALRM signal to the possibly blocked thread.
150
   *
151
   * @see Break
152
   * @returns true if thread is expected to exit
153
   */
154
  void Shutdown (void);
155
156
  /**
157
   * @brief Indicates to a thread doing cooperative multithreading that
158
   * its managing thread wants it to exit.
159
   *
160
   * It is often the case that we want a thread to be off doing work until such
161
   * time as its job is done.  We then want the thread to exit itself.  This
162
   * method allows a thread to query whether or not it should be running.  
163
   * Typically, the worker thread is running in a forever-loop, and will need to
164
   * "break" out of that loop to exit -- thus the name.
165
   *
166
   * @see Shutdown
167
   * @returns true if thread is expected to exit (break out of the forever-loop)
168
   */
169
  bool Break (void);
170
131
private:
171
private:
132
  SystemThreadImpl * m_impl;
172
  SystemThreadImpl * m_impl;
133
  mutable uint32_t m_count;
173
  mutable uint32_t m_count;
174
  bool m_break;
134
};
175
};
135
176
136
 void
177
 void
(-)a/src/core/unix-system-thread.cc (-3 / +37 lines)
 Lines 50-60   public: 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);
53
55
54
private:
56
private:
55
  static void *DoRun (void *arg);
57
  static void *DoRun (void *arg);
56
  Callback<void> m_callback;
58
  Callback<void> m_callback;
57
  pthread_t m_thread;
59
  pthread_t m_thread;
60
  bool m_break;
58
  void *    m_ret;
61
  void *    m_ret;
59
};
62
};
60
63
 Lines 91-99   SystemThreadImpl::Join (void) Link Here 
91
{
94
{
92
  NS_LOG_FUNCTION_NOARGS ();
95
  NS_LOG_FUNCTION_NOARGS ();
93
96
94
  // send a SIGALRM signal on the target thread to make sure that it
95
  // will unblock.
96
  pthread_kill (m_thread, SIGALRM);
97
  void *thread_return;
97
  void *thread_return;
98
  int rc = pthread_join (m_thread, &thread_return);
98
  int rc = pthread_join (m_thread, &thread_return);
99
  if (rc) 
99
  if (rc) 
 Lines 101-106   SystemThreadImpl::Join (void) Link Here 
101
      NS_FATAL_ERROR ("pthread_join failed: " << rc << "=\"" << 
101
      NS_FATAL_ERROR ("pthread_join failed: " << rc << "=\"" << 
102
        strerror(rc) << "\".");
102
        strerror(rc) << "\".");
103
    }
103
    }
104
}
105
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;
104
}
124
}
105
125
106
  void *
126
  void *
 Lines 147-150   SystemThread::Join (void) Link Here 
147
  m_impl->Join ();
167
  m_impl->Join ();
148
}  
168
}  
149
169
170
  void 
171
SystemThread::Shutdown (void) 
172
{
173
  NS_LOG_FUNCTION_NOARGS ();
174
  m_impl->Shutdown ();
175
}  
176
177
  bool 
178
SystemThread::Break (void) 
179
{
180
  NS_LOG_FUNCTION_NOARGS ();
181
  return m_impl->Break ();
182
}  
183
150
} // namespace ns3
184
} // namespace ns3

Return to bug 380