00001 /* 00002 * Scale Stack 00003 * 00004 * Copyright 2010 Eric Day 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 00024 #ifndef SCALESTACK_THREADING_THREAD_H 00025 #define SCALESTACK_THREADING_THREAD_H 00026 00027 #include <exception> 00028 #include <memory> 00029 00030 #include <scalestack/kernel/core.h> 00031 #include <scalestack/kernel/module.h> 00032 #include <scalestack/threading/service.h> 00033 00034 namespace scalestack 00035 { 00036 namespace threading 00037 { 00038 00039 class thread_provider; 00040 00044 class thread 00045 { 00046 public: 00047 00056 thread(kernel::module& module); 00057 00063 virtual ~thread(); 00064 00069 void start(void); 00070 00075 void stop(void); 00076 00080 size_t get_id(void); 00081 00085 virtual void run(void) = 0; 00086 00087 protected: 00088 00089 kernel::module& _module; 00090 00091 private: 00092 00096 thread(const thread&); 00097 00101 thread& operator=(const thread&); 00102 00103 std::auto_ptr<thread_provider> _thread_provider; 00104 }; 00105 00111 class thread_provider 00112 { 00113 public: 00114 00115 thread_provider(kernel::module& module, thread& thread); 00116 00117 virtual ~thread_provider(); 00118 00122 virtual void start(void) = 0; 00123 00127 virtual void stop(void) = 0; 00128 00132 virtual size_t get_id(void) = 0; 00133 00137 virtual void run(void); 00138 00139 protected: 00140 00141 kernel::module& _module; 00142 00143 private: 00144 00148 thread_provider(const thread_provider&); 00149 00153 thread_provider& operator=(const thread_provider&); 00154 00155 thread& _thread; 00156 }; 00157 00158 /* 00159 * Public thread methods. 00160 */ 00161 00162 inline thread::thread(kernel::module& module): 00163 _module(module), 00164 _thread_provider(static_cast<threading::service&>( 00165 module.get_core().get_service("threading"))._add_thread(*this)) 00166 { 00167 } 00168 00169 inline thread::~thread() 00170 { 00171 } 00172 00173 inline void thread::start(void) 00174 { 00175 _thread_provider->start(); 00176 } 00177 00178 inline void thread::stop(void) 00179 { 00180 _thread_provider->stop(); 00181 } 00182 00183 inline size_t thread::get_id(void) 00184 { 00185 return _thread_provider->get_id(); 00186 } 00187 00188 /* 00189 * Public thread_provider methods. 00190 */ 00191 00192 inline thread_provider::thread_provider(kernel::module& module, thread& thread): 00193 _module(module), 00194 _thread(thread) 00195 { 00196 } 00197 00198 inline thread_provider::~thread_provider() 00199 { 00200 } 00201 00202 inline void thread_provider::run(void) 00203 { 00204 /* 00205 * Catch any exceptions here and notify the coer to shutdown. Otherwise the 00206 * exception could cause the process to exit without cleanly shutting down. 00207 */ 00208 try 00209 { 00210 _thread.run(); 00211 } 00212 catch (std::exception&) 00213 { 00214 _module.get_core().shutdown(); 00215 } 00216 } 00217 00218 } /* namespace threading */ 00219 } /* namespace scalestack */ 00220 00221 #endif /* SCALESTACK_THREADING_THREAD_H */
1.6.3