blob: 6b9fc032f81c5ba43148acabf140ef9dd5bfc4b9 [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
Marc Slemko6f038a72006-08-03 18:58:09 +000023#include <boost/shared_ptr.hpp>
24#include <boost/weak_ptr.hpp>
25
T Jake Lucianib5e62212009-01-31 22:36:20 +000026namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000027
28class Thread;
29
Mark Sleef5f2be42006-09-05 21:05:31 +000030/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000031 * Minimal runnable class. More or less analogous to java.lang.Runnable.
Mark Sleef5f2be42006-09-05 21:05:31 +000032 *
Mark Sleef5f2be42006-09-05 21:05:31 +000033 * @version $Id:$
34 */
Marc Slemko66949872006-07-15 01:52:39 +000035class Runnable {
36
37 public:
Marc Slemko66949872006-07-15 01:52:39 +000038 virtual ~Runnable() {};
Marc Slemko66949872006-07-15 01:52:39 +000039 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000040
Mark Sleef5f2be42006-09-05 21:05:31 +000041 /**
42 * Gets the thread object that is hosting this runnable object - can return
Mark Slee5ea15f92007-03-05 22:55:59 +000043 * an empty boost::shared pointer if no references remain on thet thread object
Mark Sleef5f2be42006-09-05 21:05:31 +000044 */
Mark Slee5ea15f92007-03-05 22:55:59 +000045 virtual boost::shared_ptr<Thread> thread() { return thread_.lock(); }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000046
Mark Sleef5f2be42006-09-05 21:05:31 +000047 /**
48 * Sets the thread that is executing this object. This is only meant for
49 * use by concrete implementations of Thread.
50 */
Mark Slee5ea15f92007-03-05 22:55:59 +000051 virtual void thread(boost::shared_ptr<Thread> value) { thread_ = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000052
Marc Slemko6f038a72006-08-03 18:58:09 +000053 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000054 boost::weak_ptr<Thread> thread_;
Marc Slemko66949872006-07-15 01:52:39 +000055};
56
Mark Sleef5f2be42006-09-05 21:05:31 +000057/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000058 * Minimal thread class. Returned by thread factory bound to a Runnable object
Mark Sleef5f2be42006-09-05 21:05:31 +000059 * and ready to start execution. More or less analogous to java.lang.Thread
60 * (minus all the thread group, priority, mode and other baggage, since that
61 * is difficult to abstract across platforms and is left for platform-specific
62 * ThreadFactory implemtations to deal with
63 *
T Jake Lucianib5e62212009-01-31 22:36:20 +000064 * @see apache::thrift::concurrency::ThreadFactory)
Mark Sleef5f2be42006-09-05 21:05:31 +000065 */
Marc Slemko66949872006-07-15 01:52:39 +000066class Thread {
Marc Slemko3a3b53b2007-05-22 23:59:54 +000067
Marc Slemko66949872006-07-15 01:52:39 +000068 public:
Marc Slemko3a3b53b2007-05-22 23:59:54 +000069
Mark Slee9b82d272007-05-23 05:16:07 +000070 typedef uint64_t id_t;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000071
Marc Slemko66949872006-07-15 01:52:39 +000072 virtual ~Thread() {};
73
Mark Sleef5f2be42006-09-05 21:05:31 +000074 /**
75 * Starts the thread. Does platform specific thread creation and
76 * configuration then invokes the run method of the Runnable object bound
77 * to this thread.
78 */
Marc Slemko66949872006-07-15 01:52:39 +000079 virtual void start() = 0;
80
Mark Sleef5f2be42006-09-05 21:05:31 +000081 /**
82 * Join this thread. Current thread blocks until this target thread
83 * completes.
84 */
Marc Slemko66949872006-07-15 01:52:39 +000085 virtual void join() = 0;
86
Mark Sleef5f2be42006-09-05 21:05:31 +000087 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000088 * Gets the thread's platform-specific ID
89 */
Marc Slemkoa6479032007-06-05 22:20:14 +000090 virtual id_t getId() = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000091
92 /**
Mark Sleef5f2be42006-09-05 21:05:31 +000093 * Gets the runnable object this thread is hosting
94 */
Mark Slee5ea15f92007-03-05 22:55:59 +000095 virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000096
97 protected:
Mark Slee5ea15f92007-03-05 22:55:59 +000098 virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000099
100 private:
Mark Slee5ea15f92007-03-05 22:55:59 +0000101 boost::shared_ptr<Runnable> _runnable;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000102
Marc Slemko66949872006-07-15 01:52:39 +0000103};
104
Mark Sleef5f2be42006-09-05 21:05:31 +0000105/**
106 * Factory to create platform-specific thread object and bind them to Runnable
107 * object for execution
108 */
Marc Slemko66949872006-07-15 01:52:39 +0000109class ThreadFactory {
110
111 public:
Marc Slemko66949872006-07-15 01:52:39 +0000112 virtual ~ThreadFactory() {}
Mark Slee5ea15f92007-03-05 22:55:59 +0000113 virtual boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000114
115 /** Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread */
116
117 static const Thread::id_t unknown_thread_id;
118
Marc Slemkoa6479032007-06-05 22:20:14 +0000119 virtual Thread::id_t getCurrentThreadId() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000120};
121
T Jake Lucianib5e62212009-01-31 22:36:20 +0000122}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000123
Mark Sleef5f2be42006-09-05 21:05:31 +0000124#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_