libfoedus-core
FOEDUS Core Library
impersonate_session.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
19 
20 #include <chrono>
21 #include <iostream>
22 
23 #include "foedus/thread/thread.hpp"
26 
27 namespace foedus {
28 namespace thread {
29 
31  ticket_ = other.ticket_;
32  thread_ = other.thread_;
33  other.ticket_ = 0;
34  other.thread_ = nullptr;
35 }
36 
38  ticket_ = other.ticket_;
39  thread_ = other.thread_;
40  other.ticket_ = 0;
41  other.thread_ = nullptr;
42  return *this;
43 }
44 
45 
47  wait();
48  if (is_valid()) {
50  if (block->current_ticket_ != ticket_ || block->status_ != kWaitingForClientRelease) {
52  }
53  return block->proc_result_.to_error_stack();
54  } else {
56  }
57 }
58 void ImpersonateSession::get_output(void* output_buffer) {
59  std::memcpy(output_buffer, thread_->get_task_output_memory(), get_output_size());
60 }
63 }
64 
67 }
68 
70  if (!is_valid() || !is_running()) {
71  return;
72  }
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 }
82 
83 bool ImpersonateSession::wait_for(uint64_t timeout_microsec) const {
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 
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 }
104 
108  return false;
109  }
112 }
113 
115  if (!is_valid()) {
116  return;
117  }
118 
119  wait();
121  if (block->current_ticket_ == ticket_ && block->status_ == kWaitingForClientRelease) {
122  block->status_ = kWaitingForTask;
123  }
124 
125  ticket_ = 0;
126  thread_ = nullptr;
127 }
128 
129 
130 std::ostream& operator<<(std::ostream& o, const ImpersonateSession& v) {
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 }
137 
138 } // namespace thread
139 } // namespace foedus
void release()
Releases all resources and ownerships this session has acquired.
ThreadTicket current_ticket_
The most recently issued impersonation ticket.
Idle state, receiving a new task.
Definition: thread_id.hpp:207
ErrorStack get_result()
Waits until the completion of the asynchronous session and retrieves the result.
#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.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
uint64_t get_output_size()
Returns the byte size of output.
Shared data of ThreadPimpl.
soc::SharedPolling task_complete_cond_
When the current task has been completed, the thread signals this.
void wait() const
Blocks until the completion of the asynchronous session.
A client has set a next task.
Definition: thread_id.hpp:209
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
thread::ThreadTicket ticket_
The ticket issued as of impersonation.
A user session running on an impersonated thread.
uint64_t acquire_ticket() const
Gives the ticket to.
bool is_running() const
Returns if the task is still running.
ErrorStack to_error_stack() const
Instantiates an ErrorStack object based on this object.
const void * get_raw_output_buffer()
Returns the pointer to the raw output buffer on shared memory.
0x000A : "GENERAL: The session has expired." .
Definition: error_code.hpp:114
The thread has picked the task up and is now running.
Definition: thread_id.hpp:211
bool is_valid() const
Returns if the impersonation succeeded.
void get_output(void *output_buffer)
Copies the output to the given buffer, whose size must be at least get_output_size().
ThreadControlBlock * get_control_block() const
Definition: thread_ref.hpp:71
bool wait_for(uint64_t timeout_microsec) const
Blocks until either the asynchronous session completes or the specified time elapses.
ImpersonateSession & operator=(const ImpersonateSession &other)=delete
ThreadStatus status_
Impersonation status of this thread.
std::ostream & operator<<(std::ostream &o, const ImpersonateSession &v)
void * get_task_output_memory() const
Definition: thread_ref.hpp:63
The thread has completed the task and set the result.
Definition: thread_id.hpp:213
uint32_t output_len_
Byte size of output as the result of the procedure.
#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
FixedErrorStack proc_result_
Error code as the result of the procedure.
ThreadId get_thread_id() const
Definition: thread_ref.hpp:60
bool timedwait(uint64_t demanded_ticket, uint64_t timeout_microsec, uint64_t polling_spins=kDefaultPollingSpins, uint64_t max_interval_us=kDefaultPollingMaxIntervalUs) const
Wait for signal up to the given timeout.