Page Speed Optimization Libraries  1.2.24.1
net/instaweb/util/public/queued_worker_pool.h
Go to the documentation of this file.
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/thread_system.h"
00037 
00038 namespace net_instaweb {
00039 
00040 class AbstractMutex;
00041 class QueuedWorker;
00042 class Waveform;
00043 
00046 class QueuedWorkerPool {
00047  public:
00048   static const int kNoLoadShedding = -1;
00049 
00050   QueuedWorkerPool(int max_workers, ThreadSystem* thread_system);
00051   ~QueuedWorkerPool();
00052 
00057   class Sequence {
00058    public:
00063     class AddFunction : public Function {
00064      public:
00065       AddFunction(Sequence* sequence, Function* callback)
00066           : sequence_(sequence), callback_(callback) { }
00067       virtual ~AddFunction();
00068 
00069      protected:
00070       virtual void Run() {
00071         sequence_->Add(callback_);
00072       }
00073       virtual void Cancel() {
00074         sequence_->Add(MakeFunction(callback_, &Function::CallCancel));
00075       }
00076 
00077      private:
00078       Sequence* sequence_;
00079       Function* callback_;
00080       DISALLOW_COPY_AND_ASSIGN(AddFunction);
00081     };
00082 
00095     void Add(Function* function);
00096 
00097     void set_queue_size_stat(Waveform* x) { queue_size_ = x; }
00098 
00102     void set_max_queue_size(size_t x) { max_queue_size_ = x; }
00103 
00105     void CancelPendingFunctions();
00106 
00107    private:
00109     Sequence(ThreadSystem* thread_system, QueuedWorkerPool* pool);
00110 
00112     ~Sequence();
00113 
00115     void Reset();
00116 
00123     void WaitForShutDown();
00124 
00128     bool InitiateShutDown();
00129 
00132     Function* NextFunction();
00133 
00135     bool IsBusy();
00136 
00138     int CancelTasksOnWorkQueue();
00139 
00142     void Cancel();
00143 
00144     friend class QueuedWorkerPool;
00145     std::deque<Function*> work_queue_;
00146     scoped_ptr<ThreadSystem::CondvarCapableMutex> sequence_mutex_;
00147     QueuedWorkerPool* pool_;
00148     bool shutdown_;
00149     bool active_;
00150     scoped_ptr<ThreadSystem::Condvar> termination_condvar_;
00151     Waveform* queue_size_;
00152     size_t max_queue_size_;
00153 
00154     DISALLOW_COPY_AND_ASSIGN(Sequence);
00155   };
00156 
00157   typedef std::set<Sequence*> SequenceSet;
00158 
00163   Sequence* NewSequence(); 
00164 
00167   void FreeSequence(Sequence* sequence);
00168 
00173   void ShutDown();
00174 
00179   void InitiateShutDown();
00180 
00183   void WaitForShutDownComplete();
00184 
00199   static bool AreBusy(const SequenceSet& sequences);
00200 
00210   void SetLoadSheddingThreshold(int x);
00211 
00215   void set_queue_size_stat(Waveform* x) { queue_size_ = x; }
00216 
00217  private:
00218   friend class Sequence;
00219   void Run(Sequence* sequence, QueuedWorker* worker);
00220   void QueueSequence(Sequence* sequence);
00221   Sequence* AssignWorkerToNextSequence(QueuedWorker* worker);
00222   void SequenceNoLongerActive(Sequence* sequence);
00223 
00224   ThreadSystem* thread_system_;
00225   scoped_ptr<AbstractMutex> mutex_;
00226 
00228   std::set<QueuedWorker*> active_workers_;
00229   std::vector<QueuedWorker*> available_workers_;
00230 
00233   std::vector<Sequence*> all_sequences_;
00234   std::deque<Sequence*> queued_sequences_;
00235   std::vector<Sequence*> free_sequences_;
00236 
00237   size_t max_workers_;
00238   bool shutdown_;
00239 
00240   Waveform* queue_size_;
00241   int load_shedding_threshold_;
00242 
00243   DISALLOW_COPY_AND_ASSIGN(QueuedWorkerPool);
00244 };
00245 
00246 }  
00247 
00248 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_QUEUED_WORKER_POOL_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines