diff --git a/src/core/model/system-thread.cc b/src/core/model/system-thread.cc new file mode 100644 index 000000000..4d9825d4b --- /dev/null +++ b/src/core/model/system-thread.cc @@ -0,0 +1,85 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2008 INRIA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ + +#include "fatal-error.h" +#include "system-thread.h" +#include "log.h" +#include + +NS_LOG_COMPONENT_DEFINE ("SystemThread"); + +namespace ns3 { + +#ifdef HAVE_PTHREAD_H + +SystemThread::SystemThread (Callback callback) + : m_callback (callback) +{ + NS_LOG_FUNCTION (this); +} + +SystemThread::~SystemThread() +{ + NS_LOG_FUNCTION (this); +} + +void +SystemThread::Start (void) +{ + NS_LOG_FUNCTION (this); + + int rc = pthread_create (&m_thread, NULL, &SystemThread::DoRun, + (void *)this); + + if (rc) + { + NS_FATAL_ERROR ("pthread_create failed: " << rc << "=\"" << + strerror (rc) << "\"."); + } +} + +void +SystemThread::Join (void) +{ + NS_LOG_FUNCTION (this); + + void *thread_return; + int rc = pthread_join (m_thread, &thread_return); + if (rc) + { + NS_FATAL_ERROR ("pthread_join failed: " << rc << "=\"" << + strerror (rc) << "\"."); + } +} + +void * +SystemThread::DoRun (void *arg) +{ + NS_LOG_FUNCTION (arg); + + SystemThread *self = static_cast (arg); + self->m_callback (); + + return 0; +} + +#endif /* HAVE_PTHREAD_H */ + +} // namespace ns3 diff --git a/src/core/model/system-thread.h b/src/core/model/system-thread.h index 881f7c251..b63acd9ca 100644 --- a/src/core/model/system-thread.h +++ b/src/core/model/system-thread.h @@ -21,12 +21,14 @@ #ifndef SYSTEM_THREAD_H #define SYSTEM_THREAD_H +#include "ns3/core-config.h" #include "callback.h" +#ifdef HAVE_PTHREAD_H +#include +#endif /* HAVE_PTHREAD_H */ namespace ns3 { -class SystemThreadImpl; - /** * @brief A class which provides a relatively platform-independent thread * primitive. @@ -123,7 +125,13 @@ public: void Join (void); private: - SystemThreadImpl * m_impl; +#ifdef HAVE_PTHREAD_H + static void *DoRun (void *arg); + + Callback m_callback; + pthread_t m_thread; + void * m_ret; +#endif }; } // namespace ns3 diff --git a/src/core/model/unix-system-thread.cc b/src/core/model/unix-system-thread.cc deleted file mode 100644 index d878006ec..000000000 --- a/src/core/model/unix-system-thread.cc +++ /dev/null @@ -1,139 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2008 INRIA - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author: Mathieu Lacage - */ - -#include -#include -#include "fatal-error.h" -#include "system-thread.h" -#include "log.h" - -NS_LOG_COMPONENT_DEFINE ("SystemThread"); - -namespace ns3 { - -// -// Private implementation class for the SystemThread class. The deal is -// that we export the SystemThread class to the user. The header just -// declares a class and its members. There is a forward declaration for -// a private implementation class there and a member declaration. Thus -// there is no knowledge of the implementation in the exported header. -// -// We provide an implementation class for each operating system. This is -// the Unix implementation of the SystemThread. -// -// In order to use the SystemThread, you will include "system-thread.h" and -// get the implementation by linking unix-system-thread.cc (if you are running -// a Posix system). -// -class SystemThreadImpl -{ -public: - SystemThreadImpl (Callback callback); - - void Start (void); - void Join (void); - -private: - static void *DoRun (void *arg); - Callback m_callback; - pthread_t m_thread; - void * m_ret; -}; - -SystemThreadImpl::SystemThreadImpl (Callback callback) - : m_callback (callback) -{ - NS_LOG_FUNCTION_NOARGS (); -} - -void -SystemThreadImpl::Start (void) -{ - NS_LOG_FUNCTION_NOARGS (); - - int rc = pthread_create (&m_thread, NULL, &SystemThreadImpl::DoRun, - (void *)this); - - if (rc) - { - NS_FATAL_ERROR ("pthread_create failed: " << rc << "=\"" << - strerror (rc) << "\"."); - } -} - -void -SystemThreadImpl::Join (void) -{ - NS_LOG_FUNCTION_NOARGS (); - - void *thread_return; - int rc = pthread_join (m_thread, &thread_return); - if (rc) - { - NS_FATAL_ERROR ("pthread_join failed: " << rc << "=\"" << - strerror (rc) << "\"."); - } -} - - -void * -SystemThreadImpl::DoRun (void *arg) -{ - NS_LOG_FUNCTION_NOARGS (); - - SystemThreadImpl *self = static_cast (arg); - self->m_callback (); - - return 0; -} - -// -// Remember that we just export the delcaration of the SystemThread class to -// the user. There is no code to implement the SystemThread methods. We -// have to do that here. We just vector the calls to our implementation -// class above. -// -SystemThread::SystemThread (Callback callback) - : m_impl (new SystemThreadImpl (callback)) -{ - NS_LOG_FUNCTION_NOARGS (); -} - -SystemThread::~SystemThread() -{ - NS_LOG_FUNCTION_NOARGS (); - delete m_impl; -} - -void -SystemThread::Start (void) -{ - NS_LOG_FUNCTION_NOARGS (); - m_impl->Start (); -} - -void -SystemThread::Join (void) -{ - NS_LOG_FUNCTION_NOARGS (); - m_impl->Join (); -} - -} // namespace ns3 diff --git a/src/core/wscript b/src/core/wscript index a313a7237..db7fb389c 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -283,8 +283,8 @@ def build(bld): if env['ENABLE_THREADING']: core.source.extend([ + 'model/system-thread.cc', 'model/unix-fd-reader.cc', - 'model/unix-system-thread.cc', 'model/unix-system-mutex.cc', 'model/unix-system-condition.cc', ])