00001 #ifndef LOADBALANCE_H 00002 #define LOADBALANCE_H 00003 00004 #include "condition.inc" 00005 #include "mutex.inc" 00006 #include "thread.h" 00007 00008 00009 00010 00011 // Load balancing utils 00012 // There is no guarantee that all the load clients will be run in a 00013 // processing operation. 00014 00015 00016 class LoadServer; 00017 00018 00019 00020 class LoadPackage 00021 { 00022 public: 00023 LoadPackage(); 00024 virtual ~LoadPackage(); 00025 00026 Condition *completion_lock; 00027 // Range to search in the total scan area 00028 // int pixel1, pixel2; 00029 }; 00030 00031 00032 class LoadClient : public Thread 00033 { 00034 public: 00035 LoadClient(LoadServer *server); 00036 LoadClient(); 00037 virtual ~LoadClient(); 00038 00039 // Called when run as distributed client 00040 void run(); 00041 // Called when run as a single_client 00042 void run_single(); 00043 virtual void process_package(LoadPackage *package); 00044 int get_package_number(); 00045 LoadServer* get_server(); 00046 00047 int done; 00048 int package_number; 00049 Condition *input_lock; 00050 Condition *completion_lock; 00051 LoadServer *server; 00052 }; 00053 00054 00055 00056 00057 class LoadServer 00058 { 00059 public: 00060 LoadServer(int total_clients, int total_packages); 00061 virtual ~LoadServer(); 00062 00063 friend class LoadClient; 00064 00065 // Called first in process_packages. Should also initialize clients. 00066 virtual void init_packages() {}; 00067 virtual LoadClient* new_client() { return 0; }; 00068 virtual LoadPackage* new_package() { return 0; }; 00069 00070 // User calls this to do an iteration with the distributed clients 00071 void process_packages(); 00072 00073 // Use this to do an iteration with one client, in the current thread. 00074 // The single client is created specifically for this call and deleted in 00075 // delete_clients. This simplifies the porting to OpenGL. 00076 // total_packages must be > 0. 00077 void process_single(); 00078 00079 // These values are computed from the value of is_single. 00080 int get_total_packages(); 00081 int get_total_clients(); 00082 LoadPackage* get_package(int number); 00083 LoadClient* get_client(int number); 00084 void set_package_count(int total_packages); 00085 00086 00087 00088 void delete_clients(); 00089 void create_clients(); 00090 void delete_packages(); 00091 void create_packages(); 00092 00093 00094 00095 00096 private: 00097 int current_package; 00098 LoadPackage **packages; 00099 int total_packages; 00100 LoadClient **clients; 00101 LoadClient *single_client; 00102 int total_clients; 00103 int is_single; 00104 Mutex *client_lock; 00105 }; 00106 00107 00108 00109 #endif 00110 00111
1.5.5