1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef _LOG4CXX_HELPERS_THREAD_H
|
---|
19 | #define _LOG4CXX_HELPERS_THREAD_H
|
---|
20 |
|
---|
21 | #include <log4cxx/log4cxx.h>
|
---|
22 | #include <log4cxx/helpers/pool.h>
|
---|
23 |
|
---|
24 | #if !defined(LOG4CXX_THREAD_FUNC)
|
---|
25 | #if defined(_WIN32)
|
---|
26 | #define LOG4CXX_THREAD_FUNC __stdcall
|
---|
27 | #else
|
---|
28 | #define LOG4CXX_THREAD_FUNC
|
---|
29 | #endif
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | extern "C" {
|
---|
33 | typedef struct apr_thread_t apr_thread_t;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | namespace log4cxx
|
---|
38 | {
|
---|
39 | namespace helpers
|
---|
40 | {
|
---|
41 | class Pool;
|
---|
42 | class ThreadLocal;
|
---|
43 |
|
---|
44 | typedef void* (LOG4CXX_THREAD_FUNC *Runnable)(apr_thread_t* thread, void* data);
|
---|
45 | /**
|
---|
46 | * This class implements an approximation of java.util.Thread.
|
---|
47 | */
|
---|
48 | class LOG4CXX_EXPORT Thread
|
---|
49 | {
|
---|
50 | public:
|
---|
51 | /**
|
---|
52 | * Create new instance.
|
---|
53 | */
|
---|
54 | Thread();
|
---|
55 | /**
|
---|
56 | * Destructor.
|
---|
57 | */
|
---|
58 | ~Thread();
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Runs the specified method on a newly created thread.
|
---|
62 | */
|
---|
63 | void run(Runnable start, void* data);
|
---|
64 | void join();
|
---|
65 |
|
---|
66 | inline bool isActive() { return thread != 0; }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Causes the currently executing thread to sleep for the
|
---|
70 | * specified number of milliseconds.
|
---|
71 | * @param millis milliseconds.
|
---|
72 | * @throws Interrupted Exception if the thread is interrupted.
|
---|
73 | */
|
---|
74 | static void sleep(int millis);
|
---|
75 | /**
|
---|
76 | * Sets interrupted status for current thread to true.
|
---|
77 | */
|
---|
78 | static void currentThreadInterrupt();
|
---|
79 | /**
|
---|
80 | * Sets interrupted status to true.
|
---|
81 | */
|
---|
82 | void interrupt();
|
---|
83 | /**
|
---|
84 | * Tests if the current thread has been interrupted and
|
---|
85 | * sets the interrupted status to false.
|
---|
86 | */
|
---|
87 | static bool interrupted();
|
---|
88 |
|
---|
89 | bool isAlive();
|
---|
90 | bool isCurrentThread() const;
|
---|
91 | void ending();
|
---|
92 |
|
---|
93 |
|
---|
94 | private:
|
---|
95 | Pool p;
|
---|
96 | apr_thread_t* thread;
|
---|
97 | volatile unsigned int alive;
|
---|
98 | volatile unsigned int interruptedStatus;
|
---|
99 | Thread(const Thread&);
|
---|
100 | Thread& operator=(const Thread&);
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * This class is used to encapsulate the parameters to
|
---|
104 | * Thread::run when they are passed to Thread::launcher.
|
---|
105 | *
|
---|
106 | */
|
---|
107 | class LaunchPackage {
|
---|
108 | public:
|
---|
109 | /**
|
---|
110 | * Placement new to create LaunchPackage in specified pool.
|
---|
111 | * LaunchPackage needs to be dynamically allocated since
|
---|
112 | * since a stack allocated instance may go out of scope
|
---|
113 | * before thread is launched.
|
---|
114 | */
|
---|
115 | static void* operator new(size_t, Pool& p);
|
---|
116 | /**
|
---|
117 | * operator delete would be called if exception during construction.
|
---|
118 | */
|
---|
119 | static void operator delete(void*, Pool& p);
|
---|
120 | /**
|
---|
121 | * Create new instance.
|
---|
122 | */
|
---|
123 | LaunchPackage(Thread* thread, Runnable runnable, void* data);
|
---|
124 | /**
|
---|
125 | * Gets thread parameter.
|
---|
126 | * @return thread.
|
---|
127 | */
|
---|
128 | Thread* getThread() const;
|
---|
129 | /**
|
---|
130 | * Gets runnable parameter.
|
---|
131 | * @return runnable.
|
---|
132 | */
|
---|
133 | Runnable getRunnable() const;
|
---|
134 | /**
|
---|
135 | * gets data parameter.
|
---|
136 | * @return thread.
|
---|
137 | */
|
---|
138 | void* getData() const;
|
---|
139 | private:
|
---|
140 | LaunchPackage(const LaunchPackage&);
|
---|
141 | LaunchPackage& operator=(const LaunchPackage&);
|
---|
142 | Thread* thread;
|
---|
143 | Runnable runnable;
|
---|
144 | void* data;
|
---|
145 | };
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * This object atomically sets the specified memory location
|
---|
149 | * to non-zero on construction and to zero on destruction.
|
---|
150 | * Used to maintain Thread.alive.
|
---|
151 | */
|
---|
152 | class LaunchStatus {
|
---|
153 | public:
|
---|
154 | /*
|
---|
155 | * Construct new instance.
|
---|
156 | * @param p address of memory to set to non-zero on construction, zero on destruction.
|
---|
157 | */
|
---|
158 | LaunchStatus(volatile unsigned int* p);
|
---|
159 | /**
|
---|
160 | * Destructor.
|
---|
161 | */
|
---|
162 | ~LaunchStatus();
|
---|
163 | private:
|
---|
164 | LaunchStatus(const LaunchStatus&);
|
---|
165 | LaunchStatus& operator=(const LaunchStatus&);
|
---|
166 | volatile unsigned int* alive;
|
---|
167 | };
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * This method runs on the created thread and sets up thread-local storage
|
---|
171 | * used to keep the reference to the corresponding Thread object and
|
---|
172 | * is responsible for maintaining Thread.alive.
|
---|
173 | */
|
---|
174 | static void* LOG4CXX_THREAD_FUNC launcher(apr_thread_t* thread, void* data);
|
---|
175 | /**
|
---|
176 | * Get a key to the thread local storage used to hold the reference to
|
---|
177 | * the corresponding Thread object.
|
---|
178 | */
|
---|
179 | static ThreadLocal& getThreadLocal();
|
---|
180 | };
|
---|
181 | } // namespace helpers
|
---|
182 | } // namespace log4cxx
|
---|
183 |
|
---|
184 | #endif //_LOG4CXX_HELPERS_THREAD_H
|
---|