Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
impala::Thread Class Reference

TODO: Consider allowing fragment IDs as category parameters. More...

#include <thread.h>

Collaboration diagram for impala::Thread:

Public Member Functions

template<class F >
 Thread (const std::string &category, const std::string &name, const F &f)
 
template<class F , class A1 >
 Thread (const std::string &category, const std::string &name, const F &f, const A1 &a1)
 
template<class F , class A1 , class A2 >
 Thread (const std::string &category, const std::string &name, const F &f, const A1 &a1, const A2 &a2)
 
template<class F , class A1 , class A2 , class A3 >
 Thread (const std::string &category, const std::string &name, const F &f, const A1 &a1, const A2 &a2, const A3 &a3)
 
template<class F , class A1 , class A2 , class A3 , class A4 >
 Thread (const std::string &category, const std::string &name, const F &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
 
template<class F , class A1 , class A2 , class A3 , class A4 , class A5 >
 Thread (const std::string &category, const std::string &name, const F &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5)
 
void Join () const
 
int64_t tid () const
 

Static Public Attributes

static const int64_t INVALID_THREAD_ID = -1
 

Private Types

typedef boost::function< void()> ThreadFunctor
 Function object that wraps the user-supplied function to run in a separate thread. More...
 

Private Member Functions

void StartThread (const ThreadFunctor &functor)
 

Static Private Member Functions

static void SuperviseThread (const std::string &name, const std::string &category, ThreadFunctor functor, Promise< int64_t > *thread_started)
 

Private Attributes

boost::scoped_ptr< boost::thread > thread_
 The actual thread object that runs the user's method via SuperviseThread(). More...
 
const std::string category_
 Name and category for this thread. More...
 
const std::string name_
 
int64_t tid_
 

Static Private Attributes

static const int64_t UNINITIALISED_THREAD_ID = -2
 

Detailed Description

TODO: Consider allowing fragment IDs as category parameters.

Thin wrapper around boost::thread that can register itself with the singleton ThreadMgr (a private class implemented in thread.cc entirely, which tracks all live threads so that they may be monitored via the debug webpages). This class has a limited subset of boost::thread's API. Construction is almost the same, but clients must supply a category and a name for each thread so that they can be identified in the debug web UI. Otherwise, Join() is the only supported method from boost::thread. Each Thread object knows its operating system thread ID (tid), which can be used to attach debuggers to specific threads, to retrieve resource-usage statistics from the operating system, and to assign threads to resource control groups.

Definition at line 45 of file thread.h.

Member Typedef Documentation

typedef boost::function<void ()> impala::Thread::ThreadFunctor
private

Function object that wraps the user-supplied function to run in a separate thread.

Definition at line 117 of file thread.h.

Constructor & Destructor Documentation

template<class F >
impala::Thread::Thread ( const std::string &  category,
const std::string &  name,
const F &  f 
)
inline

This constructor pattern mimics that in boost::thread. There is one constructor for each number of arguments that the thread function accepts. To extend the set of acceptable signatures, add another constructor with <class F, class A1.... class An>. In general:

  • category: string identifying the thread category to which this thread belongs, used for organising threads together on the debug UI.
  • name: name of this thread. Will be appended with "-<thread-id>" to ensure uniqueness.
  • F - a method type that supports operator(), and the instance passed to the constructor is executed immediately in a separate thread.
  • A1...An - argument types whose instances are passed to f(...)

Definition at line 61 of file thread.h.

References StartThread().

template<class F , class A1 >
impala::Thread::Thread ( const std::string &  category,
const std::string &  name,
const F &  f,
const A1 &  a1 
)
inline

Definition at line 67 of file thread.h.

References StartThread().

template<class F , class A1 , class A2 >
impala::Thread::Thread ( const std::string &  category,
const std::string &  name,
const F &  f,
const A1 &  a1,
const A2 &  a2 
)
inline

Definition at line 73 of file thread.h.

References StartThread().

template<class F , class A1 , class A2 , class A3 >
impala::Thread::Thread ( const std::string &  category,
const std::string &  name,
const F &  f,
const A1 &  a1,
const A2 &  a2,
const A3 &  a3 
)
inline

Definition at line 80 of file thread.h.

References StartThread().

template<class F , class A1 , class A2 , class A3 , class A4 >
impala::Thread::Thread ( const std::string &  category,
const std::string &  name,
const F &  f,
const A1 &  a1,
const A2 &  a2,
const A3 &  a3,
const A4 &  a4 
)
inline

Definition at line 87 of file thread.h.

References StartThread().

template<class F , class A1 , class A2 , class A3 , class A4 , class A5 >
impala::Thread::Thread ( const std::string &  category,
const std::string &  name,
const F &  f,
const A1 &  a1,
const A2 &  a2,
const A3 &  a3,
const A4 &  a4,
const A5 &  a5 
)
inline

Definition at line 94 of file thread.h.

References StartThread().

Member Function Documentation

void impala::Thread::Join ( ) const
inline

Blocks until this thread finishes execution. Once this method returns, the thread will be unregistered with the ThreadMgr and will not appear in the debug UI.

Definition at line 102 of file thread.h.

References thread_.

Referenced by ImpalaThreadStarter(), and impala::ThreadGroup::JoinAll().

void impala::Thread::StartThread ( const ThreadFunctor functor)
private

Starts the thread running SuperviseThread(), and returns once that thread has initialised and its TID read. Waits for notification from the started thread that initialisation is complete before returning.

Definition at line 270 of file thread.cc.

References impala::Promise< T >::Get(), and impala::thread_manager.

Referenced by Thread().

void impala::Thread::SuperviseThread ( const std::string &  name,
const std::string &  category,
ThreadFunctor  functor,
Promise< int64_t > *  thread_started 
)
staticprivate

Wrapper for the user-supplied function. Always invoked from thread_. Executes the method in functor_, but before doing so registers with the global ThreadMgr and reads the thread's system TID. After the method terminates, it is unregistered. SuperviseThread() notifies StartThread() when thread initialisation is completed via the promise parameter, which is set to the new thread's system ID. After this point, it is no longer safe for SuperviseThread() to refer to parameters passed by reference or pointer to this method, because of a wrinkle in the lifecycle of boost threads: if the thread object representing a thread should be destroyed, the actual operating-system thread continues to run (the thread is detached, not terminated). Therefore it's not safe to make reference to the Thread object or any of its members in SuperviseThread() after it notifies the caller via thread_started that initialisation is completed. An alternative is to join() in the destructor of Thread, but that's not the same semantics as boost::thread, which we are trying to emulate here. As a result, the 'functor' parameter is deliberately copied into this method, since it is used after the notification completes.h The tid parameter is written to exactly once before SuperviseThread() notifies the caller.

Definition at line 288 of file thread.cc.

References impala::GetStrErrMsg(), impala::name, impala::Promise< T >::Set(), and impala::thread_manager.

int64_t impala::Thread::tid ( ) const
inline

The thread ID assigned to this thread by the operating system. If the OS does not support retrieving the tid, returns Thread::INVALID_THREAD_ID.

Definition at line 106 of file thread.h.

References tid_.

Referenced by impala::CgroupsMgr::AssignThreadToCgroup().

Member Data Documentation

const std::string impala::Thread::category_
private

Name and category for this thread.

Definition at line 123 of file thread.h.

const int64_t impala::Thread::INVALID_THREAD_ID = -1
static

Definition at line 108 of file thread.h.

const std::string impala::Thread::name_
private

Definition at line 124 of file thread.h.

boost::scoped_ptr<boost::thread> impala::Thread::thread_
private

The actual thread object that runs the user's method via SuperviseThread().

Definition at line 120 of file thread.h.

Referenced by Join().

int64_t impala::Thread::tid_
private

OS-specific thread ID. Set to UNINITIALISED_THREAD_ID initially, but once the constructor returns from StartThread() the tid_ is guaranteed to be set either to a non-negative integer, or INVALID_THREAD_ID.

Definition at line 129 of file thread.h.

Referenced by tid().

const int64_t impala::Thread::UNINITIALISED_THREAD_ID = -2
staticprivate

To distinguish between a thread ID that can't be determined, and one that hasn't been assigned. Since tid_ is set in the constructor, this value will never be seen by clients of this class.

Definition at line 114 of file thread.h.


The documentation for this class was generated from the following files: