blob: f7c7bd6187db3cd96383c18de6dc2862fac9c1f5 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_CONCURRENCY_THREAD_H_
21#define _THRIFT_CONCURRENCY_THREAD_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +000022
David Reiss7247b8c2009-04-02 23:05:40 +000023#include <stdint.h>
Marc Slemko6f038a72006-08-03 18:58:09 +000024#include <boost/shared_ptr.hpp>
25#include <boost/weak_ptr.hpp>
26
Konrad Grochowski9be4e682013-06-22 22:03:31 +020027#include <thrift/thrift-config.h>
Roger Meier12d70532011-12-14 23:35:28 +000028
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040029#if USE_BOOST_THREAD
Konrad Grochowski16a23a62014-11-13 15:33:38 +010030#include <boost/thread.hpp>
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040031#elif USE_STD_THREAD
Konrad Grochowski16a23a62014-11-13 15:33:38 +010032#include <thread>
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040033#else
Konrad Grochowski16a23a62014-11-13 15:33:38 +010034#ifdef HAVE_PTHREAD_H
35#include <pthread.h>
36#endif
Roger Meier12d70532011-12-14 23:35:28 +000037#endif
38
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039namespace apache {
40namespace thrift {
41namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000042
43class Thread;
44
Mark Sleef5f2be42006-09-05 21:05:31 +000045/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000046 * Minimal runnable class. More or less analogous to java.lang.Runnable.
Mark Sleef5f2be42006-09-05 21:05:31 +000047 *
Mark Sleef5f2be42006-09-05 21:05:31 +000048 * @version $Id:$
49 */
Marc Slemko66949872006-07-15 01:52:39 +000050class Runnable {
51
Konrad Grochowski16a23a62014-11-13 15:33:38 +010052public:
53 virtual ~Runnable(){};
Marc Slemko66949872006-07-15 01:52:39 +000054 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000055
Mark Sleef5f2be42006-09-05 21:05:31 +000056 /**
57 * Gets the thread object that is hosting this runnable object - can return
Jens Geyer1a8e0482015-04-30 20:29:20 +020058 * an empty boost::shared pointer if no references remain on that thread object
Mark Sleef5f2be42006-09-05 21:05:31 +000059 */
Mark Slee5ea15f92007-03-05 22:55:59 +000060 virtual boost::shared_ptr<Thread> thread() { return thread_.lock(); }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000061
Mark Sleef5f2be42006-09-05 21:05:31 +000062 /**
63 * Sets the thread that is executing this object. This is only meant for
64 * use by concrete implementations of Thread.
65 */
Mark Slee5ea15f92007-03-05 22:55:59 +000066 virtual void thread(boost::shared_ptr<Thread> value) { thread_ = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000067
Konrad Grochowski16a23a62014-11-13 15:33:38 +010068private:
Mark Slee5ea15f92007-03-05 22:55:59 +000069 boost::weak_ptr<Thread> thread_;
Marc Slemko66949872006-07-15 01:52:39 +000070};
71
Mark Sleef5f2be42006-09-05 21:05:31 +000072/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000073 * Minimal thread class. Returned by thread factory bound to a Runnable object
Mark Sleef5f2be42006-09-05 21:05:31 +000074 * and ready to start execution. More or less analogous to java.lang.Thread
75 * (minus all the thread group, priority, mode and other baggage, since that
76 * is difficult to abstract across platforms and is left for platform-specific
77 * ThreadFactory implemtations to deal with
78 *
T Jake Lucianib5e62212009-01-31 22:36:20 +000079 * @see apache::thrift::concurrency::ThreadFactory)
Mark Sleef5f2be42006-09-05 21:05:31 +000080 */
Marc Slemko66949872006-07-15 01:52:39 +000081class Thread {
Marc Slemko3a3b53b2007-05-22 23:59:54 +000082
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083public:
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040084#if USE_BOOST_THREAD
Roger Meier3faaedf2011-10-02 10:51:45 +000085 typedef boost::thread::id id_t;
Roger Meier12d70532011-12-14 23:35:28 +000086
87 static inline bool is_current(id_t t) { return t == boost::this_thread::get_id(); }
88 static inline id_t get_current() { return boost::this_thread::get_id(); }
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040089#elif USE_STD_THREAD
90 typedef std::thread::id id_t;
91
92 static inline bool is_current(id_t t) { return t == std::this_thread::get_id(); }
93 static inline id_t get_current() { return std::this_thread::get_id(); }
Roger Meier3faaedf2011-10-02 10:51:45 +000094#else
Roger Meierb6617832012-05-23 19:17:20 +000095 typedef pthread_t id_t;
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040096
Roger Meierb6617832012-05-23 19:17:20 +000097 static inline bool is_current(id_t t) { return pthread_equal(pthread_self(), t); }
98 static inline id_t get_current() { return pthread_self(); }
Roger Meier3faaedf2011-10-02 10:51:45 +000099#endif
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000100
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100101 virtual ~Thread(){};
Marc Slemko66949872006-07-15 01:52:39 +0000102
Mark Sleef5f2be42006-09-05 21:05:31 +0000103 /**
104 * Starts the thread. Does platform specific thread creation and
105 * configuration then invokes the run method of the Runnable object bound
106 * to this thread.
107 */
Marc Slemko66949872006-07-15 01:52:39 +0000108 virtual void start() = 0;
109
Mark Sleef5f2be42006-09-05 21:05:31 +0000110 /**
111 * Join this thread. Current thread blocks until this target thread
112 * completes.
113 */
Marc Slemko66949872006-07-15 01:52:39 +0000114 virtual void join() = 0;
115
Mark Sleef5f2be42006-09-05 21:05:31 +0000116 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000117 * Gets the thread's platform-specific ID
118 */
Marc Slemkoa6479032007-06-05 22:20:14 +0000119 virtual id_t getId() = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000120
121 /**
Mark Sleef5f2be42006-09-05 21:05:31 +0000122 * Gets the runnable object this thread is hosting
123 */
Mark Slee5ea15f92007-03-05 22:55:59 +0000124 virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +0000125
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100126protected:
Mark Slee5ea15f92007-03-05 22:55:59 +0000127 virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +0000128
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100129private:
Mark Slee5ea15f92007-03-05 22:55:59 +0000130 boost::shared_ptr<Runnable> _runnable;
Marc Slemko66949872006-07-15 01:52:39 +0000131};
132
Mark Sleef5f2be42006-09-05 21:05:31 +0000133/**
134 * Factory to create platform-specific thread object and bind them to Runnable
135 * object for execution
136 */
Marc Slemko66949872006-07-15 01:52:39 +0000137class ThreadFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100138public:
Marc Slemko66949872006-07-15 01:52:39 +0000139 virtual ~ThreadFactory() {}
Jim King5a3f8552016-04-05 12:17:51 -0400140
141 /**
142 * Gets current detached mode
143 */
144 virtual bool isDetached() const = 0;
145
146 /**
147 * Create a new thread.
148 */
Mark Slee5ea15f92007-03-05 22:55:59 +0000149 virtual boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000150
Jim King5a3f8552016-04-05 12:17:51 -0400151 /**
152 * Sets detached mode of threads
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100153 */
Jim King5a3f8552016-04-05 12:17:51 -0400154 virtual void setDetached(bool detached) = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000155
156 static const Thread::id_t unknown_thread_id;
157
Jim King5a3f8552016-04-05 12:17:51 -0400158 /**
159 * Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread
160 */
Marc Slemkoa6479032007-06-05 22:20:14 +0000161 virtual Thread::id_t getCurrentThreadId() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000162};
Jim King5a3f8552016-04-05 12:17:51 -0400163
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100164}
165}
166} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000167
Mark Sleef5f2be42006-09-05 21:05:31 +0000168#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_