A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
system-thread.cc
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 
26 NS_LOG_COMPONENT_DEFINE ("SystemThread");
27 
28 namespace ns3 {
29 
30 #ifdef HAVE_PTHREAD_H
31 
32 SystemThread::SystemThread (Callback<void> callback)
33  : m_callback (callback)
34 {
35  NS_LOG_FUNCTION (this << &callback);
36 }
37 
39 {
40  NS_LOG_FUNCTION (this);
41 }
42 
43 void
45 {
46  NS_LOG_FUNCTION (this);
47 
48  int rc = pthread_create (&m_thread, NULL, &SystemThread::DoRun,
49  (void *)this);
50 
51  if (rc)
52  {
53  NS_FATAL_ERROR ("pthread_create failed: " << rc << "=\"" <<
54  strerror (rc) << "\".");
55  }
56 }
57 
58 void
59 SystemThread::Join (void)
60 {
61  NS_LOG_FUNCTION (this);
62 
63  void *thread_return;
64  int rc = pthread_join (m_thread, &thread_return);
65  if (rc)
66  {
67  NS_FATAL_ERROR ("pthread_join failed: " << rc << "=\"" <<
68  strerror (rc) << "\".");
69  }
70 }
71 
72 void *
73 SystemThread::DoRun (void *arg)
74 {
75  NS_LOG_FUNCTION (arg);
76 
77  SystemThread *self = static_cast<SystemThread *> (arg);
78  self->m_callback ();
79 
80  return 0;
81 }
82 SystemThread::ThreadId
83 SystemThread::Self (void)
84 {
86  return pthread_self ();
87 }
88 
89 bool
90 SystemThread::Equals (SystemThread::ThreadId id)
91 {
92  NS_LOG_FUNCTION (id);
93  return (pthread_equal (pthread_self (), id) != 0);
94 }
95 
96 #endif /* HAVE_PTHREAD_H */
97 
98 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
void Start(void)
Start a thread of execution, running the provided callback.
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
~SystemThread()
Destroy a SystemThread object.
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
SystemThread(Callback< void > callback)
Create a SystemThread object.
static bool Equals(ThreadId id)
Compares an TharedId with the current ThreadId .
void Join(void)
Suspend the caller until the thread of execution, running the provided callback, finishes.
static ThreadId Self(void)
Returns the current thread Id.