libfoedus-core
FOEDUS Core Library
foedus::thread::ImpersonateSession Struct Referencefinal

A user session running on an impersonated thread. More...

Detailed Description

A user session running on an impersonated thread.

Overview
This object represents an impersonated session, which is obtained by calling ThreadPool::impersonate() and running the given task on a pre-allocated thread. This object also works as a future to synchronously or asynchronously wait for the completion of the functor from the client program's thread.
Result of Impersonation
The client program has to first check is_valid() for the given session because there are two possible cases as the result of impersonation.
  • Impersonation succeded (is_valid()==true), running the task on the impersonated thread. In this case, the client's thread (original thread that invoked ThreadPool::impersonate()) can either wait for the end of the impersonated thread (call wait() or wait_for()), or do other things (e.g., launch more impersonated threads).
  • Impersonation failed (is_valid()==false). This might happen for various reasons; timeout, the engine shuts down while waiting, unexpected errors, etc. In this case, get_invalid_cause() indicates the causes of the failed impersonation.
Wait for the completion of the session
When is_valid()==true, this object also behaves as std::shared_future<ErrorStack>, and we actually use it. But, because of the C++11 Keywords in Public Headers issue, we wrap it as a usual class. This object is copiable like std::shared_future, not std::future. Actually, this class is based on std::shared_future just to provide copy semantics for non-C++11 clients. Additional overheads shouldn't matter, hopeully.
Copy/Move
This object is not copy-able because the destructor closes the session in shared memory. However, it is moveable, which moves the ownership (available only with C++11 though).

Definition at line 61 of file impersonate_session.hpp.

#include <impersonate_session.hpp>

Collaboration diagram for foedus::thread::ImpersonateSession:

Public Member Functions

 ImpersonateSession ()
 
 ~ImpersonateSession ()
 
 ImpersonateSession (const ImpersonateSession &other)=delete
 
ImpersonateSessionoperator= (const ImpersonateSession &other)=delete
 
 ImpersonateSession (ImpersonateSession &&other)
 
ImpersonateSessionoperator= (ImpersonateSession &&other)
 
bool is_valid () const
 Returns if the impersonation succeeded. More...
 
bool is_running () const
 Returns if the task is still running. More...
 
ErrorStack get_result ()
 Waits until the completion of the asynchronous session and retrieves the result. More...
 
uint64_t get_output_size ()
 Returns the byte size of output. More...
 
void get_output (void *output_buffer)
 Copies the output to the given buffer, whose size must be at least get_output_size(). More...
 
const void * get_raw_output_buffer ()
 Returns the pointer to the raw output buffer on shared memory. More...
 
void wait () const
 Blocks until the completion of the asynchronous session. More...
 
bool wait_for (uint64_t timeout_microsec) const
 Blocks until either the asynchronous session completes or the specified time elapses. More...
 
void release ()
 Releases all resources and ownerships this session has acquired. More...
 

Public Attributes

thread::ThreadRefthread_
 The impersonated thread. More...
 
thread::ThreadTicket ticket_
 The ticket issued as of impersonation. More...
 

Friends

std::ostream & operator<< (std::ostream &o, const ImpersonateSession &v)
 

Constructor & Destructor Documentation

foedus::thread::ImpersonateSession::ImpersonateSession ( )
inline

Definition at line 62 of file impersonate_session.hpp.

#define CXX11_NULLPTR
Used in public headers in place of "nullptr" of C++11.
Definition: cxx11.hpp:132
thread::ThreadRef * thread_
The impersonated thread.
thread::ThreadTicket ticket_
The ticket issued as of impersonation.
foedus::thread::ImpersonateSession::~ImpersonateSession ( )
inline

Definition at line 63 of file impersonate_session.hpp.

References release().

63 { release(); }
void release()
Releases all resources and ownerships this session has acquired.

Here is the call graph for this function:

foedus::thread::ImpersonateSession::ImpersonateSession ( const ImpersonateSession other)
delete
foedus::thread::ImpersonateSession::ImpersonateSession ( ImpersonateSession &&  other)

Definition at line 30 of file impersonate_session.cpp.

References thread_, and ticket_.

30  {
31  ticket_ = other.ticket_;
32  thread_ = other.thread_;
33  other.ticket_ = 0;
34  other.thread_ = nullptr;
35 }
thread::ThreadRef * thread_
The impersonated thread.
thread::ThreadTicket ticket_
The ticket issued as of impersonation.

Member Function Documentation

void foedus::thread::ImpersonateSession::get_output ( void *  output_buffer)

Copies the output to the given buffer, whose size must be at least get_output_size().

Definition at line 58 of file impersonate_session.cpp.

References get_output_size(), foedus::thread::ThreadRef::get_task_output_memory(), and thread_.

58  {
59  std::memcpy(output_buffer, thread_->get_task_output_memory(), get_output_size());
60 }
thread::ThreadRef * thread_
The impersonated thread.
uint64_t get_output_size()
Returns the byte size of output.
void * get_task_output_memory() const
Definition: thread_ref.hpp:63

Here is the call graph for this function:

uint64_t foedus::thread::ImpersonateSession::get_output_size ( )

Returns the byte size of output.

Definition at line 65 of file impersonate_session.cpp.

References foedus::thread::ThreadRef::get_control_block(), foedus::thread::ThreadControlBlock::output_len_, and thread_.

Referenced by get_output().

65  {
67 }
thread::ThreadRef * thread_
The impersonated thread.
ThreadControlBlock * get_control_block() const
Definition: thread_ref.hpp:71
uint32_t output_len_
Byte size of output as the result of the procedure.

Here is the call graph for this function:

Here is the caller graph for this function:

const void * foedus::thread::ImpersonateSession::get_raw_output_buffer ( )

Returns the pointer to the raw output buffer on shared memory.

Definition at line 61 of file impersonate_session.cpp.

References foedus::thread::ThreadRef::get_task_output_memory(), and thread_.

61  {
63 }
thread::ThreadRef * thread_
The impersonated thread.
void * get_task_output_memory() const
Definition: thread_ref.hpp:63

Here is the call graph for this function:

ErrorStack foedus::thread::ImpersonateSession::get_result ( )

Waits until the completion of the asynchronous session and retrieves the result.

It effectively calls wait() in order to wait for the result. The behavior is undefined if is_valid()== false.

Precondition
is_valid()==true

Definition at line 46 of file impersonate_session.cpp.

References foedus::thread::ThreadControlBlock::current_ticket_, ERROR_STACK, foedus::thread::ThreadRef::get_control_block(), is_valid(), foedus::kErrorCodeSessionExpired, foedus::thread::kWaitingForClientRelease, foedus::thread::ThreadControlBlock::proc_result_, foedus::thread::ThreadControlBlock::status_, thread_, ticket_, foedus::FixedErrorStack::to_error_stack(), and wait().

Referenced by foedus::thread::ThreadPool::impersonate_on_numa_core_synchronous(), foedus::thread::ThreadPool::impersonate_on_numa_node_synchronous(), and foedus::thread::ThreadPool::impersonate_synchronous().

46  {
47  wait();
48  if (is_valid()) {
49  ThreadControlBlock* block = thread_->get_control_block();
50  if (block->current_ticket_ != ticket_ || block->status_ != kWaitingForClientRelease) {
52  }
53  return block->proc_result_.to_error_stack();
54  } else {
56  }
57 }
#define ERROR_STACK(e)
Instantiates ErrorStack with the given foedus::error_code, creating an error stack with the current f...
thread::ThreadRef * thread_
The impersonated thread.
void wait() const
Blocks until the completion of the asynchronous session.
thread::ThreadTicket ticket_
The ticket issued as of impersonation.
0x000A : "GENERAL: The session has expired." .
Definition: error_code.hpp:114
bool is_valid() const
Returns if the impersonation succeeded.
ThreadControlBlock * get_control_block() const
Definition: thread_ref.hpp:71
The thread has completed the task and set the result.
Definition: thread_id.hpp:213

Here is the call graph for this function:

Here is the caller graph for this function:

bool foedus::thread::ImpersonateSession::is_running ( ) const

Returns if the task is still running.

Definition at line 105 of file impersonate_session.cpp.

References ASSERT_ND, foedus::thread::ThreadControlBlock::current_ticket_, foedus::thread::ThreadRef::get_control_block(), foedus::thread::kRunningTask, foedus::thread::kWaitingForExecution, foedus::thread::ThreadControlBlock::status_, thread_, and ticket_.

Referenced by wait(), and wait_for().

105  {
108  return false;
109  }
112 }
ThreadTicket current_ticket_
The most recently issued impersonation ticket.
thread::ThreadRef * thread_
The impersonated thread.
A client has set a next task.
Definition: thread_id.hpp:209
thread::ThreadTicket ticket_
The ticket issued as of impersonation.
The thread has picked the task up and is now running.
Definition: thread_id.hpp:211
ThreadControlBlock * get_control_block() const
Definition: thread_ref.hpp:71
ThreadStatus status_
Impersonation status of this thread.
#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

Here is the call graph for this function:

Here is the caller graph for this function:

bool foedus::thread::ImpersonateSession::is_valid ( ) const
inline

Returns if the impersonation succeeded.

Definition at line 78 of file impersonate_session.hpp.

References CXX11_NULLPTR, thread_, and ticket_.

Referenced by get_result(), foedus::thread::operator<<(), release(), foedus::thread::ThreadRef::try_impersonate(), wait(), and wait_for().

78 { return thread_ != CXX11_NULLPTR && ticket_ != 0; }
#define CXX11_NULLPTR
Used in public headers in place of "nullptr" of C++11.
Definition: cxx11.hpp:132
thread::ThreadRef * thread_
The impersonated thread.
thread::ThreadTicket ticket_
The ticket issued as of impersonation.

Here is the caller graph for this function:

ImpersonateSession& foedus::thread::ImpersonateSession::operator= ( const ImpersonateSession other)
delete
ImpersonateSession & foedus::thread::ImpersonateSession::operator= ( ImpersonateSession &&  other)

Definition at line 37 of file impersonate_session.cpp.

References thread_, and ticket_.

37  {
38  ticket_ = other.ticket_;
39  thread_ = other.thread_;
40  other.ticket_ = 0;
41  other.thread_ = nullptr;
42  return *this;
43 }
thread::ThreadRef * thread_
The impersonated thread.
thread::ThreadTicket ticket_
The ticket issued as of impersonation.
void foedus::thread::ImpersonateSession::release ( )

Releases all resources and ownerships this session has acquired.

This method is idempotent, you can call it in any state and many times. Actually, this is also called from the destructor. This method might block if the session is still running. We do not allow the user to discard this session without completing the currently running task.

Attention
Once you invoke this method, you can't call get_result(), get_output(), get_output_size() any longer.

Definition at line 114 of file impersonate_session.cpp.

References foedus::thread::ThreadControlBlock::current_ticket_, foedus::thread::ThreadRef::get_control_block(), is_valid(), foedus::thread::kWaitingForClientRelease, foedus::thread::kWaitingForTask, foedus::thread::ThreadControlBlock::status_, thread_, ticket_, and wait().

Referenced by foedus::thread::ThreadRef::try_impersonate(), and ~ImpersonateSession().

114  {
115  if (!is_valid()) {
116  return;
117  }
118 
119  wait();
120  ThreadControlBlock* block = thread_->get_control_block();
121  if (block->current_ticket_ == ticket_ && block->status_ == kWaitingForClientRelease) {
122  block->status_ = kWaitingForTask;
123  }
124 
125  ticket_ = 0;
126  thread_ = nullptr;
127 }
Idle state, receiving a new task.
Definition: thread_id.hpp:207
thread::ThreadRef * thread_
The impersonated thread.
void wait() const
Blocks until the completion of the asynchronous session.
thread::ThreadTicket ticket_
The ticket issued as of impersonation.
bool is_valid() const
Returns if the impersonation succeeded.
ThreadControlBlock * get_control_block() const
Definition: thread_ref.hpp:71
ThreadStatus status_
Impersonation status of this thread.
The thread has completed the task and set the result.
Definition: thread_id.hpp:213

Here is the call graph for this function:

Here is the caller graph for this function:

void foedus::thread::ImpersonateSession::wait ( ) const

Blocks until the completion of the asynchronous session.

The behavior is undefined if is_valid()== false. It effectively calls wait_for(-1) in order to unconditionally wait for the result. This is analogous to std::future::wait().

Precondition
is_valid()==true

Definition at line 69 of file impersonate_session.cpp.

References foedus::soc::SharedPolling::acquire_ticket(), foedus::thread::ThreadRef::get_control_block(), is_running(), is_valid(), foedus::thread::ThreadControlBlock::task_complete_cond_, thread_, and foedus::soc::SharedPolling::timedwait().

Referenced by get_result(), and release().

69  {
70  if (!is_valid() || !is_running()) {
71  return;
72  }
73  ThreadControlBlock* block = thread_->get_control_block();
74  while (is_running()) {
75  uint64_t demand = block->task_complete_cond_.acquire_ticket();
76  if (!is_running()) {
77  break;
78  }
79  block->task_complete_cond_.timedwait(demand, 100000ULL);
80  }
81 }
thread::ThreadRef * thread_
The impersonated thread.
soc::SharedPolling task_complete_cond_
When the current task has been completed, the thread signals this.
uint64_t acquire_ticket() const
Gives the ticket to.
bool is_running() const
Returns if the task is still running.
bool is_valid() const
Returns if the impersonation succeeded.
ThreadControlBlock * get_control_block() const
Definition: thread_ref.hpp:71

Here is the call graph for this function:

Here is the caller graph for this function:

bool foedus::thread::ImpersonateSession::wait_for ( uint64_t  timeout_microsec) const

Blocks until either the asynchronous session completes or the specified time elapses.

Parameters
[in]timeout_microsectimeout in microsec. 0 means an instant check without waiting.
Returns
True when we observed completion.

The behavior is undefined if is_valid()== false. It effectively calls wait_for(timeout) in order to conditionally wait for the result. This is analogous to std::future::wait_for().

Precondition
is_valid()==true

Definition at line 83 of file impersonate_session.cpp.

References foedus::soc::SharedPolling::acquire_ticket(), foedus::thread::ThreadRef::get_control_block(), is_running(), is_valid(), foedus::thread::ThreadControlBlock::task_complete_cond_, thread_, and foedus::soc::SharedPolling::timedwait().

83  {
84  if (!is_valid()) {
85  return false;
86  } else if (!is_running()) {
87  return true;
88  } else if (timeout_microsec == 0) {
89  // instant check. return immediately
90  return false;
91  }
92 
93  ThreadControlBlock* block = thread_->get_control_block();
94  if (is_running()) {
95  uint64_t demand = block->task_complete_cond_.acquire_ticket();
96  if (!is_running()) { // in case it completes between is_running above and acquire_ticket
97  return true;
98  }
99  block->task_complete_cond_.timedwait(demand, timeout_microsec);
100  }
101 
102  return !is_running();
103 }
thread::ThreadRef * thread_
The impersonated thread.
soc::SharedPolling task_complete_cond_
When the current task has been completed, the thread signals this.
uint64_t acquire_ticket() const
Gives the ticket to.
bool is_running() const
Returns if the task is still running.
bool is_valid() const
Returns if the impersonation succeeded.
ThreadControlBlock * get_control_block() const
Definition: thread_ref.hpp:71

Here is the call graph for this function:

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  o,
const ImpersonateSession v 
)
friend

Definition at line 130 of file impersonate_session.cpp.

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 }

Member Data Documentation

thread::ThreadRef* foedus::thread::ImpersonateSession::thread_
thread::ThreadTicket foedus::thread::ImpersonateSession::ticket_

The ticket issued as of impersonation.

If impersonation failed, 0.

Definition at line 139 of file impersonate_session.hpp.

Referenced by get_result(), ImpersonateSession(), is_running(), is_valid(), operator=(), release(), and foedus::thread::ThreadRef::try_impersonate().


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