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_MUTEX_H 00025 #define SCALESTACK_THREADING_MUTEX_H 00026 00027 #include <memory> 00028 00029 #include <scalestack/kernel/core.h> 00030 #include <scalestack/kernel/module.h> 00031 #include <scalestack/threading/service.h> 00032 00033 namespace scalestack 00034 { 00035 namespace threading 00036 { 00037 00038 class mutex_provider; 00039 00043 class mutex 00044 { 00045 public: 00046 00052 mutex(kernel::module& module); 00053 00057 void lock(void); 00058 00062 void unlock(void); 00063 00064 protected: 00065 00066 kernel::module& _module; 00067 00068 private: 00069 00073 mutex(const mutex&); 00074 00078 mutex& operator=(const mutex&); 00079 00080 std::auto_ptr<mutex_provider> _mutex_provider; 00081 }; 00082 00088 class mutex_provider 00089 { 00090 public: 00091 00092 mutex_provider(kernel::module& module, mutex& mutex); 00093 00094 virtual ~mutex_provider(); 00095 00099 virtual void lock(void) = 0; 00100 00104 virtual void unlock(void) = 0; 00105 00106 protected: 00107 00108 kernel::module& _module; 00109 00110 private: 00111 00115 mutex_provider(const mutex_provider&); 00116 00120 mutex_provider& operator=(const mutex_provider&); 00121 00122 mutex& _mutex; 00123 }; 00124 00125 /* 00126 * Public mutex methods. 00127 */ 00128 00129 inline mutex::mutex(kernel::module& module): 00130 _module(module), 00131 _mutex_provider(static_cast<threading::service&>( 00132 module.get_core().get_service("threading"))._add_mutex(*this)) 00133 { 00134 } 00135 00136 inline void mutex::lock(void) 00137 { 00138 _mutex_provider->lock(); 00139 } 00140 00141 inline void mutex::unlock(void) 00142 { 00143 _mutex_provider->unlock(); 00144 } 00145 00146 /* 00147 * Public mutex_provider methods. 00148 */ 00149 00150 inline mutex_provider::mutex_provider(kernel::module& module, mutex& mutex): 00151 _module(module), 00152 _mutex(mutex) 00153 { 00154 } 00155 00156 inline mutex_provider::~mutex_provider() 00157 { 00158 } 00159 00160 } /* namespace threading */ 00161 } /* namespace scalestack */ 00162 00163 #endif /* SCALESTACK_THREADING_MUTEX_H */
1.6.3