Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

/home/jeroen/FOX/fox/fox-1.7.33/include/FXConcurrent.h
00001 /********************************************************************************
00002 *                                                                               *
00003 *            C o n c u r r e n t   T a s k   E x e c u t i o n                  *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2010,2012 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXCONCURRENT_H
00022 #define FXCONCURRENT_H
00023 
00024 namespace FX {
00025 
00026 
00027 class FXWorker;
00028 class FXConcurrent;
00029 
00030 
00038 class FXTask {
00039 private:
00040   FXTask(const FXTask&);
00041   FXTask &operator=(const FXTask&);
00042 public:
00043   virtual void exec(void*) const = 0;
00044   virtual ~FXTask(){}
00045   };
00046 
00047 
00058 class FXCompletion {
00059   friend class FXConcurrent;
00060 private:
00061   FXCondition    condition;
00062   FXMutex        mutex;
00063   volatile FXint count;
00064 private:
00065   FXCompletion(const FXCompletion&);
00066   FXCompletion &operator=(const FXCompletion&);
00067 private:
00068   void expect(FXint cnt);
00069   void notify();
00070 public:
00071 
00073   FXCompletion();
00074 
00076   void wait();
00077 
00079   FXbool done() const;
00080 
00082   virtual ~FXCompletion();
00083   };
00084 
00085 
00087 class FXWorkQueue;
00088 
00090 typedef FXPtrListOf<FXWorkQueue> FXWorkQueues;
00091 
00092 
00124 class FXAPI FXConcurrent : public FXRunnable {
00125 private:
00126   FXWorkQueues    queues;       // One argument for each thread
00127   FXMutex         qmutex;       // Queues mutex
00128   FXCondition     qcondition;   // Queues condition
00129   volatile FXint  started;      // Started threads
00130   volatile FXint  stopped;      // Stopped threads
00131   volatile FXbool running;      // Are we running
00132   FXuint          slots;        // Slots in queues
00133 private:
00134   FXWorker* startWorker();
00135   FXbool appendThreadQueue(FXWorkQueue* wq);
00136   FXbool removeThreadQueue(FXWorkQueue* wq);
00137 private:
00138   FXConcurrent(const FXConcurrent&);
00139   FXConcurrent &operator=(const FXConcurrent&);
00140 protected:
00141   virtual FXWorker* createWorker();
00142 public:
00143 
00145   FXConcurrent();
00146 
00148   FXint start(FXint cnt=1,FXint sz=32);
00149 
00151   FXbool active() const;
00152 
00154   FXint numThreads() const;
00155 
00157   virtual FXint run();
00158 
00160   FXint execute(const FXTask* task,FXint argc,FXint indx=0);
00161 
00163   FXint execute(const FXTask* task,void** argv,FXint argc,FXint indx=0);
00164 
00166   FXint execute(FXCompletion* completion,const FXTask* task,FXint argc,FXint indx=0);
00167 
00169   FXint execute(FXCompletion* completion,const FXTask* task,void** argv,FXint argc,FXint indx=0);
00170 
00172   FXint wait();
00173 
00175   FXint stop();
00176 
00178   virtual ~FXConcurrent();
00179   };
00180 
00181 
00182 }
00183 
00184 #endif
00185 

Copyright © 1997-2011 Jeroen van der Zijp