Page Speed Optimization Libraries
1.4.26.1
|
00001 /* 00002 * Copyright 2011 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00024 00025 #ifndef NET_INSTAWEB_UTIL_PUBLIC_QUEUED_WORKER_POOL_H_ 00026 #define NET_INSTAWEB_UTIL_PUBLIC_QUEUED_WORKER_POOL_H_ 00027 00028 #include <cstddef> 00029 #include <deque> 00030 #include <set> 00031 #include <vector> 00032 00033 #include "net/instaweb/util/public/basictypes.h" 00034 #include "net/instaweb/util/public/function.h" 00035 #include "net/instaweb/util/public/scoped_ptr.h" 00036 #include "net/instaweb/util/public/string.h" 00037 #include "net/instaweb/util/public/string_util.h" 00038 #include "net/instaweb/util/public/thread_system.h" 00039 00040 namespace net_instaweb { 00041 00042 class AbstractMutex; 00043 class QueuedWorker; 00044 class Waveform; 00045 00048 class QueuedWorkerPool { 00049 public: 00050 static const int kNoLoadShedding = -1; 00051 00052 QueuedWorkerPool(int max_workers, StringPiece thread_name_base, 00053 ThreadSystem* thread_system); 00054 ~QueuedWorkerPool(); 00055 00060 class Sequence { 00061 public: 00066 class AddFunction : public Function { 00067 public: 00068 AddFunction(Sequence* sequence, Function* callback) 00069 : sequence_(sequence), callback_(callback) { } 00070 virtual ~AddFunction(); 00071 00072 protected: 00073 virtual void Run() { 00074 sequence_->Add(callback_); 00075 } 00076 virtual void Cancel() { 00077 sequence_->Add(MakeFunction(callback_, &Function::CallCancel)); 00078 } 00079 00080 private: 00081 Sequence* sequence_; 00082 Function* callback_; 00083 DISALLOW_COPY_AND_ASSIGN(AddFunction); 00084 }; 00085 00098 void Add(Function* function); 00099 00100 void set_queue_size_stat(Waveform* x) { queue_size_ = x; } 00101 00105 void set_max_queue_size(size_t x) { max_queue_size_ = x; } 00106 00108 void CancelPendingFunctions(); 00109 00110 private: 00112 Sequence(ThreadSystem* thread_system, QueuedWorkerPool* pool); 00113 00115 ~Sequence(); 00116 00118 void Reset(); 00119 00126 void WaitForShutDown(); 00127 00131 bool InitiateShutDown(); 00132 00135 Function* NextFunction(); 00136 00138 bool IsBusy(); 00139 00141 int CancelTasksOnWorkQueue(); 00142 00145 void Cancel(); 00146 00147 friend class QueuedWorkerPool; 00148 std::deque<Function*> work_queue_; 00149 scoped_ptr<ThreadSystem::CondvarCapableMutex> sequence_mutex_; 00150 QueuedWorkerPool* pool_; 00151 bool shutdown_; 00152 bool active_; 00153 scoped_ptr<ThreadSystem::Condvar> termination_condvar_; 00154 Waveform* queue_size_; 00155 size_t max_queue_size_; 00156 00157 DISALLOW_COPY_AND_ASSIGN(Sequence); 00158 }; 00159 00160 typedef std::set<Sequence*> SequenceSet; 00161 00166 Sequence* NewSequence(); 00167 00170 void FreeSequence(Sequence* sequence); 00171 00176 void ShutDown(); 00177 00182 void InitiateShutDown(); 00183 00186 void WaitForShutDownComplete(); 00187 00202 static bool AreBusy(const SequenceSet& sequences); 00203 00213 void SetLoadSheddingThreshold(int x); 00214 00218 void set_queue_size_stat(Waveform* x) { queue_size_ = x; } 00219 00220 private: 00221 friend class Sequence; 00222 void Run(Sequence* sequence, QueuedWorker* worker); 00223 void QueueSequence(Sequence* sequence); 00224 Sequence* AssignWorkerToNextSequence(QueuedWorker* worker); 00225 void SequenceNoLongerActive(Sequence* sequence); 00226 00227 ThreadSystem* thread_system_; 00228 scoped_ptr<AbstractMutex> mutex_; 00229 00231 std::set<QueuedWorker*> active_workers_; 00232 std::vector<QueuedWorker*> available_workers_; 00233 00236 std::vector<Sequence*> all_sequences_; 00237 std::deque<Sequence*> queued_sequences_; 00238 std::vector<Sequence*> free_sequences_; 00239 00240 GoogleString thread_name_base_; 00241 00242 size_t max_workers_; 00243 bool shutdown_; 00244 00245 Waveform* queue_size_; 00246 int load_shedding_threshold_; 00247 00248 DISALLOW_COPY_AND_ASSIGN(QueuedWorkerPool); 00249 }; 00250 00251 } 00252 00253 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_QUEUED_WORKER_POOL_H_