blob: d4282adbcfaca434a1f3033a16d53a75176f50ae [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
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000028
29class Thread;
30
Mark Sleef5f2be42006-09-05 21:05:31 +000031/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000032 * Minimal runnable class. More or less analogous to java.lang.Runnable.
Mark Sleef5f2be42006-09-05 21:05:31 +000033 *
Mark Sleef5f2be42006-09-05 21:05:31 +000034 * @version $Id:$
35 */
Marc Slemko66949872006-07-15 01:52:39 +000036class Runnable {
37
38 public:
Marc Slemko66949872006-07-15 01:52:39 +000039 virtual ~Runnable() {};
Marc Slemko66949872006-07-15 01:52:39 +000040 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000041
Mark Sleef5f2be42006-09-05 21:05:31 +000042 /**
43 * Gets the thread object that is hosting this runnable object - can return
Mark Slee5ea15f92007-03-05 22:55:59 +000044 * an empty boost::shared pointer if no references remain on thet thread object
Mark Sleef5f2be42006-09-05 21:05:31 +000045 */
Mark Slee5ea15f92007-03-05 22:55:59 +000046 virtual boost::shared_ptr<Thread> thread() { return thread_.lock(); }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000047
Mark Sleef5f2be42006-09-05 21:05:31 +000048 /**
49 * Sets the thread that is executing this object. This is only meant for
50 * use by concrete implementations of Thread.
51 */
Mark Slee5ea15f92007-03-05 22:55:59 +000052 virtual void thread(boost::shared_ptr<Thread> value) { thread_ = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000053
Marc Slemko6f038a72006-08-03 18:58:09 +000054 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000055 boost::weak_ptr<Thread> thread_;
Marc Slemko66949872006-07-15 01:52:39 +000056};
57
Mark Sleef5f2be42006-09-05 21:05:31 +000058/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000059 * Minimal thread class. Returned by thread factory bound to a Runnable object
Mark Sleef5f2be42006-09-05 21:05:31 +000060 * and ready to start execution. More or less analogous to java.lang.Thread
61 * (minus all the thread group, priority, mode and other baggage, since that
62 * is difficult to abstract across platforms and is left for platform-specific
63 * ThreadFactory implemtations to deal with
64 *
T Jake Lucianib5e62212009-01-31 22:36:20 +000065 * @see apache::thrift::concurrency::ThreadFactory)
Mark Sleef5f2be42006-09-05 21:05:31 +000066 */
Marc Slemko66949872006-07-15 01:52:39 +000067class Thread {
Marc Slemko3a3b53b2007-05-22 23:59:54 +000068
Marc Slemko66949872006-07-15 01:52:39 +000069 public:
Marc Slemko3a3b53b2007-05-22 23:59:54 +000070
Mark Slee9b82d272007-05-23 05:16:07 +000071 typedef uint64_t id_t;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000072
Marc Slemko66949872006-07-15 01:52:39 +000073 virtual ~Thread() {};
74
Mark Sleef5f2be42006-09-05 21:05:31 +000075 /**
76 * Starts the thread. Does platform specific thread creation and
77 * configuration then invokes the run method of the Runnable object bound
78 * to this thread.
79 */
Marc Slemko66949872006-07-15 01:52:39 +000080 virtual void start() = 0;
81
Mark Sleef5f2be42006-09-05 21:05:31 +000082 /**
83 * Join this thread. Current thread blocks until this target thread
84 * completes.
85 */
Marc Slemko66949872006-07-15 01:52:39 +000086 virtual void join() = 0;
87
Mark Sleef5f2be42006-09-05 21:05:31 +000088 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000089 * Gets the thread's platform-specific ID
90 */
Marc Slemkoa6479032007-06-05 22:20:14 +000091 virtual id_t getId() = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000092
93 /**
Mark Sleef5f2be42006-09-05 21:05:31 +000094 * Gets the runnable object this thread is hosting
95 */
Mark Slee5ea15f92007-03-05 22:55:59 +000096 virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000097
98 protected:
Mark Slee5ea15f92007-03-05 22:55:59 +000099 virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +0000100
101 private:
Mark Slee5ea15f92007-03-05 22:55:59 +0000102 boost::shared_ptr<Runnable> _runnable;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000103
Marc Slemko66949872006-07-15 01:52:39 +0000104};
105
Mark Sleef5f2be42006-09-05 21:05:31 +0000106/**
107 * Factory to create platform-specific thread object and bind them to Runnable
108 * object for execution
109 */
Marc Slemko66949872006-07-15 01:52:39 +0000110class ThreadFactory {
111
112 public:
Marc Slemko66949872006-07-15 01:52:39 +0000113 virtual ~ThreadFactory() {}
Mark Slee5ea15f92007-03-05 22:55:59 +0000114 virtual boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000115
116 /** Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread */
117
118 static const Thread::id_t unknown_thread_id;
119
Marc Slemkoa6479032007-06-05 22:20:14 +0000120 virtual Thread::id_t getCurrentThreadId() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000121};
122
T Jake Lucianib5e62212009-01-31 22:36:20 +0000123}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000124
Mark Sleef5f2be42006-09-05 21:05:31 +0000125#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_