libfoedus-core
FOEDUS Core Library
foedus::thread Namespace Reference

Thread and Thread-Group, which abstracts NUMA-core/node and provides API to attach/detach tasks to pooled threads. More...

Detailed Description

Thread and Thread-Group, which abstracts NUMA-core/node and provides API to attach/detach tasks to pooled threads.

This package is analogous to std::thread or boost::thread with a few additional features specific to our database engine. Among all, the main difference is that this package is fully aware of NUMA (Non Uniform Memory Access) architecture in hardware.

Worker Thread vs NUMA-Core
In our library, each worker thread (a thread that runs user/system transactions) runs on exactly one NUMA core (using numa_run_on_node()), and each NUMA core runs at most one worker thread (except when the user runs multiple engines). Hence, we use the words "thread" and "NUMA-core" interchangablly. The one-to-one mapping is to save thread-context switches and processor cache misses.
Thread and ThreadGroup: Thread Memory Hierarchy
NUMA cores have a hierarchy in hardware, which is explained in Memory Hierarchy. Our thread representations reflect the hierarchy; Thread=NUMA Core, and ThreadGroup=NUMA Node. Unfortunately, the terminology "thread group" has a different meaning in some other context, but I couldn't come with a better name.
Thread Pool and Thread Impersonation
We pre-allocate worker threads in the engine and user-programs impersonate one of the threads to run transactions. For more details, see Thread-Pool and Impersonation.
C++11 and public headers
We do NOT allow C++11 features in public headers; see C++11 Keywords in Public Headers. However, C++11's std::thread and related classes are the only cross-platform library that has no dependencies (boost::thread is NOT header-only). Hence, we use C++11's std::thread but encapsulates the details in implementations (cpp) and non-public header files (-impl.hpp).

Classes

class  ConditionVariable
 An analogue of pthread's condition variable and std::condition_variable to avoid glibc's bug in pthread_cond_broadcast/signal (thus in notify_all/one in turn). More...
 
class  GrabFreeVolatilePagesScope
 Obtains multiple free volatile pages at once and releases them automatically when this object gets out of scope. More...
 
struct  ImpersonateSession
 A user session running on an impersonated thread. More...
 
struct  NumaThreadScope
 Pin the current thread to the given NUMA node in this object's scope. More...
 
class  Rendezvous
 The frequently appearing triplet of condition_varible, "signal" flag for spurious wakeup, and mutex for a one-time single-producer multiple-consumer event synchronization. More...
 
class  StoppableThread
 The frequently appearing quartet of std::thread, condition_varible, stop-request flag, and mutex. More...
 
class  Thread
 Represents one thread running on one NUMA core. More...
 
struct  ThreadControlBlock
 Shared data of ThreadPimpl. More...
 
class  ThreadGroup
 Represents a group of pre-allocated threads running in one NUMA node. More...
 
class  ThreadGroupRef
 A view of Thread group object for other SOCs and master engine. More...
 
struct  ThreadOptions
 Set of options about threads and thread-groups. More...
 
class  ThreadPimpl
 Pimpl object of Thread. More...
 
struct  ThreadPimplCllReleaseAllFunctor
 
class  ThreadPimplMcsAdaptor
 Implements McsAdaptorConcept over ThreadPimpl. More...
 
class  ThreadPool
 The pool of pre-allocated threads in the engine to execute transactions. More...
 
class  ThreadPoolPimpl
 Pimpl object of ThreadPool. More...
 
class  ThreadRef
 A view of Thread object for other SOCs and master engine. More...
 

Typedefs

typedef uint8_t ThreadGroupId
 Typedef for an ID of ThreadGroup (NUMA node). More...
 
typedef uint8_t ThreadLocalOrdinal
 Typedef for a local ID of Thread (core), which is NOT unique across NUMA nodes. More...
 
typedef uint16_t ThreadId
 Typedef for a global ID of Thread (core), which is unique across NUMA nodes. More...
 
typedef uint16_t ThreadGlobalOrdinal
 Typedef for a globally and contiguously numbered ID of thread. More...
 
typedef uint64_t ThreadTicket
 Typedef for a monotonically increasing ticket for thread impersonation. More...
 
typedef int64_t TimeoutMicrosec
 Used as a general timeout parameter (in microseconds) for synchronous methods. More...
 

Enumerations

enum  ThreadPolicy {
  kScheduleOther = 0, kScheduleFifo = 1, kScheduleRr = 2, kScheduleBatch = 3,
  kScheduleIdle = 5
}
 Thread policy for worker threads. More...
 
enum  ThreadPriority { kPriorityIdle = 0, kPriorityLowest = 1, kPriorityDefault = 50, kPriorityHighest = 99 }
 Thread priority for worker threads. More...
 
enum  ThreadStatus {
  kNotInitialized = 0, kWaitingForTask, kWaitingForExecution, kRunningTask,
  kWaitingForClientRelease, kWaitingForTerminate, kTerminated
}
 Impersonation status of each worker thread. More...
 

Functions

ThreadId compose_thread_id (ThreadGroupId node, ThreadLocalOrdinal local_core)
 Returns a globally unique ID of Thread (core) for the given node and ordinal in the node. More...
 
ThreadGroupId decompose_numa_node (ThreadId global_id)
 Extracts NUMA node ID from the given globally unique ID of Thread (core). More...
 
ThreadLocalOrdinal decompose_numa_local_ordinal (ThreadId global_id)
 Extracts local ordinal from the given globally unique ID of Thread (core). More...
 
ThreadGlobalOrdinal to_global_ordinal (ThreadId thread_id, uint8_t threads_per_nodes)
 Calculate ThreadGlobalOrdinal from ThreadId. More...
 
std::ostream & operator<< (std::ostream &o, const ImpersonateSession &v)
 
std::ostream & operator<< (std::ostream &o, const StoppableThread &v)
 
std::ostream & operator<< (std::ostream &o, const Thread &v)
 
std::ostream & operator<< (std::ostream &o, const ThreadGroup &v)
 
void assert_mcs_aligned (const void *address)
 ThreadPimpl MCS implementations. More...
 
template<typename RW_BLOCK >
xct::McsImpl< ThreadPimplMcsAdaptor< RW_BLOCK >, RW_BLOCK > get_mcs_impl (ThreadPimpl *pimpl)
 
std::ostream & operator<< (std::ostream &o, const ThreadPool &v)
 
std::ostream & operator<< (std::ostream &o, const ThreadPoolPimpl &v)
 
std::ostream & operator<< (std::ostream &o, const ThreadGroupRef &v)
 
std::ostream & operator<< (std::ostream &o, const ThreadRef &v)
 

Variables

const ThreadGroupId kMaxThreadGroupId = 0xFF
 Maximum possible value of ThreadGroupId. More...
 
const ThreadLocalOrdinal kMaxThreadLocalOrdinal = 0xFF
 Maximum possible value of ThreadLocalOrdinal. More...
 
const ThreadId kMaxThreadId = 0xFFFF
 Maximum possible value of ThreadId. More...
 

Typedef Documentation

Used as a general timeout parameter (in microseconds) for synchronous methods.

If the method had to wait for this length, it gives up and returns a failure. Negative value means forever. 0 means no wait, in other words it's conditional (we execute the function on the condition of immediate availability).

Definition at line 159 of file thread_id.hpp.

Function Documentation

void foedus::thread::assert_mcs_aligned ( const void *  address)
inline

ThreadPimpl MCS implementations.

Definition at line 859 of file thread_pimpl.cpp.

References ASSERT_ND.

859  {
860  ASSERT_ND(address);
861  ASSERT_ND(reinterpret_cast<uintptr_t>(address) % 4 == 0);
862 }
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
template<typename RW_BLOCK >
xct::McsImpl< ThreadPimplMcsAdaptor< RW_BLOCK >, RW_BLOCK > foedus::thread::get_mcs_impl ( ThreadPimpl pimpl)

Definition at line 865 of file thread_pimpl.cpp.

865  {
866  ThreadPimplMcsAdaptor< RW_BLOCK > adaptor(pimpl);
867  xct::McsImpl< ThreadPimplMcsAdaptor< RW_BLOCK > , RW_BLOCK> impl(adaptor);
868  return impl;
869 }
std::ostream& foedus::thread::operator<< ( std::ostream &  o,
const ThreadGroup v 
)

Definition at line 57 of file thread_group.cpp.

57  {
58  o << "<ThreadGroup>";
59  o << "<group_id_>" << static_cast<int>(v.group_id_) << "</group_id_>";
60  o << "<threads_>";
61  for (Thread* child_thread : v.threads_) {
62  o << *child_thread;
63  }
64  o << "</threads_>";
65  o << "</ThreadGroup>";
66  return o;
67 }
std::ostream& foedus::thread::operator<< ( std::ostream &  o,
const ThreadPool v 
)

Definition at line 75 of file thread_pool.cpp.

75  {
76  o << *v.pimpl_;
77  return o;
78 }
std::ostream& foedus::thread::operator<< ( std::ostream &  o,
const StoppableThread v 
)

Definition at line 93 of file stoppable_thread_impl.cpp.

93  {
94  o << "<StoppableThread>"
95  << "<name_>" << v.name_ << "</name_>"
96  << "<native_thread_id>" << v.thread_.get_id() << "</native_thread_id>"
97  << "<sleep_interval_>" << v.sleep_interval_.count() << "</sleep_interval_>"
98  << "<started_>" << v.started_.load() << "</started_>"
99  << "<stop_requested_>" << v.stop_requested_.load() << "</stop_requested_>"
100  << "<stopped_>" << v.stopped_.load() << "</stopped_>"
101  << "</StoppableThread>";
102  return o;
103 }
std::ostream& foedus::thread::operator<< ( std::ostream &  o,
const Thread v 
)

Definition at line 118 of file thread.cpp.

References foedus::thread::ThreadPimpl::control_block_, foedus::thread::Thread::get_thread_global_ordinal(), foedus::thread::Thread::get_thread_id(), and foedus::thread::ThreadControlBlock::status_.

118  {
119  o << "Thread-" << v.get_thread_global_ordinal() << "(id=" << v.get_thread_id() << ") [";
120  o << "status=" << (v.pimpl_->control_block_->status_);
121  o << "]";
122  return o;
123 }

Here is the call graph for this function:

std::ostream& foedus::thread::operator<< ( std::ostream &  o,
const ThreadPoolPimpl v 
)

Definition at line 121 of file thread_pool_pimpl.cpp.

References foedus::thread::ThreadPoolPimpl::groups_.

121  {
122  o << "<ThreadPool>";
123  o << "<groups>";
124  for (const ThreadGroupRef& group : v.groups_) {
125  o << group;
126  }
127  o << "</groups>";
128  o << "</ThreadPool>";
129  return o;
130 }
std::ostream& foedus::thread::operator<< ( std::ostream &  o,
const ImpersonateSession v 
)

Definition at line 130 of file impersonate_session.cpp.

References foedus::thread::ThreadRef::get_thread_id(), foedus::thread::ImpersonateSession::is_valid(), and foedus::thread::ImpersonateSession::thread_.

130  {
131  o << "ImpersonateSession: valid=" << v.is_valid();
132  if (v.is_valid()) {
133  o << ", thread_id=" << v.thread_->get_thread_id();
134  }
135  return o;
136 }

Here is the call graph for this function:

std::ostream& foedus::thread::operator<< ( std::ostream &  o,
const ThreadGroupRef v 
)

Definition at line 160 of file thread_ref.cpp.

References foedus::thread::ThreadGroupRef::get_group_id().

160  {
161  o << "<ThreadGroupRef>";
162  o << "<group_id_>" << static_cast<int>(v.get_group_id()) << "</group_id_>";
163  o << "<threads_>";
164  for (const ThreadRef& child_thread : v.threads_) {
165  o << child_thread;
166  }
167  o << "</threads_>";
168  o << "</ThreadGroup>";
169  return o;
170 }

Here is the call graph for this function:

std::ostream& foedus::thread::operator<< ( std::ostream &  o,
const ThreadRef v 
)

Definition at line 172 of file thread_ref.cpp.

References foedus::thread::ThreadRef::get_control_block(), foedus::thread::ThreadRef::get_thread_id(), and foedus::thread::ThreadControlBlock::status_.

172  {
173  o << "ThreadRef-" << v.get_thread_id() << "[";
174  o << "status=" << (v.get_control_block()->status_);
175  o << "]";
176  return o;
177 }

Here is the call graph for this function: