blob: ca4b42b477f98a634448c771ca7e833477431c9b [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_CONCURRENCY_THREAD_H_
8#define _THRIFT_CONCURRENCY_THREAD_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00009
Marc Slemko6f038a72006-08-03 18:58:09 +000010#include <boost/shared_ptr.hpp>
11#include <boost/weak_ptr.hpp>
12
T Jake Lucianib5e62212009-01-31 22:36:20 +000013namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000014
15class Thread;
16
Mark Sleef5f2be42006-09-05 21:05:31 +000017/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000018 * Minimal runnable class. More or less analogous to java.lang.Runnable.
Mark Sleef5f2be42006-09-05 21:05:31 +000019 *
Mark Sleef5f2be42006-09-05 21:05:31 +000020 * @version $Id:$
21 */
Marc Slemko66949872006-07-15 01:52:39 +000022class Runnable {
23
24 public:
Marc Slemko66949872006-07-15 01:52:39 +000025 virtual ~Runnable() {};
Marc Slemko66949872006-07-15 01:52:39 +000026 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000027
Mark Sleef5f2be42006-09-05 21:05:31 +000028 /**
29 * Gets the thread object that is hosting this runnable object - can return
Mark Slee5ea15f92007-03-05 22:55:59 +000030 * an empty boost::shared pointer if no references remain on thet thread object
Mark Sleef5f2be42006-09-05 21:05:31 +000031 */
Mark Slee5ea15f92007-03-05 22:55:59 +000032 virtual boost::shared_ptr<Thread> thread() { return thread_.lock(); }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000033
Mark Sleef5f2be42006-09-05 21:05:31 +000034 /**
35 * Sets the thread that is executing this object. This is only meant for
36 * use by concrete implementations of Thread.
37 */
Mark Slee5ea15f92007-03-05 22:55:59 +000038 virtual void thread(boost::shared_ptr<Thread> value) { thread_ = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000039
Marc Slemko6f038a72006-08-03 18:58:09 +000040 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000041 boost::weak_ptr<Thread> thread_;
Marc Slemko66949872006-07-15 01:52:39 +000042};
43
Mark Sleef5f2be42006-09-05 21:05:31 +000044/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000045 * Minimal thread class. Returned by thread factory bound to a Runnable object
Mark Sleef5f2be42006-09-05 21:05:31 +000046 * and ready to start execution. More or less analogous to java.lang.Thread
47 * (minus all the thread group, priority, mode and other baggage, since that
48 * is difficult to abstract across platforms and is left for platform-specific
49 * ThreadFactory implemtations to deal with
50 *
T Jake Lucianib5e62212009-01-31 22:36:20 +000051 * @see apache::thrift::concurrency::ThreadFactory)
Mark Sleef5f2be42006-09-05 21:05:31 +000052 */
Marc Slemko66949872006-07-15 01:52:39 +000053class Thread {
Marc Slemko3a3b53b2007-05-22 23:59:54 +000054
Marc Slemko66949872006-07-15 01:52:39 +000055 public:
Marc Slemko3a3b53b2007-05-22 23:59:54 +000056
Mark Slee9b82d272007-05-23 05:16:07 +000057 typedef uint64_t id_t;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000058
Marc Slemko66949872006-07-15 01:52:39 +000059 virtual ~Thread() {};
60
Mark Sleef5f2be42006-09-05 21:05:31 +000061 /**
62 * Starts the thread. Does platform specific thread creation and
63 * configuration then invokes the run method of the Runnable object bound
64 * to this thread.
65 */
Marc Slemko66949872006-07-15 01:52:39 +000066 virtual void start() = 0;
67
Mark Sleef5f2be42006-09-05 21:05:31 +000068 /**
69 * Join this thread. Current thread blocks until this target thread
70 * completes.
71 */
Marc Slemko66949872006-07-15 01:52:39 +000072 virtual void join() = 0;
73
Mark Sleef5f2be42006-09-05 21:05:31 +000074 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000075 * Gets the thread's platform-specific ID
76 */
Marc Slemkoa6479032007-06-05 22:20:14 +000077 virtual id_t getId() = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000078
79 /**
Mark Sleef5f2be42006-09-05 21:05:31 +000080 * Gets the runnable object this thread is hosting
81 */
Mark Slee5ea15f92007-03-05 22:55:59 +000082 virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000083
84 protected:
Mark Slee5ea15f92007-03-05 22:55:59 +000085 virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000086
87 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000088 boost::shared_ptr<Runnable> _runnable;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000089
Marc Slemko66949872006-07-15 01:52:39 +000090};
91
Mark Sleef5f2be42006-09-05 21:05:31 +000092/**
93 * Factory to create platform-specific thread object and bind them to Runnable
94 * object for execution
95 */
Marc Slemko66949872006-07-15 01:52:39 +000096class ThreadFactory {
97
98 public:
Marc Slemko66949872006-07-15 01:52:39 +000099 virtual ~ThreadFactory() {}
Mark Slee5ea15f92007-03-05 22:55:59 +0000100 virtual boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000101
102 /** Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread */
103
104 static const Thread::id_t unknown_thread_id;
105
Marc Slemkoa6479032007-06-05 22:20:14 +0000106 virtual Thread::id_t getCurrentThreadId() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000107};
108
T Jake Lucianib5e62212009-01-31 22:36:20 +0000109}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000110
Mark Sleef5f2be42006-09-05 21:05:31 +0000111#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_