blob: e928fc4432c54308af2066ad3f45db972548d812 [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
Marc Slemko3a3b53b2007-05-22 23:59:54 +000013namespace facebook { 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 *
20 * @author marc
21 * @version $Id:$
22 */
Marc Slemko66949872006-07-15 01:52:39 +000023class Runnable {
24
25 public:
Marc Slemko66949872006-07-15 01:52:39 +000026 virtual ~Runnable() {};
Marc Slemko66949872006-07-15 01:52:39 +000027 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000028
Mark Sleef5f2be42006-09-05 21:05:31 +000029 /**
30 * Gets the thread object that is hosting this runnable object - can return
Mark Slee5ea15f92007-03-05 22:55:59 +000031 * an empty boost::shared pointer if no references remain on thet thread object
Mark Sleef5f2be42006-09-05 21:05:31 +000032 */
Mark Slee5ea15f92007-03-05 22:55:59 +000033 virtual boost::shared_ptr<Thread> thread() { return thread_.lock(); }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000034
Mark Sleef5f2be42006-09-05 21:05:31 +000035 /**
36 * Sets the thread that is executing this object. This is only meant for
37 * use by concrete implementations of Thread.
38 */
Mark Slee5ea15f92007-03-05 22:55:59 +000039 virtual void thread(boost::shared_ptr<Thread> value) { thread_ = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000040
Marc Slemko6f038a72006-08-03 18:58:09 +000041 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000042 boost::weak_ptr<Thread> thread_;
Marc Slemko66949872006-07-15 01:52:39 +000043};
44
Mark Sleef5f2be42006-09-05 21:05:31 +000045/**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000046 * Minimal thread class. Returned by thread factory bound to a Runnable object
Mark Sleef5f2be42006-09-05 21:05:31 +000047 * and ready to start execution. More or less analogous to java.lang.Thread
48 * (minus all the thread group, priority, mode and other baggage, since that
49 * is difficult to abstract across platforms and is left for platform-specific
50 * ThreadFactory implemtations to deal with
51 *
52 * @see facebook::thrift::concurrency::ThreadFactory)
53 */
Marc Slemko66949872006-07-15 01:52:39 +000054class Thread {
Marc Slemko3a3b53b2007-05-22 23:59:54 +000055
Marc Slemko66949872006-07-15 01:52:39 +000056 public:
Marc Slemko3a3b53b2007-05-22 23:59:54 +000057
58 typedef unsigned long long id_t;
59
Marc Slemko66949872006-07-15 01:52:39 +000060 virtual ~Thread() {};
61
Mark Sleef5f2be42006-09-05 21:05:31 +000062 /**
63 * Starts the thread. Does platform specific thread creation and
64 * configuration then invokes the run method of the Runnable object bound
65 * to this thread.
66 */
Marc Slemko66949872006-07-15 01:52:39 +000067 virtual void start() = 0;
68
Mark Sleef5f2be42006-09-05 21:05:31 +000069 /**
70 * Join this thread. Current thread blocks until this target thread
71 * completes.
72 */
Marc Slemko66949872006-07-15 01:52:39 +000073 virtual void join() = 0;
74
Mark Sleef5f2be42006-09-05 21:05:31 +000075 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +000076 * Gets the thread's platform-specific ID
77 */
78 virtual id_t id() = 0;
79
80 /**
Mark Sleef5f2be42006-09-05 21:05:31 +000081 * Gets the runnable object this thread is hosting
82 */
Mark Slee5ea15f92007-03-05 22:55:59 +000083 virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000084
85 protected:
Mark Slee5ea15f92007-03-05 22:55:59 +000086 virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000087
88 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000089 boost::shared_ptr<Runnable> _runnable;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000090
Marc Slemko66949872006-07-15 01:52:39 +000091};
92
Mark Sleef5f2be42006-09-05 21:05:31 +000093/**
94 * Factory to create platform-specific thread object and bind them to Runnable
95 * object for execution
96 */
Marc Slemko66949872006-07-15 01:52:39 +000097class ThreadFactory {
98
99 public:
Marc Slemko66949872006-07-15 01:52:39 +0000100 virtual ~ThreadFactory() {}
Mark Slee5ea15f92007-03-05 22:55:59 +0000101 virtual boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000102
103 /** Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread */
104
105 static const Thread::id_t unknown_thread_id;
106
107 virtual Thread::id_t currentThreadId() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000108};
109
110}}} // facebook::thrift::concurrency
111
Mark Sleef5f2be42006-09-05 21:05:31 +0000112#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_