blob: 96ca66819d8b810cf1bd60f34e541674fc49a7ea [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 Slemko66949872006-07-15 01:52:39 +000013namespace facebook { namespace thrift { namespace concurrency {
14
15class Thread;
16
Mark Sleef5f2be42006-09-05 21:05:31 +000017/**
18 * Minimal runnable class. More or less analogous to java.lang.Runnable.
19 *
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/**
46 * Minimal thread class. Returned by thread factory bound to a Runnable object
47 * 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 {
55
56 public:
Marc Slemko66949872006-07-15 01:52:39 +000057 virtual ~Thread() {};
58
Mark Sleef5f2be42006-09-05 21:05:31 +000059 /**
60 * Starts the thread. Does platform specific thread creation and
61 * configuration then invokes the run method of the Runnable object bound
62 * to this thread.
63 */
Marc Slemko66949872006-07-15 01:52:39 +000064 virtual void start() = 0;
65
Mark Sleef5f2be42006-09-05 21:05:31 +000066 /**
67 * Join this thread. Current thread blocks until this target thread
68 * completes.
69 */
Marc Slemko66949872006-07-15 01:52:39 +000070 virtual void join() = 0;
71
Mark Sleef5f2be42006-09-05 21:05:31 +000072 /**
73 * Gets the runnable object this thread is hosting
74 */
Mark Slee5ea15f92007-03-05 22:55:59 +000075 virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000076
77 protected:
Mark Slee5ea15f92007-03-05 22:55:59 +000078 virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000079
80 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000081 boost::shared_ptr<Runnable> _runnable;
Marc Slemko66949872006-07-15 01:52:39 +000082};
83
Mark Sleef5f2be42006-09-05 21:05:31 +000084/**
85 * Factory to create platform-specific thread object and bind them to Runnable
86 * object for execution
87 */
Marc Slemko66949872006-07-15 01:52:39 +000088class ThreadFactory {
89
90 public:
Marc Slemko66949872006-07-15 01:52:39 +000091 virtual ~ThreadFactory() {}
Mark Slee5ea15f92007-03-05 22:55:59 +000092 virtual boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000093};
94
95}}} // facebook::thrift::concurrency
96
Mark Sleef5f2be42006-09-05 21:05:31 +000097#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_