src/core/system-thread.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu Nov 12 13:01:01 2009 +0100 (2009-11-12)
changeset 5505 c0ac392289c3
parent 5498 d5f70ed490a8
child 6273 8d70de29d514
permissions -rw-r--r--
replace RefCountBase with SimpleRefCount<> to avoid duplicate refcounting implementations.
craigdo@3425
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
craigdo@3425
     2
/*
craigdo@3425
     3
 * Copyright (c) 2008 INRIA
craigdo@3425
     4
 *
craigdo@3425
     5
 * This program is free software; you can redistribute it and/or modify
craigdo@3425
     6
 * it under the terms of the GNU General Public License version 2 as
craigdo@3425
     7
 * published by the Free Software Foundation;
craigdo@3425
     8
 *
craigdo@3425
     9
 * This program is distributed in the hope that it will be useful,
craigdo@3425
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
craigdo@3425
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
craigdo@3425
    12
 * GNU General Public License for more details.
craigdo@3425
    13
 *
craigdo@3425
    14
 * You should have received a copy of the GNU General Public License
craigdo@3425
    15
 * along with this program; if not, write to the Free Software
craigdo@3425
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
craigdo@3425
    17
 *
craigdo@3425
    18
 * Author: Mathieu Lacage <mathieu.lacage.inria.fr>
craigdo@3425
    19
 */
craigdo@3425
    20
craigdo@3425
    21
#ifndef SYSTEM_THREAD_H
craigdo@3425
    22
#define SYSTEM_THREAD_H
craigdo@3425
    23
craigdo@3425
    24
#include "callback.h"
craigdo@3425
    25
craigdo@3425
    26
namespace ns3 { 
craigdo@3425
    27
craigdo@3425
    28
class SystemThreadImpl;
craigdo@3425
    29
craigdo@3425
    30
/**
craigdo@3425
    31
 * @brief A class which provides a relatively platform-independent thread
craigdo@3425
    32
 * primitive.
craigdo@3425
    33
 *
craigdo@3425
    34
 * This class allows for creation of multiple threads of execution in a
craigdo@3425
    35
 * process.  The exact implementation of the thread functionality is 
craigdo@3425
    36
 * operating system dependent, but typically in ns-3 one is using an 
craigdo@3425
    37
 * environment in which Posix Threads are supported (either navively or
craigdo@3425
    38
 * in the case of Windows via Cygwin's implementation of pthreads on the 
craigdo@3425
    39
 * Win32 API.  In either case we expect that these will be kernel-level
craigdo@3425
    40
 * threads and therefore a system with multiple CPUs will see truly concurrent 
craigdo@3425
    41
 * execution.
craigdo@3425
    42
 *
craigdo@3425
    43
 * Synchronization between threads is provided via the SystemMutex class.
craigdo@3425
    44
 */
mathieu@5505
    45
class SystemThread : public SimpleRefCount<SystemThread>
craigdo@3425
    46
{
craigdo@3425
    47
public:
craigdo@3425
    48
  /**
craigdo@3425
    49
   * @brief Create a SystemThread object.
craigdo@3425
    50
   *
craigdo@3425
    51
   * A system thread object is not created running.  A thread of execution
craigdo@3425
    52
   * must be explicitly started by calling the Start method.  When the 
craigdo@3425
    53
   * Start method is called, it will spawn a thread of execution and cause
craigdo@3425
    54
   * that thread to call out into the callback function provided here as
craigdo@3425
    55
   * a parameter.
craigdo@3425
    56
   *
craigdo@3425
    57
   * Like all ns-3 callbacks, the provided callback may refer to a function
craigdo@3425
    58
   * or a method of an object depending on how the MakeCallback function is
craigdo@3425
    59
   * used.
craigdo@3425
    60
   *
craigdo@3425
    61
   * The most common use is expected to be creating a thread of execution in
craigdo@3425
    62
   * a method.  In this case you would use code similar to,
craigdo@3425
    63
   *
craigdo@3425
    64
   *   MyClass myObject;
craigdo@3425
    65
   *   Ptr<SystemThread> st = Create<SystemThread> (
craigdo@3425
    66
   *     MakeCallback (&MyClass::MyMethod, &myObject));
craigdo@3425
    67
   *   st->Start ();
craigdo@3425
    68
   *
craigdo@3425
    69
   * The SystemThread is passed a callback that calls out to the function
craigdo@3425
    70
   * MyClass::MyMethod.  When this function is called, it is called as an
craigdo@3425
    71
   * object method on the myObject object.  Essentially what you are doing
craigdo@3425
    72
   * is asking the SystemThread to call object->MyMethod () in a new thread
craigdo@3425
    73
   * of execution.
craigdo@3425
    74
   *
craigdo@5498
    75
   * If starting a thread in your currently executing object, you can use the
craigdo@5498
    76
   * "this" pointer:
craigdo@5498
    77
   *
craigdo@5498
    78
   *   Ptr<SystemThread> st = Create<SystemThread> (
craigdo@5498
    79
   *     MakeCallback (&MyClass::MyMethod, this));
craigdo@5498
    80
   *   st->Start ();
craigdo@5498
    81
   *
craigdo@5498
    82
   * Object lifetime is always an issue with threads, so it is common to use
craigdo@5498
    83
   * smart pointers.  If you are spinning up a thread in an object that is 
craigdo@5498
    84
   * managed by a smart pointer, you can use that pointer directly:
craigdo@3425
    85
   *
craigdo@3425
    86
   *   Ptr<MyClass> myPtr = Create<MyClass> ();
craigdo@3425
    87
   *   Ptr<SystemThread> st = Create<SystemThread> (
craigdo@5498
    88
   *     MakeCallback (&MyClass::MyMethod, myPtr));
craigdo@3425
    89
   *   st->Start ();
craigdo@3425
    90
   *
craigdo@3425
    91
   * Just like any thread, you can synchronize with its termination.  The 
craigdo@3425
    92
   * method provided to do this is Join (). If you call Join() you will block
craigdo@3425
    93
   * until the SystemThread run method returns.
craigdo@3425
    94
   *
vincent@5227
    95
   * @param callback entry point of the thread
vincent@5227
    96
   * 
craigdo@3791
    97
   * @warning The SystemThread uses SIGALRM to wake threads that are possibly
craigdo@3791
    98
   * blocked on IO.
craigdo@3791
    99
   * @see Shutdown
craigdo@3791
   100
   *
craigdo@3425
   101
   * @warning I've made the system thread class look like a normal ns3 object
craigdo@3425
   102
   * with smart pointers, and living in the heap.  This makes it very easy to
craigdo@3425
   103
   * manage threads from a single master thread context.  You should be very
craigdo@3425
   104
   * aware though that I have not made Ptr multithread safe!  This means that
craigdo@3425
   105
   * if you pass Ptr<SystemThread> around in a multithreaded environment, it is
craigdo@3425
   106
   * possible that the reference count will get messed up since it is not an
craigdo@3425
   107
   * atomic operation.  CREATE AND MANAGE YOUR THREADS IN ONE PLACE -- LEAVE
craigdo@3425
   108
   * THE PTR THERE.
craigdo@3425
   109
   */
craigdo@3425
   110
  SystemThread(Callback<void> callback);
craigdo@3425
   111
craigdo@3425
   112
  /**
craigdo@3425
   113
   * @brief Destroy a SystemThread object.
craigdo@3425
   114
   *
craigdo@3425
   115
   */
craigdo@3425
   116
  ~SystemThread();
craigdo@3425
   117
craigdo@3425
   118
  /**
craigdo@3425
   119
   * @brief Start a thread of execution, running the provided callback.
craigdo@3425
   120
   */
craigdo@3425
   121
  void Start (void);
craigdo@3425
   122
craigdo@3425
   123
  /**
craigdo@3425
   124
   * @brief Suspend the caller until the thread of execution, running the 
craigdo@3425
   125
   * provided callback, finishes.
craigdo@3425
   126
   */
craigdo@3425
   127
  void Join (void);
craigdo@3425
   128
craigdo@3791
   129
  /**
craigdo@3791
   130
   * @brief Indicates to a managed thread doing cooperative multithreading that
craigdo@3791
   131
   * its managing thread wants it to exit.
craigdo@3791
   132
   *
craigdo@3791
   133
   * It is often the case that we want a thread to be off doing work until such
craigdo@3791
   134
   * time as its job is done (typically when the simulation is done).  We then 
craigdo@3791
   135
   * want the thread to exit itself.  This method provides a consistent way for
craigdo@3791
   136
   * the managing thread to communicate with the managed thread.  After the
craigdo@3791
   137
   * manager thread calls this method, the Break() method will begin returning
craigdo@3791
   138
   * true, telling the managed thread to exit.
craigdo@3791
   139
   *
craigdo@3791
   140
   * This alone isn't really enough to merit these events, but in Unix, if a
craigdo@3791
   141
   * worker thread is doing blocking IO, it will need to be woken up from that
craigdo@3791
   142
   * read somehow.  This method also provides that functionality, by sending a
craigdo@3791
   143
   * SIGALRM signal to the possibly blocked thread.
craigdo@3791
   144
   *
craigdo@3792
   145
   * @warning Uses SIGALRM to notifiy threads possibly blocked on IO.  Beware
craigdo@3792
   146
   * if you are using signals.
craigdo@3791
   147
   * @see Break
craigdo@3791
   148
   */
craigdo@3791
   149
  void Shutdown (void);
craigdo@3791
   150
craigdo@3791
   151
  /**
craigdo@3791
   152
   * @brief Indicates to a thread doing cooperative multithreading that
craigdo@3791
   153
   * its managing thread wants it to exit.
craigdo@3791
   154
   *
craigdo@3791
   155
   * It is often the case that we want a thread to be off doing work until such
craigdo@3791
   156
   * time as its job is done.  We then want the thread to exit itself.  This
craigdo@3791
   157
   * method allows a thread to query whether or not it should be running.  
craigdo@3791
   158
   * Typically, the worker thread is running in a forever-loop, and will need to
craigdo@3791
   159
   * "break" out of that loop to exit -- thus the name.
craigdo@3791
   160
   *
craigdo@3791
   161
   * @see Shutdown
craigdo@3791
   162
   * @returns true if thread is expected to exit (break out of the forever-loop)
craigdo@3791
   163
   */
craigdo@3791
   164
  bool Break (void);
craigdo@3791
   165
craigdo@3425
   166
private:
craigdo@3425
   167
  SystemThreadImpl * m_impl;
craigdo@3791
   168
  bool m_break;
craigdo@3425
   169
};
craigdo@3425
   170
craigdo@3425
   171
} //namespace ns3
craigdo@3425
   172
craigdo@3425
   173
#endif /* SYSTEM_THREAD_H */
craigdo@3425
   174
craigdo@3425
   175