A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
core
model
system-thread.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2008 INRIA
4
*
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;
8
*
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.
13
*
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
17
*
18
* Author: Mathieu Lacage <mathieu.lacage.inria.fr>
19
*/
20
21
#include "
fatal-error.h
"
22
#include "
system-thread.h
"
23
#include "
log.h
"
24
#include <cstring>
25
32
namespace
ns3
{
33
34
NS_LOG_COMPONENT_DEFINE
(
"SystemThread"
);
35
36
#ifdef HAVE_PTHREAD_H
37
38
SystemThread::SystemThread
(
Callback<void>
callback)
39
: m_callback (callback)
40
{
41
NS_LOG_FUNCTION
(
this
<< &callback);
42
}
43
44
SystemThread::~SystemThread
()
45
{
46
NS_LOG_FUNCTION
(
this
);
47
}
48
49
void
50
SystemThread::Start
(
void
)
51
{
52
NS_LOG_FUNCTION
(
this
);
53
54
int
rc = pthread_create (&
m_thread
, NULL, &
SystemThread::DoRun
,
55
(
void
*)
this
);
56
57
if
(rc)
58
{
59
NS_FATAL_ERROR
(
"pthread_create failed: "
<< rc <<
"=\""
<<
60
strerror (rc) <<
"\"."
);
61
}
62
}
63
64
void
65
SystemThread::Join
(
void
)
66
{
67
NS_LOG_FUNCTION
(
this
);
68
69
void
*thread_return;
70
int
rc = pthread_join (
m_thread
, &thread_return);
71
if
(rc)
72
{
73
NS_FATAL_ERROR
(
"pthread_join failed: "
<< rc <<
"=\""
<<
74
strerror (rc) <<
"\"."
);
75
}
76
}
77
78
void
*
79
SystemThread::DoRun
(
void
*arg)
80
{
81
NS_LOG_FUNCTION
(arg);
82
83
SystemThread
*
self
=
static_cast<
SystemThread
*
>
(arg);
84
self
->
m_callback
();
85
86
return
0;
87
}
88
SystemThread::ThreadId
89
SystemThread::Self
(
void
)
90
{
91
NS_LOG_FUNCTION_NOARGS
();
92
return
pthread_self ();
93
}
94
95
bool
96
SystemThread::Equals
(
SystemThread::ThreadId
id
)
97
{
98
NS_LOG_FUNCTION
(
id
);
99
return
(pthread_equal (pthread_self (),
id
) != 0);
100
}
101
102
#endif
/* HAVE_PTHREAD_H */
103
104
}
// namespace ns3
fatal-error.h
NS_FATAL_x macro definitions.
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Definition:
log-macros-enabled.h:213
ns3::Callback< void >
system-thread.h
System-independent thread class ns3::SystemThread declaration.
ns3::SystemThread::Start
void Start(void)
Start a thread of execution, running the provided callback.
Definition:
system-thread.cc:50
ns3::SystemThread::Self
static ThreadId Self(void)
Returns the current thread Id.
Definition:
system-thread.cc:89
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:202
ns3::SystemThread::~SystemThread
~SystemThread()
Destroy a SystemThread object.
Definition:
system-thread.cc:44
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition:
fatal-error.h:162
ns3::SystemThread::m_thread
pthread_t m_thread
The thread id of the child thread.
Definition:
system-thread.h:170
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition:
log-macros-enabled.h:176
ns3::SystemThread
A class which provides a relatively platform-independent thread primitive.
Definition:
system-thread.h:56
ns3::SystemThread::SystemThread
SystemThread(Callback< void > callback)
Create a SystemThread object.
Definition:
system-thread.cc:38
ns3::SystemThread::ThreadId
pthread_t ThreadId
Type alias for the system-dependent thread object.
Definition:
system-thread.h:62
ns3::SystemThread::DoRun
static void * DoRun(void *arg)
Invoke the callback in the new thread.
Definition:
system-thread.cc:79
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SystemThread::Equals
static bool Equals(ThreadId id)
Compares an ThreadId with the current ThreadId .
Definition:
system-thread.cc:96
ns3::SystemThread::Join
void Join(void)
Suspend the caller until the thread of execution, running the provided callback, finishes.
Definition:
system-thread.cc:65
ns3::SystemThread::m_callback
Callback< void > m_callback
The main function for this thread when launched.
Definition:
system-thread.h:169
log.h
Debug message logging.
Generated on Wed Nov 7 2018 10:01:50 for ns-3 by
1.8.14