blob: 1b07fd50178916afdf39e008ebf03cec1b1ffbf5 [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
Marc Slemko6f038a72006-08-03 18:58:09 +000015using namespace boost;
16
Marc Slemko66949872006-07-15 01:52:39 +000017class Thread;
18
Mark Sleef5f2be42006-09-05 21:05:31 +000019/**
20 * Minimal runnable class. More or less analogous to java.lang.Runnable.
21 *
22 * @author marc
23 * @version $Id:$
24 */
Marc Slemko66949872006-07-15 01:52:39 +000025class Runnable {
26
27 public:
Marc Slemko66949872006-07-15 01:52:39 +000028 virtual ~Runnable() {};
Marc Slemko66949872006-07-15 01:52:39 +000029 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000030
Mark Sleef5f2be42006-09-05 21:05:31 +000031 /**
32 * Gets the thread object that is hosting this runnable object - can return
33 * an empty shared pointer if no references remain on thet thread object
34 */
Mark Slee2f6404d2006-10-10 01:37:40 +000035 virtual shared_ptr<Thread> thread() { return thread_.lock(); }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000036
Mark Sleef5f2be42006-09-05 21:05:31 +000037 /**
38 * Sets the thread that is executing this object. This is only meant for
39 * use by concrete implementations of Thread.
40 */
Mark Slee2f6404d2006-10-10 01:37:40 +000041 virtual void thread(shared_ptr<Thread> value) { thread_ = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000042
Marc Slemko6f038a72006-08-03 18:58:09 +000043 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000044 weak_ptr<Thread> thread_;
Marc Slemko66949872006-07-15 01:52:39 +000045};
46
Mark Sleef5f2be42006-09-05 21:05:31 +000047/**
48 * Minimal thread class. Returned by thread factory bound to a Runnable object
49 * and ready to start execution. More or less analogous to java.lang.Thread
50 * (minus all the thread group, priority, mode and other baggage, since that
51 * is difficult to abstract across platforms and is left for platform-specific
52 * ThreadFactory implemtations to deal with
53 *
54 * @see facebook::thrift::concurrency::ThreadFactory)
55 */
Marc Slemko66949872006-07-15 01:52:39 +000056class Thread {
57
58 public:
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 /**
75 * Gets the runnable object this thread is hosting
76 */
77 virtual shared_ptr<Runnable> runnable() const { return _runnable; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000078
79 protected:
Mark Sleef5f2be42006-09-05 21:05:31 +000080 virtual void runnable(shared_ptr<Runnable> value) { _runnable = value; }
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000081
82 private:
Marc Slemko6f038a72006-08-03 18:58:09 +000083 shared_ptr<Runnable> _runnable;
Marc Slemko66949872006-07-15 01:52:39 +000084};
85
Mark Sleef5f2be42006-09-05 21:05:31 +000086/**
87 * Factory to create platform-specific thread object and bind them to Runnable
88 * object for execution
89 */
Marc Slemko66949872006-07-15 01:52:39 +000090class ThreadFactory {
91
92 public:
Marc Slemko66949872006-07-15 01:52:39 +000093 virtual ~ThreadFactory() {}
Marc Slemko6f038a72006-08-03 18:58:09 +000094 virtual shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000095};
96
97}}} // facebook::thrift::concurrency
98
Mark Sleef5f2be42006-09-05 21:05:31 +000099#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_