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.
     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 #ifndef SYSTEM_THREAD_H
    22 #define SYSTEM_THREAD_H
    23 
    24 #include "callback.h"
    25 
    26 namespace ns3 { 
    27 
    28 class SystemThreadImpl;
    29 
    30 /**
    31  * @brief A class which provides a relatively platform-independent thread
    32  * primitive.
    33  *
    34  * This class allows for creation of multiple threads of execution in a
    35  * process.  The exact implementation of the thread functionality is 
    36  * operating system dependent, but typically in ns-3 one is using an 
    37  * environment in which Posix Threads are supported (either navively or
    38  * in the case of Windows via Cygwin's implementation of pthreads on the 
    39  * Win32 API.  In either case we expect that these will be kernel-level
    40  * threads and therefore a system with multiple CPUs will see truly concurrent 
    41  * execution.
    42  *
    43  * Synchronization between threads is provided via the SystemMutex class.
    44  */
    45 class SystemThread : public SimpleRefCount<SystemThread>
    46 {
    47 public:
    48   /**
    49    * @brief Create a SystemThread object.
    50    *
    51    * A system thread object is not created running.  A thread of execution
    52    * must be explicitly started by calling the Start method.  When the 
    53    * Start method is called, it will spawn a thread of execution and cause
    54    * that thread to call out into the callback function provided here as
    55    * a parameter.
    56    *
    57    * Like all ns-3 callbacks, the provided callback may refer to a function
    58    * or a method of an object depending on how the MakeCallback function is
    59    * used.
    60    *
    61    * The most common use is expected to be creating a thread of execution in
    62    * a method.  In this case you would use code similar to,
    63    *
    64    *   MyClass myObject;
    65    *   Ptr<SystemThread> st = Create<SystemThread> (
    66    *     MakeCallback (&MyClass::MyMethod, &myObject));
    67    *   st->Start ();
    68    *
    69    * The SystemThread is passed a callback that calls out to the function
    70    * MyClass::MyMethod.  When this function is called, it is called as an
    71    * object method on the myObject object.  Essentially what you are doing
    72    * is asking the SystemThread to call object->MyMethod () in a new thread
    73    * of execution.
    74    *
    75    * If starting a thread in your currently executing object, you can use the
    76    * "this" pointer:
    77    *
    78    *   Ptr<SystemThread> st = Create<SystemThread> (
    79    *     MakeCallback (&MyClass::MyMethod, this));
    80    *   st->Start ();
    81    *
    82    * Object lifetime is always an issue with threads, so it is common to use
    83    * smart pointers.  If you are spinning up a thread in an object that is 
    84    * managed by a smart pointer, you can use that pointer directly:
    85    *
    86    *   Ptr<MyClass> myPtr = Create<MyClass> ();
    87    *   Ptr<SystemThread> st = Create<SystemThread> (
    88    *     MakeCallback (&MyClass::MyMethod, myPtr));
    89    *   st->Start ();
    90    *
    91    * Just like any thread, you can synchronize with its termination.  The 
    92    * method provided to do this is Join (). If you call Join() you will block
    93    * until the SystemThread run method returns.
    94    *
    95    * @param callback entry point of the thread
    96    * 
    97    * @warning The SystemThread uses SIGALRM to wake threads that are possibly
    98    * blocked on IO.
    99    * @see Shutdown
   100    *
   101    * @warning I've made the system thread class look like a normal ns3 object
   102    * with smart pointers, and living in the heap.  This makes it very easy to
   103    * manage threads from a single master thread context.  You should be very
   104    * aware though that I have not made Ptr multithread safe!  This means that
   105    * if you pass Ptr<SystemThread> around in a multithreaded environment, it is
   106    * possible that the reference count will get messed up since it is not an
   107    * atomic operation.  CREATE AND MANAGE YOUR THREADS IN ONE PLACE -- LEAVE
   108    * THE PTR THERE.
   109    */
   110   SystemThread(Callback<void> callback);
   111 
   112   /**
   113    * @brief Destroy a SystemThread object.
   114    *
   115    */
   116   ~SystemThread();
   117 
   118   /**
   119    * @brief Start a thread of execution, running the provided callback.
   120    */
   121   void Start (void);
   122 
   123   /**
   124    * @brief Suspend the caller until the thread of execution, running the 
   125    * provided callback, finishes.
   126    */
   127   void Join (void);
   128 
   129   /**
   130    * @brief Indicates to a managed thread doing cooperative multithreading that
   131    * its managing thread wants it to exit.
   132    *
   133    * It is often the case that we want a thread to be off doing work until such
   134    * time as its job is done (typically when the simulation is done).  We then 
   135    * want the thread to exit itself.  This method provides a consistent way for
   136    * the managing thread to communicate with the managed thread.  After the
   137    * manager thread calls this method, the Break() method will begin returning
   138    * true, telling the managed thread to exit.
   139    *
   140    * This alone isn't really enough to merit these events, but in Unix, if a
   141    * worker thread is doing blocking IO, it will need to be woken up from that
   142    * read somehow.  This method also provides that functionality, by sending a
   143    * SIGALRM signal to the possibly blocked thread.
   144    *
   145    * @warning Uses SIGALRM to notifiy threads possibly blocked on IO.  Beware
   146    * if you are using signals.
   147    * @see Break
   148    */
   149   void Shutdown (void);
   150 
   151   /**
   152    * @brief Indicates to a thread doing cooperative multithreading that
   153    * its managing thread wants it to exit.
   154    *
   155    * It is often the case that we want a thread to be off doing work until such
   156    * time as its job is done.  We then want the thread to exit itself.  This
   157    * method allows a thread to query whether or not it should be running.  
   158    * Typically, the worker thread is running in a forever-loop, and will need to
   159    * "break" out of that loop to exit -- thus the name.
   160    *
   161    * @see Shutdown
   162    * @returns true if thread is expected to exit (break out of the forever-loop)
   163    */
   164   bool Break (void);
   165 
   166 private:
   167   SystemThreadImpl * m_impl;
   168   bool m_break;
   169 };
   170 
   171 } //namespace ns3
   172 
   173 #endif /* SYSTEM_THREAD_H */
   174 
   175