//------- Begin of function FirmCamp::pay_weapon_expense -------// // void FirmCamp::pay_weapon_expense() { Worker* workerPtr = worker_array; Nation* nationPtr = nation_array[nation_recno]; for( int i=1 ; i<=worker_count ; i++, workerPtr++ ) { if( workerPtr->unit_id && unit_res[workerPtr->unit_id]->unit_class == UNIT_CLASS_WEAPON ) { if( nationPtr->cash > 0 ) { nationPtr->add_expense( EXPENSE_WEAPON, (float) unit_res[workerPtr->unit_id]->year_cost / 365, 1 ); } else // decrease hit points if the nation cannot pay the unit { if( workerPtr->hit_points > 0 ) workerPtr->hit_points--; if( workerPtr->hit_points == 0 ) kill_worker(i); // if its hit points is zero, delete it err_when( workerPtr->hit_points < 0 ); } } } }
//--------- Begin of function Firm::kill_all_worker ---------// // // All the workers in the firm are deleted // void Firm::kill_all_worker() { for(int i=worker_count; i>0; i--) kill_worker(i); }
size_t get_worker(void) { // get a worker // off, so we don't check worker 0 a million times static size_t off = -1; static int tries = 0; ++off; int which; for(which=0;which<numworkers;++which) { size_t derp = (which+off)%numworkers; switch(workers[derp].status) { case IDLE: workers[derp].status = BUSY; PFD(derp).events = POLLIN; tries = 0; return derp; }; } if(tries < 3) { ++tries; // if we timeout 3 times, stop waiting for idle workers. return -1; } /* no idle found, try starting some workers */ if(numworkers < MAXWORKERS) { // add a worker to the end if(start_worker()) { tries = 0; return numworkers-1; } } else { reap_workers(); for(which=0;which<numworkers;++which) { if(workers[which].status == DOOMED) { /* if 995 ns left (expiration - now) and doom delay is 1000ns 1000 - 995 < 50, so wait a teensy bit longer please */ Time diff = timediff(DOOM_DELAY, timediff(workers[which].expiration, getnow())); if(diff.tv_nsec > 50) { // waited too long, kill the thing. kill_worker(which); if(start_worker()) { tries = 0; return numworkers-1; } } } } } if(wait_for_accept()) { if(accept_workers()) { return get_worker(); } } // have to wait until the new worker connects errno = EAGAIN; // eh return -1; }
work_queue_for_tests::~work_queue_for_tests () { kill_worker (); }