00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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;
00127 FXMutex qmutex;
00128 FXCondition qcondition;
00129 volatile FXint started;
00130 volatile FXint stopped;
00131 volatile FXbool running;
00132 FXuint slots;
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