blob: 600b50891bf9cf48085b0d58a0d93fc567cb2860 [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_CONCURRENCY_THREAD_H_
2#define _THRIFT_CONCURRENCY_THREAD_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00003
Marc Slemko6f038a72006-08-03 18:58:09 +00004#include <boost/shared_ptr.hpp>
5#include <boost/weak_ptr.hpp>
6
Marc Slemko66949872006-07-15 01:52:39 +00007namespace facebook { namespace thrift { namespace concurrency {
8
Marc Slemko6f038a72006-08-03 18:58:09 +00009using namespace boost;
10
Marc Slemko66949872006-07-15 01:52:39 +000011class Thread;
12
Mark Sleef5f2be42006-09-05 21:05:31 +000013/**
14 * Minimal runnable class. More or less analogous to java.lang.Runnable.
15 *
16 * @author marc
17 * @version $Id:$
18 */
Marc Slemko66949872006-07-15 01:52:39 +000019class Runnable {
20
21 public:
Marc Slemko66949872006-07-15 01:52:39 +000022 virtual ~Runnable() {};
Marc Slemko66949872006-07-15 01:52:39 +000023 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000024
Mark Sleef5f2be42006-09-05 21:05:31 +000025 /**
26 * Gets the thread object that is hosting this runnable object - can return
27 * an empty shared pointer if no references remain on thet thread object
28 */
Mark Slee2f6404d2006-10-10 01:37:40 +000029 virtual shared_ptr<Thread> thread() { return thread_.lock(); }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000030
Mark Sleef5f2be42006-09-05 21:05:31 +000031 /**
32 * Sets the thread that is executing this object. This is only meant for
33 * use by concrete implementations of Thread.
34 */
Mark Slee2f6404d2006-10-10 01:37:40 +000035 virtual void thread(shared_ptr<Thread> value) { thread_ = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000036
Marc Slemko6f038a72006-08-03 18:58:09 +000037 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000038 weak_ptr<Thread> thread_;
Marc Slemko66949872006-07-15 01:52:39 +000039};
40
Mark Sleef5f2be42006-09-05 21:05:31 +000041/**
42 * Minimal thread class. Returned by thread factory bound to a Runnable object
43 * and ready to start execution. More or less analogous to java.lang.Thread
44 * (minus all the thread group, priority, mode and other baggage, since that
45 * is difficult to abstract across platforms and is left for platform-specific
46 * ThreadFactory implemtations to deal with
47 *
48 * @see facebook::thrift::concurrency::ThreadFactory)
49 */
Marc Slemko66949872006-07-15 01:52:39 +000050class Thread {
51
52 public:
Marc Slemko66949872006-07-15 01:52:39 +000053 virtual ~Thread() {};
54
Mark Sleef5f2be42006-09-05 21:05:31 +000055 /**
56 * Starts the thread. Does platform specific thread creation and
57 * configuration then invokes the run method of the Runnable object bound
58 * to this thread.
59 */
Marc Slemko66949872006-07-15 01:52:39 +000060 virtual void start() = 0;
61
Mark Sleef5f2be42006-09-05 21:05:31 +000062 /**
63 * Join this thread. Current thread blocks until this target thread
64 * completes.
65 */
Marc Slemko66949872006-07-15 01:52:39 +000066 virtual void join() = 0;
67
Mark Sleef5f2be42006-09-05 21:05:31 +000068 /**
69 * Gets the runnable object this thread is hosting
70 */
71 virtual shared_ptr<Runnable> runnable() const { return _runnable; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000072
73 protected:
Mark Sleef5f2be42006-09-05 21:05:31 +000074 virtual void runnable(shared_ptr<Runnable> value) { _runnable = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000075
76 private:
Marc Slemko6f038a72006-08-03 18:58:09 +000077 shared_ptr<Runnable> _runnable;
Marc Slemko66949872006-07-15 01:52:39 +000078};
79
Mark Sleef5f2be42006-09-05 21:05:31 +000080/**
81 * Factory to create platform-specific thread object and bind them to Runnable
82 * object for execution
83 */
Marc Slemko66949872006-07-15 01:52:39 +000084class ThreadFactory {
85
86 public:
Marc Slemko66949872006-07-15 01:52:39 +000087 virtual ~ThreadFactory() {}
Marc Slemko6f038a72006-08-03 18:58:09 +000088 virtual shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000089};
90
91}}} // facebook::thrift::concurrency
92
Mark Sleef5f2be42006-09-05 21:05:31 +000093#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_