author | Vedran Miletić <rivanvx@gmail.com> |
Tue, 02 Aug 2011 17:43:32 -0400 | |
changeset 7386 | 2310ed220a61 |
parent 7169 | 358f71a624d8 |
child 7777 | f1e73a2cdb95 |
permissions | -rw-r--r-- |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
2 |
/* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
3 |
* Copyright (c) 2008 INRIA |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
4 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License version 2 as |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation; |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
8 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
13 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
17 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
18 |
* Author: Mathieu Lacage <mathieu.lacage.inria.fr> |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
19 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
20 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
21 |
#ifndef SYSTEM_THREAD_H |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
22 |
#define SYSTEM_THREAD_H |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
23 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
24 |
#include "callback.h" |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
25 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
26 |
namespace ns3 { |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
27 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
28 |
class SystemThreadImpl; |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
29 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
30 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
31 |
* @brief A class which provides a relatively platform-independent thread |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
32 |
* primitive. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
33 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
34 |
* This class allows for creation of multiple threads of execution in a |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
35 |
* process. The exact implementation of the thread functionality is |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
36 |
* operating system dependent, but typically in ns-3 one is using an |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
37 |
* environment in which Posix Threads are supported (either navively or |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
38 |
* in the case of Windows via Cygwin's implementation of pthreads on the |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
39 |
* Win32 API. In either case we expect that these will be kernel-level |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
40 |
* threads and therefore a system with multiple CPUs will see truly concurrent |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
41 |
* execution. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
42 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
43 |
* Synchronization between threads is provided via the SystemMutex class. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
44 |
*/ |
5505
c0ac392289c3
replace RefCountBase with SimpleRefCount<> to avoid duplicate refcounting implementations.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
5498
diff
changeset
|
45 |
class SystemThread : public SimpleRefCount<SystemThread> |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
46 |
{ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
47 |
public: |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
48 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
49 |
* @brief Create a SystemThread object. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
50 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
51 |
* A system thread object is not created running. A thread of execution |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
52 |
* must be explicitly started by calling the Start method. When the |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
53 |
* Start method is called, it will spawn a thread of execution and cause |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
54 |
* that thread to call out into the callback function provided here as |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
55 |
* a parameter. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
56 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
57 |
* Like all ns-3 callbacks, the provided callback may refer to a function |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
58 |
* or a method of an object depending on how the MakeCallback function is |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
59 |
* used. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
60 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
61 |
* The most common use is expected to be creating a thread of execution in |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
62 |
* a method. In this case you would use code similar to, |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
63 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
64 |
* MyClass myObject; |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
65 |
* Ptr<SystemThread> st = Create<SystemThread> ( |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
66 |
* MakeCallback (&MyClass::MyMethod, &myObject)); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
67 |
* st->Start (); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
68 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
69 |
* The SystemThread is passed a callback that calls out to the function |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
70 |
* MyClass::MyMethod. When this function is called, it is called as an |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
71 |
* object method on the myObject object. Essentially what you are doing |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
72 |
* is asking the SystemThread to call object->MyMethod () in a new thread |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
73 |
* of execution. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
74 |
* |
5498
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
75 |
* If starting a thread in your currently executing object, you can use the |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
76 |
* "this" pointer: |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
77 |
* |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
78 |
* Ptr<SystemThread> st = Create<SystemThread> ( |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
79 |
* MakeCallback (&MyClass::MyMethod, this)); |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
80 |
* st->Start (); |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
81 |
* |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
82 |
* Object lifetime is always an issue with threads, so it is common to use |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
83 |
* smart pointers. If you are spinning up a thread in an object that is |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
84 |
* managed by a smart pointer, you can use that pointer directly: |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
85 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
86 |
* Ptr<MyClass> myPtr = Create<MyClass> (); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
87 |
* Ptr<SystemThread> st = Create<SystemThread> ( |
5498
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
88 |
* MakeCallback (&MyClass::MyMethod, myPtr)); |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
89 |
* st->Start (); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
90 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
91 |
* Just like any thread, you can synchronize with its termination. The |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
92 |
* method provided to do this is Join (). If you call Join() you will block |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
93 |
* until the SystemThread run method returns. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
94 |
* |
5227
ecb08c1fc273
Fix some doxygen warnings revealed by WARN_NO_PARAMDOC=YES in doc/doxygen.conf.
Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
parents:
3792
diff
changeset
|
95 |
* @param callback entry point of the thread |
ecb08c1fc273
Fix some doxygen warnings revealed by WARN_NO_PARAMDOC=YES in doc/doxygen.conf.
Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
parents:
3792
diff
changeset
|
96 |
* |
3791
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
97 |
* @warning The SystemThread uses SIGALRM to wake threads that are possibly |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
98 |
* blocked on IO. |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
99 |
* @see Shutdown |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
100 |
* |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
101 |
* @warning I've made the system thread class look like a normal ns3 object |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
102 |
* with smart pointers, and living in the heap. This makes it very easy to |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
103 |
* manage threads from a single master thread context. You should be very |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
104 |
* aware though that I have not made Ptr multithread safe! This means that |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
105 |
* if you pass Ptr<SystemThread> around in a multithreaded environment, it is |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
106 |
* possible that the reference count will get messed up since it is not an |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
107 |
* atomic operation. CREATE AND MANAGE YOUR THREADS IN ONE PLACE -- LEAVE |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
108 |
* THE PTR THERE. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
109 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
110 |
SystemThread(Callback<void> callback); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
111 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
112 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
113 |
* @brief Destroy a SystemThread object. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
114 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
115 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
116 |
~SystemThread(); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
117 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
118 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
119 |
* @brief Start a thread of execution, running the provided callback. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
120 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
121 |
void Start (void); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
122 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
123 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
124 |
* @brief Suspend the caller until the thread of execution, running the |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
125 |
* provided callback, finishes. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
126 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
127 |
void Join (void); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
128 |
|
3791
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
129 |
/** |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
130 |
* @brief Indicates to a managed thread doing cooperative multithreading that |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
131 |
* its managing thread wants it to exit. |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
132 |
* |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
133 |
* It is often the case that we want a thread to be off doing work until such |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
134 |
* time as its job is done (typically when the simulation is done). We then |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
135 |
* want the thread to exit itself. This method provides a consistent way for |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
136 |
* the managing thread to communicate with the managed thread. After the |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
137 |
* manager thread calls this method, the Break() method will begin returning |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
138 |
* true, telling the managed thread to exit. |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
139 |
* |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
140 |
* This alone isn't really enough to merit these events, but in Unix, if a |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
141 |
* worker thread is doing blocking IO, it will need to be woken up from that |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
142 |
* read somehow. This method also provides that functionality, by sending a |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
143 |
* SIGALRM signal to the possibly blocked thread. |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
144 |
* |
6273
8d70de29d514
spell check, mostly in comments.
Andrey Mazo <mazo@iitp.ru>
parents:
5505
diff
changeset
|
145 |
* @warning Uses SIGALRM to notify threads possibly blocked on IO. Beware |
3792 | 146 |
* if you are using signals. |
3791
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
147 |
* @see Break |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
148 |
*/ |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
149 |
void Shutdown (void); |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
150 |
|
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
151 |
/** |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
152 |
* @brief Indicates to a thread doing cooperative multithreading that |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
153 |
* its managing thread wants it to exit. |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
154 |
* |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
155 |
* It is often the case that we want a thread to be off doing work until such |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
156 |
* time as its job is done. We then want the thread to exit itself. This |
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
157 |
* method allows a thread to query whether or not it should be running. |
3791
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
158 |
* Typically, the worker thread is running in a forever-loop, and will need to |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
159 |
* "break" out of that loop to exit -- thus the name. |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
160 |
* |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
161 |
* @see Shutdown |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
162 |
* @returns true if thread is expected to exit (break out of the forever-loop) |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
163 |
*/ |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
164 |
bool Break (void); |
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
165 |
|
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
166 |
private: |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
167 |
SystemThreadImpl * m_impl; |
3791
cf62138bd445
structured thread exit methods
Craig Dowell <craigdo@ee.washington.edu>
parents:
3425
diff
changeset
|
168 |
bool m_break; |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
169 |
}; |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
170 |
|
7386
2310ed220a61
standardize ns-3 namespace declaration format
Vedran Miletić <rivanvx@gmail.com>
parents:
7169
diff
changeset
|
171 |
} // namespace ns3 |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
172 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
173 |
#endif /* SYSTEM_THREAD_H */ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
174 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
175 |