Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thrift-thread.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "rpc/thrift-thread.h"
16 
17 #include <boost/bind.hpp>
18 #include <sstream>
19 
20 #include "common/names.h"
21 
22 using namespace impala;
23 
24 // Can't import the whole namespace, since impala::Thread will clash with atc::Thread
25 namespace atc = apache::thrift::concurrency;
26 
30  bind(&ThriftThread::RunRunnable, this, runnable(), &promise)));
31 
32  // Blocks until the thread id has been set
33  tid_ = promise.Get();
34 }
35 
36 atc::Thread::id_t ThriftThread::getId() {
37  return tid_;
38 }
39 
41  impala_thread_->Join();
42 }
43 
44 shared_ptr<atc::Thread> ThriftThreadFactory::newThread(
45  shared_ptr<atc::Runnable> runnable) const {
46  stringstream name;
47  name << prefix_ << "-" << count_++;
48  shared_ptr<ThriftThread> result =
49  shared_ptr<ThriftThread>(new ThriftThread(group_, name.str(), runnable));
50  runnable->thread(result);
51  return result;
52 }
53 
54 void ThriftThread::RunRunnable(shared_ptr<atc::Runnable> runnable,
55  Promise<atc::Thread::id_t>* promise) {
56  promise->Set(get_current());
57  // Passing runnable in to this method (rather than reading from this->runnable())
58  // ensures that it will live as long as this method, otherwise the ThriftThread could be
59  // destroyed between the previous statement and this one (according to my reading of
60  // PosixThread)
61  runnable->run();
62 }
63 
64 atc::Thread::id_t ThriftThreadFactory::getCurrentThreadId() const {
65  return atc::Thread::get_current();
66 }
67 
68 ThriftThread::ThriftThread(const string& group, const string& name,
69  shared_ptr<atc::Runnable> runnable)
70  : group_(group), name_(name) {
71  // Sets this::runnable (and no, I don't know why it's not protected in atc::Thread)
72  this->Thread::runnable(runnable);
73 }
virtual void start()
TODO: Consider allowing fragment IDs as category parameters.
Definition: thread.h:45
void Set(const T &val)
Definition: promise.h:38
ThriftThread(const std::string &group, const std::string &name, boost::shared_ptr< apache::thrift::concurrency::Runnable > runnable)
virtual apache::thrift::concurrency::Thread::id_t getCurrentThreadId() const
virtual id_t getId()
Returns the Thrift thread ID of the execution thread.
std::string prefix_
Thread name prefix for the Impala ThreadManager.
Definition: thrift-thread.h:58
virtual boost::shared_ptr< apache::thrift::concurrency::Thread > newThread(boost::shared_ptr< apache::thrift::concurrency::Runnable > runnable) const
(From ThreadFactory) - creates a new ThriftThread to run the supplied Runnable.
boost::scoped_ptr< impala::Thread > impala_thread_
Definition: thrift-thread.h:93
void RunRunnable(boost::shared_ptr< apache::thrift::concurrency::Runnable > runnable, Promise< apache::thrift::concurrency::Thread::id_t > *promise)
const T & Get()
Definition: promise.h:59
std::string name_
Individual thread name for the Impala ThreadManager.
apache::thrift::concurrency::Thread::id_t tid_
Thrift thread ID, set by RunRunnable.
Definition: thrift-thread.h:96
std::string group_
Group name for the Impala ThreadManager.
Definition: thrift-thread.h:99
virtual void join()
Joins the separate thread.
string name
Definition: cpu-info.cc:50
std::string group_
Group name for the Impala ThreadManager.
Definition: thrift-thread.h:55