author | Peter D. Barnes, Jr. <barnes26@llnl.gov> |
Tue, 09 Dec 2014 13:17:49 -0800 | |
changeset 11097 | caafe12b0378 |
parent 9904 | 58f68dac8bf3 |
child 11538 | 397bd6465dd9 |
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 |
|
7778
7b2c9728d2fb
remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
7777
diff
changeset
|
24 |
#include "ns3/core-config.h" |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
25 |
#include "callback.h" |
7778
7b2c9728d2fb
remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
7777
diff
changeset
|
26 |
#ifdef HAVE_PTHREAD_H |
7b2c9728d2fb
remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
7777
diff
changeset
|
27 |
#include <pthread.h> |
7b2c9728d2fb
remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
7777
diff
changeset
|
28 |
#endif /* HAVE_PTHREAD_H */ |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
29 |
|
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
30 |
/** |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
31 |
* @file |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
32 |
* @ingroup thread |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
33 |
* System-independent thread class ns3::SystemThread declaration. |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
34 |
*/ |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
35 |
|
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
36 |
namespace ns3 { |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
37 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
38 |
/** |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
39 |
* @ingroup thread |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
40 |
* @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
|
41 |
* primitive. |
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 |
* 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
|
44 |
* 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
|
45 |
* 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
|
46 |
* 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
|
47 |
* 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
|
48 |
* 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
|
49 |
* 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
|
50 |
* execution. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
51 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
52 |
* Synchronization between threads is provided via the SystemMutex class. |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
53 |
* |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
54 |
* See @ref main-test-sync.cc for example usage. |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
55 |
*/ |
5505
c0ac392289c3
replace RefCountBase with SimpleRefCount<> to avoid duplicate refcounting implementations.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
5498
diff
changeset
|
56 |
class SystemThread : public SimpleRefCount<SystemThread> |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
57 |
{ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
58 |
public: |
7779
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
59 |
|
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
60 |
#ifdef HAVE_PTHREAD_H |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
61 |
/** Type alias for the system-dependent thread object. */ |
7779
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
62 |
typedef pthread_t ThreadId; |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
63 |
#endif |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
64 |
|
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
65 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
66 |
* @brief Create a SystemThread object. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
67 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
68 |
* 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
|
69 |
* 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
|
70 |
* 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
|
71 |
* 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
|
72 |
* a parameter. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
73 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
74 |
* 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
|
75 |
* 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
|
76 |
* used. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
77 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
78 |
* 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
|
79 |
* a method. In this case you would use code similar to, |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
80 |
* @code |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
81 |
* MyClass myObject; |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
82 |
* Ptr<SystemThread> st = Create<SystemThread> ( |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
83 |
* MakeCallback (&MyClass::MyMethod, &myObject)); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
84 |
* st->Start (); |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
85 |
* @endcode |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
86 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
87 |
* The SystemThread is passed a callback that calls out to the function |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
88 |
* @c MyClass::MyMethod. When this function is called, it is called as an |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
89 |
* object method on the @c myObject object. Essentially what you are doing |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
90 |
* is asking the SystemThread to call @c object->MyMethod() in a new thread |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
91 |
* of execution. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
92 |
* |
5498
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
93 |
* 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
|
94 |
* "this" pointer: |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
95 |
* @code |
5498
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
96 |
* Ptr<SystemThread> st = Create<SystemThread> ( |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
97 |
* MakeCallback (&MyClass::MyMethod, this)); |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
98 |
* st->Start (); |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
99 |
* @endcode |
5498
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
100 |
* |
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
101 |
* 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
|
102 |
* 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
|
103 |
* managed by a smart pointer, you can use that pointer directly: |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
104 |
* @code |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
105 |
* Ptr<MyClass> myPtr = Create<MyClass> (); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
106 |
* Ptr<SystemThread> st = Create<SystemThread> ( |
5498
d5f70ed490a8
correct some wildly out-of-date doxygen
Craig Dowell <craigdo@ee.washington.edu>
parents:
5227
diff
changeset
|
107 |
* MakeCallback (&MyClass::MyMethod, myPtr)); |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
108 |
* st->Start (); |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
109 |
* @endcode |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
110 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
111 |
* Just like any thread, you can synchronize with its termination. The |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
112 |
* method provided to do this is Join(). If you call Join() you will block |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
113 |
* until the SystemThread run method returns. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
114 |
* |
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
|
115 |
* @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
|
116 |
* |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
117 |
* @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
|
118 |
* 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
|
119 |
* 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
|
120 |
* 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
|
121 |
* 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
|
122 |
* 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
|
123 |
* 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
|
124 |
* THE PTR THERE. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
125 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
126 |
SystemThread(Callback<void> callback); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
127 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
128 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
129 |
* @brief Destroy a SystemThread object. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
130 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
131 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
132 |
~SystemThread(); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
133 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
134 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
135 |
* @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
|
136 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
137 |
void Start (void); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
138 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
139 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
140 |
* @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
|
141 |
* provided callback, finishes. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
142 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
143 |
void Join (void); |
7779
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
144 |
/** |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
145 |
* @brief Returns the current thread Id. |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
146 |
* |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
147 |
* @returns current thread Id. |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
148 |
*/ |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
149 |
static ThreadId Self(void); |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
150 |
|
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
151 |
/** |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
152 |
* @brief Compares an ThreadId with the current ThreadId . |
7779
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
153 |
* |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
154 |
* @param [in] id The ThreadId to compare to. |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
155 |
* @returns true if @c id matches the current ThreadId. |
7779
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
156 |
*/ |
06b679a6e1b6
add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents:
7778
diff
changeset
|
157 |
static bool Equals(ThreadId id); |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
158 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
159 |
private: |
7778
7b2c9728d2fb
remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
7777
diff
changeset
|
160 |
#ifdef HAVE_PTHREAD_H |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
161 |
/** |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
162 |
* Invoke the callback in the new thread. |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
163 |
* |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
164 |
* @param [in] arg This SystemThread instance to communicate to the newly |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
165 |
* launched thread. |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
166 |
*/ |
7778
7b2c9728d2fb
remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
7777
diff
changeset
|
167 |
static void *DoRun (void *arg); |
7b2c9728d2fb
remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
7777
diff
changeset
|
168 |
|
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
169 |
Callback<void> m_callback; /**< The main function for this thread when launched. */ |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9904
diff
changeset
|
170 |
pthread_t m_thread; /**< The thread id of the child thread. */ |
7778
7b2c9728d2fb
remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents:
7777
diff
changeset
|
171 |
#endif |
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 |
|
7386
2310ed220a61
standardize ns-3 namespace declaration format
Vedran Miletić <rivanvx@gmail.com>
parents:
7169
diff
changeset
|
174 |
} // namespace ns3 |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
175 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
176 |
#endif /* SYSTEM_THREAD_H */ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
177 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
178 |