3 #include "loadbalance.h"
8 LoadPackage::LoadPackage()
10 completion_lock = new Condition(0, "LoadPackage::completion_lock");
12 LoadPackage::~LoadPackage()
14 delete completion_lock;
25 LoadClient::LoadClient(LoadServer *server)
28 Thread::set_synchronous(1);
29 this->server = server;
32 input_lock = new Condition(0, "LoadClient::input_lock");
33 completion_lock = new Condition(0, "LoadClient::completion_lock");
36 LoadClient::LoadClient()
39 Thread::set_synchronous(1);
43 input_lock = new Condition(0, "LoadClient::input_lock");
44 completion_lock = new Condition(0, "LoadClient::completion_lock");
47 LoadClient::~LoadClient()
53 delete completion_lock;
56 int LoadClient::get_package_number()
58 return package_number;
61 LoadServer* LoadClient::get_server()
67 void LoadClient::run()
71 input_lock->lock("LoadClient::run");
79 server->client_lock->lock("LoadClient::run");
80 if(server->current_package < server->total_packages)
82 package_number = server->current_package;
83 package = server->packages[server->current_package++];
84 server->client_lock->unlock();
87 process_package(package);
89 package->completion_lock->unlock();
93 completion_lock->unlock();
94 server->client_lock->unlock();
100 void LoadClient::run_single()
102 if(server->total_packages)
103 process_package(server->packages[0]);
106 void LoadClient::process_package(LoadPackage *package)
108 printf("LoadClient::process_package\n");
115 LoadServer::LoadServer(int total_clients, int total_packages)
117 if(total_clients <= 0)
118 printf("LoadServer::LoadServer total_clients == %d\n", total_clients);
119 this->total_clients = total_clients;
120 this->total_packages = total_packages;
124 client_lock = new Mutex("LoadServer::client_lock");
129 LoadServer::~LoadServer()
136 void LoadServer::delete_clients()
140 for(int i = 0; i < total_clients; i++)
144 if(single_client) delete single_client;
149 void LoadServer::delete_packages()
153 for(int i = 0; i < total_packages; i++)
160 void LoadServer::set_package_count(int total_packages)
163 this->total_packages = total_packages;
168 void LoadServer::create_clients()
170 if(!is_single && !clients)
172 clients = new LoadClient*[total_clients];
173 for(int i = 0; i < total_clients; i++)
175 clients[i] = new_client();
176 clients[i]->server = this;
181 if(is_single && !single_client)
183 single_client = new_client();
184 single_client->server = this;
188 void LoadServer::create_packages()
192 packages = new LoadPackage*[total_packages];
193 for(int i = 0; i < total_packages; i++)
194 packages[i] = new_package();
198 LoadPackage* LoadServer::get_package(int number)
200 return packages[number];
203 LoadClient* LoadServer::get_client(int number)
205 return clients[number];
208 int LoadServer::get_total_packages()
210 if(is_single) return 1;
211 return total_packages;
214 int LoadServer::get_total_clients()
216 if(is_single) return 1;
217 return total_clients;
220 void LoadServer::process_packages()
233 for(int i = 0; i < total_clients; i++)
235 clients[i]->input_lock->unlock();
238 // Wait for packages to get finished
239 for(int i = 0; i < total_packages; i++)
241 packages[i]->completion_lock->lock("LoadServer::process_packages 1");
244 // Wait for clients to finish before allowing changes to packages
245 for(int i = 0; i < total_clients; i++)
247 clients[i]->completion_lock->lock("LoadServer::process_packages 2");
251 void LoadServer::process_single()
258 single_client->run_single();
264 // c-file-style: "linux"