src/core/model/system-thread.cc
author Vedran Miletić <rivanvx@gmail.com>
Sat, 01 Sep 2012 20:57:21 +0200
changeset 9063 32755d0516f4
parent 7779 06b679a6e1b6
child 9134 7a750f032acd
permissions -rw-r--r--
Bug 1237 - code cleanups related to includes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
#include "fatal-error.h"
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "system-thread.h"
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include "log.h"
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7779
diff changeset
    24
#include <cstring>
3425
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
NS_LOG_COMPONENT_DEFINE ("SystemThread");
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
namespace ns3 {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    30
#ifdef HAVE_PTHREAD_H
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    32
SystemThread::SystemThread (Callback<void> callback)
7777
f1e73a2cdb95 kill unused code
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7256
diff changeset
    33
  : m_callback (callback)
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
{
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    35
  NS_LOG_FUNCTION (this);
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    36
}
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    37
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    38
SystemThread::~SystemThread()
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    39
{
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    40
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    43
void
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    44
SystemThread::Start (void)
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
{
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    46
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    48
  int rc = pthread_create (&m_thread, NULL, &SystemThread::DoRun,
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    49
                           (void *)this);
3425
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
  if (rc) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
      NS_FATAL_ERROR ("pthread_create failed: " << rc << "=\"" << 
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7169
diff changeset
    54
                      strerror (rc) << "\".");
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
    }
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
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    58
void
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    59
SystemThread::Join (void)
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
{
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    61
  NS_LOG_FUNCTION (this);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
  void *thread_return;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
  int rc = pthread_join (m_thread, &thread_return);
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  if (rc) 
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
    {
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
      NS_FATAL_ERROR ("pthread_join failed: " << rc << "=\"" << 
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7169
diff changeset
    68
                      strerror (rc) << "\".");
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
    }
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
}
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    72
void *
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    73
SystemThread::DoRun (void *arg)
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
{
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    75
  NS_LOG_FUNCTION (arg);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    77
  SystemThread *self = static_cast<SystemThread *> (arg);
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  self->m_callback ();
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
  return 0;
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
}
7779
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    82
SystemThread::ThreadId 
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    83
SystemThread::Self (void)
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    84
{
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    85
  return pthread_self ();
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    86
}
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    87
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    88
bool 
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    89
SystemThread::Equals (SystemThread::ThreadId id)
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    90
{
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    91
  return (pthread_equal (pthread_self (), id) != 0);
06b679a6e1b6 add API to know which thread we are in
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7778
diff changeset
    92
}
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
7778
7b2c9728d2fb remove abstraction that is going to be broken
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7777
diff changeset
    94
#endif /* HAVE_PTHREAD_H */
3425
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
c69779f5e51e add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
} // namespace ns3