blob: ea6b999f1d8e21719713698a06dd39f7d1e7cefb [file] [log] [blame]
Marc Slemko66949872006-07-15 01:52:39 +00001#if !defined(_concurrency_Thread_h_)
2#define _concurrency_Thread_h_ 1
3
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
Marc Slemko0e53ccd2006-07-17 23:51:05 +000013/** Minimal runnable class. More or less analogous to java.lang.Runnable.
14
15 @author marc
16 @version $Id:$ */
Marc Slemko66949872006-07-15 01:52:39 +000017
18class Runnable {
19
20 public:
21
22 virtual ~Runnable() {};
23
24 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000025
Marc Slemko6f038a72006-08-03 18:58:09 +000026 /** Gets the thread object that is hosting this runnable object - can return an empty shared pointer if no references remain on thet thread object */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000027
Marc Slemko6f038a72006-08-03 18:58:09 +000028 virtual shared_ptr<Thread> thread() {return _thread.lock();}
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000029
30 /** Sets the thread that is executing this object. This is only meant for use by concrete implementations of Thread. */
31
Marc Slemko6f038a72006-08-03 18:58:09 +000032 virtual void thread(shared_ptr<Thread> value) {_thread = value;}
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000033
Marc Slemko6f038a72006-08-03 18:58:09 +000034 private:
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000035
Marc Slemko6f038a72006-08-03 18:58:09 +000036 weak_ptr<Thread> _thread;
Marc Slemko66949872006-07-15 01:52:39 +000037};
38
39/** Minimal thread class. Returned by thread factory bound to a Runnable object and ready to start execution. More or less analogous to java.lang.Thread
40 (minus all the thread group, priority, mode and other baggage, since that is difficult to abstract across platforms and is left for platform-specific
41 ThreadFactory implemtations to deal with - @see facebook::thrift::concurrency::ThreadFactory) */
42
43
44class Thread {
45
46 public:
47
48 virtual ~Thread() {};
49
50 /** Starts the thread. Does platform specific thread creation and configuration then invokes the run method of the Runnable object bound to this
51 thread. */
52
53 virtual void start() = 0;
54
55 /** Join this thread
56
57 Current thread blocks until this target thread completes. */
58
59 virtual void join() = 0;
60
61 /** Gets the runnable object this thread is hosting */
62
Marc Slemko6f038a72006-08-03 18:58:09 +000063 virtual shared_ptr<Runnable> runnable() const {return _runnable;}
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000064
65 protected:
66
Marc Slemko6f038a72006-08-03 18:58:09 +000067 virtual void runnable(shared_ptr<Runnable> value) {_runnable = value;}
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000068
69 private:
Marc Slemko6f038a72006-08-03 18:58:09 +000070 shared_ptr<Runnable> _runnable;
Marc Slemko66949872006-07-15 01:52:39 +000071};
72
73/** Factory to create platform-specific thread object and bind them to Runnable object for execution */
74
75class ThreadFactory {
76
77 public:
78
79 virtual ~ThreadFactory() {}
80
Marc Slemko6f038a72006-08-03 18:58:09 +000081 virtual shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000082};
83
84}}} // facebook::thrift::concurrency
85
86#endif // !defined(_concurrency_Thread_h_)