| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
#ifndef _THRIFT_CONCURRENCY_THREAD_H_
|
|
|
21 |
#define _THRIFT_CONCURRENCY_THREAD_H_ 1
|
|
|
22 |
|
|
|
23 |
#include <stdint.h>
|
|
|
24 |
#include <boost/shared_ptr.hpp>
|
|
|
25 |
#include <boost/weak_ptr.hpp>
|
|
|
26 |
|
|
|
27 |
namespace apache { namespace thrift { namespace concurrency {
|
|
|
28 |
|
|
|
29 |
class Thread;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Minimal runnable class. More or less analogous to java.lang.Runnable.
|
|
|
33 |
*
|
|
|
34 |
* @version $Id:$
|
|
|
35 |
*/
|
|
|
36 |
class Runnable {
|
|
|
37 |
|
|
|
38 |
public:
|
|
|
39 |
virtual ~Runnable() {};
|
|
|
40 |
virtual void run() = 0;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Gets the thread object that is hosting this runnable object - can return
|
|
|
44 |
* an empty boost::shared pointer if no references remain on thet thread object
|
|
|
45 |
*/
|
|
|
46 |
virtual boost::shared_ptr<Thread> thread() { return thread_.lock(); }
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Sets the thread that is executing this object. This is only meant for
|
|
|
50 |
* use by concrete implementations of Thread.
|
|
|
51 |
*/
|
|
|
52 |
virtual void thread(boost::shared_ptr<Thread> value) { thread_ = value; }
|
|
|
53 |
|
|
|
54 |
private:
|
|
|
55 |
boost::weak_ptr<Thread> thread_;
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Minimal thread class. Returned by thread factory bound to a Runnable object
|
|
|
60 |
* and ready to start execution. More or less analogous to java.lang.Thread
|
|
|
61 |
* (minus all the thread group, priority, mode and other baggage, since that
|
|
|
62 |
* is difficult to abstract across platforms and is left for platform-specific
|
|
|
63 |
* ThreadFactory implemtations to deal with
|
|
|
64 |
*
|
|
|
65 |
* @see apache::thrift::concurrency::ThreadFactory)
|
|
|
66 |
*/
|
|
|
67 |
class Thread {
|
|
|
68 |
|
|
|
69 |
public:
|
|
|
70 |
|
|
|
71 |
typedef uint64_t id_t;
|
|
|
72 |
|
|
|
73 |
virtual ~Thread() {};
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Starts the thread. Does platform specific thread creation and
|
|
|
77 |
* configuration then invokes the run method of the Runnable object bound
|
|
|
78 |
* to this thread.
|
|
|
79 |
*/
|
|
|
80 |
virtual void start() = 0;
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Join this thread. Current thread blocks until this target thread
|
|
|
84 |
* completes.
|
|
|
85 |
*/
|
|
|
86 |
virtual void join() = 0;
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Gets the thread's platform-specific ID
|
|
|
90 |
*/
|
|
|
91 |
virtual id_t getId() = 0;
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Gets the runnable object this thread is hosting
|
|
|
95 |
*/
|
|
|
96 |
virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; }
|
|
|
97 |
|
|
|
98 |
protected:
|
|
|
99 |
virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; }
|
|
|
100 |
|
|
|
101 |
private:
|
|
|
102 |
boost::shared_ptr<Runnable> _runnable;
|
|
|
103 |
|
|
|
104 |
};
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Factory to create platform-specific thread object and bind them to Runnable
|
|
|
108 |
* object for execution
|
|
|
109 |
*/
|
|
|
110 |
class ThreadFactory {
|
|
|
111 |
|
|
|
112 |
public:
|
|
|
113 |
virtual ~ThreadFactory() {}
|
|
|
114 |
virtual boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const = 0;
|
|
|
115 |
|
|
|
116 |
/** Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread */
|
|
|
117 |
|
|
|
118 |
static const Thread::id_t unknown_thread_id;
|
|
|
119 |
|
|
|
120 |
virtual Thread::id_t getCurrentThreadId() const = 0;
|
|
|
121 |
};
|
|
|
122 |
|
|
|
123 |
}}} // apache::thrift::concurrency
|
|
|
124 |
|
|
|
125 |
#endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_
|