replace RefCountBase with SimpleRefCount<> to avoid duplicate refcounting implementations.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INRIA
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;
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.
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
18 * Author: Mathieu Lacage <mathieu.lacage.inria.fr>
21 #ifndef SYSTEM_THREAD_H
22 #define SYSTEM_THREAD_H
28 class SystemThreadImpl;
31 * @brief A class which provides a relatively platform-independent thread
34 * This class allows for creation of multiple threads of execution in a
35 * process. The exact implementation of the thread functionality is
36 * operating system dependent, but typically in ns-3 one is using an
37 * environment in which Posix Threads are supported (either navively or
38 * in the case of Windows via Cygwin's implementation of pthreads on the
39 * Win32 API. In either case we expect that these will be kernel-level
40 * threads and therefore a system with multiple CPUs will see truly concurrent
43 * Synchronization between threads is provided via the SystemMutex class.
45 class SystemThread : public SimpleRefCount<SystemThread>
49 * @brief Create a SystemThread object.
51 * A system thread object is not created running. A thread of execution
52 * must be explicitly started by calling the Start method. When the
53 * Start method is called, it will spawn a thread of execution and cause
54 * that thread to call out into the callback function provided here as
57 * Like all ns-3 callbacks, the provided callback may refer to a function
58 * or a method of an object depending on how the MakeCallback function is
61 * The most common use is expected to be creating a thread of execution in
62 * a method. In this case you would use code similar to,
65 * Ptr<SystemThread> st = Create<SystemThread> (
66 * MakeCallback (&MyClass::MyMethod, &myObject));
69 * The SystemThread is passed a callback that calls out to the function
70 * MyClass::MyMethod. When this function is called, it is called as an
71 * object method on the myObject object. Essentially what you are doing
72 * is asking the SystemThread to call object->MyMethod () in a new thread
75 * If starting a thread in your currently executing object, you can use the
78 * Ptr<SystemThread> st = Create<SystemThread> (
79 * MakeCallback (&MyClass::MyMethod, this));
82 * Object lifetime is always an issue with threads, so it is common to use
83 * smart pointers. If you are spinning up a thread in an object that is
84 * managed by a smart pointer, you can use that pointer directly:
86 * Ptr<MyClass> myPtr = Create<MyClass> ();
87 * Ptr<SystemThread> st = Create<SystemThread> (
88 * MakeCallback (&MyClass::MyMethod, myPtr));
91 * Just like any thread, you can synchronize with its termination. The
92 * method provided to do this is Join (). If you call Join() you will block
93 * until the SystemThread run method returns.
95 * @param callback entry point of the thread
97 * @warning The SystemThread uses SIGALRM to wake threads that are possibly
101 * @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
103 * manage threads from a single master thread context. You should be very
104 * aware though that I have not made Ptr multithread safe! This means that
105 * if you pass Ptr<SystemThread> around in a multithreaded environment, it is
106 * possible that the reference count will get messed up since it is not an
107 * atomic operation. CREATE AND MANAGE YOUR THREADS IN ONE PLACE -- LEAVE
110 SystemThread(Callback<void> callback);
113 * @brief Destroy a SystemThread object.
119 * @brief Start a thread of execution, running the provided callback.
124 * @brief Suspend the caller until the thread of execution, running the
125 * provided callback, finishes.
130 * @brief Indicates to a managed thread doing cooperative multithreading that
131 * its managing thread wants it to exit.
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.
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.
145 * @warning Uses SIGALRM to notifiy threads possibly blocked on IO. Beware
146 * if you are using signals.
149 void Shutdown (void);
152 * @brief Indicates to a thread doing cooperative multithreading that
153 * its managing thread wants it to exit.
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.
162 * @returns true if thread is expected to exit (break out of the forever-loop)
167 SystemThreadImpl * m_impl;
173 #endif /* SYSTEM_THREAD_H */