4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "condition.h"
24 #include "loadbalance.h"
29 LoadPackage::LoadPackage()
31 completion_lock = new Condition(0, "LoadPackage::completion_lock");
33 LoadPackage::~LoadPackage()
35 delete completion_lock;
46 LoadClient::LoadClient(LoadServer *server)
49 Thread::set_synchronous(1);
50 this->server = server;
53 input_lock = new Condition(0, "LoadClient::input_lock");
54 completion_lock = new Condition(0, "LoadClient::completion_lock");
57 LoadClient::LoadClient()
60 Thread::set_synchronous(1);
64 input_lock = new Condition(0, "LoadClient::input_lock");
65 completion_lock = new Condition(0, "LoadClient::completion_lock");
68 LoadClient::~LoadClient()
74 delete completion_lock;
77 int LoadClient::get_package_number()
79 return package_number;
82 LoadServer* LoadClient::get_server()
88 void LoadClient::run()
92 input_lock->lock("LoadClient::run");
100 server->client_lock->lock("LoadClient::run");
101 if(server->current_package < server->total_packages)
103 package_number = server->current_package;
104 package = server->packages[server->current_package++];
105 server->client_lock->unlock();
106 input_lock->unlock();
108 process_package(package);
110 package->completion_lock->unlock();
114 server->client_lock->unlock();
115 completion_lock->unlock();
121 void LoadClient::run_single()
123 if(server->total_packages)
125 for(int i = 0; i < server->total_packages; i++)
127 process_package(server->packages[i]);
132 void LoadClient::process_package(LoadPackage *package)
134 printf("LoadClient::process_package\n");
141 LoadServer::LoadServer(int total_clients, int total_packages)
143 if(total_clients <= 0)
144 printf("LoadServer::LoadServer total_clients == %d\n", total_clients);
145 this->total_clients = total_clients;
146 this->total_packages = total_packages;
150 client_lock = new Mutex("LoadServer::client_lock");
155 LoadServer::~LoadServer()
162 void LoadServer::delete_clients()
166 for(int i = 0; i < total_clients; i++)
171 if(single_client) delete single_client;
177 void LoadServer::delete_packages()
181 for(int i = 0; i < total_packages; i++)
188 void LoadServer::set_package_count(int total_packages)
191 this->total_packages = total_packages;
196 void LoadServer::create_clients()
198 if(!is_single && !clients)
200 clients = new LoadClient*[total_clients];
201 for(int i = 0; i < total_clients; i++)
203 clients[i] = new_client();
204 clients[i]->server = this;
209 if(is_single && !single_client)
211 single_client = new_client();
212 single_client->server = this;
216 void LoadServer::create_packages()
220 packages = new LoadPackage*[total_packages];
221 for(int i = 0; i < total_packages; i++)
222 packages[i] = new_package();
226 LoadPackage* LoadServer::get_package(int number)
228 return packages[number];
231 LoadClient* LoadServer::get_client(int number)
233 return clients[number];
236 int LoadServer::get_total_packages()
238 // if(is_single) return 1;
239 return total_packages;
242 int LoadServer::get_total_clients()
244 if(is_single) return 1;
245 return total_clients;
248 void LoadServer::process_packages()
250 if(total_clients == 1)
267 for(int i = 0; i < total_clients; i++)
269 clients[i]->input_lock->unlock();
272 // Wait for packages to get finished
273 for(int i = 0; i < total_packages; i++)
275 packages[i]->completion_lock->lock("LoadServer::process_packages 1");
278 // Wait for clients to finish before allowing changes to packages
279 for(int i = 0; i < total_clients; i++)
281 clients[i]->completion_lock->lock("LoadServer::process_packages 2");
285 void LoadServer::process_single()
292 single_client->run_single();